OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 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_ = ArcAppListPrefs::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) { | |
xiyuan
2015/11/18 03:42:16
nit: for (auto& app_id : app_ids) {
khmel1
2015/11/18 06:10:04
Done.
| |
50 const std::string app_id = app_ids[i]; | |
51 scoped_ptr<ArcAppListPrefs::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 ArcAppListPrefs::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( | |
91 const std::string& app_id, | |
92 const ArcAppListPrefs::AppInfo& app_info) { | |
93 InsertApp(CreateApp(app_id, app_info)); | |
94 } | |
95 | |
96 void ArcAppModelBuilder::OnAppReadyChanged(const std::string& app_id, | |
97 bool ready) { | |
98 ArcAppItem* app_item = GetArcAppItem(app_id); | |
99 if (!app_item) { | |
100 LOG(WARNING) << "Could not update the state of ARC app(" << app_id | |
101 << ") because it was not found."; | |
102 return; | |
103 } | |
104 | |
105 app_item->SetReady(ready); | |
106 } | |
107 | |
108 void ArcAppModelBuilder::OnAppIconUpdated(const std::string& app_id, | |
109 ui::ScaleFactor scale_factor) { | |
110 ArcAppItem* app_item = GetArcAppItem(app_id); | |
111 if (!app_item) { | |
112 LOG(WARNING) << "Could not update the icon of ARC app(" << app_id | |
113 << ") because it was not found."; | |
114 return; | |
115 } | |
116 | |
117 // Initiate async icon reloading. | |
118 app_item->arc_app_icon()->LoadForScaleFactor(scale_factor); | |
119 } | |
OLD | NEW |