1 // Copyright 2013 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/apps_client.h"
6 #include "apps/shell_window.h"
7 #include "apps/shell_window_registry.h"
8 #include "apps/ui/native_app_window.h"
9 #include "chrome/browser/profiles/incognito_helpers.h"
10 #include "components/browser_context_keyed_service/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/devtools_manager.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/common/extension.h"
22 // Create a key that identifies a ShellWindow 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 ShellWindow'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 ShellWindow, return an empty string.
27 std::string GetWindowKeyForRenderViewHost(
28 const apps::ShellWindowRegistry* registry,
29 content::RenderViewHost* render_view_host) {
30 apps::ShellWindow* shell_window =
31 registry->GetShellWindowForRenderViewHost(render_view_host);
33 return std::string(); // Not a ShellWindow.
35 if (shell_window->window_key().empty())
36 return shell_window->web_contents()->GetURL().possibly_invalid_spec();
38 std::string key = shell_window->extension()->id();
40 key += shell_window->window_key();
48 ShellWindowRegistry::ShellWindowRegistry(content::BrowserContext* context)
50 devtools_callback_(base::Bind(
51 &ShellWindowRegistry::OnDevToolsStateChanged,
52 base::Unretained(this))) {
53 content::DevToolsManager::GetInstance()->AddAgentStateCallback(
57 ShellWindowRegistry::~ShellWindowRegistry() {
58 content::DevToolsManager::GetInstance()->RemoveAgentStateCallback(
63 ShellWindowRegistry* ShellWindowRegistry::Get(
64 content::BrowserContext* context) {
65 return Factory::GetForBrowserContext(context, true /* create */);
68 void ShellWindowRegistry::AddShellWindow(ShellWindow* shell_window) {
69 BringToFront(shell_window);
70 FOR_EACH_OBSERVER(Observer, observers_, OnShellWindowAdded(shell_window));
73 void ShellWindowRegistry::ShellWindowIconChanged(ShellWindow* shell_window) {
74 AddShellWindowToList(shell_window);
75 FOR_EACH_OBSERVER(Observer, observers_,
76 OnShellWindowIconChanged(shell_window));
79 void ShellWindowRegistry::ShellWindowActivated(ShellWindow* shell_window) {
80 BringToFront(shell_window);
83 void ShellWindowRegistry::RemoveShellWindow(ShellWindow* shell_window) {
84 const ShellWindowList::iterator it = std::find(shell_windows_.begin(),
87 if (it != shell_windows_.end())
88 shell_windows_.erase(it);
89 FOR_EACH_OBSERVER(Observer, observers_, OnShellWindowRemoved(shell_window));
92 void ShellWindowRegistry::AddObserver(Observer* observer) {
93 observers_.AddObserver(observer);
96 void ShellWindowRegistry::RemoveObserver(Observer* observer) {
97 observers_.RemoveObserver(observer);
100 ShellWindowRegistry::ShellWindowList ShellWindowRegistry::GetShellWindowsForApp(
101 const std::string& app_id) const {
102 ShellWindowList app_windows;
103 for (ShellWindowList::const_iterator i = shell_windows_.begin();
104 i != shell_windows_.end(); ++i) {
105 if ((*i)->extension_id() == app_id)
106 app_windows.push_back(*i);
111 void ShellWindowRegistry::CloseAllShellWindowsForApp(
112 const std::string& app_id) {
113 for (ShellWindowList::const_iterator i = shell_windows_.begin();
114 i != shell_windows_.end(); ) {
115 ShellWindow* shell_window = *(i++);
116 if (shell_window->extension_id() == app_id)
117 shell_window->GetBaseWindow()->Close();
121 ShellWindow* ShellWindowRegistry::GetShellWindowForRenderViewHost(
122 content::RenderViewHost* render_view_host) const {
123 for (ShellWindowList::const_iterator i = shell_windows_.begin();
124 i != shell_windows_.end(); ++i) {
125 if ((*i)->web_contents()->GetRenderViewHost() == render_view_host)
132 ShellWindow* ShellWindowRegistry::GetShellWindowForNativeWindow(
133 gfx::NativeWindow window) const {
134 for (ShellWindowList::const_iterator i = shell_windows_.begin();
135 i != shell_windows_.end(); ++i) {
136 if ((*i)->GetNativeWindow() == window)
143 ShellWindow* ShellWindowRegistry::GetCurrentShellWindowForApp(
144 const std::string& app_id) const {
145 ShellWindow* result = NULL;
146 for (ShellWindowList::const_iterator i = shell_windows_.begin();
147 i != shell_windows_.end(); ++i) {
148 if ((*i)->extension()->id() == app_id) {
150 if (result->GetBaseWindow()->IsActive())
158 ShellWindow* ShellWindowRegistry::GetShellWindowForAppAndKey(
159 const std::string& app_id,
160 const std::string& window_key) const {
161 ShellWindow* result = NULL;
162 for (ShellWindowList::const_iterator i = shell_windows_.begin();
163 i != shell_windows_.end(); ++i) {
164 if ((*i)->extension()->id() == app_id && (*i)->window_key() == window_key) {
166 if (result->GetBaseWindow()->IsActive())
173 bool ShellWindowRegistry::HadDevToolsAttached(
174 content::RenderViewHost* render_view_host) const {
175 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host);
176 return key.empty() ? false : inspected_windows_.count(key) != 0;
180 ShellWindow* ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile(
181 gfx::NativeWindow window) {
182 std::vector<content::BrowserContext*> contexts =
183 AppsClient::Get()->GetLoadedBrowserContexts();
184 for (std::vector<content::BrowserContext*>::const_iterator i =
186 i != contexts.end(); ++i) {
187 ShellWindowRegistry* registry = Factory::GetForBrowserContext(
188 *i, false /* create */);
192 ShellWindow* shell_window = registry->GetShellWindowForNativeWindow(window);
201 bool ShellWindowRegistry::IsShellWindowRegisteredInAnyProfile(
202 int window_type_mask) {
203 std::vector<content::BrowserContext*> contexts =
204 AppsClient::Get()->GetLoadedBrowserContexts();
205 for (std::vector<content::BrowserContext*>::const_iterator i =
207 i != contexts.end(); ++i) {
208 ShellWindowRegistry* registry = Factory::GetForBrowserContext(
209 *i, false /* create */);
213 const ShellWindowList& shell_windows = registry->shell_windows();
214 if (shell_windows.empty())
217 if (window_type_mask == 0)
220 for (const_iterator j = shell_windows.begin(); j != shell_windows.end();
222 if ((*j)->window_type() & window_type_mask)
230 void ShellWindowRegistry::OnDevToolsStateChanged(
231 content::DevToolsAgentHost* agent_host, bool attached) {
232 content::RenderViewHost* rvh = agent_host->GetRenderViewHost();
233 // Ignore unrelated notifications.
235 rvh->GetSiteInstance()->GetProcess()->GetBrowserContext() != context_)
238 std::string key = GetWindowKeyForRenderViewHost(this, rvh);
243 inspected_windows_.insert(key);
245 inspected_windows_.erase(key);
248 void ShellWindowRegistry::AddShellWindowToList(ShellWindow* shell_window) {
249 const ShellWindowList::iterator it = std::find(shell_windows_.begin(),
250 shell_windows_.end(),
252 if (it != shell_windows_.end())
254 shell_windows_.push_back(shell_window);
257 void ShellWindowRegistry::BringToFront(ShellWindow* shell_window) {
258 const ShellWindowList::iterator it = std::find(shell_windows_.begin(),
259 shell_windows_.end(),
261 if (it != shell_windows_.end())
262 shell_windows_.erase(it);
263 shell_windows_.push_front(shell_window);
266 ///////////////////////////////////////////////////////////////////////////////
267 // Factory boilerplate
270 ShellWindowRegistry* ShellWindowRegistry::Factory::GetForBrowserContext(
271 content::BrowserContext* context, bool create) {
272 return static_cast<ShellWindowRegistry*>(
273 GetInstance()->GetServiceForBrowserContext(context, create));
276 ShellWindowRegistry::Factory* ShellWindowRegistry::Factory::GetInstance() {
277 return Singleton<ShellWindowRegistry::Factory>::get();
280 ShellWindowRegistry::Factory::Factory()
281 : BrowserContextKeyedServiceFactory(
282 "ShellWindowRegistry",
283 BrowserContextDependencyManager::GetInstance()) {
286 ShellWindowRegistry::Factory::~Factory() {
289 BrowserContextKeyedService*
290 ShellWindowRegistry::Factory::BuildServiceInstanceFor(
291 content::BrowserContext* context) const {
292 return new ShellWindowRegistry(context);
295 bool ShellWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const {
299 bool ShellWindowRegistry::Factory::ServiceIsNULLWhileTesting() const {
303 content::BrowserContext* ShellWindowRegistry::Factory::GetBrowserContextToUse(
304 content::BrowserContext* context) const {
305 return chrome::GetBrowserContextRedirectedInIncognito(context);
308 } // namespace extensions