Home | History | Annotate | Download | only in app_window
      1 // Copyright 2014 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 EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_
      6 #define EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_
      7 
      8 #include <list>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "base/callback.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/singleton.h"
     15 #include "base/observer_list.h"
     16 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
     17 #include "components/keyed_service/core/keyed_service.h"
     18 #include "ui/gfx/native_widget_types.h"
     19 
     20 namespace content {
     21 class BrowserContext;
     22 class DevToolsAgentHost;
     23 class RenderViewHost;
     24 }
     25 
     26 namespace extensions {
     27 
     28 class AppWindow;
     29 
     30 // The AppWindowRegistry tracks the AppWindows for all platform apps for a
     31 // particular browser context.
     32 class AppWindowRegistry : public KeyedService {
     33  public:
     34   class Observer {
     35    public:
     36     // Called just after a app window was added.
     37     virtual void OnAppWindowAdded(AppWindow* app_window);
     38     // Called when the window icon changes.
     39     virtual void OnAppWindowIconChanged(AppWindow* app_window);
     40     // Called just after a app window was removed.
     41     virtual void OnAppWindowRemoved(AppWindow* app_window);
     42     // Called just after a app window was hidden. This is different from
     43     // window visibility as a minimize does not hide a window, but does make
     44     // it not visible.
     45     virtual void OnAppWindowHidden(AppWindow* app_window);
     46     // Called just after a app window was shown.
     47     virtual void OnAppWindowShown(AppWindow* app_window);
     48 
     49    protected:
     50     virtual ~Observer();
     51   };
     52 
     53   typedef std::list<AppWindow*> AppWindowList;
     54   typedef AppWindowList::const_iterator const_iterator;
     55   typedef std::set<std::string> InspectedWindowSet;
     56 
     57   explicit AppWindowRegistry(content::BrowserContext* context);
     58   virtual ~AppWindowRegistry();
     59 
     60   // Returns the instance for the given browser context, or NULL if none. This
     61   // is a convenience wrapper around
     62   // AppWindowRegistry::Factory::GetForBrowserContext().
     63   static AppWindowRegistry* Get(content::BrowserContext* context);
     64 
     65   void AddAppWindow(AppWindow* app_window);
     66   void AppWindowIconChanged(AppWindow* app_window);
     67   // Called by |app_window| when it is activated.
     68   void AppWindowActivated(AppWindow* app_window);
     69   void AppWindowHidden(AppWindow* app_window);
     70   void AppWindowShown(AppWindow* app_window);
     71   void RemoveAppWindow(AppWindow* app_window);
     72 
     73   void AddObserver(Observer* observer);
     74   void RemoveObserver(Observer* observer);
     75 
     76   // Returns a set of windows owned by the application identified by app_id.
     77   AppWindowList GetAppWindowsForApp(const std::string& app_id) const;
     78   const AppWindowList& app_windows() const { return app_windows_; }
     79 
     80   // Close all app windows associated with an app.
     81   void CloseAllAppWindowsForApp(const std::string& app_id);
     82 
     83   // Helper functions to find app windows with particular attributes.
     84   AppWindow* GetAppWindowForRenderViewHost(
     85       content::RenderViewHost* render_view_host) const;
     86   AppWindow* GetAppWindowForNativeWindow(gfx::NativeWindow window) const;
     87   // Returns an app window for the given app, or NULL if no app windows are
     88   // open. If there is a window for the given app that is active, that one will
     89   // be returned, otherwise an arbitrary window will be returned.
     90   AppWindow* GetCurrentAppWindowForApp(const std::string& app_id) const;
     91   // Returns an app window for the given app and window key, or NULL if no app
     92   // window with the key are open. If there is a window for the given app and
     93   // key that is active, that one will be returned, otherwise an arbitrary
     94   // window will be returned.
     95   AppWindow* GetAppWindowForAppAndKey(const std::string& app_id,
     96                                       const std::string& window_key) const;
     97 
     98   // Returns whether a AppWindow's ID was last known to have a DevToolsAgent
     99   // attached to it, which should be restored during a reload of a corresponding
    100   // newly created |render_view_host|.
    101   bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
    102 
    103   class Factory : public BrowserContextKeyedServiceFactory {
    104    public:
    105     static AppWindowRegistry* GetForBrowserContext(
    106         content::BrowserContext* context,
    107         bool create);
    108 
    109     static Factory* GetInstance();
    110 
    111    private:
    112     friend struct DefaultSingletonTraits<Factory>;
    113 
    114     Factory();
    115     virtual ~Factory();
    116 
    117     // BrowserContextKeyedServiceFactory
    118     virtual KeyedService* BuildServiceInstanceFor(
    119         content::BrowserContext* context) const OVERRIDE;
    120     virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
    121     virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
    122     virtual content::BrowserContext* GetBrowserContextToUse(
    123         content::BrowserContext* context) const OVERRIDE;
    124   };
    125 
    126  protected:
    127   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
    128 
    129  private:
    130   // Ensures the specified |app_window| is included in |app_windows_|.
    131   // Otherwise adds |app_window| to the back of |app_windows_|.
    132   void AddAppWindowToList(AppWindow* app_window);
    133 
    134   // Bring |app_window| to the front of |app_windows_|. If it is not in the
    135   // list, add it first.
    136   void BringToFront(AppWindow* app_window);
    137 
    138   content::BrowserContext* context_;
    139   AppWindowList app_windows_;
    140   InspectedWindowSet inspected_windows_;
    141   ObserverList<Observer> observers_;
    142   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
    143 };
    144 
    145 }  // namespace extensions
    146 
    147 #endif  // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_
    148