Home | History | Annotate | Download | only in launcher
      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.
      4 
      5 #ifndef CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
      6 #define CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
      7 
      8 #include <string>
      9 
     10 #include "ash/launcher/launcher_delegate.h"
     11 #include "ash/launcher/launcher_types.h"
     12 #include "ash/shelf/shelf_types.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "chrome/browser/extensions/app_icon_loader.h"
     15 #include "chrome/browser/extensions/extension_prefs.h"
     16 
     17 class LauncherItemControllerPerAppTest;
     18 class LauncherItemController;
     19 class Profile;
     20 class ChromeLauncherAppMenuItem;
     21 class ChromeLauncherControllerPerApp;
     22 
     23 namespace ash {
     24 class LauncherModel;
     25 }
     26 
     27 namespace aura {
     28 class Window;
     29 class RootWindow;
     30 }
     31 
     32 namespace content {
     33 class WebContents;
     34 }
     35 
     36 namespace ui {
     37 class BaseWindow;
     38 }
     39 
     40 // A list of the elements which makes up a simple menu description.
     41 typedef ScopedVector<ChromeLauncherAppMenuItem> ChromeLauncherAppMenuItems;
     42 
     43 // ChromeLauncherController manages the launcher items needed for content
     44 // windows. Launcher items have a type, an optional app id, and a controller.
     45 // ChromeLauncherController will furthermore create the particular
     46 // implementation of interest - either sorting by application (new) or sorting
     47 // by browser (old).
     48 // * Tabbed browsers and browser app windows have BrowserLauncherItemController,
     49 //   owned by the BrowserView instance.
     50 // * App shell windows have ShellWindowLauncherItemController, owned by
     51 //   ShellWindowLauncherController.
     52 // * Shortcuts have no LauncherItemController.
     53 class ChromeLauncherController
     54     : public ash::LauncherDelegate,
     55       public extensions::AppIconLoader::Delegate {
     56  public:
     57   // Indicates if a launcher item is incognito or not.
     58   enum IncognitoState {
     59     STATE_INCOGNITO,
     60     STATE_NOT_INCOGNITO,
     61   };
     62 
     63   // Used to update the state of non plaform apps, as web contents change.
     64   enum AppState {
     65     APP_STATE_ACTIVE,
     66     APP_STATE_WINDOW_ACTIVE,
     67     APP_STATE_INACTIVE,
     68     APP_STATE_REMOVED
     69   };
     70 
     71   // Mockable interface to get app ids from tabs.
     72   class AppTabHelper {
     73    public:
     74     virtual ~AppTabHelper() {}
     75 
     76     // Returns the app id of the specified tab, or an empty string if there is
     77     // no app.
     78     virtual std::string GetAppID(content::WebContents* tab) = 0;
     79 
     80     // Returns true if |id| is valid. Used during restore to ignore no longer
     81     // valid extensions.
     82     virtual bool IsValidID(const std::string& id) = 0;
     83   };
     84 
     85   ChromeLauncherController() {}
     86   virtual ~ChromeLauncherController();
     87 
     88   // Initializes this ChromeLauncherController.
     89   virtual void Init() = 0;
     90 
     91   // Returns the new per application interface of the given launcher. If it is
     92   // a per browser (old) controller, it will return NULL;
     93   // TODO(skuhne): Remove when we rip out the old launcher.
     94   virtual ChromeLauncherControllerPerApp* GetPerAppInterface() = 0;
     95 
     96   // Creates an instance.
     97   static ChromeLauncherController* CreateInstance(Profile* profile,
     98                                                   ash::LauncherModel* model);
     99 
    100   // Returns the single ChromeLauncherController instance.
    101   static ChromeLauncherController* instance() { return instance_; }
    102 
    103   // Creates a new tabbed item on the launcher for |controller|.
    104   virtual ash::LauncherID CreateTabbedLauncherItem(
    105       LauncherItemController* controller,
    106       IncognitoState is_incognito,
    107       ash::LauncherItemStatus status) = 0;
    108 
    109   // Creates a new app item on the launcher for |controller|.
    110   virtual ash::LauncherID CreateAppLauncherItem(
    111       LauncherItemController* controller,
    112       const std::string& app_id,
    113       ash::LauncherItemStatus status) = 0;
    114 
    115   // Updates the running status of an item.
    116   virtual void SetItemStatus(ash::LauncherID id,
    117                              ash::LauncherItemStatus status) = 0;
    118 
    119   // Updates the controller associated with id (which should be a shortcut).
    120   // |controller| remains owned by caller.
    121   virtual void SetItemController(ash::LauncherID id,
    122                                  LauncherItemController* controller) = 0;
    123 
    124   // Closes or unpins the launcher item.
    125   virtual void CloseLauncherItem(ash::LauncherID id) = 0;
    126 
    127   // Pins the specified id. Currently only supports platform apps.
    128   virtual void Pin(ash::LauncherID id) = 0;
    129 
    130   // Unpins the specified id, closing if not running.
    131   virtual void Unpin(ash::LauncherID id) = 0;
    132 
    133   // Returns true if the item identified by |id| is pinned.
    134   virtual bool IsPinned(ash::LauncherID id) = 0;
    135 
    136   // Pins/unpins the specified id.
    137   virtual void TogglePinned(ash::LauncherID id) = 0;
    138 
    139   // Returns true if the specified item can be pinned or unpinned. Only apps can
    140   // be pinned.
    141   virtual bool IsPinnable(ash::LauncherID id) const = 0;
    142 
    143   // If there is no launcher item in the launcher for application |app_id|, one
    144   // gets created. The (existing or created) launcher items get then locked
    145   // against a users un-pinning removal.
    146   virtual void LockV1AppWithID(const std::string& app_id) = 0;
    147 
    148   // A previously locked launcher item of type |app_id| gets unlocked. If the
    149   // lock count reaches 0 and the item is not pinned it will go away.
    150   virtual void UnlockV1AppWithID(const std::string& app_id) = 0;
    151 
    152   // Requests that the launcher item controller specified by |id| open a new
    153   // instance of the app.  |event_flags| holds the flags of the event which
    154   // triggered this command.
    155   virtual void Launch(ash::LauncherID id, int event_flags) = 0;
    156 
    157   // Closes the specified item.
    158   virtual void Close(ash::LauncherID id) = 0;
    159 
    160   // Returns true if the specified item is open.
    161   virtual bool IsOpen(ash::LauncherID id) = 0;
    162 
    163   // Returns true if the specified item is for a platform app.
    164   virtual bool IsPlatformApp(ash::LauncherID id) = 0;
    165 
    166   // Opens a new instance of the application identified by |app_id|.
    167   // Used by the app-list, and by pinned-app launcher items.
    168   virtual void LaunchApp(const std::string& app_id, int event_flags) = 0;
    169 
    170   // If |app_id| is running, reactivates the app's most recently active window,
    171   // otherwise launches and activates the app.
    172   // Used by the app-list, and by pinned-app launcher items.
    173   virtual void ActivateApp(const std::string& app_id, int event_flags) = 0;
    174 
    175   // Returns the launch type of app for the specified id.
    176   virtual extensions::ExtensionPrefs::LaunchType GetLaunchType(
    177       ash::LauncherID id) = 0;
    178 
    179   // Returns the id of the app for the specified tab.
    180   virtual std::string GetAppID(content::WebContents* tab) = 0;
    181 
    182   // Returns the launcherID of the first non-panel item whose app_id
    183   // matches |app_id| or 0 if none match.
    184   virtual ash::LauncherID GetLauncherIDForAppID(const std::string& app_id) = 0;
    185 
    186   // Returns the id of the app for the specified id (which must exist).
    187   virtual std::string GetAppIDForLauncherID(ash::LauncherID id) = 0;
    188 
    189   // Set the image for a specific launcher item (e.g. when set by the app).
    190   virtual void SetLauncherItemImage(ash::LauncherID launcher_id,
    191                                     const gfx::ImageSkia& image) = 0;
    192 
    193   // Returns true if a pinned launcher item with given |app_id| could be found.
    194   virtual bool IsAppPinned(const std::string& app_id) = 0;
    195 
    196   // Pins an app with |app_id| to launcher. If there is a running instance in
    197   // launcher, the running instance is pinned. If there is no running instance,
    198   // a new launcher item is created and pinned.
    199   virtual void PinAppWithID(const std::string& app_id) = 0;
    200 
    201   // Updates the launche type of the app for the specified id to |launch_type|.
    202   virtual void SetLaunchType(
    203       ash::LauncherID id,
    204       extensions::ExtensionPrefs::LaunchType launch_type) = 0;
    205 
    206   // Unpins any app items whose id is |app_id|.
    207   virtual void UnpinAppsWithID(const std::string& app_id) = 0;
    208 
    209   // Returns true if the user is currently logged in as a guest.
    210   virtual bool IsLoggedInAsGuest() = 0;
    211 
    212   // Invoked when user clicks on button in the launcher and there is no last
    213   // used window (or CTRL is held with the click).
    214   virtual void CreateNewWindow() = 0;
    215 
    216   // Invoked when the user clicks on button in the launcher to create a new
    217   // incognito window.
    218   virtual void CreateNewIncognitoWindow() = 0;
    219 
    220   // Checks whether the user is allowed to pin apps. Pinning may be disallowed
    221   // by policy in case there is a pre-defined set of pinned apps.
    222   virtual bool CanPin() const = 0;
    223 
    224   // Updates the pinned pref state. The pinned state consists of a list pref.
    225   // Each item of the list is a dictionary. The key |kAppIDPath| gives the
    226   // id of the app.
    227   virtual void PersistPinnedState() = 0;
    228 
    229   virtual ash::LauncherModel* model() = 0;
    230 
    231   virtual Profile* profile() = 0;
    232 
    233   // Gets the shelf auto-hide behavior on |root_window|.
    234   virtual ash::ShelfAutoHideBehavior GetShelfAutoHideBehavior(
    235       aura::RootWindow* root_window) const = 0;
    236 
    237   // Returns |true| if the user is allowed to modify the shelf auto-hide
    238   // behavior on |root_window|.
    239   virtual bool CanUserModifyShelfAutoHideBehavior(
    240       aura::RootWindow* root_window) const = 0;
    241 
    242   // Toggles the shelf auto-hide behavior on |root_window|. Does nothing if the
    243   // user is not allowed to modify the auto-hide behavior.
    244   virtual void ToggleShelfAutoHideBehavior(aura::RootWindow* root_window) = 0;
    245 
    246   // The tab no longer represents its previously identified application.
    247   virtual void RemoveTabFromRunningApp(content::WebContents* tab,
    248                                        const std::string& app_id) = 0;
    249 
    250   // Notify the controller that the state of an non platform app's tabs
    251   // have changed,
    252   virtual void UpdateAppState(content::WebContents* contents,
    253                               AppState app_state) = 0;
    254 
    255   // Limits application refocusing to urls that match |url| for |id|.
    256   virtual void SetRefocusURLPatternForTest(ash::LauncherID id,
    257                                            const GURL& url) = 0;
    258 
    259   // Returns the extension identified by |app_id|.
    260   virtual const extensions::Extension* GetExtensionForAppID(
    261       const std::string& app_id) const = 0;
    262 
    263   // Activates a |window|. If |allow_minimize| is true and the system allows
    264   // it, the the window will get minimized instead.
    265   virtual void ActivateWindowOrMinimizeIfActive(ui::BaseWindow* window,
    266                                                 bool allow_minimize) = 0;
    267   // ash::LauncherDelegate overrides:
    268   virtual void ItemSelected(const ash::LauncherItem& item,
    269                            const ui::Event& event) OVERRIDE = 0;
    270   virtual string16 GetTitle(const ash::LauncherItem& item) OVERRIDE = 0;
    271   virtual ui::MenuModel* CreateContextMenu(
    272       const ash::LauncherItem& item, aura::RootWindow* root) OVERRIDE = 0;
    273   virtual ash::LauncherMenuModel* CreateApplicationMenu(
    274       const ash::LauncherItem& item,
    275       int event_flags) OVERRIDE = 0;
    276   virtual ash::LauncherID GetIDByWindow(aura::Window* window) OVERRIDE = 0;
    277   virtual bool IsDraggable(const ash::LauncherItem& item) OVERRIDE = 0;
    278   virtual bool ShouldShowTooltip(const ash::LauncherItem& item) OVERRIDE = 0;
    279   virtual bool IsPerAppLauncher() OVERRIDE;
    280 
    281   // extensions::AppIconLoader overrides:
    282   virtual void SetAppImage(const std::string& app_id,
    283                            const gfx::ImageSkia& image) OVERRIDE = 0;
    284 
    285  protected:
    286   friend class LauncherItemControllerPerAppTest;
    287   friend class LauncherPlatformAppBrowserTest;
    288   friend class LauncherAppBrowserTest;
    289   // TODO(skuhne): Remove these when the old launcher get removed.
    290   friend class LauncherPlatformPerAppAppBrowserTest;
    291   friend class LauncherPerAppAppBrowserTest;
    292 
    293   // Creates a new app shortcut item and controller on the launcher at |index|.
    294   // Use kInsertItemAtEnd to add a shortcut as the last item.
    295   virtual ash::LauncherID CreateAppShortcutLauncherItem(
    296       const std::string& app_id,
    297       int index) = 0;
    298 
    299   // Sets the AppTabHelper/AppIconLoader, taking ownership of the helper class.
    300   // These are intended for testing.
    301   virtual void SetAppTabHelperForTest(AppTabHelper* helper) = 0;
    302   virtual void SetAppIconLoaderForTest(extensions::AppIconLoader* loader) = 0;
    303   virtual const std::string& GetAppIdFromLauncherIdForTest(
    304       ash::LauncherID id) = 0;
    305 
    306  private:
    307   static ChromeLauncherController* instance_;
    308 };
    309 
    310 #endif  // CHROME_BROWSER_UI_ASH_LAUNCHER_CHROME_LAUNCHER_CONTROLLER_H_
    311