Upstream version 7.35.136.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
41    protected:
42     virtual ~Observer() {}
43   };
44
45   typedef std::list<apps::AppWindow*> AppWindowList;
46   typedef AppWindowList::const_iterator const_iterator;
47   typedef std::set<std::string> InspectedWindowSet;
48
49   explicit AppWindowRegistry(content::BrowserContext* context);
50   virtual ~AppWindowRegistry();
51
52   // Returns the instance for the given browser context, or NULL if none. This
53   // is a convenience wrapper around
54   // AppWindowRegistry::Factory::GetForBrowserContext().
55   static AppWindowRegistry* Get(content::BrowserContext* context);
56
57   void AddAppWindow(apps::AppWindow* app_window);
58   void AppWindowIconChanged(apps::AppWindow* app_window);
59   // Called by |app_window| when it is activated.
60   void AppWindowActivated(apps::AppWindow* app_window);
61   void RemoveAppWindow(apps::AppWindow* app_window);
62
63   void AddObserver(Observer* observer);
64   void RemoveObserver(Observer* observer);
65
66   // Returns a set of windows owned by the application identified by app_id.
67   AppWindowList GetAppWindowsForApp(const std::string& app_id) const;
68   const AppWindowList& app_windows() const { return app_windows_; }
69
70   // Close all app windows associated with an app.
71   void CloseAllAppWindowsForApp(const std::string& app_id);
72
73   // Helper functions to find app windows with particular attributes.
74   apps::AppWindow* GetAppWindowForRenderViewHost(
75       content::RenderViewHost* render_view_host) const;
76   apps::AppWindow* GetAppWindowForNativeWindow(gfx::NativeWindow window) const;
77   // Returns an app window for the given app, or NULL if no app windows are
78   // open. If there is a window for the given app that is active, that one will
79   // be returned, otherwise an arbitrary window will be returned.
80   apps::AppWindow* GetCurrentAppWindowForApp(const std::string& app_id) const;
81   // Returns an app window for the given app and window key, or NULL if no app
82   // window with the key are open. If there is a window for the given app and
83   // key that is active, that one will be returned, otherwise an arbitrary
84   // window will be returned.
85   apps::AppWindow* GetAppWindowForAppAndKey(const std::string& app_id,
86                                             const std::string& window_key)
87       const;
88
89   // Returns whether a AppWindow's ID was last known to have a DevToolsAgent
90   // attached to it, which should be restored during a reload of a corresponding
91   // newly created |render_view_host|.
92   bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
93
94   // Returns the app window for |window|, looking in all browser contexts.
95   static apps::AppWindow* GetAppWindowForNativeWindowAnyProfile(
96       gfx::NativeWindow window);
97
98   // Returns true if the number of app windows registered across all browser
99   // contexts is non-zero. |window_type_mask| is a bitwise OR filter of
100   // AppWindow::WindowType, or 0 for any window type.
101   static bool IsAppWindowRegisteredInAnyProfile(int window_type_mask);
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(apps::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(apps::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  // APPS_APP_WINDOW_REGISTRY_H_