Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / ash / launcher / chrome_launcher_controller.h
blob525c2c16a5144ad1f0ec0e53cf0093c0f4ce19a1
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_
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
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/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"
38 class AppSyncUIState;
39 class Browser;
40 class BrowserShortcutLauncherItemController;
41 class BrowserStatusMonitor;
42 class ExtensionEnableFlow;
43 class GURL;
44 class LauncherItemController;
45 class Profile;
46 class AppWindowLauncherController;
47 class TabContents;
49 namespace ash {
50 class ShelfItemDelegateManager;
51 class ShelfModel;
54 namespace aura {
55 class Window;
58 namespace content {
59 class BrowserContext;
60 class WebContents;
63 namespace extensions {
64 class Extension;
67 namespace ui {
68 class BaseWindow;
71 #if defined(OS_CHROMEOS)
72 class ChromeLauncherControllerUserSwitchObserver;
73 #endif
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 : public ash::ShelfDelegate,
87 public ash::ShelfModelObserver,
88 public ash::ShellObserver,
89 public ash::WindowTreeHostManager::Observer,
90 public extensions::ExtensionRegistryObserver,
91 public extensions::AppIconLoader::Delegate,
92 public PrefServiceSyncableObserver,
93 public AppSyncUIStateObserver,
94 public ExtensionEnableFlowDelegate,
95 public ash::ShelfLayoutManagerObserver,
96 public ash::ShelfItemDelegateManagerObserver {
97 public:
98 // Indicates if a shelf item is incognito or not.
99 enum IncognitoState {
100 STATE_INCOGNITO,
101 STATE_NOT_INCOGNITO,
104 // Used to update the state of non plaform apps, as web contents change.
105 enum AppState {
106 APP_STATE_ACTIVE,
107 APP_STATE_WINDOW_ACTIVE,
108 APP_STATE_INACTIVE,
109 APP_STATE_REMOVED
112 // Mockable interface to get app ids from tabs.
113 class AppTabHelper {
114 public:
115 virtual ~AppTabHelper() {}
117 // Returns the app id of the specified tab, or an empty string if there is
118 // no app. All known profiles will be queried for this.
119 virtual std::string GetAppID(content::WebContents* tab) = 0;
121 // Returns true if |id| is valid for the currently active profile.
122 // Used during restore to ignore no longer valid extensions.
123 // Note that already running applications are ignored by the restore
124 // process.
125 virtual bool IsValidIDForCurrentUser(const std::string& id) = 0;
127 // Sets the currently active profile for the usage of |GetAppID|.
128 virtual void SetCurrentUser(Profile* profile) = 0;
131 ChromeLauncherController(Profile* profile, ash::ShelfModel* model);
132 ~ChromeLauncherController() override;
134 // Initializes this ChromeLauncherController.
135 void Init();
137 // Creates an instance.
138 static ChromeLauncherController* CreateInstance(Profile* profile,
139 ash::ShelfModel* model);
141 // Returns the single ChromeLauncherController instance.
142 static ChromeLauncherController* instance() { return instance_; }
144 // Creates a new app item on the shelf for |controller|.
145 ash::ShelfID CreateAppLauncherItem(LauncherItemController* controller,
146 const std::string& app_id,
147 ash::ShelfItemStatus status);
149 // Updates the running status of an item. It will also update the status of
150 // browsers shelf item if needed.
151 void SetItemStatus(ash::ShelfID id, ash::ShelfItemStatus status);
153 // Updates the controller associated with id (which should be a shortcut).
154 // |controller| will be owned by the |ChromeLauncherController| and then
155 // passed on to |ShelfItemDelegateManager|.
156 // TODO(skuhne): Pass in scoped_ptr to make ownership clear.
157 void SetItemController(ash::ShelfID id, LauncherItemController* controller);
159 // Closes or unpins the shelf item.
160 void CloseLauncherItem(ash::ShelfID id);
162 // Pins the specified id. Currently only supports platform apps.
163 void Pin(ash::ShelfID id);
165 // Unpins the specified id, closing if not running.
166 void Unpin(ash::ShelfID id);
168 // Returns true if the item identified by |id| is pinned.
169 bool IsPinned(ash::ShelfID id);
171 // Pins/unpins the specified id.
172 void TogglePinned(ash::ShelfID id);
174 // Returns true if the specified item can be pinned or unpinned. Only apps can
175 // be pinned.
176 bool IsPinnable(ash::ShelfID id) const;
178 // If there is no shelf item in the shelf for application |app_id|, one
179 // gets created. The (existing or created) shelf items get then locked
180 // against a users un-pinning removal.
181 void LockV1AppWithID(const std::string& app_id);
183 // A previously locked shelf item of type |app_id| gets unlocked. If the
184 // lock count reaches 0 and the item is not pinned it will go away.
185 void UnlockV1AppWithID(const std::string& app_id);
187 // Requests that the shelf item controller specified by |id| open a new
188 // instance of the app. |event_flags| holds the flags of the event which
189 // triggered this command.
190 void Launch(ash::ShelfID id, int event_flags);
192 // Closes the specified item.
193 void Close(ash::ShelfID id);
195 // Returns true if the specified item is open.
196 bool IsOpen(ash::ShelfID id);
198 // Returns true if the specified item is for a platform app.
199 bool IsPlatformApp(ash::ShelfID id);
201 // Opens a new instance of the application identified by |app_id|.
202 // Used by the app-list, and by pinned-app shelf items.
203 void LaunchApp(const std::string& app_id,
204 ash::LaunchSource source,
205 int event_flags);
207 // If |app_id| is running, reactivates the app's most recently active window,
208 // otherwise launches and activates the app.
209 // Used by the app-list, and by pinned-app shelf items.
210 void ActivateApp(const std::string& app_id,
211 ash::LaunchSource source,
212 int event_flags);
214 // Returns the launch type of app for the specified id.
215 extensions::LaunchType GetLaunchType(ash::ShelfID id);
217 // Set the image for a specific shelf item (e.g. when set by the app).
218 void SetLauncherItemImage(ash::ShelfID shelf_id, const gfx::ImageSkia& image);
220 // Find out if the given application |id| is a windowed app item and not a
221 // pinned item in the shelf.
222 bool IsWindowedAppInLauncher(const std::string& app_id);
224 // Updates the launch type of the app for the specified id to |launch_type|.
225 void SetLaunchType(ash::ShelfID id, extensions::LaunchType launch_type);
227 // Returns true if the user is currently logged in as a guest.
228 // Makes virtual for unittest in LauncherContextMenuTest.
229 virtual bool IsLoggedInAsGuest();
231 // Invoked when user clicks on button in the shelf and there is no last
232 // used window (or CTRL is held with the click).
233 void CreateNewWindow();
235 // Invoked when the user clicks on button in the shelf to create a new
236 // incognito window.
237 void CreateNewIncognitoWindow();
239 // Updates the pinned pref state. The pinned state consists of a list pref.
240 // Each item of the list is a dictionary. The key |kAppIDPath| gives the
241 // id of the app.
242 void PersistPinnedState();
244 ash::ShelfModel* model();
246 // Accessor to the currently loaded profile. Note that in multi profile use
247 // cases this might change over time.
248 Profile* profile();
250 // Gets the shelf auto-hide behavior on |root_window|.
251 ash::ShelfAutoHideBehavior GetShelfAutoHideBehavior(
252 aura::Window* root_window) const;
254 // Returns |true| if the user is allowed to modify the shelf auto-hide
255 // behavior on |root_window|.
256 bool CanUserModifyShelfAutoHideBehavior(aura::Window* root_window) const;
258 // Toggles the shelf auto-hide behavior on |root_window|. Does nothing if the
259 // user is not allowed to modify the auto-hide behavior.
260 void ToggleShelfAutoHideBehavior(aura::Window* root_window);
262 // Notify the controller that the state of an non platform app's tabs
263 // have changed,
264 void UpdateAppState(content::WebContents* contents, AppState app_state);
266 // Returns ShelfID for |contents|. If |contents| is not an app or is not
267 // pinned, returns the id of browser shrotcut.
268 ash::ShelfID GetShelfIDForWebContents(content::WebContents* contents);
270 // Limits application refocusing to urls that match |url| for |id|.
271 void SetRefocusURLPatternForTest(ash::ShelfID id, const GURL& url);
273 // Returns the extension identified by |app_id|.
274 const extensions::Extension* GetExtensionForAppID(
275 const std::string& app_id) const;
277 // Activates a |window|. If |allow_minimize| is true and the system allows
278 // it, the the window will get minimized instead.
279 // Returns the action performed. Should be one of kNoAction,
280 // kExistingWindowActivated, or kExistingWindowMinimized.
281 ash::ShelfItemDelegate::PerformedAction ActivateWindowOrMinimizeIfActive(
282 ui::BaseWindow* window,
283 bool allow_minimize);
285 // ash::ShelfDelegate:
286 void OnShelfCreated(ash::Shelf* shelf) override;
287 void OnShelfDestroyed(ash::Shelf* shelf) override;
288 ash::ShelfID GetShelfIDForAppID(const std::string& app_id) override;
289 bool HasShelfIDToAppIDMapping(ash::ShelfID id) const override;
290 const std::string& GetAppIDForShelfID(ash::ShelfID id) override;
291 void PinAppWithID(const std::string& app_id) override;
292 bool IsAppPinned(const std::string& app_id) override;
293 bool CanPin() const override;
294 void UnpinAppWithID(const std::string& app_id) override;
296 // ash::ShelfItemDelegateManagerObserver:
297 void OnSetShelfItemDelegate(ash::ShelfID id,
298 ash::ShelfItemDelegate* item_delegate) override;
300 // ash::ShelfModelObserver:
301 void ShelfItemAdded(int index) override;
302 void ShelfItemRemoved(int index, ash::ShelfID id) override;
303 void ShelfItemMoved(int start_index, int target_index) override;
304 void ShelfItemChanged(int index, const ash::ShelfItem& old_item) override;
305 void ShelfStatusChanged() override;
307 // ash::ShellObserver:
308 void OnShelfAlignmentChanged(aura::Window* root_window) override;
310 // ash::WindowTreeHostManager::Observer:
311 void OnDisplayConfigurationChanged() override;
313 // ExtensionRegistryObserver:
314 void OnExtensionLoaded(content::BrowserContext* browser_context,
315 const extensions::Extension* extension) override;
316 void OnExtensionUnloaded(
317 content::BrowserContext* browser_context,
318 const extensions::Extension* extension,
319 extensions::UnloadedExtensionInfo::Reason reason) override;
321 // PrefServiceSyncableObserver:
322 void OnIsSyncingChanged() override;
324 // AppSyncUIStateObserver:
325 void OnAppSyncUIStatusChanged() override;
327 // ExtensionEnableFlowDelegate:
328 void ExtensionEnableFlowFinished() override;
329 void ExtensionEnableFlowAborted(bool user_initiated) override;
331 // extensions::AppIconLoader:
332 void SetAppImage(const std::string& app_id,
333 const gfx::ImageSkia& image) override;
335 // ash::ShelfLayoutManagerObserver:
336 void OnAutoHideBehaviorChanged(
337 aura::Window* root_window,
338 ash::ShelfAutoHideBehavior new_behavior) override;
340 // Called when the active user has changed.
341 void ActiveUserChanged(const std::string& user_email);
343 // Called when a user got added to the session.
344 void AdditionalUserAddedToSession(Profile* profile);
346 // Get the list of all running incarnations of this item.
347 // |event_flags| specifies the flags which were set by the event which
348 // triggered this menu generation. It can be used to generate different lists.
349 ChromeLauncherAppMenuItems GetApplicationList(const ash::ShelfItem& item,
350 int event_flags);
352 // Get the list of all tabs which belong to a certain application type.
353 std::vector<content::WebContents*> GetV1ApplicationsFromAppId(
354 std::string app_id);
356 // Activates a specified shell application.
357 void ActivateShellApp(const std::string& app_id, int index);
359 // Checks if a given |web_contents| is known to be associated with an
360 // application of type |app_id|.
361 bool IsWebContentHandledByApplication(content::WebContents* web_contents,
362 const std::string& app_id);
364 // Check if the gMail app is loaded and it can handle the given web content.
365 // This special treatment is required to address crbug.com/234268.
366 bool ContentCanBeHandledByGmailApp(content::WebContents* web_contents);
368 // Get the favicon for the application list entry for |web_contents|.
369 // Note that for incognito windows the incognito icon will be returned.
370 // If |web_contents| has not loaded, returns the default favicon.
371 gfx::Image GetAppListIcon(content::WebContents* web_contents) const;
373 // Get the title for the applicatoin list entry for |web_contents|.
374 // If |web_contents| has not loaded, returns "Net Tab".
375 base::string16 GetAppListTitle(content::WebContents* web_contents) const;
377 // Returns the LauncherItemController of BrowserShortcut.
378 BrowserShortcutLauncherItemController*
379 GetBrowserShortcutLauncherItemController();
381 LauncherItemController* GetLauncherItemController(const ash::ShelfID id);
383 // Returns true if |browser| is owned by the active user.
384 bool IsBrowserFromActiveUser(Browser* browser);
386 // Check if the shelf visibility (location, visibility) will change with a new
387 // user profile or not. However, since the full visibility calculation of the
388 // shelf cannot be performed here, this is only a probability used for
389 // animation predictions.
390 bool ShelfBoundsChangesProbablyWithUser(aura::Window* root_window,
391 const std::string& user_id) const;
393 // Called when the user profile is fully loaded and ready to switch to.
394 void OnUserProfileReadyToSwitch(Profile* profile);
396 // Access to the BrowserStatusMonitor for tests.
397 BrowserStatusMonitor* browser_status_monitor_for_test() {
398 return browser_status_monitor_.get();
401 // Access to the AppWindowLauncherController for tests.
402 AppWindowLauncherController* app_window_controller_for_test() {
403 return app_window_controller_.get();
406 protected:
407 // Creates a new app shortcut item and controller on the shelf at |index|.
408 // Use kInsertItemAtEnd to add a shortcut as the last item.
409 ash::ShelfID CreateAppShortcutLauncherItem(const std::string& app_id,
410 int index);
412 // Sets the AppTabHelper/AppIconLoader, taking ownership of the helper class.
413 // These are intended for testing.
414 void SetAppTabHelperForTest(AppTabHelper* helper);
415 void SetAppIconLoaderForTest(extensions::AppIconLoader* loader);
416 const std::string& GetAppIdFromShelfIdForTest(ash::ShelfID id);
418 // Sets the ash::ShelfItemDelegateManager only for unittests and doesn't
419 // take an ownership of it.
420 void SetShelfItemDelegateManagerForTest(
421 ash::ShelfItemDelegateManager* manager);
423 private:
424 friend class ChromeLauncherControllerTest;
425 friend class ShelfAppBrowserTest;
426 friend class LauncherPlatformAppBrowserTest;
428 typedef std::map<ash::ShelfID, LauncherItemController*> IDToItemControllerMap;
429 typedef std::map<content::WebContents*, std::string> WebContentsToAppIDMap;
431 // Remembers / restores list of running applications.
432 // Note that this order will neither be stored in the preference nor will it
433 // remember the order of closed applications since it is only temporary.
434 void RememberUnpinnedRunningApplicationOrder();
435 void RestoreUnpinnedRunningApplicationOrder(const std::string& user_id);
437 // Creates a new app shortcut item and controller on the shelf at |index|.
438 // Use kInsertItemAtEnd to add a shortcut as the last item.
439 ash::ShelfID CreateAppShortcutLauncherItemWithType(
440 const std::string& app_id,
441 int index,
442 ash::ShelfItemType shelf_item_type);
444 // Invoked when the associated browser or app is closed.
445 void LauncherItemClosed(ash::ShelfID id);
447 // Internal helpers for pinning and unpinning that handle both
448 // client-triggered and internal pinning operations.
449 void DoPinAppWithID(const std::string& app_id);
450 void DoUnpinAppWithID(const std::string& app_id);
452 // Pin a running app with |shelf_id| internally to |index|. It returns
453 // the index where the item was pinned.
454 int PinRunningAppInternal(int index, ash::ShelfID shelf_id);
456 // Unpin a locked application. This is an internal call which converts the
457 // model type of the given app index from a shortcut into an unpinned running
458 // app.
459 void UnpinRunningAppInternal(int index);
461 // Re-syncs shelf model with prefs::kPinnedLauncherApps.
462 void UpdateAppLaunchersFromPref();
464 // Persists the shelf auto-hide behavior to prefs.
465 void SetShelfAutoHideBehaviorPrefs(ash::ShelfAutoHideBehavior behavior,
466 aura::Window* root_window);
468 // Sets the shelf auto-hide behavior from prefs.
469 void SetShelfAutoHideBehaviorFromPrefs();
471 // Sets the shelf alignment from prefs.
472 void SetShelfAlignmentFromPrefs();
474 // Sets both of auto-hide behavior and alignment from prefs.
475 void SetShelfBehaviorsFromPrefs();
477 #if defined(OS_CHROMEOS)
478 // Sets whether the virtual keyboard is enabled from prefs.
479 void SetVirtualKeyboardBehaviorFromPrefs();
480 #endif // defined(OS_CHROMEOS)
482 // Returns the shelf item status for the given |app_id|, which can be either
483 // STATUS_ACTIVE (if the app is active), STATUS_RUNNING (if there is such an
484 // app) or STATUS_CLOSED.
485 ash::ShelfItemStatus GetAppState(const std::string& app_id);
487 // Creates an app launcher to insert at |index|. Note that |index| may be
488 // adjusted by the model to meet ordering constraints.
489 // The |shelf_item_type| will be set into the ShelfModel.
490 ash::ShelfID InsertAppLauncherItem(LauncherItemController* controller,
491 const std::string& app_id,
492 ash::ShelfItemStatus status,
493 int index,
494 ash::ShelfItemType shelf_item_type);
496 // Enumerate all Web contents which match a given shortcut |controller|.
497 std::vector<content::WebContents*> GetV1ApplicationsFromController(
498 LauncherItemController* controller);
500 // Create ShelfItem for Browser Shortcut.
501 ash::ShelfID CreateBrowserShortcutLauncherItem();
503 // Check if the given |web_contents| is in incognito mode.
504 bool IsIncognito(const content::WebContents* web_contents) const;
506 // Update browser shortcut's index.
507 void PersistChromeItemIndex(int index);
509 // Get browser shortcut's index from pref.
510 int GetChromeIconIndexFromPref() const;
512 // Depending on the provided flags, move either the chrome icon, the app icon
513 // or none to the given |target_index|. The provided |chrome_index| and
514 // |app_list_index| locations will get adjusted within this call to finalize
515 // the action and to make sure that the other item can still be moved
516 // afterwards (index adjustments).
517 void MoveChromeOrApplistToFinalPosition(
518 bool is_chrome,
519 bool is_app_list,
520 int target_index,
521 int* chrome_index,
522 int* app_list_index);
524 // Finds the index of where to insert the next item.
525 int FindInsertionPoint(bool is_app_list);
527 // Get the browser shortcut's index in the shelf using the current's systems
528 // configuration of pinned and known (but not running) apps.
529 int GetChromeIconIndexForCreation();
531 // Get the list of pinned programs from the preferences.
532 std::vector<std::string> GetListOfPinnedAppsAndBrowser();
534 // Close all windowed V1 applications of a certain extension which was already
535 // deleted.
536 void CloseWindowedAppsFromRemovedExtension(const std::string& app_id,
537 const Profile* profile);
539 // Set ShelfItemDelegate |item_delegate| for |id| and take an ownership.
540 // TODO(simon.hong81): Make this take a scoped_ptr of |item_delegate|.
541 void SetShelfItemDelegate(ash::ShelfID id,
542 ash::ShelfItemDelegate* item_delegate);
544 // Attach to a specific profile.
545 void AttachProfile(Profile* proifile);
547 // Forget the current profile to allow attaching to a new one.
548 void ReleaseProfile();
550 // Returns true if |app_id| is a Packaged App that has already launched on the
551 // native desktop and, if so, executes it as a desktop shortcut to activate
552 // desktop mode and send another OnLaunched event to the Extension.
553 bool LaunchedInNativeDesktop(const std::string& app_id);
555 static ChromeLauncherController* instance_;
557 ash::ShelfModel* model_;
559 ash::ShelfItemDelegateManager* item_delegate_manager_;
561 // Profile used for prefs and loading extensions. This is NOT necessarily the
562 // profile new windows are created with.
563 Profile* profile_;
565 // Controller items in this map are owned by |ShelfItemDelegateManager|.
566 IDToItemControllerMap id_to_item_controller_map_;
568 // Direct access to app_id for a web contents.
569 WebContentsToAppIDMap web_contents_to_app_id_;
571 // Used to track app windows.
572 scoped_ptr<AppWindowLauncherController> app_window_controller_;
574 // Used to get app info for tabs.
575 scoped_ptr<AppTabHelper> app_tab_helper_;
577 // Used to load the image for an app item.
578 scoped_ptr<extensions::AppIconLoader> app_icon_loader_;
580 PrefChangeRegistrar pref_change_registrar_;
582 AppSyncUIState* app_sync_ui_state_;
584 scoped_ptr<ExtensionEnableFlow> extension_enable_flow_;
586 // Shelves that are currently being observed.
587 std::set<ash::Shelf*> shelves_;
589 // The owned browser status monitor.
590 scoped_ptr<BrowserStatusMonitor> browser_status_monitor_;
592 #if defined(OS_CHROMEOS)
593 // A special observer class to detect user switches.
594 scoped_ptr<ChromeLauncherControllerUserSwitchObserver> user_switch_observer_;
595 #endif
597 // If true, incoming pinned state changes should be ignored.
598 bool ignore_persist_pinned_state_change_;
600 // The list of running & un-pinned applications for different users on hidden
601 // desktops.
602 typedef std::vector<std::string> RunningAppListIds;
603 typedef std::map<std::string, RunningAppListIds> RunningAppListIdMap;
604 RunningAppListIdMap last_used_running_application_order_;
606 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherController);
609 #endif // CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_