Upstream version 7.35.139.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
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) = 0;
36     // Called when the window icon changes.
37     virtual void OnAppWindowIconChanged(apps::AppWindow* app_window) = 0;
38     // Called just after a app window was removed.
39     virtual void OnAppWindowRemoved(apps::AppWindow* app_window) = 0;
40 #if defined(OS_CHROMEOS)
41     // Called just after a app window was hidden. This is different from
42     // window visibility as a minimize does not hide a window, but does make
43     // it not visible.
44     virtual void OnAppWindowHidden(apps::AppWindow* app_window);
45     // Called just after a app window was shown.
46     virtual void OnAppWindowShown(apps::AppWindow* app_window);
47 #endif
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 #if defined(OS_CHROMEOS)
70   void AppWindowHidden(apps::AppWindow* app_window);
71   void AppWindowShown(apps::AppWindow* app_window);
72 #endif
73   void RemoveAppWindow(apps::AppWindow* app_window);
74
75   void AddObserver(Observer* observer);
76   void RemoveObserver(Observer* observer);
77
78   // Returns a set of windows owned by the application identified by app_id.
79   AppWindowList GetAppWindowsForApp(const std::string& app_id) const;
80   const AppWindowList& app_windows() const { return app_windows_; }
81
82   // Close all app windows associated with an app.
83   void CloseAllAppWindowsForApp(const std::string& app_id);
84
85   // Helper functions to find app windows with particular attributes.
86   apps::AppWindow* GetAppWindowForRenderViewHost(
87       content::RenderViewHost* render_view_host) const;
88   apps::AppWindow* GetAppWindowForNativeWindow(gfx::NativeWindow window) const;
89   // Returns an app window for the given app, or NULL if no app windows are
90   // open. If there is a window for the given app that is active, that one will
91   // be returned, otherwise an arbitrary window will be returned.
92   apps::AppWindow* GetCurrentAppWindowForApp(const std::string& app_id) const;
93   // Returns an app window for the given app and window key, or NULL if no app
94   // window with the key are open. If there is a window for the given app and
95   // key that is active, that one will be returned, otherwise an arbitrary
96   // window will be returned.
97   apps::AppWindow* GetAppWindowForAppAndKey(const std::string& app_id,
98                                             const std::string& window_key)
99       const;
100
101   // Returns whether a AppWindow's ID was last known to have a DevToolsAgent
102   // attached to it, which should be restored during a reload of a corresponding
103   // newly created |render_view_host|.
104   bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
105
106   // Returns the app window for |window|, looking in all browser contexts.
107   static apps::AppWindow* GetAppWindowForNativeWindowAnyProfile(
108       gfx::NativeWindow window);
109
110   // Returns true if the number of app windows registered across all browser
111   // contexts is non-zero. |window_type_mask| is a bitwise OR filter of
112   // AppWindow::WindowType, or 0 for any window type.
113   static bool IsAppWindowRegisteredInAnyProfile(int window_type_mask);
114
115   class Factory : public BrowserContextKeyedServiceFactory {
116    public:
117     static AppWindowRegistry* GetForBrowserContext(
118         content::BrowserContext* context,
119         bool create);
120
121     static Factory* GetInstance();
122
123    private:
124     friend struct DefaultSingletonTraits<Factory>;
125
126     Factory();
127     virtual ~Factory();
128
129     // BrowserContextKeyedServiceFactory
130     virtual KeyedService* BuildServiceInstanceFor(
131         content::BrowserContext* context) const OVERRIDE;
132     virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
133     virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
134     virtual content::BrowserContext* GetBrowserContextToUse(
135         content::BrowserContext* context) const OVERRIDE;
136   };
137
138  protected:
139   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
140
141  private:
142   // Ensures the specified |app_window| is included in |app_windows_|.
143   // Otherwise adds |app_window| to the back of |app_windows_|.
144   void AddAppWindowToList(apps::AppWindow* app_window);
145
146   // Bring |app_window| to the front of |app_windows_|. If it is not in the
147   // list, add it first.
148   void BringToFront(apps::AppWindow* app_window);
149
150   content::BrowserContext* context_;
151   AppWindowList app_windows_;
152   InspectedWindowSet inspected_windows_;
153   ObserverList<Observer> observers_;
154   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
155 };
156
157 }  // namespace extensions
158
159 #endif  // APPS_APP_WINDOW_REGISTRY_H_