OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "apps/app_window.h" |
| 6 #include "apps/app_window_registry.h" |
5 #include "apps/apps_client.h" | 7 #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" | 8 #include "apps/ui/native_app_window.h" |
9 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" | 9 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" |
10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
11 #include "content/public/browser/devtools_agent_host.h" | 11 #include "content/public/browser/devtools_agent_host.h" |
12 #include "content/public/browser/devtools_manager.h" | 12 #include "content/public/browser/devtools_manager.h" |
13 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/render_view_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/extensions_browser_client.h" | 17 #include "extensions/browser/extensions_browser_client.h" |
18 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Create a key that identifies a ShellWindow in a RenderViewHost across App | 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 | 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 | 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 | 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. | 26 // RenderViewHost is not for a AppWindow, return an empty string. |
27 std::string GetWindowKeyForRenderViewHost( | 27 std::string GetWindowKeyForRenderViewHost( |
28 const apps::ShellWindowRegistry* registry, | 28 const apps::AppWindowRegistry* registry, |
29 content::RenderViewHost* render_view_host) { | 29 content::RenderViewHost* render_view_host) { |
30 apps::ShellWindow* shell_window = | 30 apps::AppWindow* app_window = |
31 registry->GetShellWindowForRenderViewHost(render_view_host); | 31 registry->GetAppWindowForRenderViewHost(render_view_host); |
32 if (!shell_window) | 32 if (!app_window) |
33 return std::string(); // Not a ShellWindow. | 33 return std::string(); // Not a AppWindow. |
34 | 34 |
35 if (shell_window->window_key().empty()) | 35 if (app_window->window_key().empty()) |
36 return shell_window->web_contents()->GetURL().possibly_invalid_spec(); | 36 return app_window->web_contents()->GetURL().possibly_invalid_spec(); |
37 | 37 |
38 std::string key = shell_window->extension()->id(); | 38 std::string key = app_window->extension()->id(); |
39 key += ':'; | 39 key += ':'; |
40 key += shell_window->window_key(); | 40 key += app_window->window_key(); |
41 return key; | 41 return key; |
42 } | 42 } |
43 | 43 |
44 } // namespace | 44 } // namespace |
45 | 45 |
46 namespace apps { | 46 namespace apps { |
47 | 47 |
48 ShellWindowRegistry::ShellWindowRegistry(content::BrowserContext* context) | 48 AppWindowRegistry::AppWindowRegistry(content::BrowserContext* context) |
49 : context_(context), | 49 : context_(context), |
50 devtools_callback_(base::Bind( | 50 devtools_callback_(base::Bind(&AppWindowRegistry::OnDevToolsStateChanged, |
51 &ShellWindowRegistry::OnDevToolsStateChanged, | 51 base::Unretained(this))) { |
52 base::Unretained(this))) { | |
53 content::DevToolsManager::GetInstance()->AddAgentStateCallback( | 52 content::DevToolsManager::GetInstance()->AddAgentStateCallback( |
54 devtools_callback_); | 53 devtools_callback_); |
55 } | 54 } |
56 | 55 |
57 ShellWindowRegistry::~ShellWindowRegistry() { | 56 AppWindowRegistry::~AppWindowRegistry() { |
58 content::DevToolsManager::GetInstance()->RemoveAgentStateCallback( | 57 content::DevToolsManager::GetInstance()->RemoveAgentStateCallback( |
59 devtools_callback_); | 58 devtools_callback_); |
60 } | 59 } |
61 | 60 |
62 // static | 61 // static |
63 ShellWindowRegistry* ShellWindowRegistry::Get( | 62 AppWindowRegistry* AppWindowRegistry::Get(content::BrowserContext* context) { |
64 content::BrowserContext* context) { | |
65 return Factory::GetForBrowserContext(context, true /* create */); | 63 return Factory::GetForBrowserContext(context, true /* create */); |
66 } | 64 } |
67 | 65 |
68 void ShellWindowRegistry::AddShellWindow(ShellWindow* shell_window) { | 66 void AppWindowRegistry::AddAppWindow(AppWindow* app_window) { |
69 BringToFront(shell_window); | 67 BringToFront(app_window); |
70 FOR_EACH_OBSERVER(Observer, observers_, OnShellWindowAdded(shell_window)); | 68 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowAdded(app_window)); |
71 } | 69 } |
72 | 70 |
73 void ShellWindowRegistry::ShellWindowIconChanged(ShellWindow* shell_window) { | 71 void AppWindowRegistry::AppWindowIconChanged(AppWindow* app_window) { |
74 AddShellWindowToList(shell_window); | 72 AddAppWindowToList(app_window); |
75 FOR_EACH_OBSERVER(Observer, observers_, | 73 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowIconChanged(app_window)); |
76 OnShellWindowIconChanged(shell_window)); | |
77 } | 74 } |
78 | 75 |
79 void ShellWindowRegistry::ShellWindowActivated(ShellWindow* shell_window) { | 76 void AppWindowRegistry::AppWindowActivated(AppWindow* app_window) { |
80 BringToFront(shell_window); | 77 BringToFront(app_window); |
81 } | 78 } |
82 | 79 |
83 void ShellWindowRegistry::RemoveShellWindow(ShellWindow* shell_window) { | 80 void AppWindowRegistry::RemoveAppWindow(AppWindow* app_window) { |
84 const ShellWindowList::iterator it = std::find(shell_windows_.begin(), | 81 const AppWindowList::iterator it = |
85 shell_windows_.end(), | 82 std::find(app_windows_.begin(), app_windows_.end(), app_window); |
86 shell_window); | 83 if (it != app_windows_.end()) |
87 if (it != shell_windows_.end()) | 84 app_windows_.erase(it); |
88 shell_windows_.erase(it); | 85 FOR_EACH_OBSERVER(Observer, observers_, OnAppWindowRemoved(app_window)); |
89 FOR_EACH_OBSERVER(Observer, observers_, OnShellWindowRemoved(shell_window)); | |
90 } | 86 } |
91 | 87 |
92 void ShellWindowRegistry::AddObserver(Observer* observer) { | 88 void AppWindowRegistry::AddObserver(Observer* observer) { |
93 observers_.AddObserver(observer); | 89 observers_.AddObserver(observer); |
94 } | 90 } |
95 | 91 |
96 void ShellWindowRegistry::RemoveObserver(Observer* observer) { | 92 void AppWindowRegistry::RemoveObserver(Observer* observer) { |
97 observers_.RemoveObserver(observer); | 93 observers_.RemoveObserver(observer); |
98 } | 94 } |
99 | 95 |
100 ShellWindowRegistry::ShellWindowList ShellWindowRegistry::GetShellWindowsForApp( | 96 AppWindowRegistry::AppWindowList AppWindowRegistry::GetAppWindowsForApp( |
101 const std::string& app_id) const { | 97 const std::string& app_id) const { |
102 ShellWindowList app_windows; | 98 AppWindowList app_windows; |
103 for (ShellWindowList::const_iterator i = shell_windows_.begin(); | 99 for (AppWindowList::const_iterator i = app_windows_.begin(); |
104 i != shell_windows_.end(); ++i) { | 100 i != app_windows_.end(); |
| 101 ++i) { |
105 if ((*i)->extension_id() == app_id) | 102 if ((*i)->extension_id() == app_id) |
106 app_windows.push_back(*i); | 103 app_windows.push_back(*i); |
107 } | 104 } |
108 return app_windows; | 105 return app_windows; |
109 } | 106 } |
110 | 107 |
111 void ShellWindowRegistry::CloseAllShellWindowsForApp( | 108 void AppWindowRegistry::CloseAllAppWindowsForApp(const std::string& app_id) { |
112 const std::string& app_id) { | 109 for (AppWindowList::const_iterator i = app_windows_.begin(); |
113 for (ShellWindowList::const_iterator i = shell_windows_.begin(); | 110 i != app_windows_.end();) { |
114 i != shell_windows_.end(); ) { | 111 AppWindow* app_window = *(i++); |
115 ShellWindow* shell_window = *(i++); | 112 if (app_window->extension_id() == app_id) |
116 if (shell_window->extension_id() == app_id) | 113 app_window->GetBaseWindow()->Close(); |
117 shell_window->GetBaseWindow()->Close(); | |
118 } | 114 } |
119 } | 115 } |
120 | 116 |
121 ShellWindow* ShellWindowRegistry::GetShellWindowForRenderViewHost( | 117 AppWindow* AppWindowRegistry::GetAppWindowForRenderViewHost( |
122 content::RenderViewHost* render_view_host) const { | 118 content::RenderViewHost* render_view_host) const { |
123 for (ShellWindowList::const_iterator i = shell_windows_.begin(); | 119 for (AppWindowList::const_iterator i = app_windows_.begin(); |
124 i != shell_windows_.end(); ++i) { | 120 i != app_windows_.end(); |
| 121 ++i) { |
125 if ((*i)->web_contents()->GetRenderViewHost() == render_view_host) | 122 if ((*i)->web_contents()->GetRenderViewHost() == render_view_host) |
126 return *i; | 123 return *i; |
127 } | 124 } |
128 | 125 |
129 return NULL; | 126 return NULL; |
130 } | 127 } |
131 | 128 |
132 ShellWindow* ShellWindowRegistry::GetShellWindowForNativeWindow( | 129 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindow( |
133 gfx::NativeWindow window) const { | 130 gfx::NativeWindow window) const { |
134 for (ShellWindowList::const_iterator i = shell_windows_.begin(); | 131 for (AppWindowList::const_iterator i = app_windows_.begin(); |
135 i != shell_windows_.end(); ++i) { | 132 i != app_windows_.end(); |
| 133 ++i) { |
136 if ((*i)->GetNativeWindow() == window) | 134 if ((*i)->GetNativeWindow() == window) |
137 return *i; | 135 return *i; |
138 } | 136 } |
139 | 137 |
140 return NULL; | 138 return NULL; |
141 } | 139 } |
142 | 140 |
143 ShellWindow* ShellWindowRegistry::GetCurrentShellWindowForApp( | 141 AppWindow* AppWindowRegistry::GetCurrentAppWindowForApp( |
144 const std::string& app_id) const { | 142 const std::string& app_id) const { |
145 ShellWindow* result = NULL; | 143 AppWindow* result = NULL; |
146 for (ShellWindowList::const_iterator i = shell_windows_.begin(); | 144 for (AppWindowList::const_iterator i = app_windows_.begin(); |
147 i != shell_windows_.end(); ++i) { | 145 i != app_windows_.end(); |
| 146 ++i) { |
148 if ((*i)->extension()->id() == app_id) { | 147 if ((*i)->extension()->id() == app_id) { |
149 result = *i; | 148 result = *i; |
150 if (result->GetBaseWindow()->IsActive()) | 149 if (result->GetBaseWindow()->IsActive()) |
151 return result; | 150 return result; |
152 } | 151 } |
153 } | 152 } |
154 | 153 |
155 return result; | 154 return result; |
156 } | 155 } |
157 | 156 |
158 ShellWindow* ShellWindowRegistry::GetShellWindowForAppAndKey( | 157 AppWindow* AppWindowRegistry::GetAppWindowForAppAndKey( |
159 const std::string& app_id, | 158 const std::string& app_id, |
160 const std::string& window_key) const { | 159 const std::string& window_key) const { |
161 ShellWindow* result = NULL; | 160 AppWindow* result = NULL; |
162 for (ShellWindowList::const_iterator i = shell_windows_.begin(); | 161 for (AppWindowList::const_iterator i = app_windows_.begin(); |
163 i != shell_windows_.end(); ++i) { | 162 i != app_windows_.end(); |
| 163 ++i) { |
164 if ((*i)->extension()->id() == app_id && (*i)->window_key() == window_key) { | 164 if ((*i)->extension()->id() == app_id && (*i)->window_key() == window_key) { |
165 result = *i; | 165 result = *i; |
166 if (result->GetBaseWindow()->IsActive()) | 166 if (result->GetBaseWindow()->IsActive()) |
167 return result; | 167 return result; |
168 } | 168 } |
169 } | 169 } |
170 return result; | 170 return result; |
171 } | 171 } |
172 | 172 |
173 bool ShellWindowRegistry::HadDevToolsAttached( | 173 bool AppWindowRegistry::HadDevToolsAttached( |
174 content::RenderViewHost* render_view_host) const { | 174 content::RenderViewHost* render_view_host) const { |
175 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host); | 175 std::string key = GetWindowKeyForRenderViewHost(this, render_view_host); |
176 return key.empty() ? false : inspected_windows_.count(key) != 0; | 176 return key.empty() ? false : inspected_windows_.count(key) != 0; |
177 } | 177 } |
178 | 178 |
179 // static | 179 // static |
180 ShellWindow* ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( | 180 AppWindow* AppWindowRegistry::GetAppWindowForNativeWindowAnyProfile( |
181 gfx::NativeWindow window) { | 181 gfx::NativeWindow window) { |
182 std::vector<content::BrowserContext*> contexts = | 182 std::vector<content::BrowserContext*> contexts = |
183 AppsClient::Get()->GetLoadedBrowserContexts(); | 183 AppsClient::Get()->GetLoadedBrowserContexts(); |
184 for (std::vector<content::BrowserContext*>::const_iterator i = | 184 for (std::vector<content::BrowserContext*>::const_iterator i = |
185 contexts.begin(); | 185 contexts.begin(); |
186 i != contexts.end(); ++i) { | 186 i != contexts.end(); |
187 ShellWindowRegistry* registry = Factory::GetForBrowserContext( | 187 ++i) { |
188 *i, false /* create */); | 188 AppWindowRegistry* registry = |
| 189 Factory::GetForBrowserContext(*i, false /* create */); |
189 if (!registry) | 190 if (!registry) |
190 continue; | 191 continue; |
191 | 192 |
192 ShellWindow* shell_window = registry->GetShellWindowForNativeWindow(window); | 193 AppWindow* app_window = registry->GetAppWindowForNativeWindow(window); |
193 if (shell_window) | 194 if (app_window) |
194 return shell_window; | 195 return app_window; |
195 } | 196 } |
196 | 197 |
197 return NULL; | 198 return NULL; |
198 } | 199 } |
199 | 200 |
200 // static | 201 // static |
201 bool ShellWindowRegistry::IsShellWindowRegisteredInAnyProfile( | 202 bool AppWindowRegistry::IsAppWindowRegisteredInAnyProfile( |
202 int window_type_mask) { | 203 int window_type_mask) { |
203 std::vector<content::BrowserContext*> contexts = | 204 std::vector<content::BrowserContext*> contexts = |
204 AppsClient::Get()->GetLoadedBrowserContexts(); | 205 AppsClient::Get()->GetLoadedBrowserContexts(); |
205 for (std::vector<content::BrowserContext*>::const_iterator i = | 206 for (std::vector<content::BrowserContext*>::const_iterator i = |
206 contexts.begin(); | 207 contexts.begin(); |
207 i != contexts.end(); ++i) { | 208 i != contexts.end(); |
208 ShellWindowRegistry* registry = Factory::GetForBrowserContext( | 209 ++i) { |
209 *i, false /* create */); | 210 AppWindowRegistry* registry = |
| 211 Factory::GetForBrowserContext(*i, false /* create */); |
210 if (!registry) | 212 if (!registry) |
211 continue; | 213 continue; |
212 | 214 |
213 const ShellWindowList& shell_windows = registry->shell_windows(); | 215 const AppWindowList& app_windows = registry->app_windows(); |
214 if (shell_windows.empty()) | 216 if (app_windows.empty()) |
215 continue; | 217 continue; |
216 | 218 |
217 if (window_type_mask == 0) | 219 if (window_type_mask == 0) |
218 return true; | 220 return true; |
219 | 221 |
220 for (const_iterator j = shell_windows.begin(); j != shell_windows.end(); | 222 for (const_iterator j = app_windows.begin(); j != app_windows.end(); ++j) { |
221 ++j) { | |
222 if ((*j)->window_type() & window_type_mask) | 223 if ((*j)->window_type() & window_type_mask) |
223 return true; | 224 return true; |
224 } | 225 } |
225 } | 226 } |
226 | 227 |
227 return false; | 228 return false; |
228 } | 229 } |
229 | 230 |
230 void ShellWindowRegistry::OnDevToolsStateChanged( | 231 void AppWindowRegistry::OnDevToolsStateChanged( |
231 content::DevToolsAgentHost* agent_host, bool attached) { | 232 content::DevToolsAgentHost* agent_host, |
| 233 bool attached) { |
232 content::RenderViewHost* rvh = agent_host->GetRenderViewHost(); | 234 content::RenderViewHost* rvh = agent_host->GetRenderViewHost(); |
233 // Ignore unrelated notifications. | 235 // Ignore unrelated notifications. |
234 if (!rvh || | 236 if (!rvh || |
235 rvh->GetSiteInstance()->GetProcess()->GetBrowserContext() != context_) | 237 rvh->GetSiteInstance()->GetProcess()->GetBrowserContext() != context_) |
236 return; | 238 return; |
237 | 239 |
238 std::string key = GetWindowKeyForRenderViewHost(this, rvh); | 240 std::string key = GetWindowKeyForRenderViewHost(this, rvh); |
239 if (key.empty()) | 241 if (key.empty()) |
240 return; | 242 return; |
241 | 243 |
242 if (attached) | 244 if (attached) |
243 inspected_windows_.insert(key); | 245 inspected_windows_.insert(key); |
244 else | 246 else |
245 inspected_windows_.erase(key); | 247 inspected_windows_.erase(key); |
246 } | 248 } |
247 | 249 |
248 void ShellWindowRegistry::AddShellWindowToList(ShellWindow* shell_window) { | 250 void AppWindowRegistry::AddAppWindowToList(AppWindow* app_window) { |
249 const ShellWindowList::iterator it = std::find(shell_windows_.begin(), | 251 const AppWindowList::iterator it = |
250 shell_windows_.end(), | 252 std::find(app_windows_.begin(), app_windows_.end(), app_window); |
251 shell_window); | 253 if (it != app_windows_.end()) |
252 if (it != shell_windows_.end()) | |
253 return; | 254 return; |
254 shell_windows_.push_back(shell_window); | 255 app_windows_.push_back(app_window); |
255 } | 256 } |
256 | 257 |
257 void ShellWindowRegistry::BringToFront(ShellWindow* shell_window) { | 258 void AppWindowRegistry::BringToFront(AppWindow* app_window) { |
258 const ShellWindowList::iterator it = std::find(shell_windows_.begin(), | 259 const AppWindowList::iterator it = |
259 shell_windows_.end(), | 260 std::find(app_windows_.begin(), app_windows_.end(), app_window); |
260 shell_window); | 261 if (it != app_windows_.end()) |
261 if (it != shell_windows_.end()) | 262 app_windows_.erase(it); |
262 shell_windows_.erase(it); | 263 app_windows_.push_front(app_window); |
263 shell_windows_.push_front(shell_window); | |
264 } | 264 } |
265 | 265 |
266 /////////////////////////////////////////////////////////////////////////////// | 266 /////////////////////////////////////////////////////////////////////////////// |
267 // Factory boilerplate | 267 // Factory boilerplate |
268 | 268 |
269 // static | 269 // static |
270 ShellWindowRegistry* ShellWindowRegistry::Factory::GetForBrowserContext( | 270 AppWindowRegistry* AppWindowRegistry::Factory::GetForBrowserContext( |
271 content::BrowserContext* context, bool create) { | 271 content::BrowserContext* context, |
272 return static_cast<ShellWindowRegistry*>( | 272 bool create) { |
| 273 return static_cast<AppWindowRegistry*>( |
273 GetInstance()->GetServiceForBrowserContext(context, create)); | 274 GetInstance()->GetServiceForBrowserContext(context, create)); |
274 } | 275 } |
275 | 276 |
276 ShellWindowRegistry::Factory* ShellWindowRegistry::Factory::GetInstance() { | 277 AppWindowRegistry::Factory* AppWindowRegistry::Factory::GetInstance() { |
277 return Singleton<ShellWindowRegistry::Factory>::get(); | 278 return Singleton<AppWindowRegistry::Factory>::get(); |
278 } | 279 } |
279 | 280 |
280 ShellWindowRegistry::Factory::Factory() | 281 AppWindowRegistry::Factory::Factory() |
281 : BrowserContextKeyedServiceFactory( | 282 : BrowserContextKeyedServiceFactory( |
282 "ShellWindowRegistry", | 283 "AppWindowRegistry", |
283 BrowserContextDependencyManager::GetInstance()) { | 284 BrowserContextDependencyManager::GetInstance()) {} |
| 285 |
| 286 AppWindowRegistry::Factory::~Factory() {} |
| 287 |
| 288 BrowserContextKeyedService* AppWindowRegistry::Factory::BuildServiceInstanceFor( |
| 289 content::BrowserContext* context) const { |
| 290 return new AppWindowRegistry(context); |
284 } | 291 } |
285 | 292 |
286 ShellWindowRegistry::Factory::~Factory() { | 293 bool AppWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const { |
287 } | |
288 | |
289 BrowserContextKeyedService* | |
290 ShellWindowRegistry::Factory::BuildServiceInstanceFor( | |
291 content::BrowserContext* context) const { | |
292 return new ShellWindowRegistry(context); | |
293 } | |
294 | |
295 bool ShellWindowRegistry::Factory::ServiceIsCreatedWithBrowserContext() const { | |
296 return true; | 294 return true; |
297 } | 295 } |
298 | 296 |
299 bool ShellWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { | 297 bool AppWindowRegistry::Factory::ServiceIsNULLWhileTesting() const { |
300 return false; | 298 return false; |
301 } | 299 } |
302 | 300 |
303 content::BrowserContext* ShellWindowRegistry::Factory::GetBrowserContextToUse( | 301 content::BrowserContext* AppWindowRegistry::Factory::GetBrowserContextToUse( |
304 content::BrowserContext* context) const { | 302 content::BrowserContext* context) const { |
305 return extensions::ExtensionsBrowserClient::Get()-> | 303 return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext( |
306 GetOriginalContext(context); | 304 context); |
307 } | 305 } |
308 | 306 |
309 } // namespace extensions | 307 } // namespace extensions |
OLD | NEW |