OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/ash/launcher/chrome_mash_shelf_controller.h" | |
6 | |
7 #include "chrome/browser/extensions/extension_app_icon_loader.h" | |
8 #include "chrome/browser/profiles/profile_manager.h" | |
9 #include "chrome/browser/ui/app_list/arc/arc_app_icon_loader.h" | |
10 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h" | |
11 #include "chrome/browser/ui/ash/launcher/chrome_launcher_types.h" | |
12 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h" | |
13 #include "chrome/grit/theme_resources.h" | |
14 #include "content/public/common/service_manager_connection.h" | |
15 #include "extensions/common/constants.h" | |
16 #include "extensions/grit/extensions_browser_resources.h" | |
17 #include "mojo/common/common_type_converters.h" | |
18 #include "services/shell/public/cpp/connector.h" | |
19 #include "ui/base/resource/resource_bundle.h" | |
20 #include "ui/display/display.h" | |
21 #include "ui/display/screen.h" | |
22 | |
23 class ChromeShelfItemDelegate : public ash::mojom::ShelfItemDelegate { | |
24 public: | |
25 explicit ChromeShelfItemDelegate(const std::string& app_id, | |
26 ChromeMashShelfController* controller) | |
27 : app_id_(app_id), | |
28 item_delegate_binding_(this), | |
29 controller_(controller) {} | |
30 ~ChromeShelfItemDelegate() override {} | |
31 | |
32 ash::mojom::ShelfItemDelegateAssociatedPtrInfo CreateInterfacePtrInfoAndBind( | |
33 mojo::AssociatedGroup* associated_group) { | |
34 DCHECK(!item_delegate_binding_.is_bound()); | |
35 ash::mojom::ShelfItemDelegateAssociatedPtrInfo ptr_info; | |
36 item_delegate_binding_.Bind(&ptr_info, associated_group); | |
37 return ptr_info; | |
38 } | |
39 | |
40 private: | |
41 // ash::mojom::ShelfItemDelegate: | |
42 void LaunchItem() override { controller_->LaunchItem(app_id_); } | |
43 void ExecuteCommand(uint32_t command_id, int32_t event_flags) override { | |
44 NOTIMPLEMENTED(); | |
45 } | |
46 void ItemPinned() override { NOTIMPLEMENTED(); } | |
47 void ItemUnpinned() override { NOTIMPLEMENTED(); } | |
48 void ItemReordered(uint32_t order) override { NOTIMPLEMENTED(); } | |
49 | |
50 std::string app_id_; | |
51 mojo::AssociatedBinding<ash::mojom::ShelfItemDelegate> item_delegate_binding_; | |
52 | |
53 // Not owned. | |
54 ChromeMashShelfController* controller_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(ChromeShelfItemDelegate); | |
57 }; | |
58 | |
59 ChromeMashShelfController::ChromeMashShelfController() | |
60 : helper_(ProfileManager::GetActiveUserProfile()), | |
61 observer_binding_(this) { | |
62 Init(); | |
63 } | |
64 | |
65 ChromeMashShelfController::~ChromeMashShelfController() {} | |
66 | |
67 void ChromeMashShelfController::LaunchItem(const std::string& app_id) { | |
68 helper_.LaunchApp(app_id, ash::LAUNCH_FROM_UNKNOWN, ui::EF_NONE); | |
69 } | |
70 | |
71 void ChromeMashShelfController::Init() { | |
72 shell::Connector* connector = | |
73 content::ServiceManagerConnection::GetForProcess()->GetConnector(); | |
74 connector->ConnectToInterface("service:ash", &shelf_controller_); | |
75 | |
76 // Initialize shelf alignment and auto-hide behavior from preferences. | |
77 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) | |
78 OnShelfCreated(display.id()); | |
79 | |
80 // TODO(skuhne): The AppIconLoaderImpl has the same problem. Each loaded | |
81 // image is associated with a profile (its loader requires the profile). | |
82 // Since icon size changes are possible, the icon could be requested to be | |
83 // reloaded. However - having it not multi profile aware would cause problems | |
84 // if the icon cache gets deleted upon user switch. | |
85 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
86 std::unique_ptr<AppIconLoader> extension_app_icon_loader( | |
87 new extensions::ExtensionAppIconLoader( | |
88 profile, extension_misc::EXTENSION_ICON_SMALL, this)); | |
89 app_icon_loaders_.push_back(std::move(extension_app_icon_loader)); | |
90 | |
91 if (arc::ArcAuthService::IsAllowedForProfile(profile)) { | |
92 std::unique_ptr<AppIconLoader> arc_app_icon_loader(new ArcAppIconLoader( | |
93 profile, extension_misc::EXTENSION_ICON_SMALL, this)); | |
94 app_icon_loaders_.push_back(std::move(arc_app_icon_loader)); | |
95 } | |
96 | |
97 PinAppsFromPrefs(); | |
98 | |
99 // Start observing the shelf now that it has been initialized. | |
100 ash::mojom::ShelfObserverAssociatedPtrInfo ptr_info; | |
101 observer_binding_.Bind(&ptr_info, shelf_controller_.associated_group()); | |
102 shelf_controller_->AddObserver(std::move(ptr_info)); | |
103 } | |
104 | |
105 void ChromeMashShelfController::PinAppsFromPrefs() { | |
106 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
107 std::vector<std::string> pinned_apps = | |
108 ash::launcher::GetPinnedAppsFromPrefs(profile->GetPrefs(), &helper_); | |
109 | |
110 for (const auto& app : pinned_apps) { | |
111 if (app == ash::launcher::kPinnedAppsPlaceholder) | |
112 continue; | |
113 | |
114 ash::mojom::ShelfItemPtr item(ash::mojom::ShelfItem::New()); | |
115 item->app_id = app; | |
116 item->app_title = mojo::String::From(helper_.GetAppTitle(profile, app)); | |
117 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
118 const gfx::Image& image = rb.GetImageNamed(IDR_APP_DEFAULT_ICON); | |
119 item->image = *image.ToSkBitmap(); | |
120 std::unique_ptr<ChromeShelfItemDelegate> delegate( | |
121 new ChromeShelfItemDelegate(app, this)); | |
122 shelf_controller_->PinItem(std::move(item), | |
123 delegate->CreateInterfacePtrInfoAndBind( | |
124 shelf_controller_.associated_group())); | |
125 app_id_to_item_delegate_.insert(std::make_pair(app, std::move(delegate))); | |
126 | |
127 AppIconLoader* app_icon_loader = GetAppIconLoaderForApp(app); | |
128 if (app_icon_loader) { | |
129 app_icon_loader->FetchImage(app); | |
130 app_icon_loader->UpdateImage(app); | |
131 } | |
132 } | |
133 } | |
134 | |
135 AppIconLoader* ChromeMashShelfController::GetAppIconLoaderForApp( | |
136 const std::string& app_id) { | |
137 for (const auto& app_icon_loader : app_icon_loaders_) { | |
138 if (app_icon_loader->CanLoadImageForApp(app_id)) | |
139 return app_icon_loader.get(); | |
140 } | |
141 | |
142 return nullptr; | |
143 } | |
144 | |
145 void ChromeMashShelfController::OnShelfCreated(int64_t display_id) { | |
146 // The pref helper functions return default values for invalid display ids. | |
147 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
148 shelf_controller_->SetAlignment( | |
149 ash::launcher::GetShelfAlignmentPref(profile->GetPrefs(), display_id), | |
150 display_id); | |
151 shelf_controller_->SetAutoHideBehavior( | |
152 ash::launcher::GetShelfAutoHideBehaviorPref(profile->GetPrefs(), | |
153 display_id), | |
154 display_id); | |
155 } | |
156 | |
157 void ChromeMashShelfController::OnAlignmentChanged( | |
158 ash::ShelfAlignment alignment, | |
159 int64_t display_id) { | |
160 // The locked alignment is set temporarily and not saved to preferences. | |
161 if (alignment == ash::SHELF_ALIGNMENT_BOTTOM_LOCKED) | |
162 return; | |
163 // This will uselessly store a preference value for invalid display ids. | |
164 ash::launcher::SetShelfAlignmentPref( | |
165 ProfileManager::GetActiveUserProfile()->GetPrefs(), display_id, | |
166 alignment); | |
167 } | |
168 | |
169 void ChromeMashShelfController::OnAutoHideBehaviorChanged( | |
170 ash::ShelfAutoHideBehavior auto_hide, | |
171 int64_t display_id) { | |
172 // This will uselessly store a preference value for invalid display ids. | |
173 ash::launcher::SetShelfAutoHideBehaviorPref( | |
174 ProfileManager::GetActiveUserProfile()->GetPrefs(), display_id, | |
175 auto_hide); | |
176 } | |
177 | |
178 void ChromeMashShelfController::OnAppImageUpdated(const std::string& app_id, | |
179 const gfx::ImageSkia& image) { | |
180 shelf_controller_->SetItemImage(app_id, *image.bitmap()); | |
181 } | |
OLD | NEW |