OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/app_list/arc_app_model_builder.h" |
| 6 |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" |
| 9 #include "chrome/browser/ui/app_list/arc_app_item.h" |
| 10 #include "ui/app_list/app_list_model.h" |
| 11 |
| 12 ArcAppModelBuilder::ArcAppModelBuilder(AppListControllerDelegate* controller) |
| 13 : controller_(controller) { |
| 14 } |
| 15 |
| 16 ArcAppModelBuilder::~ArcAppModelBuilder() { |
| 17 prefs_->RemoveObserver(this); |
| 18 } |
| 19 |
| 20 void ArcAppModelBuilder::InitializeWithService( |
| 21 app_list::AppListSyncableService* service, |
| 22 app_list::AppListModel* model) { |
| 23 DCHECK(!service_ && !profile_); |
| 24 model_ = model; |
| 25 service_ = service; |
| 26 profile_ = service->profile(); |
| 27 |
| 28 BuildModel(); |
| 29 } |
| 30 |
| 31 void ArcAppModelBuilder::InitializeWithProfile(Profile* profile, |
| 32 app_list::AppListModel* model) { |
| 33 DCHECK(!service_ && !profile_); |
| 34 model_ = model; |
| 35 profile_ = profile; |
| 36 |
| 37 BuildModel(); |
| 38 } |
| 39 |
| 40 void ArcAppModelBuilder::BuildModel() { |
| 41 prefs_ = ArcAppPrefs::Get(profile_); |
| 42 |
| 43 PopulateApps(); |
| 44 prefs_->AddObserver(this); |
| 45 } |
| 46 |
| 47 void ArcAppModelBuilder::PopulateApps() { |
| 48 std::vector<std::string> app_ids = prefs_->GetAppIds(); |
| 49 for (size_t i = 0; i < app_ids.size(); ++i) { |
| 50 const std::string app_id = app_ids[i]; |
| 51 scoped_ptr<ArcAppPrefs::AppInfo> app_info = prefs_->GetApp(app_id); |
| 52 if (!app_info) { |
| 53 continue; |
| 54 } |
| 55 InsertApp(CreateApp(app_id, *app_info)); |
| 56 } |
| 57 } |
| 58 |
| 59 ArcAppItem* ArcAppModelBuilder::GetArcAppItem(const std::string& item_id) { |
| 60 app_list::AppListItem* item = model_->FindItem(item_id); |
| 61 LOG_IF(ERROR, item && |
| 62 item->GetItemType() != ArcAppItem::kItemType) |
| 63 << "App Item matching id: " << item_id |
| 64 << " has incorrect type: '" << item->GetItemType() << "'"; |
| 65 return static_cast<ArcAppItem*>(item); |
| 66 } |
| 67 |
| 68 scoped_ptr<ArcAppItem> ArcAppModelBuilder::CreateApp( |
| 69 const std::string& app_id, |
| 70 const ArcAppPrefs::AppInfo& app_info) { |
| 71 const app_list::AppListSyncableService::SyncItem* sync_item = |
| 72 service_ ? service_->GetSyncItem(app_id) : NULL; |
| 73 scoped_ptr<ArcAppItem> app_item(new ArcAppItem(profile_, |
| 74 sync_item, |
| 75 app_id, |
| 76 app_info.name, |
| 77 app_info.ready)); |
| 78 return app_item; |
| 79 } |
| 80 |
| 81 void ArcAppModelBuilder::InsertApp(scoped_ptr<ArcAppItem> app) { |
| 82 DCHECK(!GetArcAppItem(app->id())); |
| 83 if (service_) { |
| 84 service_->AddItem(app.Pass()); |
| 85 return; |
| 86 } |
| 87 model_->AddItem(app.Pass()); |
| 88 } |
| 89 |
| 90 void ArcAppModelBuilder::OnAppRegistered(const std::string& app_id, |
| 91 const ArcAppPrefs::AppInfo& app_info) { |
| 92 InsertApp(CreateApp(app_id, app_info)); |
| 93 } |
| 94 |
| 95 void ArcAppModelBuilder::OnAppReadyChanged(const std::string& app_id, |
| 96 bool ready) { |
| 97 ArcAppItem* app_item = GetArcAppItem(app_id); |
| 98 if (!app_item) { |
| 99 LOG(WARNING) << "Could not update the state of arc app(" << app_id |
| 100 << ") because it was not found."; |
| 101 return; |
| 102 } |
| 103 |
| 104 app_item->SetReady(ready); |
| 105 } |
| 106 |
| 107 void ArcAppModelBuilder::OnAppIconUpdated(const std::string& app_id, |
| 108 ui::ScaleFactor scale_factor) { |
| 109 ArcAppItem* app_item = GetArcAppItem(app_id); |
| 110 if (!app_item) { |
| 111 LOG(WARNING) << "Could not update the icon of arc app(" << app_id |
| 112 << ") because it was not found."; |
| 113 return; |
| 114 } |
| 115 |
| 116 // Initiate async icon reloading. |
| 117 app_item->arc_app_icon()->LoadForScaleFactor(scale_factor); |
| 118 } |
OLD | NEW |