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.h"
6 #include "apps/app_window_registry.h"
7 #include "apps/apps_client.h"
8 #include "apps/ui/native_app_window.h"
9 #include "components/keyed_service/content/browser_context_dependency_manager.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/devtools_agent_host.h"
12 #include "content/public/browser/devtools_manager.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/extensions_browser_client.h"
18 #include "extensions/common/extension.h"
22 // Create a key that identifies a AppWindow in a RenderViewHost across App
23 // reloads. If the window was given an id in CreateParams, the key is the
24 // extension id, a colon separator, and the AppWindow's |id|. If there is no
25 // |id|, the chrome-extension://extension-id/page.html URL will be used. If the
26 // RenderViewHost is not for a AppWindow, return an empty string.
27 std::string
GetWindowKeyForRenderViewHost(
28 const apps::AppWindowRegistry
* registry
,
29 content::RenderViewHost
* render_view_host
) {
30 apps::AppWindow
* app_window
=
31 registry
->GetAppWindowForRenderViewHost(render_view_host
);
33 return std::string(); // Not a AppWindow.
35 if (app_window
->window_key().empty())
36 return app_window
->web_contents()->GetURL().possibly_invalid_spec();
38 std::string key
= app_window
->extension()->id();
40 key
+= app_window
->window_key();
48 void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow
* app_window
) {}
50 void AppWindowRegistry::Observer::OnAppWindowShown(AppWindow
* app_window
) {}
52 AppWindowRegistry::Observer::~Observer() {}
54 AppWindowRegistry::AppWindowRegistry(content::BrowserContext
* context
)
56 devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged
,
57 base::Unretained(this))) {
58 content::DevToolsManager::GetInstance()->AddAgentStateCallback(
62 AppWindowRegistry::~AppWindowRegistry() {
63 content::DevToolsManager::GetInstance()->RemoveAgentStateCallback(
68 AppWindowRegistry
* AppWindowRegistry::Get(content::BrowserContext
* context
) {
69 return Factory::GetForBrowserContext(context
, true /* create */);
72 void AppWindowRegistry::AddAppWindow(AppWindow
* app_window
) {
73 BringToFront(app_window
);
74 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowAdded(app_window
));
77 void AppWindowRegistry::AppWindowIconChanged(AppWindow
* app_window
) {
78 AddAppWindowToList(app_window
);
79 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowIconChanged(app_window
));
82 void AppWindowRegistry::AppWindowActivated(AppWindow
* app_window
) {
83 BringToFront(app_window
);
86 void AppWindowRegistry::AppWindowHidden(AppWindow
* app_window
) {
87 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowHidden(app_window
));
90 void AppWindowRegistry::AppWindowShown(AppWindow
* app_window
) {
91 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowShown(app_window
));
94 void AppWindowRegistry::RemoveAppWindow(AppWindow
* app_window
) {
95 const AppWindowList::iterator it
=
96 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
97 if (it
!= app_windows_
.end())
98 app_windows_
.erase(it
);
99 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppWindowRemoved(app_window
));
102 void AppWindowRegistry::AddObserver(Observer
* observer
) {
103 observers_
.AddObserver(observer
);
106 void AppWindowRegistry::RemoveObserver(Observer
* observer
) {
107 observers_
.RemoveObserver(observer
);
110 AppWindowRegistry::AppWindowList
AppWindowRegistry::GetAppWindowsForApp(
111 const std::string
& app_id
) const {
112 AppWindowList app_windows
;
113 for (AppWindowList::const_iterator i
= app_windows_
.begin();
114 i
!= app_windows_
.end();
116 if ((*i
)->extension_id() == app_id
)
117 app_windows
.push_back(*i
);
122 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string
& app_id
) {
123 const AppWindowList windows
= GetAppWindowsForApp(app_id
);
124 for (AppWindowRegistry::const_iterator it
= windows
.begin();
127 (*it
)->GetBaseWindow()->Close();
131 AppWindow
* AppWindowRegistry::GetAppWindowForRenderViewHost(
132 content::RenderViewHost
* render_view_host
) const {
133 for (AppWindowList::const_iterator i
= app_windows_
.begin();
134 i
!= app_windows_
.end();
136 if ((*i
)->web_contents()->GetRenderViewHost() == render_view_host
)
143 AppWindow
* AppWindowRegistry::GetAppWindowForNativeWindow(
144 gfx::NativeWindow window
) const {
145 for (AppWindowList::const_iterator i
= app_windows_
.begin();
146 i
!= app_windows_
.end();
148 if ((*i
)->GetNativeWindow() == window
)
155 AppWindow
* AppWindowRegistry::GetCurrentAppWindowForApp(
156 const std::string
& app_id
) const {
157 AppWindow
* result
= NULL
;
158 for (AppWindowList::const_iterator i
= app_windows_
.begin();
159 i
!= app_windows_
.end();
161 if ((*i
)->extension()->id() == app_id
) {
163 if (result
->GetBaseWindow()->IsActive())
171 AppWindow
* AppWindowRegistry::GetAppWindowForAppAndKey(
172 const std::string
& app_id
,
173 const std::string
& window_key
) const {
174 AppWindow
* result
= NULL
;
175 for (AppWindowList::const_iterator i
= app_windows_
.begin();
176 i
!= app_windows_
.end();
178 if ((*i
)->extension()->id() == app_id
&& (*i
)->window_key() == window_key
) {
180 if (result
->GetBaseWindow()->IsActive())
187 bool AppWindowRegistry::HadDevToolsAttached(
188 content::RenderViewHost
* render_view_host
) const {
189 std::string key
= GetWindowKeyForRenderViewHost(this, render_view_host
);
190 return key
.empty() ? false : inspected_windows_
.count(key
) != 0;
194 AppWindow
* AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile(
195 gfx::NativeWindow window
) {
196 std::vector
<content::BrowserContext
*> contexts
=
197 AppsClient::Get()->GetLoadedBrowserContexts();
198 for (std::vector
<content::BrowserContext
*>::const_iterator i
=
202 AppWindowRegistry
* registry
=
203 Factory::GetForBrowserContext(*i
, false /* create */);
207 AppWindow
* app_window
= registry
->GetAppWindowForNativeWindow(window
);
216 bool AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(
217 int window_type_mask
) {
218 std::vector
<content::BrowserContext
*> contexts
=
219 AppsClient::Get()->GetLoadedBrowserContexts();
220 for (std::vector
<content::BrowserContext
*>::const_iterator i
=
224 AppWindowRegistry
* registry
=
225 Factory::GetForBrowserContext(*i
, false /* create */);
229 const AppWindowList
& app_windows
= registry
->app_windows();
230 if (app_windows
.empty())
233 if (window_type_mask
== 0)
236 for (const_iterator j
= app_windows
.begin(); j
!= app_windows
.end(); ++j
) {
237 if ((*j
)->window_type() & window_type_mask
)
246 void AppWindowRegistry::CloseAllAppWindows() {
247 std::vector
<content::BrowserContext
*> contexts
=
248 AppsClient::Get()->GetLoadedBrowserContexts();
249 for (std::vector
<content::BrowserContext
*>::const_iterator i
=
253 AppWindowRegistry
* registry
=
254 Factory::GetForBrowserContext(*i
, false /* create */);
258 while (!registry
->app_windows().empty())
259 registry
->app_windows().front()->GetBaseWindow()->Close();
263 void AppWindowRegistry::OnDevToolsStateChanged(
264 content::DevToolsAgentHost
* agent_host
,
266 content::RenderViewHost
* rvh
= agent_host
->GetRenderViewHost();
267 // Ignore unrelated notifications.
269 rvh
->GetSiteInstance()->GetProcess()->GetBrowserContext() != context_
)
272 std::string key
= GetWindowKeyForRenderViewHost(this, rvh
);
277 inspected_windows_
.insert(key
);
279 inspected_windows_
.erase(key
);
282 void AppWindowRegistry::AddAppWindowToList(AppWindow
* app_window
) {
283 const AppWindowList::iterator it
=
284 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
285 if (it
!= app_windows_
.end())
287 app_windows_
.push_back(app_window
);
290 void AppWindowRegistry::BringToFront(AppWindow
* app_window
) {
291 const AppWindowList::iterator it
=
292 std::find(app_windows_
.begin(), app_windows_
.end(), app_window
);
293 if (it
!= app_windows_
.end())
294 app_windows_
.erase(it
);
295 app_windows_
.push_front(app_window
);
298 ///////////////////////////////////////////////////////////////////////////////
299 // Factory boilerplate
302 AppWindowRegistry
* AppWindowRegistry::Factory::GetForBrowserContext(
303 content::BrowserContext
* context
,
305 return static_cast<AppWindowRegistry
*>(
306 GetInstance()->GetServiceForBrowserContext(context
, create
));
309 AppWindowRegistry::Factory
* AppWindowRegistry::Factory::GetInstance() {
310 return Singleton
<AppWindowRegistry::Factory
>::get();
313 AppWindowRegistry::Factory::Factory()
314 : BrowserContextKeyedServiceFactory(
316 BrowserContextDependencyManager::GetInstance()) {}
318 AppWindowRegistry::Factory::~Factory() {}
320 KeyedService
* AppWindowRegistry::Factory::BuildServiceInstanceFor(
321 content::BrowserContext
* context
) const {
322 return new AppWindowRegistry(context
);
325 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
329 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
333 content::BrowserContext
* AppWindowRegistry::Factory::GetBrowserContextToUse(
334 content::BrowserContext
* context
) const {
335 return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext(