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 "apps/app_window_registry.h"
10 #include "apps/app_window.h"
11 #include "apps/ui/apps_client.h"
12 #include "apps/ui/native_app_window.h"
13 #include "components/keyed_service/content/browser_context_dependency_manager.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/devtools_agent_host.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/site_instance.h"
19 #include "content/public/browser/web_contents.h"
20 #include "extensions/browser/extensions_browser_client.h"
21 #include "extensions/common/extension.h"
25 // Create a key that identifies a AppWindow in a RenderViewHost across App
26 // reloads. If the window was given an id in CreateParams, the key is the
27 // extension id, a colon separator, and the AppWindow's |id|. If there is no
28 // |id|, the chrome-extension://extension-id/page.html URL will be used. If the
29 // RenderViewHost is not for a AppWindow, return an empty string.
30 std::string
GetWindowKeyForRenderViewHost(
31 const apps::AppWindowRegistry
* registry
,
32 content::RenderViewHost
* render_view_host
) {
33 apps::AppWindow
* app_window
=
34 registry
->GetAppWindowForRenderViewHost(render_view_host
);
36 return std::string(); // Not a AppWindow.
38 if (app_window
->window_key().empty())
39 return app_window
->web_contents()->GetURL().possibly_invalid_spec();
41 std::string key
= app_window
->extension_id();
43 key
+= app_window
->window_key();
51 void AppWindowRegistry::Observer::OnAppWindowAdded(AppWindow
* app_window
) {
54 void AppWindowRegistry::Observer::OnAppWindowIconChanged(
55 AppWindow
* app_window
) {
58 void AppWindowRegistry::Observer::OnAppWindowRemoved(AppWindow
* app_window
) {
61 void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow
* app_window
) {
64 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
) {
105 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowShown(app_window
));
108 void AppWindowRegistry::RemoveAppWindow(AppWindow
* app_window
) {
109 const AppWindowList::iterator it
=
110 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
111 if (it
!= app_windows_
.end())
112 app_windows_
.erase(it
);
113 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowRemoved(app_window
));
116 void AppWindowRegistry::AddObserver(Observer
* observer
) {
117 observers_
.AddObserver(observer
);
120 void AppWindowRegistry::RemoveObserver(Observer
* observer
) {
121 observers_
.RemoveObserver(observer
);
124 AppWindowRegistry::AppWindowList
AppWindowRegistry::GetAppWindowsForApp(
125 const std::string
& app_id
) const {
126 AppWindowList app_windows
;
127 for (AppWindowList::const_iterator i
= app_windows_
.begin();
128 i
!= app_windows_
.end();
130 if ((*i
)->extension_id() == app_id
)
131 app_windows
.push_back(*i
);
136 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string
& app_id
) {
137 const AppWindowList windows
= GetAppWindowsForApp(app_id
);
138 for (AppWindowRegistry::const_iterator it
= windows
.begin();
141 (*it
)->GetBaseWindow()->Close();
145 AppWindow
* AppWindowRegistry::GetAppWindowForRenderViewHost(
146 content::RenderViewHost
* render_view_host
) const {
147 for (AppWindowList::const_iterator i
= app_windows_
.begin();
148 i
!= app_windows_
.end();
150 if ((*i
)->web_contents()->GetRenderViewHost() == render_view_host
)
157 AppWindow
* AppWindowRegistry::GetAppWindowForNativeWindow(
158 gfx::NativeWindow window
) const {
159 for (AppWindowList::const_iterator i
= app_windows_
.begin();
160 i
!= app_windows_
.end();
162 if ((*i
)->GetNativeWindow() == window
)
169 AppWindow
* AppWindowRegistry::GetCurrentAppWindowForApp(
170 const std::string
& app_id
) const {
171 AppWindow
* result
= NULL
;
172 for (AppWindowList::const_iterator i
= app_windows_
.begin();
173 i
!= app_windows_
.end();
175 if ((*i
)->extension_id() == app_id
) {
177 if (result
->GetBaseWindow()->IsActive())
185 AppWindow
* AppWindowRegistry::GetAppWindowForAppAndKey(
186 const std::string
& app_id
,
187 const std::string
& window_key
) const {
188 AppWindow
* result
= NULL
;
189 for (AppWindowList::const_iterator i
= app_windows_
.begin();
190 i
!= app_windows_
.end();
192 if ((*i
)->extension_id() == app_id
&& (*i
)->window_key() == window_key
) {
194 if (result
->GetBaseWindow()->IsActive())
201 bool AppWindowRegistry::HadDevToolsAttached(
202 content::RenderViewHost
* render_view_host
) const {
203 std::string key
= GetWindowKeyForRenderViewHost(this, render_view_host
);
204 return key
.empty() ? false : inspected_windows_
.count(key
) != 0;
208 AppWindow
* AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile(
209 gfx::NativeWindow window
) {
210 std::vector
<content::BrowserContext
*> contexts
=
211 AppsClient::Get()->GetLoadedBrowserContexts();
212 for (std::vector
<content::BrowserContext
*>::const_iterator i
=
216 AppWindowRegistry
* registry
=
217 Factory::GetForBrowserContext(*i
, false /* create */);
221 AppWindow
* app_window
= registry
->GetAppWindowForNativeWindow(window
);
230 bool AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(
231 int window_type_mask
) {
232 std::vector
<content::BrowserContext
*> contexts
=
233 AppsClient::Get()->GetLoadedBrowserContexts();
234 for (std::vector
<content::BrowserContext
*>::const_iterator i
=
238 AppWindowRegistry
* registry
=
239 Factory::GetForBrowserContext(*i
, false /* create */);
243 const AppWindowList
& app_windows
= registry
->app_windows();
244 if (app_windows
.empty())
247 if (window_type_mask
== 0)
250 for (const_iterator j
= app_windows
.begin(); j
!= app_windows
.end(); ++j
) {
251 if ((*j
)->window_type() & window_type_mask
)
260 void AppWindowRegistry::CloseAllAppWindows() {
261 std::vector
<content::BrowserContext
*> contexts
=
262 AppsClient::Get()->GetLoadedBrowserContexts();
263 for (std::vector
<content::BrowserContext
*>::const_iterator i
=
267 AppWindowRegistry
* registry
=
268 Factory::GetForBrowserContext(*i
, false /* create */);
272 while (!registry
->app_windows().empty())
273 registry
->app_windows().front()->GetBaseWindow()->Close();
277 void AppWindowRegistry::OnDevToolsStateChanged(
278 content::DevToolsAgentHost
* agent_host
,
280 content::WebContents
* web_contents
= agent_host
->GetWebContents();
281 // Ignore unrelated notifications.
282 if (!web_contents
|| web_contents
->GetBrowserContext() != context_
)
286 GetWindowKeyForRenderViewHost(this, web_contents
->GetRenderViewHost());
291 inspected_windows_
.insert(key
);
293 inspected_windows_
.erase(key
);
296 void AppWindowRegistry::AddAppWindowToList(AppWindow
* app_window
) {
297 const AppWindowList::iterator it
=
298 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
299 if (it
!= app_windows_
.end())
301 app_windows_
.push_back(app_window
);
304 void AppWindowRegistry::BringToFront(AppWindow
* app_window
) {
305 const AppWindowList::iterator it
=
306 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
307 if (it
!= app_windows_
.end())
308 app_windows_
.erase(it
);
309 app_windows_
.push_front(app_window
);
312 ///////////////////////////////////////////////////////////////////////////////
313 // Factory boilerplate
316 AppWindowRegistry
* AppWindowRegistry::Factory::GetForBrowserContext(
317 content::BrowserContext
* context
,
319 return static_cast<AppWindowRegistry
*>(
320 GetInstance()->GetServiceForBrowserContext(context
, create
));
323 AppWindowRegistry::Factory
* AppWindowRegistry::Factory::GetInstance() {
324 return Singleton
<AppWindowRegistry::Factory
>::get();
327 AppWindowRegistry::Factory::Factory()
328 : BrowserContextKeyedServiceFactory(
330 BrowserContextDependencyManager::GetInstance()) {}
332 AppWindowRegistry::Factory::~Factory() {}
334 KeyedService
* AppWindowRegistry::Factory::BuildServiceInstanceFor(
335 content::BrowserContext
* context
) const {
336 return new AppWindowRegistry(context
);
339 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
343 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
347 content::BrowserContext
* AppWindowRegistry::Factory::GetBrowserContextToUse(
348 content::BrowserContext
* context
) const {
349 return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext(