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.
5 #ifndef CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
14 #include "ash/display/window_tree_host_manager.h"
15 #include "ash/shelf/shelf_delegate.h"
16 #include "ash/shelf/shelf_item_delegate.h"
17 #include "ash/shelf/shelf_item_delegate_manager.h"
18 #include "ash/shelf/shelf_item_types.h"
19 #include "ash/shelf/shelf_layout_manager_observer.h"
20 #include "ash/shelf/shelf_model_observer.h"
21 #include "ash/shelf/shelf_types.h"
22 #include "ash/shell_observer.h"
23 #include "base/basictypes.h"
24 #include "base/compiler_specific.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/scoped_vector.h"
27 #include "base/prefs/pref_change_registrar.h"
28 #include "chrome/browser/extensions/app_icon_loader.h"
29 #include "chrome/browser/ui/ash/app_sync_ui_state_observer.h"
30 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h"
31 #include "chrome/browser/ui/ash/launcher/chrome_launcher_types.h"
32 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
33 #include "components/syncable_prefs/pref_service_syncable_observer.h"
34 #include "extensions/browser/extension_registry_observer.h"
35 #include "extensions/common/constants.h"
36 #include "ui/aura/window_observer.h"
40 class BrowserShortcutLauncherItemController
;
41 class BrowserStatusMonitor
;
42 class ExtensionEnableFlow
;
44 class LauncherItemController
;
46 class AppWindowLauncherController
;
50 class ShelfItemDelegateManager
;
63 namespace extensions
{
71 #if defined(OS_CHROMEOS)
72 class ChromeLauncherControllerUserSwitchObserver
;
75 // A list of the elements which makes up a simple menu description.
76 typedef ScopedVector
<ChromeLauncherAppMenuItem
> ChromeLauncherAppMenuItems
;
78 // ChromeLauncherController manages the launcher items needed for content
79 // windows. Launcher items have a type, an optional app id, and a controller.
80 // This incarnation groups running tabs/windows in application specific lists.
81 // * Browser app windows have BrowserLauncherItemController, owned by the
82 // BrowserView instance.
83 // * App windows have AppWindowLauncherItemController, owned by
84 // AppWindowLauncherController.
85 // * Shortcuts have no LauncherItemController.
86 class ChromeLauncherController
87 : public ash::ShelfDelegate
,
88 public ash::ShelfModelObserver
,
89 public ash::ShellObserver
,
90 public ash::WindowTreeHostManager::Observer
,
91 public extensions::ExtensionRegistryObserver
,
92 public extensions::AppIconLoader::Delegate
,
93 public syncable_prefs::PrefServiceSyncableObserver
,
94 public AppSyncUIStateObserver
,
95 public ExtensionEnableFlowDelegate
,
96 public ash::ShelfLayoutManagerObserver
,
97 public ash::ShelfItemDelegateManagerObserver
{
99 // Indicates if a shelf item is incognito or not.
100 enum IncognitoState
{
105 // Used to update the state of non plaform apps, as web contents change.
108 APP_STATE_WINDOW_ACTIVE
,
113 // Mockable interface to get app ids from tabs.
116 virtual ~AppTabHelper() {}
118 // Returns the app id of the specified tab, or an empty string if there is
119 // no app. All known profiles will be queried for this.
120 virtual std::string
GetAppID(content::WebContents
* tab
) = 0;
122 // Returns true if |id| is valid for the currently active profile.
123 // Used during restore to ignore no longer valid extensions.
124 // Note that already running applications are ignored by the restore
126 virtual bool IsValidIDForCurrentUser(const std::string
& id
) = 0;
128 // Sets the currently active profile for the usage of |GetAppID|.
129 virtual void SetCurrentUser(Profile
* profile
) = 0;
132 ChromeLauncherController(Profile
* profile
, ash::ShelfModel
* model
);
133 ~ChromeLauncherController() override
;
135 // Initializes this ChromeLauncherController.
138 // Creates an instance.
139 static ChromeLauncherController
* CreateInstance(Profile
* profile
,
140 ash::ShelfModel
* model
);
142 // Returns the single ChromeLauncherController instance.
143 static ChromeLauncherController
* instance() { return instance_
; }
145 // Creates a new app item on the shelf for |controller|.
146 ash::ShelfID
CreateAppLauncherItem(LauncherItemController
* controller
,
147 const std::string
& app_id
,
148 ash::ShelfItemStatus status
);
150 // Updates the running status of an item. It will also update the status of
151 // browsers shelf item if needed.
152 void SetItemStatus(ash::ShelfID id
, ash::ShelfItemStatus status
);
154 // Updates the controller associated with id (which should be a shortcut).
155 // |controller| will be owned by the |ChromeLauncherController| and then
156 // passed on to |ShelfItemDelegateManager|.
157 // TODO(skuhne): Pass in scoped_ptr to make ownership clear.
158 void SetItemController(ash::ShelfID id
, LauncherItemController
* controller
);
160 // Closes or unpins the shelf item.
161 void CloseLauncherItem(ash::ShelfID id
);
163 // Pins the specified id. Currently only supports platform apps.
164 void Pin(ash::ShelfID id
);
166 // Unpins the specified id, closing if not running.
167 void Unpin(ash::ShelfID id
);
169 // Returns true if the item identified by |id| is pinned.
170 bool IsPinned(ash::ShelfID id
);
172 // Pins/unpins the specified id.
173 void TogglePinned(ash::ShelfID id
);
175 // Returns true if the specified item can be pinned or unpinned. Only apps can
177 bool IsPinnable(ash::ShelfID id
) const;
179 // If there is no shelf item in the shelf for application |app_id|, one
180 // gets created. The (existing or created) shelf items get then locked
181 // against a users un-pinning removal.
182 void LockV1AppWithID(const std::string
& app_id
);
184 // A previously locked shelf item of type |app_id| gets unlocked. If the
185 // lock count reaches 0 and the item is not pinned it will go away.
186 void UnlockV1AppWithID(const std::string
& app_id
);
188 // Requests that the shelf item controller specified by |id| open a new
189 // instance of the app. |event_flags| holds the flags of the event which
190 // triggered this command.
191 void Launch(ash::ShelfID id
, int event_flags
);
193 // Closes the specified item.
194 void Close(ash::ShelfID id
);
196 // Returns true if the specified item is open.
197 bool IsOpen(ash::ShelfID id
);
199 // Returns true if the specified item is for a platform app.
200 bool IsPlatformApp(ash::ShelfID id
);
202 // Opens a new instance of the application identified by |app_id|.
203 // Used by the app-list, and by pinned-app shelf items.
204 void LaunchApp(const std::string
& app_id
,
205 ash::LaunchSource source
,
208 // If |app_id| is running, reactivates the app's most recently active window,
209 // otherwise launches and activates the app.
210 // Used by the app-list, and by pinned-app shelf items.
211 void ActivateApp(const std::string
& app_id
,
212 ash::LaunchSource source
,
215 // Returns the launch type of app for the specified id.
216 extensions::LaunchType
GetLaunchType(ash::ShelfID id
);
218 // Set the image for a specific shelf item (e.g. when set by the app).
219 void SetLauncherItemImage(ash::ShelfID shelf_id
, const gfx::ImageSkia
& image
);
221 // Find out if the given application |id| is a windowed app item and not a
222 // pinned item in the shelf.
223 bool IsWindowedAppInLauncher(const std::string
& app_id
);
225 // Updates the launch type of the app for the specified id to |launch_type|.
226 void SetLaunchType(ash::ShelfID id
, extensions::LaunchType launch_type
);
228 // Returns true if the user is currently logged in as a guest.
229 // Makes virtual for unittest in LauncherContextMenuTest.
230 virtual bool IsLoggedInAsGuest();
232 // Invoked when user clicks on button in the shelf and there is no last
233 // used window (or CTRL is held with the click).
234 void CreateNewWindow();
236 // Invoked when the user clicks on button in the shelf to create a new
238 void CreateNewIncognitoWindow();
240 // Updates the pinned pref state. The pinned state consists of a list pref.
241 // Each item of the list is a dictionary. The key |kAppIDPath| gives the
243 void PersistPinnedState();
245 ash::ShelfModel
* model();
247 // Accessor to the currently loaded profile. Note that in multi profile use
248 // cases this might change over time.
251 // Gets the shelf auto-hide behavior on |root_window|.
252 ash::ShelfAutoHideBehavior
GetShelfAutoHideBehavior(
253 aura::Window
* root_window
) const;
255 // Returns |true| if the user is allowed to modify the shelf auto-hide
256 // behavior on |root_window|.
257 bool CanUserModifyShelfAutoHideBehavior(aura::Window
* root_window
) const;
259 // Toggles the shelf auto-hide behavior on |root_window|. Does nothing if the
260 // user is not allowed to modify the auto-hide behavior.
261 void ToggleShelfAutoHideBehavior(aura::Window
* root_window
);
263 // Notify the controller that the state of an non platform app's tabs
265 void UpdateAppState(content::WebContents
* contents
, AppState app_state
);
267 // Returns ShelfID for |contents|. If |contents| is not an app or is not
268 // pinned, returns the id of browser shrotcut.
269 ash::ShelfID
GetShelfIDForWebContents(content::WebContents
* contents
);
271 // Limits application refocusing to urls that match |url| for |id|.
272 void SetRefocusURLPatternForTest(ash::ShelfID id
, const GURL
& url
);
274 // Returns the extension identified by |app_id|.
275 const extensions::Extension
* GetExtensionForAppID(
276 const std::string
& app_id
) const;
278 // Activates a |window|. If |allow_minimize| is true and the system allows
279 // it, the the window will get minimized instead.
280 // Returns the action performed. Should be one of kNoAction,
281 // kExistingWindowActivated, or kExistingWindowMinimized.
282 ash::ShelfItemDelegate::PerformedAction
ActivateWindowOrMinimizeIfActive(
283 ui::BaseWindow
* window
,
284 bool allow_minimize
);
286 // ash::ShelfDelegate:
287 void OnShelfCreated(ash::Shelf
* shelf
) override
;
288 void OnShelfDestroyed(ash::Shelf
* shelf
) override
;
289 ash::ShelfID
GetShelfIDForAppID(const std::string
& app_id
) override
;
290 bool HasShelfIDToAppIDMapping(ash::ShelfID id
) const override
;
291 const std::string
& GetAppIDForShelfID(ash::ShelfID id
) override
;
292 void PinAppWithID(const std::string
& app_id
) override
;
293 bool IsAppPinned(const std::string
& app_id
) override
;
294 bool CanPin() const override
;
295 void UnpinAppWithID(const std::string
& app_id
) override
;
297 // ash::ShelfItemDelegateManagerObserver:
298 void OnSetShelfItemDelegate(ash::ShelfID id
,
299 ash::ShelfItemDelegate
* item_delegate
) override
;
301 // ash::ShelfModelObserver:
302 void ShelfItemAdded(int index
) override
;
303 void ShelfItemRemoved(int index
, ash::ShelfID id
) override
;
304 void ShelfItemMoved(int start_index
, int target_index
) override
;
305 void ShelfItemChanged(int index
, const ash::ShelfItem
& old_item
) override
;
306 void ShelfStatusChanged() override
;
308 // ash::ShellObserver:
309 void OnShelfAlignmentChanged(aura::Window
* root_window
) override
;
311 // ash::WindowTreeHostManager::Observer:
312 void OnDisplayConfigurationChanged() override
;
314 // ExtensionRegistryObserver:
315 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
316 const extensions::Extension
* extension
) override
;
317 void OnExtensionUnloaded(
318 content::BrowserContext
* browser_context
,
319 const extensions::Extension
* extension
,
320 extensions::UnloadedExtensionInfo::Reason reason
) override
;
322 // syncable_prefs::PrefServiceSyncableObserver:
323 void OnIsSyncingChanged() override
;
325 // AppSyncUIStateObserver:
326 void OnAppSyncUIStatusChanged() override
;
328 // ExtensionEnableFlowDelegate:
329 void ExtensionEnableFlowFinished() override
;
330 void ExtensionEnableFlowAborted(bool user_initiated
) override
;
332 // extensions::AppIconLoader:
333 void SetAppImage(const std::string
& app_id
,
334 const gfx::ImageSkia
& image
) override
;
336 // ash::ShelfLayoutManagerObserver:
337 void OnAutoHideBehaviorChanged(
338 aura::Window
* root_window
,
339 ash::ShelfAutoHideBehavior new_behavior
) override
;
341 // Called when the active user has changed.
342 void ActiveUserChanged(const std::string
& user_email
);
344 // Called when a user got added to the session.
345 void AdditionalUserAddedToSession(Profile
* profile
);
347 // Get the list of all running incarnations of this item.
348 // |event_flags| specifies the flags which were set by the event which
349 // triggered this menu generation. It can be used to generate different lists.
350 ChromeLauncherAppMenuItems
GetApplicationList(const ash::ShelfItem
& item
,
353 // Get the list of all tabs which belong to a certain application type.
354 std::vector
<content::WebContents
*> GetV1ApplicationsFromAppId(
357 // Activates a specified shell application.
358 void ActivateShellApp(const std::string
& app_id
, int index
);
360 // Checks if a given |web_contents| is known to be associated with an
361 // application of type |app_id|.
362 bool IsWebContentHandledByApplication(content::WebContents
* web_contents
,
363 const std::string
& app_id
);
365 // Check if the gMail app is loaded and it can handle the given web content.
366 // This special treatment is required to address crbug.com/234268.
367 bool ContentCanBeHandledByGmailApp(content::WebContents
* web_contents
);
369 // Get the favicon for the application list entry for |web_contents|.
370 // Note that for incognito windows the incognito icon will be returned.
371 // If |web_contents| has not loaded, returns the default favicon.
372 gfx::Image
GetAppListIcon(content::WebContents
* web_contents
) const;
374 // Get the title for the applicatoin list entry for |web_contents|.
375 // If |web_contents| has not loaded, returns "Net Tab".
376 base::string16
GetAppListTitle(content::WebContents
* web_contents
) const;
378 // Returns the LauncherItemController of BrowserShortcut.
379 BrowserShortcutLauncherItemController
*
380 GetBrowserShortcutLauncherItemController();
382 LauncherItemController
* GetLauncherItemController(const ash::ShelfID id
);
384 // Returns true if |browser| is owned by the active user.
385 bool IsBrowserFromActiveUser(Browser
* browser
);
387 // Check if the shelf visibility (location, visibility) will change with a new
388 // user profile or not. However, since the full visibility calculation of the
389 // shelf cannot be performed here, this is only a probability used for
390 // animation predictions.
391 bool ShelfBoundsChangesProbablyWithUser(aura::Window
* root_window
,
392 const std::string
& user_id
) const;
394 // Called when the user profile is fully loaded and ready to switch to.
395 void OnUserProfileReadyToSwitch(Profile
* profile
);
397 // Access to the BrowserStatusMonitor for tests.
398 BrowserStatusMonitor
* browser_status_monitor_for_test() {
399 return browser_status_monitor_
.get();
402 // Access to the AppWindowLauncherController for tests.
403 AppWindowLauncherController
* app_window_controller_for_test() {
404 return app_window_controller_
.get();
408 // Creates a new app shortcut item and controller on the shelf at |index|.
409 // Use kInsertItemAtEnd to add a shortcut as the last item.
410 ash::ShelfID
CreateAppShortcutLauncherItem(const std::string
& app_id
,
413 // Sets the AppTabHelper/AppIconLoader, taking ownership of the helper class.
414 // These are intended for testing.
415 void SetAppTabHelperForTest(AppTabHelper
* helper
);
416 void SetAppIconLoaderForTest(extensions::AppIconLoader
* loader
);
417 const std::string
& GetAppIdFromShelfIdForTest(ash::ShelfID id
);
419 // Sets the ash::ShelfItemDelegateManager only for unittests and doesn't
420 // take an ownership of it.
421 void SetShelfItemDelegateManagerForTest(
422 ash::ShelfItemDelegateManager
* manager
);
425 friend class ChromeLauncherControllerTest
;
426 friend class ShelfAppBrowserTest
;
427 friend class LauncherPlatformAppBrowserTest
;
429 typedef std::map
<ash::ShelfID
, LauncherItemController
*> IDToItemControllerMap
;
430 typedef std::map
<content::WebContents
*, std::string
> WebContentsToAppIDMap
;
432 // Remembers / restores list of running applications.
433 // Note that this order will neither be stored in the preference nor will it
434 // remember the order of closed applications since it is only temporary.
435 void RememberUnpinnedRunningApplicationOrder();
436 void RestoreUnpinnedRunningApplicationOrder(const std::string
& user_id
);
438 // Creates a new app shortcut item and controller on the shelf at |index|.
439 // Use kInsertItemAtEnd to add a shortcut as the last item.
440 ash::ShelfID
CreateAppShortcutLauncherItemWithType(
441 const std::string
& app_id
,
443 ash::ShelfItemType shelf_item_type
);
445 // Invoked when the associated browser or app is closed.
446 void LauncherItemClosed(ash::ShelfID id
);
448 // Internal helpers for pinning and unpinning that handle both
449 // client-triggered and internal pinning operations.
450 void DoPinAppWithID(const std::string
& app_id
);
451 void DoUnpinAppWithID(const std::string
& app_id
);
453 // Pin a running app with |shelf_id| internally to |index|. It returns
454 // the index where the item was pinned.
455 int PinRunningAppInternal(int index
, ash::ShelfID shelf_id
);
457 // Unpin a locked application. This is an internal call which converts the
458 // model type of the given app index from a shortcut into an unpinned running
460 void UnpinRunningAppInternal(int index
);
462 // Re-syncs shelf model with prefs::kPinnedLauncherApps.
463 void UpdateAppLaunchersFromPref();
465 // Persists the shelf auto-hide behavior to prefs.
466 void SetShelfAutoHideBehaviorPrefs(ash::ShelfAutoHideBehavior behavior
,
467 aura::Window
* root_window
);
469 // Sets the shelf auto-hide behavior from prefs.
470 void SetShelfAutoHideBehaviorFromPrefs();
472 // Sets the shelf alignment from prefs.
473 void SetShelfAlignmentFromPrefs();
475 // Sets both of auto-hide behavior and alignment from prefs.
476 void SetShelfBehaviorsFromPrefs();
478 #if defined(OS_CHROMEOS)
479 // Sets whether the virtual keyboard is enabled from prefs.
480 void SetVirtualKeyboardBehaviorFromPrefs();
481 #endif // defined(OS_CHROMEOS)
483 // Returns the shelf item status for the given |app_id|, which can be either
484 // STATUS_ACTIVE (if the app is active), STATUS_RUNNING (if there is such an
485 // app) or STATUS_CLOSED.
486 ash::ShelfItemStatus
GetAppState(const std::string
& app_id
);
488 // Creates an app launcher to insert at |index|. Note that |index| may be
489 // adjusted by the model to meet ordering constraints.
490 // The |shelf_item_type| will be set into the ShelfModel.
491 ash::ShelfID
InsertAppLauncherItem(LauncherItemController
* controller
,
492 const std::string
& app_id
,
493 ash::ShelfItemStatus status
,
495 ash::ShelfItemType shelf_item_type
);
497 // Enumerate all Web contents which match a given shortcut |controller|.
498 std::vector
<content::WebContents
*> GetV1ApplicationsFromController(
499 LauncherItemController
* controller
);
501 // Create ShelfItem for Browser Shortcut.
502 ash::ShelfID
CreateBrowserShortcutLauncherItem();
504 // Check if the given |web_contents| is in incognito mode.
505 bool IsIncognito(const content::WebContents
* web_contents
) const;
507 // Update browser shortcut's index.
508 void PersistChromeItemIndex(int index
);
510 // Get browser shortcut's index from pref.
511 int GetChromeIconIndexFromPref() const;
513 // Depending on the provided flags, move either the chrome icon, the app icon
514 // or none to the given |target_index|. The provided |chrome_index| and
515 // |app_list_index| locations will get adjusted within this call to finalize
516 // the action and to make sure that the other item can still be moved
517 // afterwards (index adjustments).
518 void MoveChromeOrApplistToFinalPosition(
523 int* app_list_index
);
525 // Finds the index of where to insert the next item.
526 int FindInsertionPoint(bool is_app_list
);
528 // Get the browser shortcut's index in the shelf using the current's systems
529 // configuration of pinned and known (but not running) apps.
530 int GetChromeIconIndexForCreation();
532 // Get the list of pinned programs from the preferences.
533 std::vector
<std::string
> GetListOfPinnedAppsAndBrowser();
535 // Close all windowed V1 applications of a certain extension which was already
537 void CloseWindowedAppsFromRemovedExtension(const std::string
& app_id
,
538 const Profile
* profile
);
540 // Set ShelfItemDelegate |item_delegate| for |id| and take an ownership.
541 // TODO(simon.hong81): Make this take a scoped_ptr of |item_delegate|.
542 void SetShelfItemDelegate(ash::ShelfID id
,
543 ash::ShelfItemDelegate
* item_delegate
);
545 // Attach to a specific profile.
546 void AttachProfile(Profile
* proifile
);
548 // Forget the current profile to allow attaching to a new one.
549 void ReleaseProfile();
551 // Returns true if |app_id| is a Packaged App that has already launched on the
552 // native desktop and, if so, executes it as a desktop shortcut to activate
553 // desktop mode and send another OnLaunched event to the Extension.
554 bool LaunchedInNativeDesktop(const std::string
& app_id
);
556 static ChromeLauncherController
* instance_
;
558 ash::ShelfModel
* model_
;
560 ash::ShelfItemDelegateManager
* item_delegate_manager_
;
562 // Profile used for prefs and loading extensions. This is NOT necessarily the
563 // profile new windows are created with.
566 // Controller items in this map are owned by |ShelfItemDelegateManager|.
567 IDToItemControllerMap id_to_item_controller_map_
;
569 // Direct access to app_id for a web contents.
570 WebContentsToAppIDMap web_contents_to_app_id_
;
572 // Used to track app windows.
573 scoped_ptr
<AppWindowLauncherController
> app_window_controller_
;
575 // Used to get app info for tabs.
576 scoped_ptr
<AppTabHelper
> app_tab_helper_
;
578 // Used to load the image for an app item.
579 scoped_ptr
<extensions::AppIconLoader
> app_icon_loader_
;
581 PrefChangeRegistrar pref_change_registrar_
;
583 AppSyncUIState
* app_sync_ui_state_
;
585 scoped_ptr
<ExtensionEnableFlow
> extension_enable_flow_
;
587 // Shelves that are currently being observed.
588 std::set
<ash::Shelf
*> shelves_
;
590 // The owned browser status monitor.
591 scoped_ptr
<BrowserStatusMonitor
> browser_status_monitor_
;
593 #if defined(OS_CHROMEOS)
594 // A special observer class to detect user switches.
595 scoped_ptr
<ChromeLauncherControllerUserSwitchObserver
> user_switch_observer_
;
598 // If true, incoming pinned state changes should be ignored.
599 bool ignore_persist_pinned_state_change_
;
601 // The list of running & un-pinned applications for different users on hidden
603 typedef std::vector
<std::string
> RunningAppListIds
;
604 typedef std::map
<std::string
, RunningAppListIds
> RunningAppListIdMap
;
605 RunningAppListIdMap last_used_running_application_order_
;
607 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherController
);
610 #endif // CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_