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 #include "extensions/browser/app_window/app_window_registry.h"
10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/devtools_agent_host.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/site_instance.h"
16 #include "content/public/browser/web_contents.h"
17 #include "extensions/browser/app_window/app_window.h"
18 #include "extensions/browser/app_window/native_app_window.h"
19 #include "extensions/browser/extensions_browser_client.h"
20 #include "extensions/common/extension.h"
22 namespace extensions
{
26 // Create a key that identifies a AppWindow in a RenderViewHost across App
27 // reloads. If the window was given an id in CreateParams, the key is the
28 // extension id, a colon separator, and the AppWindow's |id|. If there is no
29 // |id|, the chrome-extension://extension-id/page.html URL will be used. If the
30 // RenderViewHost is not for a AppWindow, return an empty string.
31 std::string
GetWindowKeyForRenderViewHost(
32 const AppWindowRegistry
* registry
,
33 content::RenderViewHost
* render_view_host
) {
34 AppWindow
* app_window
=
35 registry
->GetAppWindowForRenderViewHost(render_view_host
);
37 return std::string(); // Not a AppWindow.
39 if (app_window
->window_key().empty())
40 return app_window
->web_contents()->GetURL().possibly_invalid_spec();
42 std::string key
= app_window
->extension_id();
44 key
+= app_window
->window_key();
50 void AppWindowRegistry::Observer::OnAppWindowAdded(AppWindow
* app_window
) {
53 void AppWindowRegistry::Observer::OnAppWindowIconChanged(
54 AppWindow
* app_window
) {
57 void AppWindowRegistry::Observer::OnAppWindowRemoved(AppWindow
* app_window
) {
60 void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow
* app_window
) {
63 void AppWindowRegistry::Observer::OnAppWindowShown(AppWindow
* app_window
,
67 AppWindowRegistry::Observer::~Observer() {
70 AppWindowRegistry::AppWindowRegistry(content::BrowserContext
* context
)
72 devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged
,
73 base::Unretained(this))) {
74 content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_
);
77 AppWindowRegistry::~AppWindowRegistry() {
78 content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_
);
82 AppWindowRegistry
* AppWindowRegistry::Get(content::BrowserContext
* context
) {
83 return Factory::GetForBrowserContext(context
, true /* create */);
86 void AppWindowRegistry::AddAppWindow(AppWindow
* app_window
) {
87 BringToFront(app_window
);
88 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowAdded(app_window
));
91 void AppWindowRegistry::AppWindowIconChanged(AppWindow
* app_window
) {
92 AddAppWindowToList(app_window
);
93 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowIconChanged(app_window
));
96 void AppWindowRegistry::AppWindowActivated(AppWindow
* app_window
) {
97 BringToFront(app_window
);
100 void AppWindowRegistry::AppWindowHidden(AppWindow
* app_window
) {
101 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowHidden(app_window
));
104 void AppWindowRegistry::AppWindowShown(AppWindow
* app_window
, bool was_hidden
) {
105 FOR_EACH_OBSERVER(Observer
, observers_
,
106 OnAppWindowShown(app_window
, was_hidden
));
109 void AppWindowRegistry::RemoveAppWindow(AppWindow
* app_window
) {
110 const AppWindowList::iterator it
=
111 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
112 if (it
!= app_windows_
.end())
113 app_windows_
.erase(it
);
114 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowRemoved(app_window
));
117 void AppWindowRegistry::AddObserver(Observer
* observer
) {
118 observers_
.AddObserver(observer
);
121 void AppWindowRegistry::RemoveObserver(Observer
* observer
) {
122 observers_
.RemoveObserver(observer
);
125 AppWindowRegistry::AppWindowList
AppWindowRegistry::GetAppWindowsForApp(
126 const std::string
& app_id
) const {
127 AppWindowList app_windows
;
128 for (AppWindowList::const_iterator i
= app_windows_
.begin();
129 i
!= app_windows_
.end();
131 if ((*i
)->extension_id() == app_id
)
132 app_windows
.push_back(*i
);
137 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string
& app_id
) {
138 const AppWindowList windows
= GetAppWindowsForApp(app_id
);
139 for (AppWindowRegistry::const_iterator it
= windows
.begin();
142 (*it
)->GetBaseWindow()->Close();
146 AppWindow
* AppWindowRegistry::GetAppWindowForRenderViewHost(
147 content::RenderViewHost
* render_view_host
) const {
148 for (AppWindowList::const_iterator i
= app_windows_
.begin();
149 i
!= app_windows_
.end();
151 if ((*i
)->web_contents()->GetRenderViewHost() == render_view_host
)
158 AppWindow
* AppWindowRegistry::GetAppWindowForNativeWindow(
159 gfx::NativeWindow window
) const {
160 for (AppWindowList::const_iterator i
= app_windows_
.begin();
161 i
!= app_windows_
.end();
163 if ((*i
)->GetNativeWindow() == window
)
170 AppWindow
* AppWindowRegistry::GetCurrentAppWindowForApp(
171 const std::string
& app_id
) const {
172 AppWindow
* result
= NULL
;
173 for (AppWindowList::const_iterator i
= app_windows_
.begin();
174 i
!= app_windows_
.end();
176 if ((*i
)->extension_id() == app_id
) {
178 if (result
->GetBaseWindow()->IsActive())
186 AppWindow
* AppWindowRegistry::GetAppWindowForAppAndKey(
187 const std::string
& app_id
,
188 const std::string
& window_key
) const {
189 AppWindow
* result
= NULL
;
190 for (AppWindowList::const_iterator i
= app_windows_
.begin();
191 i
!= app_windows_
.end();
193 if ((*i
)->extension_id() == app_id
&& (*i
)->window_key() == window_key
) {
195 if (result
->GetBaseWindow()->IsActive())
202 bool AppWindowRegistry::HadDevToolsAttached(
203 content::RenderViewHost
* render_view_host
) const {
204 std::string key
= GetWindowKeyForRenderViewHost(this, render_view_host
);
205 return key
.empty() ? false : inspected_windows_
.count(key
) != 0;
208 void AppWindowRegistry::OnDevToolsStateChanged(
209 content::DevToolsAgentHost
* agent_host
,
211 content::WebContents
* web_contents
= agent_host
->GetWebContents();
212 // Ignore unrelated notifications.
213 if (!web_contents
|| web_contents
->GetBrowserContext() != context_
)
217 GetWindowKeyForRenderViewHost(this, web_contents
->GetRenderViewHost());
222 inspected_windows_
.insert(key
);
224 inspected_windows_
.erase(key
);
227 void AppWindowRegistry::AddAppWindowToList(AppWindow
* app_window
) {
228 const AppWindowList::iterator it
=
229 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
230 if (it
!= app_windows_
.end())
232 app_windows_
.push_back(app_window
);
235 void AppWindowRegistry::BringToFront(AppWindow
* app_window
) {
236 const AppWindowList::iterator it
=
237 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
238 if (it
!= app_windows_
.end())
239 app_windows_
.erase(it
);
240 app_windows_
.push_front(app_window
);
243 ///////////////////////////////////////////////////////////////////////////////
244 // Factory boilerplate
247 AppWindowRegistry
* AppWindowRegistry::Factory::GetForBrowserContext(
248 content::BrowserContext
* context
,
250 return static_cast<AppWindowRegistry
*>(
251 GetInstance()->GetServiceForBrowserContext(context
, create
));
254 AppWindowRegistry::Factory
* AppWindowRegistry::Factory::GetInstance() {
255 return Singleton
<AppWindowRegistry::Factory
>::get();
258 AppWindowRegistry::Factory::Factory()
259 : BrowserContextKeyedServiceFactory(
261 BrowserContextDependencyManager::GetInstance()) {}
263 AppWindowRegistry::Factory::~Factory() {}
265 KeyedService
* AppWindowRegistry::Factory::BuildServiceInstanceFor(
266 content::BrowserContext
* context
) const {
267 return new AppWindowRegistry(context
);
270 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
274 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
278 content::BrowserContext
* AppWindowRegistry::Factory::GetBrowserContextToUse(
279 content::BrowserContext
* context
) const {
280 return ExtensionsBrowserClient::Get()->GetOriginalContext(context
);
283 } // namespace extensions