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/display_controller.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/prefs/pref_service_syncable_observer.h"
30 #include "chrome/browser/ui/ash/app_sync_ui_state_observer.h"
31 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h"
32 #include "chrome/browser/ui/ash/launcher/chrome_launcher_types.h"
33 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.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 // A list of the elements which makes up a simple menu description.
72 typedef ScopedVector
<ChromeLauncherAppMenuItem
> ChromeLauncherAppMenuItems
;
74 // A class which needs to be overwritten dependent on the used OS to moitor
76 class ChromeLauncherControllerUserSwitchObserver
{
78 ChromeLauncherControllerUserSwitchObserver() {}
79 virtual ~ChromeLauncherControllerUserSwitchObserver() {}
82 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherControllerUserSwitchObserver
);
85 // ChromeLauncherController manages the launcher items needed for content
86 // windows. Launcher items have a type, an optional app id, and a controller.
87 // This incarnation groups running tabs/windows in application specific lists.
88 // * Browser app windows have BrowserLauncherItemController, owned by the
89 // BrowserView instance.
90 // * App windows have AppWindowLauncherItemController, owned by
91 // AppWindowLauncherController.
92 // * Shortcuts have no LauncherItemController.
93 class ChromeLauncherController
: public ash::ShelfDelegate
,
94 public ash::ShelfModelObserver
,
95 public ash::ShellObserver
,
96 public ash::DisplayController::Observer
,
97 public extensions::ExtensionRegistryObserver
,
98 public extensions::AppIconLoader::Delegate
,
99 public PrefServiceSyncableObserver
,
100 public AppSyncUIStateObserver
,
101 public ExtensionEnableFlowDelegate
,
102 public ash::ShelfLayoutManagerObserver
,
103 public ash::ShelfItemDelegateManagerObserver
{
105 // Indicates if a shelf item is incognito or not.
106 enum IncognitoState
{
111 // Used to update the state of non plaform apps, as web contents change.
114 APP_STATE_WINDOW_ACTIVE
,
119 // Mockable interface to get app ids from tabs.
122 virtual ~AppTabHelper() {}
124 // Returns the app id of the specified tab, or an empty string if there is
125 // no app. All known profiles will be queried for this.
126 virtual std::string
GetAppID(content::WebContents
* tab
) = 0;
128 // Returns true if |id| is valid for the currently active profile.
129 // Used during restore to ignore no longer valid extensions.
130 // Note that already running applications are ignored by the restore
132 virtual bool IsValidIDForCurrentUser(const std::string
& id
) = 0;
134 // Sets the currently active profile for the usage of |GetAppID|.
135 virtual void SetCurrentUser(Profile
* profile
) = 0;
138 ChromeLauncherController(Profile
* profile
, ash::ShelfModel
* model
);
139 ~ChromeLauncherController() override
;
141 // Initializes this ChromeLauncherController.
144 // Creates an instance.
145 static ChromeLauncherController
* CreateInstance(Profile
* profile
,
146 ash::ShelfModel
* model
);
148 // Returns the single ChromeLauncherController instance.
149 static ChromeLauncherController
* instance() { return instance_
; }
151 // Creates a new app item on the shelf for |controller|.
152 ash::ShelfID
CreateAppLauncherItem(LauncherItemController
* controller
,
153 const std::string
& app_id
,
154 ash::ShelfItemStatus status
);
156 // Updates the running status of an item. It will also update the status of
157 // browsers shelf item if needed.
158 void SetItemStatus(ash::ShelfID id
, ash::ShelfItemStatus status
);
160 // Updates the controller associated with id (which should be a shortcut).
161 // |controller| will be owned by the |ChromeLauncherController| and then
162 // passed on to |ShelfItemDelegateManager|.
163 // TODO(skuhne): Pass in scoped_ptr to make ownership clear.
164 void SetItemController(ash::ShelfID id
, LauncherItemController
* controller
);
166 // Closes or unpins the shelf item.
167 void CloseLauncherItem(ash::ShelfID id
);
169 // Pins the specified id. Currently only supports platform apps.
170 void Pin(ash::ShelfID id
);
172 // Unpins the specified id, closing if not running.
173 void Unpin(ash::ShelfID id
);
175 // Returns true if the item identified by |id| is pinned.
176 bool IsPinned(ash::ShelfID id
);
178 // Pins/unpins the specified id.
179 void TogglePinned(ash::ShelfID id
);
181 // Returns true if the specified item can be pinned or unpinned. Only apps can
183 bool IsPinnable(ash::ShelfID id
) const;
185 // Installs the specified id. Only valid if the id corresponds to an ephemeral
187 void Install(ash::ShelfID id
);
189 // Returns true if the specified item can be installed. Only true for
191 bool CanInstall(ash::ShelfID id
);
193 // If there is no shelf item in the shelf for application |app_id|, one
194 // gets created. The (existing or created) shelf items get then locked
195 // against a users un-pinning removal.
196 void LockV1AppWithID(const std::string
& app_id
);
198 // A previously locked shelf item of type |app_id| gets unlocked. If the
199 // lock count reaches 0 and the item is not pinned it will go away.
200 void UnlockV1AppWithID(const std::string
& app_id
);
202 // Requests that the shelf item controller specified by |id| open a new
203 // instance of the app. |event_flags| holds the flags of the event which
204 // triggered this command.
205 void Launch(ash::ShelfID id
, int event_flags
);
207 // Closes the specified item.
208 void Close(ash::ShelfID id
);
210 // Returns true if the specified item is open.
211 bool IsOpen(ash::ShelfID id
);
213 // Returns true if the specified item is for a platform app.
214 bool IsPlatformApp(ash::ShelfID id
);
216 // Opens a new instance of the application identified by |app_id|.
217 // Used by the app-list, and by pinned-app shelf items.
218 void LaunchApp(const std::string
& app_id
,
219 ash::LaunchSource source
,
222 // If |app_id| is running, reactivates the app's most recently active window,
223 // otherwise launches and activates the app.
224 // Used by the app-list, and by pinned-app shelf items.
225 void ActivateApp(const std::string
& app_id
,
226 ash::LaunchSource source
,
229 // Returns the launch type of app for the specified id.
230 extensions::LaunchType
GetLaunchType(ash::ShelfID id
);
232 // Set the image for a specific shelf item (e.g. when set by the app).
233 void SetLauncherItemImage(ash::ShelfID shelf_id
, const gfx::ImageSkia
& image
);
235 // Find out if the given application |id| is a windowed app item and not a
236 // pinned item in the shelf.
237 bool IsWindowedAppInLauncher(const std::string
& app_id
);
239 // Updates the launch type of the app for the specified id to |launch_type|.
240 void SetLaunchType(ash::ShelfID id
, extensions::LaunchType launch_type
);
242 // Returns true if the user is currently logged in as a guest.
243 // Makes virtual for unittest in LauncherContextMenuTest.
244 virtual bool IsLoggedInAsGuest();
246 // Invoked when user clicks on button in the shelf and there is no last
247 // used window (or CTRL is held with the click).
248 void CreateNewWindow();
250 // Invoked when the user clicks on button in the shelf to create a new
252 void CreateNewIncognitoWindow();
254 // Updates the pinned pref state. The pinned state consists of a list pref.
255 // Each item of the list is a dictionary. The key |kAppIDPath| gives the
257 void PersistPinnedState();
259 ash::ShelfModel
* model();
261 // Accessor to the currently loaded profile. Note that in multi profile use
262 // cases this might change over time.
265 // Gets the shelf auto-hide behavior on |root_window|.
266 ash::ShelfAutoHideBehavior
GetShelfAutoHideBehavior(
267 aura::Window
* root_window
) const;
269 // Returns |true| if the user is allowed to modify the shelf auto-hide
270 // behavior on |root_window|.
271 bool CanUserModifyShelfAutoHideBehavior(aura::Window
* root_window
) const;
273 // Toggles the shelf auto-hide behavior on |root_window|. Does nothing if the
274 // user is not allowed to modify the auto-hide behavior.
275 void ToggleShelfAutoHideBehavior(aura::Window
* root_window
);
277 // Notify the controller that the state of an non platform app's tabs
279 void UpdateAppState(content::WebContents
* contents
, AppState app_state
);
281 // Returns ShelfID for |contents|. If |contents| is not an app or is not
282 // pinned, returns the id of browser shrotcut.
283 ash::ShelfID
GetShelfIDForWebContents(content::WebContents
* contents
);
285 // Limits application refocusing to urls that match |url| for |id|.
286 void SetRefocusURLPatternForTest(ash::ShelfID id
, const GURL
& url
);
288 // Returns the extension identified by |app_id|.
289 const extensions::Extension
* GetExtensionForAppID(
290 const std::string
& app_id
) const;
292 // Activates a |window|. If |allow_minimize| is true and the system allows
293 // it, the the window will get minimized instead.
294 // Returns the action performed. Should be one of kNoAction,
295 // kExistingWindowActivated, or kExistingWindowMinimized.
296 ash::ShelfItemDelegate::PerformedAction
ActivateWindowOrMinimizeIfActive(
297 ui::BaseWindow
* window
,
298 bool allow_minimize
);
300 // ash::ShelfDelegate:
301 void OnShelfCreated(ash::Shelf
* shelf
) override
;
302 void OnShelfDestroyed(ash::Shelf
* shelf
) override
;
303 ash::ShelfID
GetShelfIDForAppID(const std::string
& app_id
) override
;
304 bool HasShelfIDToAppIDMapping(ash::ShelfID id
) const override
;
305 const std::string
& GetAppIDForShelfID(ash::ShelfID id
) override
;
306 void PinAppWithID(const std::string
& app_id
) override
;
307 bool IsAppPinned(const std::string
& app_id
) override
;
308 bool CanPin() const override
;
309 void UnpinAppWithID(const std::string
& app_id
) override
;
311 // ash::ShelfItemDelegateManagerObserver:
312 void OnSetShelfItemDelegate(ash::ShelfID id
,
313 ash::ShelfItemDelegate
* item_delegate
) override
;
315 // ash::ShelfModelObserver:
316 void ShelfItemAdded(int index
) override
;
317 void ShelfItemRemoved(int index
, ash::ShelfID id
) override
;
318 void ShelfItemMoved(int start_index
, int target_index
) override
;
319 void ShelfItemChanged(int index
, const ash::ShelfItem
& old_item
) override
;
320 void ShelfStatusChanged() override
;
322 // ash::ShellObserver:
323 void OnShelfAlignmentChanged(aura::Window
* root_window
) override
;
325 // ash::DisplayController::Observer:
326 void OnDisplayConfigurationChanged() override
;
328 // ExtensionRegistryObserver:
329 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
330 const extensions::Extension
* extension
) override
;
331 void OnExtensionUnloaded(
332 content::BrowserContext
* browser_context
,
333 const extensions::Extension
* extension
,
334 extensions::UnloadedExtensionInfo::Reason reason
) override
;
336 // PrefServiceSyncableObserver:
337 void OnIsSyncingChanged() override
;
339 // AppSyncUIStateObserver:
340 void OnAppSyncUIStatusChanged() override
;
342 // ExtensionEnableFlowDelegate:
343 void ExtensionEnableFlowFinished() override
;
344 void ExtensionEnableFlowAborted(bool user_initiated
) override
;
346 // extensions::AppIconLoader:
347 void SetAppImage(const std::string
& app_id
,
348 const gfx::ImageSkia
& image
) override
;
350 // ash::ShelfLayoutManagerObserver:
351 void OnAutoHideBehaviorChanged(
352 aura::Window
* root_window
,
353 ash::ShelfAutoHideBehavior new_behavior
) override
;
355 // Called when the active user has changed.
356 void ActiveUserChanged(const std::string
& user_email
);
358 // Called when a user got added to the session.
359 void AdditionalUserAddedToSession(Profile
* profile
);
361 // Get the list of all running incarnations of this item.
362 // |event_flags| specifies the flags which were set by the event which
363 // triggered this menu generation. It can be used to generate different lists.
364 ChromeLauncherAppMenuItems
GetApplicationList(const ash::ShelfItem
& item
,
367 // Get the list of all tabs which belong to a certain application type.
368 std::vector
<content::WebContents
*> GetV1ApplicationsFromAppId(
371 // Activates a specified shell application.
372 void ActivateShellApp(const std::string
& app_id
, int index
);
374 // Checks if a given |web_contents| is known to be associated with an
375 // application of type |app_id|.
376 bool IsWebContentHandledByApplication(content::WebContents
* web_contents
,
377 const std::string
& app_id
);
379 // Check if the gMail app is loaded and it can handle the given web content.
380 // This special treatment is required to address crbug.com/234268.
381 bool ContentCanBeHandledByGmailApp(content::WebContents
* web_contents
);
383 // Get the favicon for the application list entry for |web_contents|.
384 // Note that for incognito windows the incognito icon will be returned.
385 // If |web_contents| has not loaded, returns the default favicon.
386 gfx::Image
GetAppListIcon(content::WebContents
* web_contents
) const;
388 // Get the title for the applicatoin list entry for |web_contents|.
389 // If |web_contents| has not loaded, returns "Net Tab".
390 base::string16
GetAppListTitle(content::WebContents
* web_contents
) const;
392 // Returns the LauncherItemController of BrowserShortcut.
393 BrowserShortcutLauncherItemController
*
394 GetBrowserShortcutLauncherItemController();
396 LauncherItemController
* GetLauncherItemController(const ash::ShelfID id
);
398 // Returns true if |browser| is owned by the active user.
399 bool IsBrowserFromActiveUser(Browser
* browser
);
401 // Check if the shelf visibility (location, visibility) will change with a new
402 // user profile or not. However, since the full visibility calculation of the
403 // shelf cannot be performed here, this is only a probability used for
404 // animation predictions.
405 bool ShelfBoundsChangesProbablyWithUser(aura::Window
* root_window
,
406 const std::string
& user_id
) const;
408 // Access to the BrowserStatusMonitor for tests.
409 BrowserStatusMonitor
* browser_status_monitor_for_test() {
410 return browser_status_monitor_
.get();
413 // Access to the AppWindowLauncherController for tests.
414 AppWindowLauncherController
* app_window_controller_for_test() {
415 return app_window_controller_
.get();
419 // Creates a new app shortcut item and controller on the shelf at |index|.
420 // Use kInsertItemAtEnd to add a shortcut as the last item.
421 ash::ShelfID
CreateAppShortcutLauncherItem(const std::string
& app_id
,
424 // Sets the AppTabHelper/AppIconLoader, taking ownership of the helper class.
425 // These are intended for testing.
426 void SetAppTabHelperForTest(AppTabHelper
* helper
);
427 void SetAppIconLoaderForTest(extensions::AppIconLoader
* loader
);
428 const std::string
& GetAppIdFromShelfIdForTest(ash::ShelfID id
);
430 // Sets the ash::ShelfItemDelegateManager only for unittests and doesn't
431 // take an ownership of it.
432 void SetShelfItemDelegateManagerForTest(
433 ash::ShelfItemDelegateManager
* manager
);
436 friend class ChromeLauncherControllerTest
;
437 friend class ShelfAppBrowserTest
;
438 friend class LauncherPlatformAppBrowserTest
;
440 typedef std::map
<ash::ShelfID
, LauncherItemController
*> IDToItemControllerMap
;
441 typedef std::map
<content::WebContents
*, std::string
> WebContentsToAppIDMap
;
443 // Remembers / restores list of running applications.
444 // Note that this order will neither be stored in the preference nor will it
445 // remember the order of closed applications since it is only temporary.
446 void RememberUnpinnedRunningApplicationOrder();
447 void RestoreUnpinnedRunningApplicationOrder(const std::string
& user_id
);
449 // Creates a new app shortcut item and controller on the shelf at |index|.
450 // Use kInsertItemAtEnd to add a shortcut as the last item.
451 ash::ShelfID
CreateAppShortcutLauncherItemWithType(
452 const std::string
& app_id
,
454 ash::ShelfItemType shelf_item_type
);
456 // Invoked when the associated browser or app is closed.
457 void LauncherItemClosed(ash::ShelfID id
);
459 // Internal helpers for pinning and unpinning that handle both
460 // client-triggered and internal pinning operations.
461 void DoPinAppWithID(const std::string
& app_id
);
462 void DoUnpinAppWithID(const std::string
& app_id
);
464 // Pin a running app with |shelf_id| internally to |index|. It returns
465 // the index where the item was pinned.
466 int PinRunningAppInternal(int index
, ash::ShelfID shelf_id
);
468 // Unpin a locked application. This is an internal call which converts the
469 // model type of the given app index from a shortcut into an unpinned running
471 void UnpinRunningAppInternal(int index
);
473 // Re-syncs shelf model with prefs::kPinnedLauncherApps.
474 void UpdateAppLaunchersFromPref();
476 // Persists the shelf auto-hide behavior to prefs.
477 void SetShelfAutoHideBehaviorPrefs(ash::ShelfAutoHideBehavior behavior
,
478 aura::Window
* root_window
);
480 // Sets the shelf auto-hide behavior from prefs.
481 void SetShelfAutoHideBehaviorFromPrefs();
483 // Sets the shelf alignment from prefs.
484 void SetShelfAlignmentFromPrefs();
486 // Sets both of auto-hide behavior and alignment from prefs.
487 void SetShelfBehaviorsFromPrefs();
489 #if defined(OS_CHROMEOS)
490 // Sets whether the virtual keyboard is enabled from prefs.
491 void SetVirtualKeyboardBehaviorFromPrefs();
492 #endif // defined(OS_CHROMEOS)
494 // Returns the shelf item status for the given |app_id|, which can be either
495 // STATUS_ACTIVE (if the app is active), STATUS_RUNNING (if there is such an
496 // app) or STATUS_CLOSED.
497 ash::ShelfItemStatus
GetAppState(const std::string
& app_id
);
499 // Creates an app launcher to insert at |index|. Note that |index| may be
500 // adjusted by the model to meet ordering constraints.
501 // The |shelf_item_type| will be set into the ShelfModel.
502 ash::ShelfID
InsertAppLauncherItem(LauncherItemController
* controller
,
503 const std::string
& app_id
,
504 ash::ShelfItemStatus status
,
506 ash::ShelfItemType shelf_item_type
);
508 // Enumerate all Web contents which match a given shortcut |controller|.
509 std::vector
<content::WebContents
*> GetV1ApplicationsFromController(
510 LauncherItemController
* controller
);
512 // Create ShelfItem for Browser Shortcut.
513 ash::ShelfID
CreateBrowserShortcutLauncherItem();
515 // Check if the given |web_contents| is in incognito mode.
516 bool IsIncognito(const content::WebContents
* web_contents
) const;
518 // Update browser shortcut's index.
519 void PersistChromeItemIndex(int index
);
521 // Get browser shortcut's index from pref.
522 int GetChromeIconIndexFromPref() const;
524 // Depending on the provided flags, move either the chrome icon, the app icon
525 // or none to the given |target_index|. The provided |chrome_index| and
526 // |app_list_index| locations will get adjusted within this call to finalize
527 // the action and to make sure that the other item can still be moved
528 // afterwards (index adjustments).
529 void MoveChromeOrApplistToFinalPosition(
534 int* app_list_index
);
536 // Finds the index of where to insert the next item.
537 int FindInsertionPoint(bool is_app_list
);
539 // Get the browser shortcut's index in the shelf using the current's systems
540 // configuration of pinned and known (but not running) apps.
541 int GetChromeIconIndexForCreation();
543 // Get the list of pinned programs from the preferences.
544 std::vector
<std::string
> GetListOfPinnedAppsAndBrowser();
546 // Close all windowed V1 applications of a certain extension which was already
548 void CloseWindowedAppsFromRemovedExtension(const std::string
& app_id
,
549 const Profile
* profile
);
551 // Set ShelfItemDelegate |item_delegate| for |id| and take an ownership.
552 // TODO(simon.hong81): Make this take a scoped_ptr of |item_delegate|.
553 void SetShelfItemDelegate(ash::ShelfID id
,
554 ash::ShelfItemDelegate
* item_delegate
);
556 // Attach to a specific profile.
557 void AttachProfile(Profile
* proifile
);
559 // Forget the current profile to allow attaching to a new one.
560 void ReleaseProfile();
562 // Returns true if |app_id| is a Packaged App that has already launched on the
563 // native desktop and, if so, executes it as a desktop shortcut to activate
564 // desktop mode and send another OnLaunched event to the Extension.
565 bool LaunchedInNativeDesktop(const std::string
& app_id
);
567 static ChromeLauncherController
* instance_
;
569 ash::ShelfModel
* model_
;
571 ash::ShelfItemDelegateManager
* item_delegate_manager_
;
573 // Profile used for prefs and loading extensions. This is NOT necessarily the
574 // profile new windows are created with.
577 // Controller items in this map are owned by |ShelfItemDelegateManager|.
578 IDToItemControllerMap id_to_item_controller_map_
;
580 // Direct access to app_id for a web contents.
581 WebContentsToAppIDMap web_contents_to_app_id_
;
583 // Used to track app windows.
584 scoped_ptr
<AppWindowLauncherController
> app_window_controller_
;
586 // Used to get app info for tabs.
587 scoped_ptr
<AppTabHelper
> app_tab_helper_
;
589 // Used to load the image for an app item.
590 scoped_ptr
<extensions::AppIconLoader
> app_icon_loader_
;
592 PrefChangeRegistrar pref_change_registrar_
;
594 AppSyncUIState
* app_sync_ui_state_
;
596 scoped_ptr
<ExtensionEnableFlow
> extension_enable_flow_
;
598 // Shelves that are currently being observed.
599 std::set
<ash::Shelf
*> shelves_
;
601 // The owned browser status monitor.
602 scoped_ptr
<BrowserStatusMonitor
> browser_status_monitor_
;
604 // A special observer class to detect user switches.
605 scoped_ptr
<ChromeLauncherControllerUserSwitchObserver
> user_switch_observer_
;
607 // If true, incoming pinned state changes should be ignored.
608 bool ignore_persist_pinned_state_change_
;
610 // The list of running & un-pinned applications for different users on hidden
612 typedef std::vector
<std::string
> RunningAppListIds
;
613 typedef std::map
<std::string
, RunningAppListIds
> RunningAppListIdMap
;
614 RunningAppListIdMap last_used_running_application_order_
;
616 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherController
);
619 #endif // CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_