OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 5 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/extensions/extension_app_icon_loader.h" |
| 9 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/browser/ui/app_list/arc/arc_app_icon_loader.h" |
| 11 #include "chrome/browser/ui/ash/ash_util.h" |
| 12 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h" |
| 13 #include "chrome/browser/ui/ash/launcher/launcher_controller_helper.h" |
| 14 #include "content/public/common/service_manager_connection.h" |
| 15 #include "services/shell/public/cpp/connector.h" |
| 16 #include "ui/display/display.h" |
| 17 #include "ui/display/screen.h" |
| 18 |
7 // static | 19 // static |
8 ChromeLauncherController* ChromeLauncherController::instance_ = nullptr; | 20 ChromeLauncherController* ChromeLauncherController::instance_ = nullptr; |
9 | 21 |
10 ChromeLauncherController::~ChromeLauncherController() { | 22 ChromeLauncherController::~ChromeLauncherController() { |
11 if (instance_ == this) | 23 if (instance_ == this) |
12 instance_ = nullptr; | 24 instance_ = nullptr; |
13 } | 25 } |
14 | 26 |
15 ChromeLauncherController::ChromeLauncherController() {} | 27 void ChromeLauncherController::LaunchApp(const std::string& app_id, |
| 28 ash::LaunchSource source, |
| 29 int event_flags) { |
| 30 launcher_controller_helper_->LaunchApp(app_id, source, event_flags); |
| 31 } |
| 32 |
| 33 ChromeLauncherController::ChromeLauncherController() : observer_binding_(this) { |
| 34 DCHECK(!instance_); |
| 35 instance_ = this; |
| 36 // Start observing the shelf controller immediately. |
| 37 if (ConnectToShelfController()) { |
| 38 ash::mojom::ShelfObserverAssociatedPtrInfo ptr_info; |
| 39 observer_binding_.Bind(&ptr_info, shelf_controller_.associated_group()); |
| 40 shelf_controller_->AddObserver(std::move(ptr_info)); |
| 41 } |
| 42 } |
| 43 |
| 44 bool ChromeLauncherController::ConnectToShelfController() { |
| 45 if (shelf_controller_.is_bound()) |
| 46 return true; |
| 47 |
| 48 auto connection = content::ServiceManagerConnection::GetForProcess(); |
| 49 auto connector = connection ? connection->GetConnector() : nullptr; |
| 50 // Unit tests may not have a connector. |
| 51 if (!connector) |
| 52 return false; |
| 53 |
| 54 // Under mash the ShelfController interface is in the ash process. In classic |
| 55 // ash we provide it to ourself. |
| 56 if (chrome::IsRunningInMash()) { |
| 57 connector->ConnectToInterface("service:ash", &shelf_controller_); |
| 58 } else { |
| 59 connector->ConnectToInterface("service:content_browser", |
| 60 &shelf_controller_); |
| 61 } |
| 62 return true; |
| 63 } |
| 64 |
| 65 AppIconLoader* ChromeLauncherController::GetAppIconLoaderForApp( |
| 66 const std::string& app_id) { |
| 67 for (const auto& app_icon_loader : app_icon_loaders_) { |
| 68 if (app_icon_loader->CanLoadImageForApp(app_id)) |
| 69 return app_icon_loader.get(); |
| 70 } |
| 71 |
| 72 return nullptr; |
| 73 } |
| 74 |
| 75 void ChromeLauncherController::SetShelfAutoHideBehaviorFromPrefs() { |
| 76 if (!ConnectToShelfController()) |
| 77 return; |
| 78 |
| 79 // The pref helper functions return default values for invalid display ids. |
| 80 PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs(); |
| 81 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 82 shelf_controller_->SetAutoHideBehavior( |
| 83 ash::launcher::GetShelfAutoHideBehaviorPref(prefs, display.id()), |
| 84 display.id()); |
| 85 } |
| 86 } |
| 87 |
| 88 void ChromeLauncherController::SetShelfAlignmentFromPrefs() { |
| 89 if (!ConnectToShelfController()) |
| 90 return; |
| 91 |
| 92 // The pref helper functions return default values for invalid display ids. |
| 93 PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs(); |
| 94 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 95 shelf_controller_->SetAlignment( |
| 96 ash::launcher::GetShelfAlignmentPref(prefs, display.id()), |
| 97 display.id()); |
| 98 } |
| 99 } |
| 100 |
| 101 void ChromeLauncherController::SetShelfBehaviorsFromPrefs() { |
| 102 SetShelfAutoHideBehaviorFromPrefs(); |
| 103 SetShelfAlignmentFromPrefs(); |
| 104 } |
| 105 |
| 106 void ChromeLauncherController::SetLauncherControllerHelperForTest( |
| 107 std::unique_ptr<LauncherControllerHelper> helper) { |
| 108 launcher_controller_helper_ = std::move(helper); |
| 109 } |
| 110 |
| 111 void ChromeLauncherController::SetAppIconLoadersForTest( |
| 112 std::vector<std::unique_ptr<AppIconLoader>>& loaders) { |
| 113 app_icon_loaders_.clear(); |
| 114 for (auto& loader : loaders) |
| 115 app_icon_loaders_.push_back(std::move(loader)); |
| 116 } |
| 117 |
| 118 void ChromeLauncherController::SetProfileForTest(Profile* profile) { |
| 119 profile_ = profile; |
| 120 } |
| 121 |
| 122 void ChromeLauncherController::AttachProfile(Profile* profile_to_attach) { |
| 123 profile_ = profile_to_attach; |
| 124 // Either add the profile to the list of known profiles and make it the active |
| 125 // one for some functions of LauncherControllerHelper or create a new one. |
| 126 if (!launcher_controller_helper_.get()) { |
| 127 launcher_controller_helper_ = |
| 128 base::MakeUnique<LauncherControllerHelper>(profile_); |
| 129 } else { |
| 130 launcher_controller_helper_->set_profile(profile_); |
| 131 } |
| 132 |
| 133 // TODO(skuhne): The AppIconLoaderImpl has the same problem. Each loaded |
| 134 // image is associated with a profile (its loader requires the profile). |
| 135 // Since icon size changes are possible, the icon could be requested to be |
| 136 // reloaded. However - having it not multi profile aware would cause problems |
| 137 // if the icon cache gets deleted upon user switch. |
| 138 std::unique_ptr<AppIconLoader> extension_app_icon_loader = |
| 139 base::MakeUnique<extensions::ExtensionAppIconLoader>( |
| 140 profile_, extension_misc::EXTENSION_ICON_SMALL, this); |
| 141 app_icon_loaders_.push_back(std::move(extension_app_icon_loader)); |
| 142 |
| 143 if (arc::ArcAuthService::IsAllowedForProfile(profile_)) { |
| 144 std::unique_ptr<AppIconLoader> arc_app_icon_loader = |
| 145 base::MakeUnique<ArcAppIconLoader>( |
| 146 profile_, extension_misc::EXTENSION_ICON_SMALL, this); |
| 147 app_icon_loaders_.push_back(std::move(arc_app_icon_loader)); |
| 148 } |
| 149 } |
| 150 |
| 151 void ChromeLauncherController::OnShelfCreated(int64_t display_id) { |
| 152 if (!ConnectToShelfController()) |
| 153 return; |
| 154 |
| 155 // The pref helper functions return default values for invalid display ids. |
| 156 PrefService* prefs = profile_->GetPrefs(); |
| 157 shelf_controller_->SetAlignment( |
| 158 ash::launcher::GetShelfAlignmentPref(prefs, display_id), display_id); |
| 159 shelf_controller_->SetAutoHideBehavior( |
| 160 ash::launcher::GetShelfAutoHideBehaviorPref(prefs, display_id), |
| 161 display_id); |
| 162 } |
| 163 |
| 164 void ChromeLauncherController::OnAlignmentChanged(ash::ShelfAlignment alignment, |
| 165 int64_t display_id) { |
| 166 // The locked alignment is set temporarily and not saved to preferences. |
| 167 if (alignment == ash::SHELF_ALIGNMENT_BOTTOM_LOCKED) |
| 168 return; |
| 169 // This will uselessly store a preference value for invalid display ids. |
| 170 // TODO(msw): Avoid handling this pref change and forwarding the value to ash. |
| 171 ash::launcher::SetShelfAlignmentPref(profile_->GetPrefs(), display_id, |
| 172 alignment); |
| 173 } |
| 174 |
| 175 void ChromeLauncherController::OnAutoHideBehaviorChanged( |
| 176 ash::ShelfAutoHideBehavior auto_hide, |
| 177 int64_t display_id) { |
| 178 // This will uselessly store a preference value for invalid display ids. |
| 179 // TODO(msw): Avoid handling this pref change and forwarding the value to ash. |
| 180 ash::launcher::SetShelfAutoHideBehaviorPref(profile_->GetPrefs(), display_id, |
| 181 auto_hide); |
| 182 } |
| 183 |
| 184 void ChromeLauncherController::OnAppImageUpdated(const std::string& app_id, |
| 185 const gfx::ImageSkia& image) { |
| 186 // Implemented by subclasses; this should not be called. |
| 187 NOTREACHED(); |
| 188 } |
OLD | NEW |