Skip to content

Commit

Permalink
desk_storage: Part 1 of refactor for fake services
Browse files Browse the repository at this point in the history
Part 1 of refactoring desk sync services to make faking services
easier for integration to testing.

BUG=b/282963187
TEST=DeskSyncBridgeTest*,LocalDeskDataManagerTest*,DeskModeWrapperTest*

Change-Id: I36b350c1726055676a7aad6d3cc1c11bfa86a317
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4552125
Commit-Queue: Matthew Zhu <zhumatthew@google.com>
Reviewed-by: Yanzhu Du <yzd@google.com>
Cr-Commit-Position: refs/heads/main@{#1149385}
  • Loading branch information
zhumatthew authored and Chromium LUCI CQ committed May 25, 2023
1 parent 71b3160 commit 21b0886
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 24 deletions.
Expand Up @@ -426,7 +426,7 @@ void FloatingWorkspaceService::OnTemplateCaptured(
// the current service will handle it. Ignore for testing.
if (!floating_workspace_uuid_.has_value() && !is_testing_) {
absl::optional<base::Uuid> floating_workspace_uuid_from_desk_model =
desk_sync_service_->GetDeskSyncBridge()->GetFloatingWorkspaceUuid();
GetFloatingWorkspaceUuidForCurrentDevice();
if (floating_workspace_uuid_from_desk_model.has_value()) {
floating_workspace_uuid_ =
floating_workspace_uuid_from_desk_model.value();
Expand All @@ -444,6 +444,7 @@ void FloatingWorkspaceService::OnTemplateCaptured(
UploadFloatingWorkspaceTemplateToDeskModel(std::move(desk_template));
}
}

void FloatingWorkspaceService::UploadFloatingWorkspaceTemplateToDeskModel(
std::unique_ptr<DeskTemplate> desk_template) {
// Upload and save the template.
Expand All @@ -461,4 +462,18 @@ void FloatingWorkspaceService::OnTemplateUploaded(
RecordFloatingWorkspaceV2TemplateUploadStatusHistogram(status);
}

absl::optional<base::Uuid>
FloatingWorkspaceService::GetFloatingWorkspaceUuidForCurrentDevice() {
std::string cache_guid = desk_sync_service_->GetDeskModel()->GetCacheGuid();
std::vector<const DeskTemplate*> entries =
desk_sync_service_->GetDeskModel()->GetAllEntries().entries;
auto iter = base::ranges::find_if(entries, [cache_guid](const auto& entry) {
return entry->client_cache_guid() == cache_guid;
});
if (iter == entries.end()) {
return absl::nullopt;
}
return (*iter)->uuid();
}

} // namespace ash
Expand Up @@ -150,6 +150,11 @@ class FloatingWorkspaceService : public KeyedService,
desks_storage::DeskModel::AddOrUpdateEntryStatus status,
std::unique_ptr<DeskTemplate> new_entry);

// Get the associated floating workspace uuid for the current device. Return
// an absl::nullopt if there is no floating workspace uuid that is associated
// with the current device.
absl::optional<base::Uuid> GetFloatingWorkspaceUuidForCurrentDevice();

const raw_ptr<Profile, ExperimentalAsh> profile_;

raw_ptr<sync_sessions::SessionSyncService, ExperimentalAsh>
Expand Down
2 changes: 2 additions & 0 deletions components/desks_storage/core/desk_model.h
Expand Up @@ -189,6 +189,8 @@ class DeskModel {
ash::DeskTemplateType type,
const base::Uuid& uuid) const = 0;

virtual std::string GetCacheGuid() = 0;

// Observer registration methods. The model will remove all observers upon
// destruction automatically.
void AddObserver(DeskModelObserver* observer);
Expand Down
5 changes: 4 additions & 1 deletion components/desks_storage/core/desk_model_wrapper.cc
Expand Up @@ -5,7 +5,6 @@
#include "components/desks_storage/core/desk_model_wrapper.h"

#include "ash/public/cpp/desk_template.h"
#include "base/logging.h"
#include "base/uuid.h"
#include "components/account_id/account_id.h"
#include "components/desks_storage/core/desk_model.h"
Expand Down Expand Up @@ -176,6 +175,10 @@ ash::DeskTemplate* DeskModelWrapper::FindOtherEntryWithName(
}
}

std::string DeskModelWrapper::GetCacheGuid() {
return GetDeskTemplateModel()->GetCacheGuid();
}

desks_storage::DeskSyncBridge* DeskModelWrapper::GetDeskTemplateModel() const {
DCHECK(desk_template_model_);
return desk_template_model_;
Expand Down
1 change: 1 addition & 0 deletions components/desks_storage/core/desk_model_wrapper.h
Expand Up @@ -56,6 +56,7 @@ class DeskModelWrapper : public DeskModel {
const std::u16string& name,
ash::DeskTemplateType type,
const base::Uuid& uuid) const override;
std::string GetCacheGuid() override;

// Setter method to set `desk_template_model_` to the correct `bridge`.
void SetDeskSyncBridge(desks_storage::DeskSyncBridge* bridge) {
Expand Down
10 changes: 3 additions & 7 deletions components/desks_storage/core/desk_sync_bridge.cc
Expand Up @@ -664,13 +664,9 @@ bool DeskSyncBridge::HasUserTemplateWithName(const std::u16string& name) {
bool DeskSyncBridge::HasUuid(const base::Uuid& uuid) const {
return uuid.is_valid() && base::Contains(desk_template_entries_, uuid);
}
absl::optional<base::Uuid> DeskSyncBridge::GetFloatingWorkspaceUuid() {
for (const auto& [cache_guid, uuid] : floating_workspace_templates_uuid_) {
if (cache_guid == change_processor()->TrackedCacheGuid()) {
return uuid;
}
}
return absl::nullopt;

std::string DeskSyncBridge::GetCacheGuid() {
return change_processor()->TrackedCacheGuid();
}

} // namespace desks_storage
9 changes: 4 additions & 5 deletions components/desks_storage/core/desk_sync_bridge.h
Expand Up @@ -86,13 +86,12 @@ class DeskSyncBridge : public syncer::ModelTypeSyncBridge, public DeskModel {
ash::DeskTemplateType type,
const base::Uuid& uuid) const override;

std::string GetCacheGuid() override;

// Other helper methods.
bool HasUuid(const base::Uuid& uuid) const;

const ash::DeskTemplate* GetUserEntryByUUID(const base::Uuid& uuid) const;

absl::optional<base::Uuid> GetFloatingWorkspaceUuid();

private:
friend class DeskModelWrapper;

Expand All @@ -104,8 +103,8 @@ class DeskSyncBridge : public syncer::ModelTypeSyncBridge, public DeskModel {
// Notify all observers that the model is loaded;
void NotifyDeskModelLoaded();

// Notify all observers of any `new_entries` when they are added/updated via
// sync.
// Notify all observers of any `new_entries` when they are added/updated
// via sync.
void NotifyRemoteDeskTemplateAddedOrUpdated(
const std::vector<const ash::DeskTemplate*>& new_entries);

Expand Down
6 changes: 1 addition & 5 deletions components/desks_storage/core/desk_sync_service.cc
Expand Up @@ -14,7 +14,7 @@
#include "components/sync/model/model_type_store.h"

namespace desks_storage {

DeskSyncService::DeskSyncService() = default;
DeskSyncService::DeskSyncService(
version_info::Channel channel,
syncer::OnceModelTypeStoreFactory create_store_callback,
Expand All @@ -32,10 +32,6 @@ DeskModel* DeskSyncService::GetDeskModel() {
return bridge_.get();
}

DeskSyncBridge* DeskSyncService::GetDeskSyncBridge() {
return bridge_.get();
}

base::WeakPtr<syncer::ModelTypeControllerDelegate>
DeskSyncService::GetControllerDelegate() {
return bridge_->change_processor()->GetControllerDelegate();
Expand Down
2 changes: 1 addition & 1 deletion components/desks_storage/core/desk_sync_service.h
Expand Up @@ -24,6 +24,7 @@ class DeskModel;
// KeyedService responsible for desk templates sync.
class DeskSyncService : public KeyedService {
public:
DeskSyncService();
DeskSyncService(version_info::Channel channel,
syncer::OnceModelTypeStoreFactory create_store_callback,
const AccountId& account_id);
Expand All @@ -32,7 +33,6 @@ class DeskSyncService : public KeyedService {
~DeskSyncService() override;

virtual DeskModel* GetDeskModel();
virtual DeskSyncBridge* GetDeskSyncBridge();

virtual base::WeakPtr<syncer::ModelTypeControllerDelegate>
GetControllerDelegate();
Expand Down
17 changes: 13 additions & 4 deletions components/desks_storage/core/local_desk_data_manager.cc
Expand Up @@ -61,8 +61,9 @@ std::unique_ptr<ash::DeskTemplate> ReadFileToTemplate(
base::BlockingType::MAY_BLOCK);

std::string value_string;
if (!base::ReadFileToString(fully_qualified_path, &value_string))
if (!base::ReadFileToString(fully_qualified_path, &value_string)) {
return nullptr;
}

std::string error_message;
int error_code;
Expand Down Expand Up @@ -92,8 +93,9 @@ bool EndsWith(const char* input, const char* suffix) {
// TODO(crbug.com/1320836): Make template creation for
// local_desk_data_manager_unittests cleaner.
bool IsValidTemplateFileName(const char* name) {
if (name == nullptr)
if (name == nullptr) {
return false;
}
return EndsWith(name, kFileExtension);
}

Expand Down Expand Up @@ -190,8 +192,9 @@ DeskModel::GetAllEntriesResult LocalDeskDataManager::GetAllEntries() {
std::move(entries));
}

for (const auto& it : policy_entries_)
for (const auto& it : policy_entries_) {
entries.push_back(it.get());
}

for (auto& saved_desk : saved_desks_list_) {
for (auto& [uuid, template_entry] : saved_desk.second) {
Expand Down Expand Up @@ -408,6 +411,11 @@ ash::DeskTemplate* LocalDeskDataManager::FindOtherEntryWithName(
saved_desks_list_.at(type));
}

// There is no cache guid associated with local desk storage.
std::string LocalDeskDataManager::GetCacheGuid() {
return std::string();
}

void LocalDeskDataManager::UpdateEntry(
std::unique_ptr<ash::DeskTemplate> entry) {
// TODO(b/281857868) implement update entry logic.
Expand Down Expand Up @@ -521,8 +529,9 @@ LocalDeskDataManager::DeleteTaskResult LocalDeskDataManager::DeleteEntryTask(
GetFullyQualifiedPath(local_saved_desk_path, uuid);
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
if (base::DeleteFile(fully_qualified_path))
if (base::DeleteFile(fully_qualified_path)) {
return {DeleteEntryStatus::kOk, std::move(roll_back_entry)};
}
return {DeleteEntryStatus::kFailure, std::move(roll_back_entry)};
}

Expand Down
1 change: 1 addition & 0 deletions components/desks_storage/core/local_desk_data_manager.h
Expand Up @@ -112,6 +112,7 @@ class LocalDeskDataManager : public DeskModel, public AdminTemplateModel {
const std::u16string& name,
ash::DeskTemplateType type,
const base::Uuid& uuid) const override;
std::string GetCacheGuid() override;

// AdminTemplateModel:
void UpdateEntry(std::unique_ptr<ash::DeskTemplate> entry) override;
Expand Down

0 comments on commit 21b0886

Please sign in to comment.