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.enabled)); | |
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::OnAppEnabled(const std::string& app_id, bool enabled) { | |
101 ArcAppItem* app_item = GetArcAppItem(app_id); | |
102 if (!app_item) { | |
103 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
| |
104 } | |
105 | |
106 app_item->SetEnabled(enabled); | |
107 } | |
108 | |
109 void ArcAppModelBuilder::OnAppIconUpdated( | |
110 const std::string& app_id, | |
111 const ArcAppPrefs::IconInfo& icon) { | |
112 ArcAppItem* app_item = GetArcAppItem(app_id); | |
113 if (!app_item) { | |
114 return; | |
115 } | |
116 | |
117 app_item->UpdateIcon(icon.scale_factor, icon.path); | |
118 } | |
OLD | NEW |