OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/app_window/app_window_registry.h" | 5 #include "extensions/browser/app_window/app_window_registry.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/strings/stringprintf.h" |
10 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 11 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
11 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
12 #include "content/public/browser/devtools_agent_host.h" | 13 #include "content/public/browser/devtools_agent_host.h" |
13 #include "content/public/browser/render_process_host.h" | 14 #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" | 15 #include "content/public/browser/site_instance.h" |
16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
17 #include "extensions/browser/app_window/app_window.h" | 17 #include "extensions/browser/app_window/app_window.h" |
18 #include "extensions/browser/app_window/native_app_window.h" | 18 #include "extensions/browser/app_window/native_app_window.h" |
19 #include "extensions/browser/extensions_browser_client.h" | 19 #include "extensions/browser/extensions_browser_client.h" |
20 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 | 23 |
24 namespace { | |
25 | |
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); | |
36 if (!app_window) | |
37 return std::string(); // Not a AppWindow. | |
38 | |
39 if (app_window->window_key().empty()) | |
40 return app_window->web_contents()->GetURL().possibly_invalid_spec(); | |
41 | |
42 std::string key = app_window->extension_id(); | |
43 key += ':'; | |
44 key += app_window->window_key(); | |
45 return key; | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 void AppWindowRegistry::Observer::OnAppWindowAdded(AppWindow* app_window) { | 24 void AppWindowRegistry::Observer::OnAppWindowAdded(AppWindow* app_window) { |
51 } | 25 } |
52 | 26 |
53 void AppWindowRegistry::Observer::OnAppWindowIconChanged( | 27 void AppWindowRegistry::Observer::OnAppWindowIconChanged( |
54 AppWindow* app_window) { | 28 AppWindow* app_window) { |
55 } | 29 } |
56 | 30 |
57 void AppWindowRegistry::Observer::OnAppWindowRemoved(AppWindow* app_window) { | 31 void AppWindowRegistry::Observer::OnAppWindowRemoved(AppWindow* app_window) { |
58 } | 32 } |
59 | 33 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) { | 115 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) { |
142 const AppWindowList windows = GetAppWindowsForApp(app_id); | 116 const AppWindowList windows = GetAppWindowsForApp(app_id); |
143 for (AppWindowRegistry::const_iterator it = windows.begin(); | 117 for (AppWindowRegistry::const_iterator it = windows.begin(); |
144 it != windows.end(); | 118 it != windows.end(); |
145 ++it) { | 119 ++it) { |
146 (*it)->GetBaseWindow()->Close(); | 120 (*it)->GetBaseWindow()->Close(); |
147 } | 121 } |
148 } | 122 } |
149 | 123 |
150 AppWindow* AppWindowRegistry::GetAppWindowForWebContents( | 124 AppWindow* AppWindowRegistry::GetAppWindowForWebContents( |
151 content::WebContents* web_contents) const { | 125 const content::WebContents* web_contents) const { |
152 for (AppWindow* window : app_windows_) { | 126 for (AppWindow* window : app_windows_) { |
153 if (window->web_contents() == web_contents) | 127 if (window->web_contents() == web_contents) |
154 return window; | 128 return window; |
155 } | 129 } |
156 return nullptr; | 130 return nullptr; |
157 } | 131 } |
158 | 132 |
159 AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost( | |
160 content::RenderViewHost* render_view_host) const { | |
161 return GetAppWindowForWebContents( | |
162 content::WebContents::FromRenderViewHost(render_view_host)); | |
163 } | |
164 | |
165 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow( | 133 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow( |
166 gfx::NativeWindow window) const { | 134 gfx::NativeWindow window) const { |
167 for (AppWindowList::const_iterator i = app_windows_.begin(); | 135 for (AppWindowList::const_iterator i = app_windows_.begin(); |
168 i != app_windows_.end(); | 136 i != app_windows_.end(); |
169 ++i) { | 137 ++i) { |
170 if ((*i)->GetNativeWindow() == window) | 138 if ((*i)->GetNativeWindow() == window) |
171 return *i; | 139 return *i; |
172 } | 140 } |
173 | 141 |
174 return NULL; | 142 return NULL; |
(...skipping 25 matching lines...) Expand all Loading... |
200 if ((*i)->extension_id() == app_id && (*i)->window_key() == window_key) { | 168 if ((*i)->extension_id() == app_id && (*i)->window_key() == window_key) { |
201 result = *i; | 169 result = *i; |
202 if (result->GetBaseWindow()->IsActive()) | 170 if (result->GetBaseWindow()->IsActive()) |
203 return result; | 171 return result; |
204 } | 172 } |
205 } | 173 } |
206 return result; | 174 return result; |
207 } | 175 } |
208 | 176 |
209 bool AppWindowRegistry::HadDevToolsAttached( | 177 bool AppWindowRegistry::HadDevToolsAttached( |
210 content::RenderViewHost* render_view_host) const { | 178 content::WebContents* web_contents) const { |
211 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host); | 179 std::string key = GetWindowKeyForWebContents(web_contents); |
212 return key.empty() ? false : inspected_windows_.count(key) != 0; | 180 return key.empty() ? false : inspected_windows_.count(key) != 0; |
213 } | 181 } |
214 | 182 |
215 void AppWindowRegistry::OnDevToolsStateChanged( | 183 void AppWindowRegistry::OnDevToolsStateChanged( |
216 content::DevToolsAgentHost* agent_host, | 184 content::DevToolsAgentHost* agent_host, |
217 bool attached) { | 185 bool attached) { |
218 content::WebContents* web_contents = agent_host->GetWebContents(); | 186 content::WebContents* web_contents = agent_host->GetWebContents(); |
219 // Ignore unrelated notifications. | 187 // Ignore unrelated notifications. |
220 if (!web_contents || web_contents->GetBrowserContext() != context_) | 188 if (!web_contents || web_contents->GetBrowserContext() != context_) |
221 return; | 189 return; |
222 | 190 |
223 std::string key = | 191 std::string key = GetWindowKeyForWebContents(web_contents); |
224 GetWindowKeyForRenderViewHost(this, web_contents->GetRenderViewHost()); | |
225 if (key.empty()) | 192 if (key.empty()) |
226 return; | 193 return; |
227 | 194 |
228 if (attached) | 195 if (attached) |
229 inspected_windows_.insert(key); | 196 inspected_windows_.insert(key); |
230 else | 197 else |
231 inspected_windows_.erase(key); | 198 inspected_windows_.erase(key); |
232 } | 199 } |
233 | 200 |
234 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) { | 201 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) { |
235 const AppWindowList::iterator it = | 202 const AppWindowList::iterator it = |
236 std::find(app_windows_.begin(), app_windows_.end(), app_window); | 203 std::find(app_windows_.begin(), app_windows_.end(), app_window); |
237 if (it != app_windows_.end()) | 204 if (it != app_windows_.end()) |
238 return; | 205 return; |
239 app_windows_.push_back(app_window); | 206 app_windows_.push_back(app_window); |
240 } | 207 } |
241 | 208 |
242 void AppWindowRegistry::BringToFront(AppWindow* app_window) { | 209 void AppWindowRegistry::BringToFront(AppWindow* app_window) { |
243 const AppWindowList::iterator it = | 210 const AppWindowList::iterator it = |
244 std::find(app_windows_.begin(), app_windows_.end(), app_window); | 211 std::find(app_windows_.begin(), app_windows_.end(), app_window); |
245 if (it != app_windows_.end()) | 212 if (it != app_windows_.end()) |
246 app_windows_.erase(it); | 213 app_windows_.erase(it); |
247 app_windows_.push_front(app_window); | 214 app_windows_.push_front(app_window); |
248 } | 215 } |
249 | 216 |
| 217 std::string AppWindowRegistry::GetWindowKeyForWebContents( |
| 218 content::WebContents* web_contents) const { |
| 219 AppWindow* app_window = GetAppWindowForWebContents(web_contents); |
| 220 if (!app_window) |
| 221 return std::string(); // Not an AppWindow. |
| 222 |
| 223 if (app_window->window_key().empty()) |
| 224 return web_contents->GetURL().possibly_invalid_spec(); |
| 225 |
| 226 return base::StringPrintf("%s:%s", app_window->extension_id().c_str(), |
| 227 app_window->window_key().c_str()); |
| 228 } |
| 229 |
250 /////////////////////////////////////////////////////////////////////////////// | 230 /////////////////////////////////////////////////////////////////////////////// |
251 // Factory boilerplate | 231 // Factory boilerplate |
252 | 232 |
253 // static | 233 // static |
254 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext( | 234 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext( |
255 content::BrowserContext* context, | 235 content::BrowserContext* context, |
256 bool create) { | 236 bool create) { |
257 return static_cast<AppWindowRegistry*>( | 237 return static_cast<AppWindowRegistry*>( |
258 GetInstance()->GetServiceForBrowserContext(context, create)); | 238 GetInstance()->GetServiceForBrowserContext(context, create)); |
259 } | 239 } |
(...skipping 21 matching lines...) Expand all Loading... |
281 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { | 261 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { |
282 return false; | 262 return false; |
283 } | 263 } |
284 | 264 |
285 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse( | 265 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse( |
286 content::BrowserContext* context) const { | 266 content::BrowserContext* context) const { |
287 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); | 267 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
288 } | 268 } |
289 | 269 |
290 } // namespace extensions | 270 } // namespace extensions |
OLD | NEW |