Chromium Code Reviews| Index: chrome/browser/ui/app_list/arc_app_model_builder.cc |
| diff --git a/chrome/browser/ui/app_list/arc_app_model_builder.cc b/chrome/browser/ui/app_list/arc_app_model_builder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8d7b29ab0043eef7150f8754b5d4ddb324698d6 |
| --- /dev/null |
| +++ b/chrome/browser/ui/app_list/arc_app_model_builder.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright (c) 2015 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 "chrome/browser/ui/app_list/arc_app_model_builder.h" |
| + |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/app_list/app_list_syncable_service.h" |
| +#include "chrome/browser/ui/app_list/arc_app_item.h" |
| +#include "ui/app_list/app_list_model.h" |
| + |
| +ArcAppModelBuilder::ArcAppModelBuilder(AppListControllerDelegate* controller) |
| + : profile_(NULL), |
| + prefs_(NULL), |
| + controller_(controller), |
| + model_(NULL), |
| + service_(NULL) { |
| +} |
| + |
| +ArcAppModelBuilder::~ArcAppModelBuilder() { |
| + prefs_->RemoveObserver(this); |
| +} |
| + |
| +void ArcAppModelBuilder::InitializeWithService( |
| + app_list::AppListSyncableService* service, |
| + app_list::AppListModel* model) { |
| + DCHECK(!service_ && !profile_); |
| + model_ = model; |
| + service_ = service; |
| + profile_ = service->profile(); |
| + |
| + BuildModel(); |
| +} |
| + |
| +void ArcAppModelBuilder::InitializeWithProfile(Profile* profile, |
| + app_list::AppListModel* model) { |
| + DCHECK(!service_ && !profile_); |
| + model_ = model; |
| + profile_ = profile; |
| + |
| + BuildModel(); |
| +} |
| + |
| +void ArcAppModelBuilder::BuildModel() { |
| + prefs_ = ArcAppPrefs::Get(profile_); |
| + |
| + PopulateApps(); |
| + prefs_->AddObserver(this); |
| +} |
| + |
| +void ArcAppModelBuilder::PopulateApps() { |
| + std::vector<std::string> app_ids = prefs_->GetAppIds(); |
| + for (size_t i = 0; i < app_ids.size(); ++i) { |
| + const std::string app_id = app_ids[i]; |
| + scoped_ptr<ArcAppPrefs::AppInfo> app_info = prefs_->GetApp(app_id); |
| + if (!app_info) { |
| + continue; |
| + } |
| + InsertApp(CreateApp(app_id, *app_info)); |
| + } |
| +} |
| + |
| +ArcAppItem* ArcAppModelBuilder::GetArcAppItem(const std::string& item_id) { |
| + app_list::AppListItem* item = model_->FindItem(item_id); |
| + LOG_IF(ERROR, item && |
| + item->GetItemType() != ArcAppItem::kItemType) |
| + << "App Item matching id: " << item_id |
| + << " has incorrect type: '" << item->GetItemType() << "'"; |
| + return static_cast<ArcAppItem*>(item); |
| +} |
| + |
| +scoped_ptr<ArcAppItem> ArcAppModelBuilder::CreateApp( |
| + const std::string& app_id, |
| + const ArcAppPrefs::AppInfo& app_info) { |
| + scoped_ptr<ArcAppItem> app_item(new ArcAppItem(profile_, |
| + app_id, |
| + app_info.name, |
| + app_info.enabled)); |
| + for (size_t j = 0; j < app_info.icons.size(); ++j) { |
| + const ArcAppPrefs::IconInfo& icon_info = app_info.icons[j]; |
| + app_item->UpdateIcon(icon_info.scale_factor, icon_info.path); |
| + } |
| + return app_item; |
| +} |
| + |
| +void ArcAppModelBuilder::InsertApp(scoped_ptr<ArcAppItem> app) { |
| + DCHECK(!GetArcAppItem(app->id())); |
| + if (service_) { |
| + service_->AddItem(app.Pass()); |
| + return; |
| + } |
| + model_->AddItem(app.Pass()); |
| +} |
| + |
| +void ArcAppModelBuilder::OnAppRegistered(const std::string& app_id, |
| + const ArcAppPrefs::AppInfo& app_info) { |
| + InsertApp(CreateApp(app_id, app_info)); |
| +} |
| + |
| +void ArcAppModelBuilder::OnAppEnabled(const std::string& app_id, bool enabled) { |
| + ArcAppItem* app_item = GetArcAppItem(app_id); |
| + if (!app_item) { |
| + return; |
|
elijahtaylor1
2015/10/28 06:32:38
should this be a CHECK? (same Q for OnAppIconUpdat
khmel1
2015/10/29 08:12:18
I took a look into ExtensionAppModelBuilder. Simil
|
| + } |
| + |
| + app_item->SetEnabled(enabled); |
| +} |
| + |
| +void ArcAppModelBuilder::OnAppIconUpdated( |
| + const std::string& app_id, |
| + const ArcAppPrefs::IconInfo& icon) { |
| + ArcAppItem* app_item = GetArcAppItem(app_id); |
| + if (!app_item) { |
| + return; |
| + } |
| + |
| + app_item->UpdateIcon(icon.scale_factor, icon.path); |
| +} |