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 void ActivateWindowOrMinimizeIfActive(ui::BaseWindow
* window
,
295 bool allow_minimize
);
297 // ash::ShelfDelegate:
298 void OnShelfCreated(ash::Shelf
* shelf
) override
;
299 void OnShelfDestroyed(ash::Shelf
* shelf
) override
;
300 ash::ShelfID
GetShelfIDForAppID(const std::string
& app_id
) override
;
301 const std::string
& GetAppIDForShelfID(ash::ShelfID id
) override
;
302 void PinAppWithID(const std::string
& app_id
) override
;
303 bool IsAppPinned(const std::string
& app_id
) override
;
304 bool CanPin() const override
;
305 void UnpinAppWithID(const std::string
& app_id
) override
;
307 // ash::ShelfItemDelegateManagerObserver:
308 void OnSetShelfItemDelegate(ash::ShelfID id
,
309 ash::ShelfItemDelegate
* item_delegate
) override
;
311 // ash::ShelfModelObserver:
312 void ShelfItemAdded(int index
) override
;
313 void ShelfItemRemoved(int index
, ash::ShelfID id
) override
;
314 void ShelfItemMoved(int start_index
, int target_index
) override
;
315 void ShelfItemChanged(int index
, const ash::ShelfItem
& old_item
) override
;
316 void ShelfStatusChanged() override
;
318 // ash::ShellObserver:
319 void OnShelfAlignmentChanged(aura::Window
* root_window
) override
;
321 // ash::DisplayController::Observer:
322 void OnDisplayConfigurationChanged() override
;
324 // ExtensionRegistryObserver:
325 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
326 const extensions::Extension
* extension
) override
;
327 void OnExtensionUnloaded(
328 content::BrowserContext
* browser_context
,
329 const extensions::Extension
* extension
,
330 extensions::UnloadedExtensionInfo::Reason reason
) override
;
332 // PrefServiceSyncableObserver:
333 void OnIsSyncingChanged() override
;
335 // AppSyncUIStateObserver:
336 void OnAppSyncUIStatusChanged() override
;
338 // ExtensionEnableFlowDelegate:
339 void ExtensionEnableFlowFinished() override
;
340 void ExtensionEnableFlowAborted(bool user_initiated
) override
;
342 // extensions::AppIconLoader:
343 void SetAppImage(const std::string
& app_id
,
344 const gfx::ImageSkia
& image
) override
;
346 // ash::ShelfLayoutManagerObserver:
347 void OnAutoHideBehaviorChanged(
348 aura::Window
* root_window
,
349 ash::ShelfAutoHideBehavior new_behavior
) override
;
351 // Called when the active user has changed.
352 void ActiveUserChanged(const std::string
& user_email
);
354 // Called when a user got added to the session.
355 void AdditionalUserAddedToSession(Profile
* profile
);
357 // Get the list of all running incarnations of this item.
358 // |event_flags| specifies the flags which were set by the event which
359 // triggered this menu generation. It can be used to generate different lists.
360 ChromeLauncherAppMenuItems
GetApplicationList(const ash::ShelfItem
& item
,
363 // Get the list of all tabs which belong to a certain application type.
364 std::vector
<content::WebContents
*> GetV1ApplicationsFromAppId(
367 // Activates a specified shell application.
368 void ActivateShellApp(const std::string
& app_id
, int index
);
370 // Checks if a given |web_contents| is known to be associated with an
371 // application of type |app_id|.
372 bool IsWebContentHandledByApplication(content::WebContents
* web_contents
,
373 const std::string
& app_id
);
375 // Check if the gMail app is loaded and it can handle the given web content.
376 // This special treatment is required to address crbug.com/234268.
377 bool ContentCanBeHandledByGmailApp(content::WebContents
* web_contents
);
379 // Get the favicon for the application list entry for |web_contents|.
380 // Note that for incognito windows the incognito icon will be returned.
381 // If |web_contents| has not loaded, returns the default favicon.
382 gfx::Image
GetAppListIcon(content::WebContents
* web_contents
) const;
384 // Get the title for the applicatoin list entry for |web_contents|.
385 // If |web_contents| has not loaded, returns "Net Tab".
386 base::string16
GetAppListTitle(content::WebContents
* web_contents
) const;
388 // Returns the LauncherItemController of BrowserShortcut.
389 BrowserShortcutLauncherItemController
*
390 GetBrowserShortcutLauncherItemController();
392 LauncherItemController
* GetLauncherItemController(const ash::ShelfID id
);
394 // Returns true if |browser| is owned by the active user.
395 bool IsBrowserFromActiveUser(Browser
* browser
);
397 // Check if the shelf visibility (location, visibility) will change with a new
398 // user profile or not. However, since the full visibility calculation of the
399 // shelf cannot be performed here, this is only a probability used for
400 // animation predictions.
401 bool ShelfBoundsChangesProbablyWithUser(aura::Window
* root_window
,
402 const std::string
& user_id
) const;
404 // Access to the BrowserStatusMonitor for tests.
405 BrowserStatusMonitor
* browser_status_monitor_for_test() {
406 return browser_status_monitor_
.get();
409 // Access to the AppWindowLauncherController for tests.
410 AppWindowLauncherController
* app_window_controller_for_test() {
411 return app_window_controller_
.get();
415 // Creates a new app shortcut item and controller on the shelf at |index|.
416 // Use kInsertItemAtEnd to add a shortcut as the last item.
417 ash::ShelfID
CreateAppShortcutLauncherItem(const std::string
& app_id
,
420 // Sets the AppTabHelper/AppIconLoader, taking ownership of the helper class.
421 // These are intended for testing.
422 void SetAppTabHelperForTest(AppTabHelper
* helper
);
423 void SetAppIconLoaderForTest(extensions::AppIconLoader
* loader
);
424 const std::string
& GetAppIdFromShelfIdForTest(ash::ShelfID id
);
426 // Sets the ash::ShelfItemDelegateManager only for unittests and doesn't
427 // take an ownership of it.
428 void SetShelfItemDelegateManagerForTest(
429 ash::ShelfItemDelegateManager
* manager
);
432 friend class ChromeLauncherControllerTest
;
433 friend class ShelfAppBrowserTest
;
434 friend class LauncherPlatformAppBrowserTest
;
436 typedef std::map
<ash::ShelfID
, LauncherItemController
*> IDToItemControllerMap
;
437 typedef std::map
<content::WebContents
*, std::string
> WebContentsToAppIDMap
;
439 // Remembers / restores list of running applications.
440 // Note that this order will neither be stored in the preference nor will it
441 // remember the order of closed applications since it is only temporary.
442 void RememberUnpinnedRunningApplicationOrder();
443 void RestoreUnpinnedRunningApplicationOrder(const std::string
& user_id
);
445 // Creates a new app shortcut item and controller on the shelf at |index|.
446 // Use kInsertItemAtEnd to add a shortcut as the last item.
447 ash::ShelfID
CreateAppShortcutLauncherItemWithType(
448 const std::string
& app_id
,
450 ash::ShelfItemType shelf_item_type
);
452 // Invoked when the associated browser or app is closed.
453 void LauncherItemClosed(ash::ShelfID id
);
455 // Internal helpers for pinning and unpinning that handle both
456 // client-triggered and internal pinning operations.
457 void DoPinAppWithID(const std::string
& app_id
);
458 void DoUnpinAppWithID(const std::string
& app_id
);
460 // Pin a running app with |shelf_id| internally to |index|. It returns
461 // the index where the item was pinned.
462 int PinRunningAppInternal(int index
, ash::ShelfID shelf_id
);
464 // Unpin a locked application. This is an internal call which converts the
465 // model type of the given app index from a shortcut into an unpinned running
467 void UnpinRunningAppInternal(int index
);
469 // Re-syncs shelf model with prefs::kPinnedLauncherApps.
470 void UpdateAppLaunchersFromPref();
472 // Persists the shelf auto-hide behavior to prefs.
473 void SetShelfAutoHideBehaviorPrefs(ash::ShelfAutoHideBehavior behavior
,
474 aura::Window
* root_window
);
476 // Sets the shelf auto-hide behavior from prefs.
477 void SetShelfAutoHideBehaviorFromPrefs();
479 // Sets the shelf alignment from prefs.
480 void SetShelfAlignmentFromPrefs();
482 // Sets both of auto-hide behavior and alignment from prefs.
483 void SetShelfBehaviorsFromPrefs();
485 #if defined(OS_CHROMEOS)
486 // Sets whether the virtual keyboard is enabled from prefs.
487 void SetVirtualKeyboardBehaviorFromPrefs();
488 #endif // defined(OS_CHROMEOS)
490 // Returns the shelf item status for the given |app_id|, which can be either
491 // STATUS_ACTIVE (if the app is active), STATUS_RUNNING (if there is such an
492 // app) or STATUS_CLOSED.
493 ash::ShelfItemStatus
GetAppState(const std::string
& app_id
);
495 // Creates an app launcher to insert at |index|. Note that |index| may be
496 // adjusted by the model to meet ordering constraints.
497 // The |shelf_item_type| will be set into the ShelfModel.
498 ash::ShelfID
InsertAppLauncherItem(LauncherItemController
* controller
,
499 const std::string
& app_id
,
500 ash::ShelfItemStatus status
,
502 ash::ShelfItemType shelf_item_type
);
504 bool HasItemController(ash::ShelfID id
) const;
506 // Enumerate all Web contents which match a given shortcut |controller|.
507 std::vector
<content::WebContents
*> GetV1ApplicationsFromController(
508 LauncherItemController
* controller
);
510 // Create ShelfItem for Browser Shortcut.
511 ash::ShelfID
CreateBrowserShortcutLauncherItem();
513 // Check if the given |web_contents| is in incognito mode.
514 bool IsIncognito(const content::WebContents
* web_contents
) const;
516 // Update browser shortcut's index.
517 void PersistChromeItemIndex(int index
);
519 // Get browser shortcut's index from pref.
520 int GetChromeIconIndexFromPref() const;
522 // Depending on the provided flags, move either the chrome icon, the app icon
523 // or none to the given |target_index|. The provided |chrome_index| and
524 // |app_list_index| locations will get adjusted within this call to finalize
525 // the action and to make sure that the other item can still be moved
526 // afterwards (index adjustments).
527 void MoveChromeOrApplistToFinalPosition(
532 int* app_list_index
);
534 // Finds the index of where to insert the next item.
535 int FindInsertionPoint(bool is_app_list
);
537 // Get the browser shortcut's index in the shelf using the current's systems
538 // configuration of pinned and known (but not running) apps.
539 int GetChromeIconIndexForCreation();
541 // Get the list of pinned programs from the preferences.
542 std::vector
<std::string
> GetListOfPinnedAppsAndBrowser();
544 // Close all windowed V1 applications of a certain extension which was already
546 void CloseWindowedAppsFromRemovedExtension(const std::string
& app_id
,
547 const Profile
* profile
);
549 // Set ShelfItemDelegate |item_delegate| for |id| and take an ownership.
550 // TODO(simon.hong81): Make this take a scoped_ptr of |item_delegate|.
551 void SetShelfItemDelegate(ash::ShelfID id
,
552 ash::ShelfItemDelegate
* item_delegate
);
554 // Attach to a specific profile.
555 void AttachProfile(Profile
* proifile
);
557 // Forget the current profile to allow attaching to a new one.
558 void ReleaseProfile();
560 // Returns true if |app_id| is a Packaged App that has already launched on the
561 // native desktop and, if so, executes it as a desktop shortcut to activate
562 // desktop mode and send another OnLaunched event to the Extension.
563 bool LaunchedInNativeDesktop(const std::string
& app_id
);
565 static ChromeLauncherController
* instance_
;
567 ash::ShelfModel
* model_
;
569 ash::ShelfItemDelegateManager
* item_delegate_manager_
;
571 // Profile used for prefs and loading extensions. This is NOT necessarily the
572 // profile new windows are created with.
575 // Controller items in this map are owned by |ShelfItemDelegateManager|.
576 IDToItemControllerMap id_to_item_controller_map_
;
578 // Direct access to app_id for a web contents.
579 WebContentsToAppIDMap web_contents_to_app_id_
;
581 // Used to track app windows.
582 scoped_ptr
<AppWindowLauncherController
> app_window_controller_
;
584 // Used to get app info for tabs.
585 scoped_ptr
<AppTabHelper
> app_tab_helper_
;
587 // Used to load the image for an app item.
588 scoped_ptr
<extensions::AppIconLoader
> app_icon_loader_
;
590 PrefChangeRegistrar pref_change_registrar_
;
592 AppSyncUIState
* app_sync_ui_state_
;
594 scoped_ptr
<ExtensionEnableFlow
> extension_enable_flow_
;
596 // Shelves that are currently being observed.
597 std::set
<ash::Shelf
*> shelves_
;
599 // The owned browser status monitor.
600 scoped_ptr
<BrowserStatusMonitor
> browser_status_monitor_
;
602 // A special observer class to detect user switches.
603 scoped_ptr
<ChromeLauncherControllerUserSwitchObserver
> user_switch_observer_
;
605 // If true, incoming pinned state changes should be ignored.
606 bool ignore_persist_pinned_state_change_
;
608 // True if each user has an own desktop.
609 bool multi_profile_desktop_separation_
;
611 // The list of running & un-pinned applications for different users on hidden
613 typedef std::vector
<std::string
> RunningAppListIds
;
614 typedef std::map
<std::string
, RunningAppListIds
> RunningAppListIdMap
;
615 RunningAppListIdMap last_used_running_application_order_
;
617 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherController
);
620 #endif // CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_