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