Home | History | Annotate | Download | only in apps
      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.
      4 
      5 #ifndef APPS_SHELL_WINDOW_REGISTRY_H_
      6 #define APPS_SHELL_WINDOW_REGISTRY_H_
      7 
      8 #include <list>
      9 
     10 #include "base/callback.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/singleton.h"
     13 #include "base/observer_list.h"
     14 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     15 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
     16 #include "ui/gfx/native_widget_types.h"
     17 
     18 class Profile;
     19 
     20 namespace content {
     21 class DevToolsAgentHost;
     22 class RenderViewHost;
     23 }
     24 
     25 namespace apps {
     26 
     27 class ShellWindow;
     28 
     29 // The ShellWindowRegistry tracks the ShellWindows for all platform apps for a
     30 // particular profile.
     31 // This class is planned to evolve into tracking all PlatformApps for a
     32 // particular profile, with a PlatformApp encapsulating all views (background
     33 // page, shell windows, tray view, panels etc.) and other app level behaviour
     34 // (e.g. notifications the app is interested in, lifetime of the background
     35 // page).
     36 class ShellWindowRegistry : public BrowserContextKeyedService {
     37  public:
     38   class Observer {
     39    public:
     40     // Called just after a shell window was added.
     41     virtual void OnShellWindowAdded(apps::ShellWindow* shell_window) = 0;
     42     // Called when the window icon changes.
     43     virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window) = 0;
     44     // Called just after a shell window was removed.
     45     virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) = 0;
     46 
     47    protected:
     48     virtual ~Observer() {}
     49   };
     50 
     51   typedef std::list<apps::ShellWindow*> ShellWindowList;
     52   typedef ShellWindowList::const_iterator const_iterator;
     53   typedef std::set<std::string> InspectedWindowSet;
     54 
     55   explicit ShellWindowRegistry(Profile* profile);
     56   virtual ~ShellWindowRegistry();
     57 
     58   // Returns the instance for the given profile, or NULL if none. This is
     59   // a convenience wrapper around ShellWindowRegistry::Factory::GetForProfile.
     60   static ShellWindowRegistry* Get(Profile* profile);
     61 
     62   void AddShellWindow(apps::ShellWindow* shell_window);
     63   void ShellWindowIconChanged(apps::ShellWindow* shell_window);
     64   // Called by |shell_window| when it is activated.
     65   void ShellWindowActivated(apps::ShellWindow* shell_window);
     66   void RemoveShellWindow(apps::ShellWindow* shell_window);
     67 
     68   void AddObserver(Observer* observer);
     69   void RemoveObserver(Observer* observer);
     70 
     71   // Returns a set of windows owned by the application identified by app_id.
     72   ShellWindowList GetShellWindowsForApp(const std::string& app_id) const;
     73   const ShellWindowList& shell_windows() const { return shell_windows_; }
     74 
     75   // Close all shell windows associated with an app.
     76   void CloseAllShellWindowsForApp(const std::string& app_id);
     77 
     78   // Helper functions to find shell windows with particular attributes.
     79   apps::ShellWindow* GetShellWindowForRenderViewHost(
     80       content::RenderViewHost* render_view_host) const;
     81   apps::ShellWindow* GetShellWindowForNativeWindow(
     82       gfx::NativeWindow window) const;
     83   // Returns an app window for the given app, or NULL if no shell windows are
     84   // open. If there is a window for the given app that is active, that one will
     85   // be returned, otherwise an arbitrary window will be returned.
     86   apps::ShellWindow* GetCurrentShellWindowForApp(
     87       const std::string& app_id) const;
     88   // Returns an app window for the given app and window key, or NULL if no shell
     89   // window with the key are open. If there is a window for the given app and
     90   // key that is active, that one will be returned, otherwise an arbitrary
     91   // window will be returned.
     92   apps::ShellWindow* GetShellWindowForAppAndKey(
     93       const std::string& app_id,
     94       const std::string& window_key) const;
     95 
     96   // Returns whether a ShellWindow's ID was last known to have a DevToolsAgent
     97   // attached to it, which should be restored during a reload of a corresponding
     98   // newly created |render_view_host|.
     99   bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
    100 
    101   // Returns the shell window for |window|, looking in all profiles.
    102   static apps::ShellWindow* GetShellWindowForNativeWindowAnyProfile(
    103       gfx::NativeWindow window);
    104 
    105   // Returns true if the number of shell windows registered across all profiles
    106   // is non-zero. |window_type_mask| is a bitwise OR filter of
    107   // ShellWindow::WindowType, or 0 for any window type.
    108   static bool IsShellWindowRegisteredInAnyProfile(int window_type_mask);
    109 
    110   class Factory : public BrowserContextKeyedServiceFactory {
    111    public:
    112     static ShellWindowRegistry* GetForProfile(Profile* profile, bool create);
    113 
    114     static Factory* GetInstance();
    115    private:
    116     friend struct DefaultSingletonTraits<Factory>;
    117 
    118     Factory();
    119     virtual ~Factory();
    120 
    121     // BrowserContextKeyedServiceFactory
    122     virtual BrowserContextKeyedService* BuildServiceInstanceFor(
    123         content::BrowserContext* profile) const OVERRIDE;
    124     virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
    125     virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
    126     virtual content::BrowserContext* GetBrowserContextToUse(
    127         content::BrowserContext* context) const OVERRIDE;
    128   };
    129 
    130  protected:
    131   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
    132 
    133  private:
    134   // Ensures the specified |shell_window| is included in |shell_windows_|.
    135   // Otherwise adds |shell_window| to the back of |shell_windows_|.
    136   void AddShellWindowToList(apps::ShellWindow* shell_window);
    137 
    138   // Bring |shell_window| to the front of |shell_windows_|. If it is not in the
    139   // list, add it first.
    140   void BringToFront(apps::ShellWindow* shell_window);
    141 
    142   Profile* profile_;
    143   ShellWindowList shell_windows_;
    144   InspectedWindowSet inspected_windows_;
    145   ObserverList<Observer> observers_;
    146   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
    147 };
    148 
    149 }  // namespace extensions
    150 
    151 #endif  // APPS_SHELL_WINDOW_REGISTRY_H_
    152