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 #if defined(OS_CHROMEOS)
49 void AppWindowRegistry::Observer::OnAppWindowHidden(AppWindow* app_window) {
52 void AppWindowRegistry::Observer::OnAppWindowShown(AppWindow* app_window) {
56 AppWindowRegistry::Observer::~Observer() {
59 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context)
61 devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged,
62 base::Unretained(this))) {
63 content::DevToolsManager::GetInstance()->AddAgentStateCallback(
67 AppWindowRegistry::~AppWindowRegistry() {
68 content::DevToolsManager::GetInstance()->RemoveAgentStateCallback(
73 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) {
74 return Factory::GetForBrowserContext(context, true /* create */);
77 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) {
78 BringToFront(app_window);
79 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window));
82 void AppWindowRegistry::AppWindowIconChanged(AppWindow* app_window) {
83 AddAppWindowToList(app_window);
84 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowIconChanged(app_window));
87 void AppWindowRegistry::AppWindowActivated(AppWindow* app_window) {
88 BringToFront(app_window);
91 #if defined(OS_CHROMEOS)
92 void AppWindowRegistry::AppWindowHidden(AppWindow* app_window) {
93 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowHidden(app_window));
96 void AppWindowRegistry::AppWindowShown(AppWindow* app_window) {
97 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowShown(app_window));
101 void AppWindowRegistry::RemoveAppWindow(AppWindow* app_window) {
102 const AppWindowList::iterator it =
103 std::find(app_windows_.begin(), app_windows_.end(), app_window);
104 if (it != app_windows_.end())
105 app_windows_.erase(it);
106 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowRemoved(app_window));
109 void AppWindowRegistry::AddObserver(Observer* observer) {
110 observers_.AddObserver(observer);
113 void AppWindowRegistry::RemoveObserver(Observer* observer) {
114 observers_.RemoveObserver(observer);
117 AppWindowRegistry::AppWindowList AppWindowRegistry::GetAppWindowsForApp(
118 const std::string& app_id) const {
119 AppWindowList app_windows;
120 for (AppWindowList::const_iterator i = app_windows_.begin();
121 i != app_windows_.end();
123 if ((*i)->extension_id() == app_id)
124 app_windows.push_back(*i);
129 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) {
130 for (AppWindowList::const_iterator i = app_windows_.begin();
131 i != app_windows_.end();) {
132 AppWindow* app_window = *(i++);
133 if (app_window->extension_id() == app_id)
134 app_window->GetBaseWindow()->Close();
138 AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost(
139 content::RenderViewHost* render_view_host) const {
140 for (AppWindowList::const_iterator i = app_windows_.begin();
141 i != app_windows_.end();
143 if ((*i)->web_contents()->GetRenderViewHost() == render_view_host)
150 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow(
151 gfx::NativeWindow window) const {
152 for (AppWindowList::const_iterator i = app_windows_.begin();
153 i != app_windows_.end();
155 if ((*i)->GetNativeWindow() == window)
162 AppWindow* AppWindowRegistry::GetCurrentAppWindowForApp(
163 const std::string& app_id) const {
164 AppWindow* result = NULL;
165 for (AppWindowList::const_iterator i = app_windows_.begin();
166 i != app_windows_.end();
168 if ((*i)->extension()->id() == app_id) {
170 if (result->GetBaseWindow()->IsActive())
178 AppWindow* AppWindowRegistry::GetAppWindowForAppAndKey(
179 const std::string& app_id,
180 const std::string& window_key) const {
181 AppWindow* result = NULL;
182 for (AppWindowList::const_iterator i = app_windows_.begin();
183 i != app_windows_.end();
185 if ((*i)->extension()->id() == app_id && (*i)->window_key() == window_key) {
187 if (result->GetBaseWindow()->IsActive())
194 bool AppWindowRegistry::HadDevToolsAttached(
195 content::RenderViewHost* render_view_host) const {
196 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host);
197 return key.empty() ? false : inspected_windows_.count(key) != 0;
201 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile(
202 gfx::NativeWindow window) {
203 std::vector<content::BrowserContext*> contexts =
204 AppsClient::Get()->GetLoadedBrowserContexts();
205 for (std::vector<content::BrowserContext*>::const_iterator i =
209 AppWindowRegistry* registry =
210 Factory::GetForBrowserContext(*i, false /* create */);
214 AppWindow* app_window = registry->GetAppWindowForNativeWindow(window);
223 bool AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(
224 int window_type_mask) {
225 std::vector<content::BrowserContext*> contexts =
226 AppsClient::Get()->GetLoadedBrowserContexts();
227 for (std::vector<content::BrowserContext*>::const_iterator i =
231 AppWindowRegistry* registry =
232 Factory::GetForBrowserContext(*i, false /* create */);
236 const AppWindowList& app_windows = registry->app_windows();
237 if (app_windows.empty())
240 if (window_type_mask == 0)
243 for (const_iterator j = app_windows.begin(); j != app_windows.end(); ++j) {
244 if ((*j)->window_type() & window_type_mask)
252 void AppWindowRegistry::OnDevToolsStateChanged(
253 content::DevToolsAgentHost* agent_host,
255 content::RenderViewHost* rvh = agent_host->GetRenderViewHost();
256 // Ignore unrelated notifications.
258 rvh->GetSiteInstance()->GetProcess()->GetBrowserContext() != context_)
261 std::string key = GetWindowKeyForRenderViewHost(this, rvh);
266 inspected_windows_.insert(key);
268 inspected_windows_.erase(key);
271 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) {
272 const AppWindowList::iterator it =
273 std::find(app_windows_.begin(), app_windows_.end(), app_window);
274 if (it != app_windows_.end())
276 app_windows_.push_back(app_window);
279 void AppWindowRegistry::BringToFront(AppWindow* app_window) {
280 const AppWindowList::iterator it =
281 std::find(app_windows_.begin(), app_windows_.end(), app_window);
282 if (it != app_windows_.end())
283 app_windows_.erase(it);
284 app_windows_.push_front(app_window);
287 ///////////////////////////////////////////////////////////////////////////////
288 // Factory boilerplate
291 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext(
292 content::BrowserContext* context,
294 return static_cast<AppWindowRegistry*>(
295 GetInstance()->GetServiceForBrowserContext(context, create));
298 AppWindowRegistry::Factory* AppWindowRegistry::Factory::GetInstance() {
299 return Singleton<AppWindowRegistry::Factory>::get();
302 AppWindowRegistry::Factory::Factory()
303 : BrowserContextKeyedServiceFactory(
305 BrowserContextDependencyManager::GetInstance()) {}
307 AppWindowRegistry::Factory::~Factory() {}
309 KeyedService* AppWindowRegistry::Factory::BuildServiceInstanceFor(
310 content::BrowserContext* context) const {
311 return new AppWindowRegistry(context);
314 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
318 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
322 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse(
323 content::BrowserContext* context) const {
324 return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext(
328 } // namespace extensions