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()), observer_binding_(this) { |
| 61 Init(); |
| 62 } |
| 63 |
| 64 ChromeMashShelfController::~ChromeMashShelfController() {} |
| 65 |
| 66 void ChromeMashShelfController::LaunchItem(const std::string& app_id) { |
| 67 helper_.LaunchApp(app_id, ash::LAUNCH_FROM_UNKNOWN, ui::EF_NONE); |
| 68 } |
| 69 |
| 70 void ChromeMashShelfController::Init() { |
| 71 shell::Connector* connector = |
| 72 content::ServiceManagerConnection::GetForProcess()->GetConnector(); |
| 73 connector->ConnectToInterface("service:ash", &shelf_controller_); |
| 74 |
| 75 // Initialize shelf alignment and auto-hide behavior from preferences. |
| 76 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) |
| 77 OnShelfCreated(display.id()); |
| 78 |
| 79 // TODO(skuhne): The AppIconLoaderImpl has the same problem. Each loaded |
| 80 // image is associated with a profile (its loader requires the profile). |
| 81 // Since icon size changes are possible, the icon could be requested to be |
| 82 // reloaded. However - having it not multi profile aware would cause problems |
| 83 // if the icon cache gets deleted upon user switch. |
| 84 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 85 std::unique_ptr<AppIconLoader> extension_app_icon_loader( |
| 86 new extensions::ExtensionAppIconLoader( |
| 87 profile, extension_misc::EXTENSION_ICON_SMALL, this)); |
| 88 app_icon_loaders_.push_back(std::move(extension_app_icon_loader)); |
| 89 |
| 90 if (arc::ArcAuthService::IsAllowedForProfile(profile)) { |
| 91 std::unique_ptr<AppIconLoader> arc_app_icon_loader(new ArcAppIconLoader( |
| 92 profile, extension_misc::EXTENSION_ICON_SMALL, this)); |
| 93 app_icon_loaders_.push_back(std::move(arc_app_icon_loader)); |
| 94 } |
| 95 |
| 96 PinAppsFromPrefs(); |
| 97 |
| 98 // Start observing the shelf now that it has been initialized. |
| 99 ash::mojom::ShelfObserverAssociatedPtrInfo ptr_info; |
| 100 observer_binding_.Bind(&ptr_info, shelf_controller_.associated_group()); |
| 101 shelf_controller_->AddObserver(std::move(ptr_info)); |
| 102 } |
| 103 |
| 104 void ChromeMashShelfController::PinAppsFromPrefs() { |
| 105 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 106 std::vector<ash::launcher::AppLauncherId> pinned_apps = |
| 107 ash::launcher::GetPinnedAppsFromPrefs(profile->GetPrefs(), &helper_); |
| 108 |
| 109 for (const auto& app_launcher_id : pinned_apps) { |
| 110 const std::string launcher_id = app_launcher_id.ToString(); |
| 111 if (launcher_id == ash::launcher::kPinnedAppsPlaceholder) |
| 112 continue; |
| 113 |
| 114 ash::mojom::ShelfItemPtr item(ash::mojom::ShelfItem::New()); |
| 115 item->app_id = launcher_id; |
| 116 item->app_title = |
| 117 mojo::String::From(helper_.GetAppTitle(profile, launcher_id)); |
| 118 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 119 const gfx::Image& image = rb.GetImageNamed(IDR_APP_DEFAULT_ICON); |
| 120 item->image = *image.ToSkBitmap(); |
| 121 std::unique_ptr<ChromeShelfItemDelegate> delegate( |
| 122 new ChromeShelfItemDelegate(launcher_id, this)); |
| 123 shelf_controller_->PinItem(std::move(item), |
| 124 delegate->CreateInterfacePtrInfoAndBind( |
| 125 shelf_controller_.associated_group())); |
| 126 app_id_to_item_delegate_.insert( |
| 127 std::make_pair(launcher_id, std::move(delegate))); |
| 128 |
| 129 AppIconLoader* app_icon_loader = GetAppIconLoaderForApp(launcher_id); |
| 130 if (app_icon_loader) { |
| 131 app_icon_loader->FetchImage(launcher_id); |
| 132 app_icon_loader->UpdateImage(launcher_id); |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 AppIconLoader* ChromeMashShelfController::GetAppIconLoaderForApp( |
| 138 const std::string& app_id) { |
| 139 for (const auto& app_icon_loader : app_icon_loaders_) { |
| 140 if (app_icon_loader->CanLoadImageForApp(app_id)) |
| 141 return app_icon_loader.get(); |
| 142 } |
| 143 |
| 144 return nullptr; |
| 145 } |
| 146 |
| 147 void ChromeMashShelfController::OnShelfCreated(int64_t display_id) { |
| 148 // The pref helper functions return default values for invalid display ids. |
| 149 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 150 shelf_controller_->SetAlignment( |
| 151 ash::launcher::GetShelfAlignmentPref(profile->GetPrefs(), display_id), |
| 152 display_id); |
| 153 shelf_controller_->SetAutoHideBehavior( |
| 154 ash::launcher::GetShelfAutoHideBehaviorPref(profile->GetPrefs(), |
| 155 display_id), |
| 156 display_id); |
| 157 } |
| 158 |
| 159 void ChromeMashShelfController::OnAlignmentChanged( |
| 160 ash::ShelfAlignment alignment, |
| 161 int64_t display_id) { |
| 162 // The locked alignment is set temporarily and not saved to preferences. |
| 163 if (alignment == ash::SHELF_ALIGNMENT_BOTTOM_LOCKED) |
| 164 return; |
| 165 // This will uselessly store a preference value for invalid display ids. |
| 166 ash::launcher::SetShelfAlignmentPref( |
| 167 ProfileManager::GetActiveUserProfile()->GetPrefs(), display_id, |
| 168 alignment); |
| 169 } |
| 170 |
| 171 void ChromeMashShelfController::OnAutoHideBehaviorChanged( |
| 172 ash::ShelfAutoHideBehavior auto_hide, |
| 173 int64_t display_id) { |
| 174 // This will uselessly store a preference value for invalid display ids. |
| 175 ash::launcher::SetShelfAutoHideBehaviorPref( |
| 176 ProfileManager::GetActiveUserProfile()->GetPrefs(), display_id, |
| 177 auto_hide); |
| 178 } |
| 179 |
| 180 void ChromeMashShelfController::OnAppImageUpdated(const std::string& app_id, |
| 181 const gfx::ImageSkia& image) { |
| 182 shelf_controller_->SetItemImage(app_id, *image.bitmap()); |
| 183 } |
OLD | NEW |