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), | |
xiyuan
2015/11/11 22:15:50
nit: Use in-class member initialization and use nu
khmel1
2015/11/12 08:05:30
Done.
| |
14 prefs_(NULL), | |
15 controller_(controller), | |
16 model_(NULL), | |
17 service_(NULL) { | |
18 } | |
19 | |
20 ArcAppModelBuilder::~ArcAppModelBuilder() { | |
21 prefs_->RemoveObserver(this); | |
xiyuan
2015/11/11 22:15:50
Think we need to make AppListSyncableService depen
khmel1
2015/11/12 08:05:30
Done.
| |
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(); | |
xiyuan
2015/11/11 22:15:50
nit: #include <vector>
khmel1
2015/11/12 08:05:30
Done.
| |
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 const app_list::AppListSyncableService::SyncItem* sync_item = | |
76 service_ ? service_->GetSyncItem(app_id) : NULL; | |
77 scoped_ptr<ArcAppItem> app_item(new ArcAppItem(profile_, | |
78 sync_item, | |
79 app_id, | |
80 app_info.name, | |
81 app_info.ready)); | |
82 return app_item; | |
83 } | |
84 | |
85 void ArcAppModelBuilder::InsertApp(scoped_ptr<ArcAppItem> app) { | |
86 DCHECK(!GetArcAppItem(app->id())); | |
87 if (service_) { | |
88 service_->AddItem(app.Pass()); | |
89 return; | |
90 } | |
91 model_->AddItem(app.Pass()); | |
92 } | |
93 | |
94 void ArcAppModelBuilder::OnAppRegistered(const std::string& app_id, | |
95 const ArcAppPrefs::AppInfo& app_info) { | |
96 InsertApp(CreateApp(app_id, app_info)); | |
97 } | |
98 | |
99 void ArcAppModelBuilder::OnAppReady(const std::string& app_id, bool ready) { | |
100 ArcAppItem* app_item = GetArcAppItem(app_id); | |
101 if (!app_item) { | |
102 LOG(WARNING) << "Could not update the state of arc app(" << app_id | |
103 << ") because it was not found."; | |
104 return; | |
105 } | |
106 | |
107 app_item->SetReady(ready); | |
108 } | |
109 | |
110 void ArcAppModelBuilder::OnAppIconUpdated(const std::string& app_id, | |
111 ui::ScaleFactor scale_factor) { | |
112 ArcAppItem* app_item = GetArcAppItem(app_id); | |
113 if (!app_item) { | |
114 LOG(WARNING) << "Could not update the icon of arc app(" << app_id | |
115 << ") because it was not found."; | |
116 return; | |
117 } | |
118 | |
119 // Initiate async icon reloading. | |
120 app_item->icon()->LoadForScaleFactor(scale_factor); | |
121 } | |
OLD | NEW |