Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / apps / app_window_registry.h
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 #include <string>
10 #include <set>
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 apps {
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(apps::AppWindow* app_window);
38     // Called when the window icon changes.
39     virtual void OnAppWindowIconChanged(apps::AppWindow* app_window);
40     // Called just after a app window was removed.
41     virtual void OnAppWindowRemoved(apps::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(apps::AppWindow* app_window);
46     // Called just after a app window was shown.
47     virtual void OnAppWindowShown(apps::AppWindow* app_window);
48
49    protected:
50     virtual ~Observer();
51   };
52
53   typedef std::list<apps::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(apps::AppWindow* app_window);
66   void AppWindowIconChanged(apps::AppWindow* app_window);
67   // Called by |app_window| when it is activated.
68   void AppWindowActivated(apps::AppWindow* app_window);
69   void AppWindowHidden(apps::AppWindow* app_window);
70   void AppWindowShown(apps::AppWindow* app_window);
71   void RemoveAppWindow(apps::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   apps::AppWindow* GetAppWindowForRenderViewHost(
85       content::RenderViewHost* render_view_host) const;
86   apps::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   apps::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   apps::AppWindow* GetAppWindowForAppAndKey(const std::string& app_id,
96                                             const std::string& window_key)
97       const;
98
99   // Returns whether a AppWindow's ID was last known to have a DevToolsAgent
100   // attached to it, which should be restored during a reload of a corresponding
101   // newly created |render_view_host|.
102   bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
103
104   // Returns the app window for |window|, looking in all browser contexts.
105   static apps::AppWindow* GetAppWindowForNativeWindowAnyProfile(
106       gfx::NativeWindow window);
107
108   // Returns true if the number of app windows registered across all browser
109   // contexts is non-zero. |window_type_mask| is a bitwise OR filter of
110   // AppWindow::WindowType, or 0 for any window type.
111   static bool IsAppWindowRegisteredInAnyProfile(int window_type_mask);
112
113   // Close all app windows in all profiles.
114   static void CloseAllAppWindows();
115
116   class Factory : public BrowserContextKeyedServiceFactory {
117    public:
118     static AppWindowRegistry* GetForBrowserContext(
119         content::BrowserContext* context,
120         bool create);
121
122     static Factory* GetInstance();
123
124    private:
125     friend struct DefaultSingletonTraits<Factory>;
126
127     Factory();
128     virtual ~Factory();
129
130     // BrowserContextKeyedServiceFactory
131     virtual KeyedService* BuildServiceInstanceFor(
132         content::BrowserContext* context) const OVERRIDE;
133     virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
134     virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
135     virtual content::BrowserContext* GetBrowserContextToUse(
136         content::BrowserContext* context) const OVERRIDE;
137   };
138
139  protected:
140   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
141
142  private:
143   // Ensures the specified |app_window| is included in |app_windows_|.
144   // Otherwise adds |app_window| to the back of |app_windows_|.
145   void AddAppWindowToList(apps::AppWindow* app_window);
146
147   // Bring |app_window| to the front of |app_windows_|. If it is not in the
148   // list, add it first.
149   void BringToFront(apps::AppWindow* app_window);
150
151   content::BrowserContext* context_;
152   AppWindowList app_windows_;
153   InspectedWindowSet inspected_windows_;
154   ObserverList<Observer> observers_;
155   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
156 };
157
158 }  // namespace apps
159
160 #endif  // APPS_APP_WINDOW_REGISTRY_H_