Skip to content

Commit

Permalink
Save And Recall: Create desk model wrapper
Browse files Browse the repository at this point in the history
This CL creates a wrapper desk model so that we can keep generic
calls to the desk model by wrapping both desk type backend storage into
one.

BUG=b/231610203
TEST=DeskTemplatesTest*, DeskSync*, DeskTemplatesClient*

Change-Id: I1979854df42a7309a79b7a1a45076c38d8aa75a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3633187
Commit-Queue: Matthew Zhu <zhumatthew@google.com>
Reviewed-by: Daniel Andersson <dandersson@chromium.org>
Reviewed-by: Yanzhu Du <yzd@google.com>
Reviewed-by: Yongshun Liu <yongshun@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1001805}
  • Loading branch information
zhumatthew authored and Chromium LUCI CQ committed May 10, 2022
1 parent 240310d commit 2e5c472
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 32 deletions.
16 changes: 16 additions & 0 deletions ash/wm/desks/templates/desks_templates_presenter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ size_t DesksTemplatesPresenter::GetMaxEntryCount() const {
return GetDeskModel()->GetMaxEntryCount();
}

size_t DesksTemplatesPresenter::GetDeskTemplateEntryCount() const {
return GetDeskModel()->GetDeskTemplateEntryCount();
}

size_t DesksTemplatesPresenter::GetMaxDeskTemplateEntryCount() const {
return GetDeskModel()->GetMaxDeskTemplateEntryCount();
}

size_t DesksTemplatesPresenter::GetSaveAndRecallDeskEntryCount() const {
return GetDeskModel()->GetSaveAndRecallDeskEntryCount();
}

size_t DesksTemplatesPresenter::GetMaxSaveAndRecallDeskEntryCount() const {
return GetDeskModel()->GetMaxSaveAndRecallDeskEntryCount();
}

void DesksTemplatesPresenter::UpdateDesksTemplatesUI() {
// The save as desk template button is hidden in tablet mode. The desks
// templates button on the desk bar view and the desks templates grid are
Expand Down
8 changes: 8 additions & 0 deletions ash/wm/desks/templates/desks_templates_presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class ASH_EXPORT DesksTemplatesPresenter : desks_storage::DeskModelObserver {

size_t GetMaxEntryCount() const;

size_t GetDeskTemplateEntryCount() const;

size_t GetMaxDeskTemplateEntryCount() const;

size_t GetSaveAndRecallDeskEntryCount() const;

size_t GetMaxSaveAndRecallDeskEntryCount() const;

// Update the buttons of the desks templates UI and the visibility of the
// templates grid. The grid contents are not updated. Updates
// `should_show_templates_ui_`.
Expand Down
47 changes: 38 additions & 9 deletions chrome/browser/ui/ash/desks_templates/desks_templates_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ash/public/cpp/desk_template.h"
#include "ash/public/cpp/session/session_controller.h"
#include "ash/wm/desks/desks_controller.h"
#include "ash/wm/desks/templates/saved_desk_util.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
Expand All @@ -25,6 +26,7 @@
#include "components/app_constants/constants.h"
#include "components/app_restore/app_restore_info.h"
#include "components/app_restore/window_properties.h"
#include "components/desks_storage/core/desk_model_wrapper.h"
#include "components/desks_storage/core/desk_sync_service.h"
#include "components/desks_storage/core/local_desk_data_manager.h"
#include "ui/views/widget/widget.h"
Expand Down Expand Up @@ -191,9 +193,23 @@ void DesksTemplatesClient::OnActiveUserSessionChanged(
active_profile_ = profile;
DCHECK(active_profile_);

if (!chromeos::features::IsDeskTemplateSyncEnabled()) {
storage_manager_ = std::make_unique<desks_storage::LocalDeskDataManager>(
active_profile_->GetPath(), account_id);
if (chromeos::features::IsSavedDesksEnabled()) {
save_and_recall_desks_storage_manager_ =
std::make_unique<desks_storage::LocalDeskDataManager>(
active_profile_->GetPath(), account_id);

if (ash::saved_desk_util::AreDesksTemplatesEnabled()) {
saved_desk_storage_manager_ =
std::make_unique<desks_storage::DeskModelWrapper>(
save_and_recall_desks_storage_manager_.get());
}

} else {
if (!chromeos::features::IsDeskTemplateSyncEnabled()) {
desk_templates_storage_manager_ =
std::make_unique<desks_storage::LocalDeskDataManager>(
active_profile_->GetPath(), account_id);
}
}

auto policy_desk_templates_it =
Expand Down Expand Up @@ -352,13 +368,26 @@ void DesksTemplatesClient::LaunchAppsFromTemplate(
}

desks_storage::DeskModel* DesksTemplatesClient::GetDeskModel() {
if (chromeos::features::IsDeskTemplateSyncEnabled()) {
return DeskSyncServiceFactory::GetForProfile(active_profile_)
->GetDeskModel();
}
if (chromeos::features::IsSavedDesksEnabled()) {
if (!ash::saved_desk_util::AreDesksTemplatesEnabled()) {
DCHECK(save_and_recall_desks_storage_manager_.get());
return save_and_recall_desks_storage_manager_.get();
}
DCHECK(saved_desk_storage_manager_);
saved_desk_storage_manager_->SetDeskSyncBridge(
static_cast<desks_storage::DeskSyncBridge*>(
DeskSyncServiceFactory::GetForProfile(active_profile_)
->GetDeskModel()));
return saved_desk_storage_manager_.get();
} else {
if (chromeos::features::IsDeskTemplateSyncEnabled()) {
return DeskSyncServiceFactory::GetForProfile(active_profile_)
->GetDeskModel();
}

DCHECK(storage_manager_.get());
return storage_manager_.get();
DCHECK(desk_templates_storage_manager_.get());
return desk_templates_storage_manager_.get();
}
}

// Sets the preconfigured desk template. Data contains the contents of the JSON
Expand Down
13 changes: 11 additions & 2 deletions chrome/browser/ui/ash/desks_templates/desks_templates_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DesksController;
namespace desks_storage {
class DeskModel;
class LocalDeskDataManager;
class DeskModelWrapper;
} // namespace desks_storage

// Class to handle all Desks in-browser functionalities. Will call into
Expand Down Expand Up @@ -217,8 +218,16 @@ class DesksTemplatesClient : public ash::SessionObserver {
// A test only template for testing `LaunchDeskTemplate`.
std::unique_ptr<ash::DeskTemplate> launch_template_for_test_;

// Local desks storage backend.
std::unique_ptr<desks_storage::LocalDeskDataManager> storage_manager_;
// Local desks storage backend for desk templates.
std::unique_ptr<desks_storage::LocalDeskDataManager>
desk_templates_storage_manager_;

// Local desks storage backend for save and recall desks.
std::unique_ptr<desks_storage::LocalDeskDataManager>
save_and_recall_desks_storage_manager_;

// Wrapper desk model to house both desk types backend storage.
std::unique_ptr<desks_storage::DeskModelWrapper> saved_desk_storage_manager_;

// The stored JSON values of preconfigured desk templates
base::flat_map<AccountId, std::string> preconfigured_desk_templates_json_;
Expand Down
2 changes: 2 additions & 0 deletions components/desks_storage/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ static_library("desks_storage") {
"core/desk_model.cc",
"core/desk_model.h",
"core/desk_model_observer.h",
"core/desk_model_wrapper.cc",
"core/desk_model_wrapper.h",
"core/desk_sync_bridge.cc",
"core/desk_sync_bridge.h",
"core/desk_sync_service.cc",
Expand Down
16 changes: 15 additions & 1 deletion components/desks_storage/core/desk_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,24 @@ class DeskModel {
virtual std::size_t GetEntryCount() const = 0;

// Gets the maximum number of templates this storage backend could hold.
// Adding more templates beyond this limit will result in |kHitMaximumLimit|
// Adding more templates beyond this limit will result in `kHitMaximumLimit`
// error.
virtual std::size_t GetMaxEntryCount() const = 0;

// Gets the number of save and recall desks currently saved.
virtual std::size_t GetSaveAndRecallDeskEntryCount() const = 0;

// Gets the number of desk templates currently saved.
virtual std::size_t GetDeskTemplateEntryCount() const = 0;

// Gets the maximum number of save and recall desks entry this storage backend
// could hold.
virtual std::size_t GetMaxSaveAndRecallDeskEntryCount() const = 0;

// Gets the maximum number of desk template entry this storage backend
// could hold.
virtual std::size_t GetMaxDeskTemplateEntryCount() const = 0;

// Returns a vector of desk template UUIDs.
// This method assumes each implementation has a cache and can return the
// UUIDs synchronously.
Expand Down
162 changes: 162 additions & 0 deletions components/desks_storage/core/desk_model_wrapper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/desks_storage/core/desk_model_wrapper.h"

#include "ash/public/cpp/desk_template.h"
#include "base/guid.h"
#include "base/logging.h"
#include "components/account_id/account_id.h"
#include "components/desks_storage/core/desk_model.h"
#include "desk_sync_bridge.h"
#include "local_desk_data_manager.h"

namespace desks_storage {

DeskModelWrapper::DeskModelWrapper(
desks_storage::DeskModel* save_and_recall_desks_model)
: save_and_recall_desks_model_(save_and_recall_desks_model) {}

DeskModelWrapper::~DeskModelWrapper() = default;

void DeskModelWrapper::GetAllEntries(
DeskModel::GetAllEntriesCallback callback) {
auto template_entries = std::vector<const ash::DeskTemplate*>();
auto desk_template_status =
GetDeskTemplateModel()->GetAllEntries(template_entries);
if (desk_template_status != DeskModel::GetAllEntriesStatus::kOk) {
std::move(callback).Run(DeskModel::GetAllEntriesStatus::kFailure,
template_entries);
return;
}

save_and_recall_desks_model_->GetAllEntries(base::BindOnce(
&DeskModelWrapper::OnGetAllEntries, weak_ptr_factory_.GetWeakPtr(),
template_entries, std::move(callback)));
}

void DeskModelWrapper::GetEntryByUUID(
const std::string& uuid,
DeskModel::GetEntryByUuidCallback callback) {
if (GetDeskTemplateModel()->HasUuid(uuid)) {
GetDeskTemplateModel()->GetEntryByUUID(uuid, std::move(callback));
} else {
save_and_recall_desks_model_->GetEntryByUUID(uuid, std::move(callback));
}
}

void DeskModelWrapper::AddOrUpdateEntry(
std::unique_ptr<ash::DeskTemplate> new_entry,
DeskModel::AddOrUpdateEntryCallback callback) {
if (new_entry->type() == ash::DeskTemplateType::kTemplate) {
GetDeskTemplateModel()->AddOrUpdateEntry(std::move(new_entry),
std::move(callback));
} else {
save_and_recall_desks_model_->AddOrUpdateEntry(std::move(new_entry),
std::move(callback));
}
}

void DeskModelWrapper::DeleteEntry(const std::string& uuid_str,
DeskModel::DeleteEntryCallback callback) {
auto status = std::make_unique<DeskModel::DeleteEntryStatus>();
if (GetDeskTemplateModel()->HasUuid(uuid_str)) {
GetDeskTemplateModel()->DeleteEntry(uuid_str, std::move(callback));
} else {
save_and_recall_desks_model_->DeleteEntry(uuid_str, std::move(callback));
}
}

void DeskModelWrapper::DeleteAllEntries(
DeskModel::DeleteEntryCallback callback) {
DeskModel::DeleteEntryStatus desk_template_delete_status =
GetDeskTemplateModel()->DeleteAllEntries();
if (desk_template_delete_status != DeskModel::DeleteEntryStatus::kOk) {
std::move(callback).Run(desk_template_delete_status);
return;
}
save_and_recall_desks_model_->DeleteAllEntries(
base::BindOnce(&DeskModelWrapper::OnDeleteAllEntries,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

// TODO(crbug.com/1320805): Remove this function once both desk models support
// desk type counts.
size_t DeskModelWrapper::GetEntryCount() const {
return GetSaveAndRecallDeskEntryCount() + GetDeskTemplateEntryCount();
}

size_t DeskModelWrapper::GetSaveAndRecallDeskEntryCount() const {
return save_and_recall_desks_model_->GetSaveAndRecallDeskEntryCount();
}

size_t DeskModelWrapper::GetDeskTemplateEntryCount() const {
return GetDeskTemplateModel()->GetDeskTemplateEntryCount();
}

size_t DeskModelWrapper::GetMaxEntryCount() const {
return GetMaxSaveAndRecallDeskEntryCount() + GetMaxDeskTemplateEntryCount();
}

size_t DeskModelWrapper::GetMaxSaveAndRecallDeskEntryCount() const {
return save_and_recall_desks_model_->GetMaxSaveAndRecallDeskEntryCount();
}

size_t DeskModelWrapper::GetMaxDeskTemplateEntryCount() const {
return GetDeskTemplateModel()->GetMaxDeskTemplateEntryCount();
}

std::vector<base::GUID> DeskModelWrapper::GetAllEntryUuids() const {
std::vector<base::GUID> keys;
for (const auto& save_and_recall_uuid :
save_and_recall_desks_model_->GetAllEntryUuids()) {
keys.emplace_back(save_and_recall_uuid);
}

for (const auto& desk_template_uuid :
GetDeskTemplateModel()->GetAllEntryUuids()) {
keys.emplace_back(desk_template_uuid);
}
return keys;
}

bool DeskModelWrapper::IsReady() const {
return save_and_recall_desks_model_->IsReady() &&
GetDeskTemplateModel()->IsReady();
}

bool DeskModelWrapper::IsSyncing() const {
return GetDeskTemplateModel()->IsSyncing();
}

desks_storage::DeskSyncBridge* DeskModelWrapper::GetDeskTemplateModel() const {
DCHECK(desk_template_model_);
return desk_template_model_;
}

void DeskModelWrapper::OnGetAllEntries(
const std::vector<const ash::DeskTemplate*>& template_entries,
DeskModel::GetAllEntriesCallback callback,
desks_storage::DeskModel::GetAllEntriesStatus status,
const std::vector<const ash::DeskTemplate*>& entries) {
if (status != DeskModel::GetAllEntriesStatus::kOk) {
std::move(callback).Run(status, template_entries);
return;
}

auto all_entries = template_entries;

for (auto* const entry : entries)
all_entries.push_back(entry);

std::move(callback).Run(status, all_entries);
}

void DeskModelWrapper::OnDeleteAllEntries(
DeskModel::DeleteEntryCallback callback,
desks_storage::DeskModel::DeleteEntryStatus status) {
std::move(callback).Run(status);
}

} // namespace desks_storage

0 comments on commit 2e5c472

Please sign in to comment.