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.
5 #ifndef EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_
6 #define EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_
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"
22 class DevToolsAgentHost
;
26 namespace extensions
{
30 // The AppWindowRegistry tracks the AppWindows for all platform apps for a
31 // particular browser context.
32 class AppWindowRegistry
: public KeyedService
{
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
45 virtual void OnAppWindowHidden(AppWindow
* app_window
);
46 // Called just after a app window was shown.
47 virtual void OnAppWindowShown(AppWindow
* app_window
, bool was_hidden
);
53 typedef std::list
<AppWindow
*> AppWindowList
;
54 typedef AppWindowList::const_iterator const_iterator
;
55 typedef std::set
<std::string
> InspectedWindowSet
;
57 explicit AppWindowRegistry(content::BrowserContext
* context
);
58 ~AppWindowRegistry() override
;
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
);
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
, bool was_hidden
);
71 void RemoveAppWindow(AppWindow
* app_window
);
73 void AddObserver(Observer
* observer
);
74 void RemoveObserver(Observer
* observer
);
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_
; }
80 // Close all app windows associated with an app.
81 void CloseAllAppWindowsForApp(const std::string
& app_id
);
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;
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;
103 class Factory
: public BrowserContextKeyedServiceFactory
{
105 static AppWindowRegistry
* GetForBrowserContext(
106 content::BrowserContext
* context
,
109 static Factory
* GetInstance();
112 friend struct DefaultSingletonTraits
<Factory
>;
117 // BrowserContextKeyedServiceFactory
118 KeyedService
* BuildServiceInstanceFor(
119 content::BrowserContext
* context
) const override
;
120 bool ServiceIsCreatedWithBrowserContext() const override
;
121 bool ServiceIsNULLWhileTesting() const override
;
122 content::BrowserContext
* GetBrowserContextToUse(
123 content::BrowserContext
* context
) const override
;
127 void OnDevToolsStateChanged(content::DevToolsAgentHost
*, bool attached
);
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
);
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
);
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_
;
145 } // namespace extensions
147 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_REGISTRY_H_