| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_IMPL_H_ | |
| 6 #define CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_IMPL_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "ash/display/window_tree_host_manager.h" | |
| 12 #include "ash/public/cpp/shelf_types.h" | |
| 13 #include "ash/shelf/shelf_model_observer.h" | |
| 14 #include "base/compiler_specific.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" | |
| 19 #include "chrome/browser/ui/ash/app_sync_ui_state_observer.h" | |
| 20 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h" | |
| 21 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | |
| 22 #include "chrome/browser/ui/ash/launcher/launcher_app_updater.h" | |
| 23 #include "components/prefs/pref_change_registrar.h" | |
| 24 #include "components/sync_preferences/pref_service_syncable_observer.h" | |
| 25 #include "ui/aura/window_observer.h" | |
| 26 | |
| 27 class AppSyncUIState; | |
| 28 class BrowserStatusMonitor; | |
| 29 class Profile; | |
| 30 class AppWindowLauncherController; | |
| 31 | |
| 32 namespace ash { | |
| 33 class ShelfModel; | |
| 34 namespace launcher { | |
| 35 class ChromeLauncherPrefsObserver; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 class ChromeLauncherControllerUserSwitchObserver; | |
| 40 | |
| 41 // Implementation of ChromeLauncherController, used for classic Ash. | |
| 42 // This class manipulates Ash's ShelfModel to support Chrome and its apps. | |
| 43 class ChromeLauncherControllerImpl | |
| 44 : public ChromeLauncherController, | |
| 45 public LauncherAppUpdater::Delegate, | |
| 46 private ash::ShelfModelObserver, | |
| 47 private ash::WindowTreeHostManager::Observer, | |
| 48 private AppSyncUIStateObserver, | |
| 49 private app_list::AppListSyncableService::Observer, | |
| 50 private sync_preferences::PrefServiceSyncableObserver { | |
| 51 public: | |
| 52 ChromeLauncherControllerImpl(Profile* profile, ash::ShelfModel* model); | |
| 53 ~ChromeLauncherControllerImpl() override; | |
| 54 | |
| 55 // ChromeLauncherController: | |
| 56 ash::ShelfID CreateAppLauncherItem( | |
| 57 std::unique_ptr<ash::ShelfItemDelegate> item_delegate, | |
| 58 ash::ShelfItemStatus status) override; | |
| 59 const ash::ShelfItem* GetItem(ash::ShelfID id) const override; | |
| 60 void SetItemType(ash::ShelfID id, ash::ShelfItemType type) override; | |
| 61 void SetItemStatus(ash::ShelfID id, ash::ShelfItemStatus status) override; | |
| 62 void CloseLauncherItem(ash::ShelfID id) override; | |
| 63 bool IsPinned(ash::ShelfID id) override; | |
| 64 void SetV1AppStatus(const std::string& app_id, | |
| 65 ash::ShelfItemStatus status) override; | |
| 66 void Launch(ash::ShelfID id, int event_flags) override; | |
| 67 void Close(ash::ShelfID id) override; | |
| 68 bool IsOpen(ash::ShelfID id) override; | |
| 69 bool IsPlatformApp(ash::ShelfID id) override; | |
| 70 void ActivateApp(const std::string& app_id, | |
| 71 ash::ShelfLaunchSource source, | |
| 72 int event_flags) override; | |
| 73 void SetLauncherItemImage(ash::ShelfID shelf_id, | |
| 74 const gfx::ImageSkia& image) override; | |
| 75 void UpdateAppState(content::WebContents* contents, | |
| 76 AppState app_state) override; | |
| 77 ash::ShelfID GetShelfIDForWebContents( | |
| 78 content::WebContents* contents) override; | |
| 79 void SetRefocusURLPatternForTest(ash::ShelfID id, const GURL& url) override; | |
| 80 ash::ShelfAction ActivateWindowOrMinimizeIfActive( | |
| 81 ui::BaseWindow* window, | |
| 82 bool allow_minimize) override; | |
| 83 void ActiveUserChanged(const std::string& user_email) override; | |
| 84 void AdditionalUserAddedToSession(Profile* profile) override; | |
| 85 ash::MenuItemList GetAppMenuItemsForTesting( | |
| 86 const ash::ShelfItem& item) override; | |
| 87 std::vector<content::WebContents*> GetV1ApplicationsFromAppId( | |
| 88 const std::string& app_id) override; | |
| 89 void ActivateShellApp(const std::string& app_id, int window_index) override; | |
| 90 bool IsWebContentHandledByApplication(content::WebContents* web_contents, | |
| 91 const std::string& app_id) override; | |
| 92 bool ContentCanBeHandledByGmailApp( | |
| 93 content::WebContents* web_contents) override; | |
| 94 gfx::Image GetAppListIcon(content::WebContents* web_contents) const override; | |
| 95 base::string16 GetAppListTitle( | |
| 96 content::WebContents* web_contents) const override; | |
| 97 BrowserShortcutLauncherItemController* | |
| 98 GetBrowserShortcutLauncherItemController() override; | |
| 99 bool ShelfBoundsChangesProbablyWithUser( | |
| 100 ash::WmShelf* shelf, | |
| 101 const AccountId& account_id) const override; | |
| 102 void OnUserProfileReadyToSwitch(Profile* profile) override; | |
| 103 ArcAppDeferredLauncherController* GetArcDeferredLauncher() override; | |
| 104 const std::string& GetLaunchIDForShelfID(ash::ShelfID id) override; | |
| 105 void AttachProfile(Profile* profile_to_attach) override; | |
| 106 | |
| 107 ash::ShelfModel* shelf_model() const { return model_; } | |
| 108 | |
| 109 // Access to the BrowserStatusMonitor for tests. | |
| 110 BrowserStatusMonitor* browser_status_monitor_for_test() { | |
| 111 return browser_status_monitor_.get(); | |
| 112 } | |
| 113 | |
| 114 // Access to the AppWindowLauncherController list for tests. | |
| 115 const std::vector<std::unique_ptr<AppWindowLauncherController>>& | |
| 116 app_window_controllers_for_test() { | |
| 117 return app_window_controllers_; | |
| 118 } | |
| 119 | |
| 120 // Helpers that call through to corresponding ShelfModel functions. | |
| 121 ash::ShelfID GetShelfIDForAppID(const std::string& app_id); | |
| 122 ash::ShelfID GetShelfIDForAppIDAndLaunchID(const std::string& app_id, | |
| 123 const std::string& launch_id); | |
| 124 const std::string& GetAppIDForShelfID(ash::ShelfID id); | |
| 125 void PinAppWithID(const std::string& app_id); | |
| 126 bool IsAppPinned(const std::string& app_id); | |
| 127 void UnpinAppWithID(const std::string& app_id); | |
| 128 | |
| 129 // LauncherAppUpdater::Delegate: | |
| 130 void OnAppInstalled(content::BrowserContext* browser_context, | |
| 131 const std::string& app_id) override; | |
| 132 void OnAppUpdated(content::BrowserContext* browser_context, | |
| 133 const std::string& app_id) override; | |
| 134 void OnAppUninstalledPrepared(content::BrowserContext* browser_context, | |
| 135 const std::string& app_id) override; | |
| 136 | |
| 137 protected: | |
| 138 // ChromeLauncherController: | |
| 139 void OnInit() override; | |
| 140 | |
| 141 // Creates a new app shortcut item and controller on the shelf at |index|. | |
| 142 ash::ShelfID CreateAppShortcutLauncherItem( | |
| 143 const ash::AppLaunchId& app_launch_id, | |
| 144 int index); | |
| 145 | |
| 146 private: | |
| 147 friend class ChromeLauncherControllerImplTest; | |
| 148 friend class ShelfAppBrowserTest; | |
| 149 friend class LauncherPlatformAppBrowserTest; | |
| 150 friend class TestChromeLauncherControllerImpl; | |
| 151 FRIEND_TEST_ALL_PREFIXES(ChromeLauncherControllerImplTest, AppPanels); | |
| 152 | |
| 153 typedef std::map<content::WebContents*, std::string> WebContentsToAppIDMap; | |
| 154 | |
| 155 // Remembers / restores list of running applications. | |
| 156 // Note that this order will neither be stored in the preference nor will it | |
| 157 // remember the order of closed applications since it is only temporary. | |
| 158 void RememberUnpinnedRunningApplicationOrder(); | |
| 159 void RestoreUnpinnedRunningApplicationOrder(const std::string& user_id); | |
| 160 | |
| 161 // Invoked when the associated browser or app is closed. | |
| 162 void RemoveShelfItem(ash::ShelfID id); | |
| 163 | |
| 164 // Internal helpers for pinning and unpinning that handle both | |
| 165 // client-triggered and internal pinning operations. | |
| 166 void DoPinAppWithID(const std::string& app_id); | |
| 167 void DoUnpinAppWithID(const std::string& app_id, bool update_prefs); | |
| 168 | |
| 169 // Pin a running app with |shelf_id| internally to |index|. | |
| 170 void PinRunningAppInternal(int index, ash::ShelfID shelf_id); | |
| 171 | |
| 172 // Unpin a locked application. This is an internal call which converts the | |
| 173 // model type of the given app index from a shortcut into an unpinned running | |
| 174 // app. | |
| 175 void UnpinRunningAppInternal(int index); | |
| 176 | |
| 177 // Updates pin position for the item specified by |id| in sync model. | |
| 178 void SyncPinPosition(ash::ShelfID id); | |
| 179 | |
| 180 // Re-syncs shelf model. | |
| 181 void UpdateAppLaunchersFromPref(); | |
| 182 | |
| 183 // Schedules re-sync of shelf model. | |
| 184 void ScheduleUpdateAppLaunchersFromPref(); | |
| 185 | |
| 186 // Update the policy-pinned flag for each shelf item. | |
| 187 void UpdatePolicyPinnedAppsFromPrefs(); | |
| 188 | |
| 189 // Sets whether the virtual keyboard is enabled from prefs. | |
| 190 void SetVirtualKeyboardBehaviorFromPrefs(); | |
| 191 | |
| 192 // Returns the shelf item status for the given |app_id|, which can be either | |
| 193 // STATUS_ACTIVE (if the app is active), STATUS_RUNNING (if there is such an | |
| 194 // app) or STATUS_CLOSED. | |
| 195 ash::ShelfItemStatus GetAppState(const std::string& app_id); | |
| 196 | |
| 197 // Creates an app launcher to insert at |index|. Note that |index| may be | |
| 198 // adjusted by the model to meet ordering constraints. | |
| 199 // The |shelf_item_type| will be set into the ShelfModel. | |
| 200 ash::ShelfID InsertAppLauncherItem( | |
| 201 std::unique_ptr<ash::ShelfItemDelegate> item_delegate, | |
| 202 ash::ShelfItemStatus status, | |
| 203 int index, | |
| 204 ash::ShelfItemType shelf_item_type); | |
| 205 | |
| 206 // Create ShelfItem for Browser Shortcut. | |
| 207 void CreateBrowserShortcutLauncherItem(); | |
| 208 | |
| 209 // Check if the given |web_contents| is in incognito mode. | |
| 210 bool IsIncognito(const content::WebContents* web_contents) const; | |
| 211 | |
| 212 // Finds the index of where to insert the next item. | |
| 213 int FindInsertionPoint(); | |
| 214 | |
| 215 // Close all windowed V1 applications of a certain extension which was already | |
| 216 // deleted. | |
| 217 void CloseWindowedAppsFromRemovedExtension(const std::string& app_id, | |
| 218 const Profile* profile); | |
| 219 | |
| 220 // Forget the current profile to allow attaching to a new one. | |
| 221 void ReleaseProfile(); | |
| 222 | |
| 223 // ash::ShelfModelObserver: | |
| 224 void ShelfItemAdded(int index) override; | |
| 225 void ShelfItemRemoved(int index, const ash::ShelfItem& old_item) override; | |
| 226 void ShelfItemMoved(int start_index, int target_index) override; | |
| 227 void ShelfItemChanged(int index, const ash::ShelfItem& old_item) override; | |
| 228 | |
| 229 // ash::WindowTreeHostManager::Observer: | |
| 230 void OnDisplayConfigurationChanged() override; | |
| 231 | |
| 232 // AppSyncUIStateObserver: | |
| 233 void OnAppSyncUIStatusChanged() override; | |
| 234 | |
| 235 // AppIconLoaderDelegate: | |
| 236 void OnAppImageUpdated(const std::string& app_id, | |
| 237 const gfx::ImageSkia& image) override; | |
| 238 | |
| 239 // app_list::AppListSyncableService::Observer: | |
| 240 void OnSyncModelUpdated() override; | |
| 241 | |
| 242 // sync_preferences::PrefServiceSyncableObserver: | |
| 243 void OnIsSyncingChanged() override; | |
| 244 | |
| 245 // An internal helper to unpin a shelf item; this does not update prefs. | |
| 246 void UnpinShelfItemInternal(ash::ShelfID id); | |
| 247 | |
| 248 ash::ShelfModel* model_; | |
| 249 | |
| 250 // Direct access to app_id for a web contents. | |
| 251 WebContentsToAppIDMap web_contents_to_app_id_; | |
| 252 | |
| 253 // Used to track app windows. | |
| 254 std::vector<std::unique_ptr<AppWindowLauncherController>> | |
| 255 app_window_controllers_; | |
| 256 | |
| 257 // Used to handle app load/unload events. | |
| 258 std::vector<std::unique_ptr<LauncherAppUpdater>> app_updaters_; | |
| 259 | |
| 260 PrefChangeRegistrar pref_change_registrar_; | |
| 261 | |
| 262 AppSyncUIState* app_sync_ui_state_ = nullptr; | |
| 263 | |
| 264 // The owned browser status monitor. | |
| 265 std::unique_ptr<BrowserStatusMonitor> browser_status_monitor_; | |
| 266 | |
| 267 // A special observer class to detect user switches. | |
| 268 std::unique_ptr<ChromeLauncherControllerUserSwitchObserver> | |
| 269 user_switch_observer_; | |
| 270 | |
| 271 std::unique_ptr<ash::launcher::ChromeLauncherPrefsObserver> prefs_observer_; | |
| 272 | |
| 273 std::unique_ptr<ArcAppDeferredLauncherController> arc_deferred_launcher_; | |
| 274 | |
| 275 // The list of running & un-pinned applications for different users on hidden | |
| 276 // desktops. | |
| 277 typedef std::vector<std::string> RunningAppListIds; | |
| 278 typedef std::map<std::string, RunningAppListIds> RunningAppListIdMap; | |
| 279 RunningAppListIdMap last_used_running_application_order_; | |
| 280 | |
| 281 base::WeakPtrFactory<ChromeLauncherControllerImpl> weak_ptr_factory_; | |
| 282 | |
| 283 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherControllerImpl); | |
| 284 }; | |
| 285 | |
| 286 #endif // CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_IMPL_H_ | |
| OLD | NEW |