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) {
66 AppWindowRegistry::Observer::~Observer() {
69 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context)
71 devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged,
72 base::Unretained(this))) {
73 content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_);
76 AppWindowRegistry::~AppWindowRegistry() {
77 content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_);
81 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) {
82 return Factory::GetForBrowserContext(context, true /* create */);
85 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) {
86 BringToFront(app_window);
87 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window));
90 void AppWindowRegistry::AppWindowIconChanged(AppWindow* app_window) {
91 AddAppWindowToList(app_window);
92 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowIconChanged(app_window));
95 void AppWindowRegistry::AppWindowActivated(AppWindow* app_window) {
96 BringToFront(app_window);
99 void AppWindowRegistry::AppWindowHidden(AppWindow* app_window) {
100 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowHidden(app_window));
103 void AppWindowRegistry::AppWindowShown(AppWindow* app_window) {
104 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowShown(app_window));
107 void AppWindowRegistry::RemoveAppWindow(AppWindow* app_window) {
108 const AppWindowList::iterator it =
109 std::find(app_windows_.begin(), app_windows_.end(), app_window);
110 if (it != app_windows_.end())
111 app_windows_.erase(it);
112 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowRemoved(app_window));
115 void AppWindowRegistry::AddObserver(Observer* observer) {
116 observers_.AddObserver(observer);
119 void AppWindowRegistry::RemoveObserver(Observer* observer) {
120 observers_.RemoveObserver(observer);
123 AppWindowRegistry::AppWindowList AppWindowRegistry::GetAppWindowsForApp(
124 const std::string& app_id) const {
125 AppWindowList app_windows;
126 for (AppWindowList::const_iterator i = app_windows_.begin();
127 i != app_windows_.end();
129 if ((*i)->extension_id() == app_id)
130 app_windows.push_back(*i);
135 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) {
136 const AppWindowList windows = GetAppWindowsForApp(app_id);
137 for (AppWindowRegistry::const_iterator it = windows.begin();
140 (*it)->GetBaseWindow()->Close();
144 AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost(
145 content::RenderViewHost* render_view_host) const {
146 for (AppWindowList::const_iterator i = app_windows_.begin();
147 i != app_windows_.end();
149 if ((*i)->web_contents()->GetRenderViewHost() == render_view_host)
156 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow(
157 gfx::NativeWindow window) const {
158 for (AppWindowList::const_iterator i = app_windows_.begin();
159 i != app_windows_.end();
161 if ((*i)->GetNativeWindow() == window)
168 AppWindow* AppWindowRegistry::GetCurrentAppWindowForApp(
169 const std::string& app_id) const {
170 AppWindow* result = NULL;
171 for (AppWindowList::const_iterator i = app_windows_.begin();
172 i != app_windows_.end();
174 if ((*i)->extension_id() == app_id) {
176 if (result->GetBaseWindow()->IsActive())
184 AppWindow* AppWindowRegistry::GetAppWindowForAppAndKey(
185 const std::string& app_id,
186 const std::string& window_key) const {
187 AppWindow* result = NULL;
188 for (AppWindowList::const_iterator i = app_windows_.begin();
189 i != app_windows_.end();
191 if ((*i)->extension_id() == app_id && (*i)->window_key() == window_key) {
193 if (result->GetBaseWindow()->IsActive())
200 bool AppWindowRegistry::HadDevToolsAttached(
201 content::RenderViewHost* render_view_host) const {
202 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host);
203 return key.empty() ? false : inspected_windows_.count(key) != 0;
206 void AppWindowRegistry::OnDevToolsStateChanged(
207 content::DevToolsAgentHost* agent_host,
209 content::WebContents* web_contents = agent_host->GetWebContents();
210 // Ignore unrelated notifications.
211 if (!web_contents || web_contents->GetBrowserContext() != context_)
215 GetWindowKeyForRenderViewHost(this, web_contents->GetRenderViewHost());
220 inspected_windows_.insert(key);
222 inspected_windows_.erase(key);
225 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) {
226 const AppWindowList::iterator it =
227 std::find(app_windows_.begin(), app_windows_.end(), app_window);
228 if (it != app_windows_.end())
230 app_windows_.push_back(app_window);
233 void AppWindowRegistry::BringToFront(AppWindow* app_window) {
234 const AppWindowList::iterator it =
235 std::find(app_windows_.begin(), app_windows_.end(), app_window);
236 if (it != app_windows_.end())
237 app_windows_.erase(it);
238 app_windows_.push_front(app_window);
241 ///////////////////////////////////////////////////////////////////////////////
242 // Factory boilerplate
245 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext(
246 content::BrowserContext* context,
248 return static_cast<AppWindowRegistry*>(
249 GetInstance()->GetServiceForBrowserContext(context, create));
252 AppWindowRegistry::Factory* AppWindowRegistry::Factory::GetInstance() {
253 return Singleton<AppWindowRegistry::Factory>::get();
256 AppWindowRegistry::Factory::Factory()
257 : BrowserContextKeyedServiceFactory(
259 BrowserContextDependencyManager::GetInstance()) {}
261 AppWindowRegistry::Factory::~Factory() {}
263 KeyedService* AppWindowRegistry::Factory::BuildServiceInstanceFor(
264 content::BrowserContext* context) const {
265 return new AppWindowRegistry(context);
268 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
272 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
276 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse(
277 content::BrowserContext* context) const {
278 return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
281 } // namespace extensions