1 // Copyright (c) 2012 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_PER_BROWSER_H_
6 #define CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_PER_BROWSER_H_
12 #include "ash/display/display_controller.h"
13 #include "ash/launcher/launcher_delegate.h"
14 #include "ash/launcher/launcher_model_observer.h"
15 #include "ash/launcher/launcher_types.h"
16 #include "ash/shelf/shelf_layout_manager_observer.h"
17 #include "ash/shelf/shelf_types.h"
18 #include "ash/shell_observer.h"
19 #include "base/basictypes.h"
20 #include "base/compiler_specific.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/prefs/pref_change_registrar.h"
23 #include "chrome/browser/extensions/extension_prefs.h"
24 #include "chrome/browser/prefs/pref_service_syncable_observer.h"
25 #include "chrome/browser/ui/ash/app_sync_ui_state_observer.h"
26 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
27 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
28 #include "content/public/browser/notification_observer.h"
29 #include "content/public/browser/notification_registrar.h"
30 #include "ui/aura/window_observer.h"
34 class BrowserLauncherItemControllerTest
;
35 class ExtensionEnableFlow
;
36 class LauncherItemController
;
38 class ShellWindowLauncherController
;
56 // ChromeLauncherControllerPerBrowser manages the launcher items needed for
57 // content windows. Launcher items have a type, an optional app id, and a
58 // controller. This incarnation manages the items on a per browser base using
59 // browser proxies and application icons.
60 // * Tabbed browsers and browser app windows have BrowserLauncherItemController,
61 // owned by the BrowserView instance.
62 // * App shell windows have ShellWindowLauncherItemController, owned by
63 // ShellWindowLauncherController.
64 // * Shortcuts have no LauncherItemController.
65 class ChromeLauncherControllerPerBrowser
66 : public ash::LauncherModelObserver
,
67 public ash::ShellObserver
,
68 public ash::DisplayController::Observer
,
69 public ChromeLauncherController
,
70 public content::NotificationObserver
,
71 public PrefServiceSyncableObserver
,
72 public AppSyncUIStateObserver
,
73 public ExtensionEnableFlowDelegate
,
74 public ash::ShelfLayoutManagerObserver
{
76 ChromeLauncherControllerPerBrowser(Profile
* profile
,
77 ash::LauncherModel
* model
);
78 virtual ~ChromeLauncherControllerPerBrowser();
80 // ChromeLauncherController overrides:
82 // Initializes this ChromeLauncherControllerPerBrowser.
83 virtual void Init() OVERRIDE
;
85 // Returns the new per application interface of the given launcher. If it is
86 // a per browser (old) controller, it will return NULL;
87 // TODO(skuhne): Remove when we rip out the old launcher.
88 virtual ChromeLauncherControllerPerApp
* GetPerAppInterface() OVERRIDE
;
90 // Creates a new tabbed item on the launcher for |controller|.
91 virtual ash::LauncherID
CreateTabbedLauncherItem(
92 LauncherItemController
* controller
,
93 IncognitoState is_incognito
,
94 ash::LauncherItemStatus status
) OVERRIDE
;
96 // Creates a new app item on the launcher for |controller|.
97 virtual ash::LauncherID
CreateAppLauncherItem(
98 LauncherItemController
* controller
,
99 const std::string
& app_id
,
100 ash::LauncherItemStatus status
) OVERRIDE
;
102 // Updates the running status of an item.
103 virtual void SetItemStatus(ash::LauncherID id
,
104 ash::LauncherItemStatus status
) OVERRIDE
;
106 // Updates the controller associated with id (which should be a shortcut).
107 // |controller| remains owned by caller.
108 virtual void SetItemController(ash::LauncherID id
,
109 LauncherItemController
* controller
) OVERRIDE
;
111 // Closes or unpins the launcher item.
112 virtual void CloseLauncherItem(ash::LauncherID id
) OVERRIDE
;
114 // Pins the specified id. Currently only supports platform apps.
115 virtual void Pin(ash::LauncherID id
) OVERRIDE
;
117 // Unpins the specified id, closing if not running.
118 virtual void Unpin(ash::LauncherID id
) OVERRIDE
;
120 // Returns true if the item identified by |id| is pinned.
121 virtual bool IsPinned(ash::LauncherID id
) OVERRIDE
;
123 // Pins/unpins the specified id.
124 virtual void TogglePinned(ash::LauncherID id
) OVERRIDE
;
126 // Returns true if the specified item can be pinned or unpinned. Only apps can
128 virtual bool IsPinnable(ash::LauncherID id
) const OVERRIDE
;
130 // If there is no launcher item in the launcher for application |app_id|, one
131 // gets created. The (existing or created) launcher items get then locked
132 // against a users un-pinning removal.
133 virtual void LockV1AppWithID(const std::string
& app_id
) OVERRIDE
;
135 // A previously locked launcher item of type |app_id| gets unlocked. If the
136 // lock count reaches 0 and the item is not pinned it will go away.
137 virtual void UnlockV1AppWithID(const std::string
& app_id
) OVERRIDE
;
139 // Requests that the launcher item controller specified by |id| open a new
140 // instance of the app. |event_flags| holds the flags of the event which
141 // triggered this command.
142 virtual void Launch(ash::LauncherID id
, int event_flags
) OVERRIDE
;
144 // Closes the specified item.
145 virtual void Close(ash::LauncherID id
) OVERRIDE
;
147 // Returns true if the specified item is open.
148 virtual bool IsOpen(ash::LauncherID id
) OVERRIDE
;
150 // Returns true if the specified item is for a platform app.
151 virtual bool IsPlatformApp(ash::LauncherID id
) OVERRIDE
;
153 // Opens a new instance of the application identified by |app_id|.
154 // Used by the app-list, and by pinned-app launcher items.
155 virtual void LaunchApp(const std::string
& app_id
, int event_flags
) OVERRIDE
;
157 // If |app_id| is running, reactivates the app's most recently active window,
158 // otherwise launches and activates the app.
159 // Used by the app-list, and by pinned-app launcher items.
160 virtual void ActivateApp(const std::string
& app_id
, int event_flags
) OVERRIDE
;
162 // Returns the launch type of app for the specified id.
163 virtual extensions::ExtensionPrefs::LaunchType
GetLaunchType(
164 ash::LauncherID id
) OVERRIDE
;
166 // Returns the id of the app for the specified tab.
167 virtual std::string
GetAppID(content::WebContents
* tab
) OVERRIDE
;
169 virtual ash::LauncherID
GetLauncherIDForAppID(
170 const std::string
& app_id
) OVERRIDE
;
171 virtual std::string
GetAppIDForLauncherID(ash::LauncherID id
) OVERRIDE
;
173 // Set the image for a specific launcher item (e.g. when set by the app).
174 virtual void SetLauncherItemImage(ash::LauncherID launcher_id
,
175 const gfx::ImageSkia
& image
) OVERRIDE
;
177 // Returns true if a pinned launcher item with given |app_id| could be found.
178 virtual bool IsAppPinned(const std::string
& app_id
) OVERRIDE
;
180 // Pins an app with |app_id| to launcher. If there is a running instance in
181 // launcher, the running instance is pinned. If there is no running instance,
182 // a new launcher item is created and pinned.
183 virtual void PinAppWithID(const std::string
& app_id
) OVERRIDE
;
185 // Updates the launche type of the app for the specified id to |launch_type|.
186 virtual void SetLaunchType(
188 extensions::ExtensionPrefs::LaunchType launch_type
) OVERRIDE
;
190 // Unpins any app items whose id is |app_id|.
191 virtual void UnpinAppsWithID(const std::string
& app_id
) OVERRIDE
;
193 // Returns true if the user is currently logged in as a guest.
194 virtual bool IsLoggedInAsGuest() OVERRIDE
;
196 // Invoked when user clicks on button in the launcher and there is no last
197 // used window (or CTRL is held with the click).
198 virtual void CreateNewWindow() OVERRIDE
;
200 // Invoked when the user clicks on button in the launcher to create a new
202 virtual void CreateNewIncognitoWindow() OVERRIDE
;
204 // Checks whether the user is allowed to pin apps. Pinning may be disallowed
205 // by policy in case there is a pre-defined set of pinned apps.
206 virtual bool CanPin() const OVERRIDE
;
208 // Updates the pinned pref state. The pinned state consists of a list pref.
209 // Each item of the list is a dictionary. The key |kAppIDPath| gives the
211 virtual void PersistPinnedState() OVERRIDE
;
213 virtual ash::LauncherModel
* model() OVERRIDE
;
215 virtual Profile
* profile() OVERRIDE
;
217 // Gets the shelf auto-hide behavior on |root_window|.
218 virtual ash::ShelfAutoHideBehavior
GetShelfAutoHideBehavior(
219 aura::RootWindow
* root_window
) const OVERRIDE
;
221 // Returns |true| if the user is allowed to modify the shelf auto-hide
222 // behavior on |root_window|.
223 virtual bool CanUserModifyShelfAutoHideBehavior(
224 aura::RootWindow
* root_window
) const OVERRIDE
;
226 // Toggles the shelf auto-hide behavior on |root_window|. Does nothing if the
227 // user is not allowed to modify the auto-hide behavior.
228 virtual void ToggleShelfAutoHideBehavior(
229 aura::RootWindow
* root_window
) OVERRIDE
;
231 // The tab no longer represents its previously identified application.
232 virtual void RemoveTabFromRunningApp(content::WebContents
* tab
,
233 const std::string
& app_id
) OVERRIDE
;
235 // Notify the controller that the state of an non platform app's tabs
237 virtual void UpdateAppState(content::WebContents
* contents
,
238 AppState app_state
) OVERRIDE
;
240 // Limits application refocusing to urls that match |url| for |id|.
241 virtual void SetRefocusURLPatternForTest(ash::LauncherID id
,
242 const GURL
& url
) OVERRIDE
;
244 // Returns the extension identified by |app_id|.
245 virtual const extensions::Extension
* GetExtensionForAppID(
246 const std::string
& app_id
) const OVERRIDE
;
248 // Activates a |window|. If |allow_minimize| is true and the system allows
249 // it, the the window will get minimized instead.
250 virtual void ActivateWindowOrMinimizeIfActive(ui::BaseWindow
* window
,
251 bool allow_minimize
) OVERRIDE
;
253 // ash::LauncherDelegate overrides:
254 virtual void ItemSelected(const ash::LauncherItem
& item
,
255 const ui::Event
& event
) OVERRIDE
;
256 virtual string16
GetTitle(const ash::LauncherItem
& item
) OVERRIDE
;
257 virtual ui::MenuModel
* CreateContextMenu(
258 const ash::LauncherItem
& item
, aura::RootWindow
* root
) OVERRIDE
;
259 virtual ash::LauncherMenuModel
* CreateApplicationMenu(
260 const ash::LauncherItem
& item
,
261 int event_flags
) OVERRIDE
;
262 virtual ash::LauncherID
GetIDByWindow(aura::Window
* window
) OVERRIDE
;
263 virtual bool IsDraggable(const ash::LauncherItem
& item
) OVERRIDE
;
264 virtual bool ShouldShowTooltip(const ash::LauncherItem
& item
) OVERRIDE
;
265 virtual void OnLauncherCreated(ash::Launcher
* launcher
) OVERRIDE
;
266 virtual void OnLauncherDestroyed(ash::Launcher
* launcher
) OVERRIDE
;
268 // ash::LauncherModelObserver overrides:
269 virtual void LauncherItemAdded(int index
) OVERRIDE
;
270 virtual void LauncherItemRemoved(int index
, ash::LauncherID id
) OVERRIDE
;
271 virtual void LauncherItemMoved(int start_index
, int target_index
) OVERRIDE
;
272 virtual void LauncherItemChanged(int index
,
273 const ash::LauncherItem
& old_item
) OVERRIDE
;
274 virtual void LauncherStatusChanged() OVERRIDE
;
276 // content::NotificationObserver overrides:
277 virtual void Observe(int type
,
278 const content::NotificationSource
& source
,
279 const content::NotificationDetails
& details
) OVERRIDE
;
281 // ash::ShellObserver overrides:
282 virtual void OnShelfAlignmentChanged(aura::RootWindow
* root_window
) OVERRIDE
;
284 // ash::DisplayController::Observer overrides:
285 virtual void OnDisplayConfigurationChanging() OVERRIDE
;
286 virtual void OnDisplayConfigurationChanged() OVERRIDE
;
288 // PrefServiceSyncableObserver overrides:
289 virtual void OnIsSyncingChanged() OVERRIDE
;
291 // AppSyncUIStateObserver overrides:
292 virtual void OnAppSyncUIStatusChanged() OVERRIDE
;
294 // ExtensionEnableFlowDelegate overrides:
295 virtual void ExtensionEnableFlowFinished() OVERRIDE
;
296 virtual void ExtensionEnableFlowAborted(bool user_initiated
) OVERRIDE
;
298 // extensions::AppIconLoader overrides:
299 virtual void SetAppImage(const std::string
& app_id
,
300 const gfx::ImageSkia
& image
) OVERRIDE
;
302 // ash::ShelfLayoutManagerObserver overrides:
303 virtual void OnAutoHideBehaviorChanged(
304 ash::ShelfAutoHideBehavior new_behavior
) OVERRIDE
;
307 // ChromeLauncherController overrides:
309 // Creates a new app shortcut item and controller on the launcher at |index|.
310 // Use kInsertItemAtEnd to add a shortcut as the last item.
311 virtual ash::LauncherID
CreateAppShortcutLauncherItem(
312 const std::string
& app_id
,
315 // Sets the AppTabHelper/AppIconLoader, taking ownership of the helper class.
316 // These are intended for testing.
317 virtual void SetAppTabHelperForTest(AppTabHelper
* helper
) OVERRIDE
;
318 virtual void SetAppIconLoaderForTest(
319 extensions::AppIconLoader
* loader
) OVERRIDE
;
320 virtual const std::string
& GetAppIdFromLauncherIdForTest(
321 ash::LauncherID id
) OVERRIDE
;
324 friend class ChromeLauncherControllerPerBrowserTest
;
326 typedef std::map
<ash::LauncherID
, LauncherItemController
*>
327 IDToItemControllerMap
;
328 typedef std::list
<content::WebContents
*> WebContentsList
;
329 typedef std::map
<std::string
, WebContentsList
> AppIDToWebContentsListMap
;
330 typedef std::map
<content::WebContents
*, std::string
> WebContentsToAppIDMap
;
332 // Returns the profile used for new windows.
333 Profile
* GetProfileForNewWindows();
335 // Invoked when the associated browser or app is closed.
336 void LauncherItemClosed(ash::LauncherID id
);
338 // Internal helpers for pinning and unpinning that handle both
339 // client-triggered and internal pinning operations.
340 void DoPinAppWithID(const std::string
& app_id
);
341 void DoUnpinAppsWithID(const std::string
& app_id
);
343 // Re-syncs launcher model with prefs::kPinnedLauncherApps.
344 void UpdateAppLaunchersFromPref();
346 // Persists the shelf auto-hide behavior to prefs.
347 void SetShelfAutoHideBehaviorPrefs(ash::ShelfAutoHideBehavior behavior
,
348 aura::RootWindow
* root_window
);
350 // Sets the shelf auto-hide behavior from prefs.
351 void SetShelfAutoHideBehaviorFromPrefs();
353 // Sets the shelf alignment from prefs.
354 void SetShelfAlignmentFromPrefs();
356 // Sets both of auto-hide behavior and alignment from prefs.
357 void SetShelfBehaviorsFromPrefs();
359 // Returns the most recently active WebContents for an app.
360 content::WebContents
* GetLastActiveWebContents(const std::string
& app_id
);
362 // Creates an app launcher to insert at |index|. Note that |index| may be
363 // adjusted by the model to meet ordering constraints.
364 ash::LauncherID
InsertAppLauncherItem(
365 LauncherItemController
* controller
,
366 const std::string
& app_id
,
367 ash::LauncherItemStatus status
,
370 bool HasItemController(ash::LauncherID id
) const;
372 // Create LauncherItem for Browser Shortcut.
373 ash::LauncherID
CreateBrowserShortcutLauncherItem();
375 // Update browser shortcut's index.
376 void SetChromeIconIndexToPref(int index
);
378 // Get browser shortcut's index from pref.
379 int GetChromeIconIndexFromPref() const;
381 // Invoked when browser shortcut is clicked.
382 void BrowserShortcutClicked(int event_flags
);
384 ash::LauncherModel
* model_
;
386 // Profile used for prefs and loading extensions. This is NOT necessarily the
387 // profile new windows are created with.
390 IDToItemControllerMap id_to_item_controller_map_
;
392 // Maintains activation order of web contents for each app.
393 AppIDToWebContentsListMap app_id_to_web_contents_list_
;
395 // Direct access to app_id for a web contents.
396 WebContentsToAppIDMap web_contents_to_app_id_
;
398 // Used to track shell windows.
399 scoped_ptr
<ShellWindowLauncherController
> shell_window_controller_
;
401 // Used to get app info for tabs.
402 scoped_ptr
<AppTabHelper
> app_tab_helper_
;
404 // Used to load the image for an app item.
405 scoped_ptr
<extensions::AppIconLoader
> app_icon_loader_
;
407 content::NotificationRegistrar notification_registrar_
;
409 PrefChangeRegistrar pref_change_registrar_
;
411 AppSyncUIState
* app_sync_ui_state_
;
413 scoped_ptr
<ExtensionEnableFlow
> extension_enable_flow_
;
415 // Launchers that are currently being observed.
416 std::set
<ash::Launcher
*> launchers_
;
418 DISALLOW_COPY_AND_ASSIGN(ChromeLauncherControllerPerBrowser
);
421 #endif // CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_PER_BROWSER_H_