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 : profile_(NULL), |
| 14 prefs_(NULL), |
| 15 controller_(controller), |
| 16 model_(NULL), |
| 17 service_(NULL) { |
| 18 } |
| 19 |
| 20 ArcAppModelBuilder::~ArcAppModelBuilder() { |
| 21 prefs_->RemoveObserver(this); |
| 22 } |
| 23 |
| 24 void ArcAppModelBuilder::InitializeWithService( |
| 25 app_list::AppListSyncableService* service, |
| 26 app_list::AppListModel* model) { |
| 27 DCHECK(!service_ && !profile_); |
| 28 model_ = model; |
| 29 service_ = service; |
| 30 profile_ = service->profile(); |
| 31 |
| 32 BuildModel(); |
| 33 } |
| 34 |
| 35 void ArcAppModelBuilder::InitializeWithProfile(Profile* profile, |
| 36 app_list::AppListModel* model) { |
| 37 DCHECK(!service_ && !profile_); |
| 38 model_ = model; |
| 39 profile_ = profile; |
| 40 |
| 41 BuildModel(); |
| 42 } |
| 43 |
| 44 void ArcAppModelBuilder::BuildModel() { |
| 45 prefs_ = ArcAppPrefs::Get(profile_); |
| 46 |
| 47 PopulateApps(); |
| 48 prefs_->AddObserver(this); |
| 49 } |
| 50 |
| 51 void ArcAppModelBuilder::PopulateApps() { |
| 52 std::vector<std::string> app_ids = prefs_->GetAppIds(); |
| 53 for (size_t i = 0; i < app_ids.size(); ++i) { |
| 54 const std::string app_id = app_ids[i]; |
| 55 scoped_ptr<ArcAppPrefs::AppInfo> app_info = prefs_->GetApp(app_id); |
| 56 if (!app_info) { |
| 57 continue; |
| 58 } |
| 59 InsertApp(CreateApp(app_id, *app_info)); |
| 60 } |
| 61 } |
| 62 |
| 63 ArcAppItem* ArcAppModelBuilder::GetArcAppItem(const std::string& item_id) { |
| 64 app_list::AppListItem* item = model_->FindItem(item_id); |
| 65 LOG_IF(ERROR, item && |
| 66 item->GetItemType() != ArcAppItem::kItemType) |
| 67 << "App Item matching id: " << item_id |
| 68 << " has incorrect type: '" << item->GetItemType() << "'"; |
| 69 return static_cast<ArcAppItem*>(item); |
| 70 } |
| 71 |
| 72 scoped_ptr<ArcAppItem> ArcAppModelBuilder::CreateApp( |
| 73 const std::string& app_id, |
| 74 const ArcAppPrefs::AppInfo& app_info) { |
| 75 scoped_ptr<ArcAppItem> app_item(new ArcAppItem(profile_, |
| 76 app_id, |
| 77 app_info.name, |
| 78 app_info.ready)); |
| 79 for (size_t j = 0; j < app_info.icons.size(); ++j) { |
| 80 const ArcAppPrefs::IconInfo& icon_info = app_info.icons[j]; |
| 81 app_item->UpdateIcon(icon_info.scale_factor, icon_info.path); |
| 82 } |
| 83 return app_item; |
| 84 } |
| 85 |
| 86 void ArcAppModelBuilder::InsertApp(scoped_ptr<ArcAppItem> app) { |
| 87 DCHECK(!GetArcAppItem(app->id())); |
| 88 if (service_) { |
| 89 service_->AddItem(app.Pass()); |
| 90 return; |
| 91 } |
| 92 model_->AddItem(app.Pass()); |
| 93 } |
| 94 |
| 95 void ArcAppModelBuilder::OnAppRegistered(const std::string& app_id, |
| 96 const ArcAppPrefs::AppInfo& app_info) { |
| 97 InsertApp(CreateApp(app_id, app_info)); |
| 98 } |
| 99 |
| 100 void ArcAppModelBuilder::OnAppReady(const std::string& app_id, bool ready) { |
| 101 ArcAppItem* app_item = GetArcAppItem(app_id); |
| 102 if (!app_item) { |
| 103 LOG(WARNING) << "Could not update the state of arc app(" << app_id |
| 104 << ") because it was not found."; |
| 105 return; |
| 106 } |
| 107 |
| 108 app_item->SetReady(ready); |
| 109 } |
| 110 |
| 111 void ArcAppModelBuilder::OnAppIconUpdated( |
| 112 const std::string& app_id, |
| 113 const ArcAppPrefs::IconInfo& icon) { |
| 114 ArcAppItem* app_item = GetArcAppItem(app_id); |
| 115 if (!app_item) { |
| 116 LOG(WARNING) << "Could not update the icon of arc app(" << app_id |
| 117 << ") because it was not found."; |
| 118 return; |
| 119 } |
| 120 |
| 121 app_item->UpdateIcon(icon.scale_factor, icon.path); |
| 122 } |
OLD | NEW |