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/extension_host.h" | 5 #include "extensions/browser/extension_host.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/chrome_notification_types.h" | |
18 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/content_browser_client.h" | 18 #include "content/public/browser/content_browser_client.h" |
20 #include "content/public/browser/native_web_keyboard_event.h" | 19 #include "content/public/browser/native_web_keyboard_event.h" |
21 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
23 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
24 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/browser/render_widget_host_view.h" | 25 #include "content/public/browser/render_widget_host_view.h" |
27 #include "content/public/browser/site_instance.h" | 26 #include "content/public/browser/site_instance.h" |
28 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
29 #include "extensions/browser/event_router.h" | 28 #include "extensions/browser/event_router.h" |
30 #include "extensions/browser/extension_error.h" | 29 #include "extensions/browser/extension_error.h" |
31 #include "extensions/browser/extension_host_delegate.h" | 30 #include "extensions/browser/extension_host_delegate.h" |
32 #include "extensions/browser/extension_system.h" | 31 #include "extensions/browser/extension_system.h" |
33 #include "extensions/browser/extensions_browser_client.h" | 32 #include "extensions/browser/extensions_browser_client.h" |
| 33 #include "extensions/browser/notification_types.h" |
34 #include "extensions/browser/process_manager.h" | 34 #include "extensions/browser/process_manager.h" |
35 #include "extensions/browser/runtime_data.h" | 35 #include "extensions/browser/runtime_data.h" |
36 #include "extensions/browser/view_type_utils.h" | 36 #include "extensions/browser/view_type_utils.h" |
37 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
38 #include "extensions/common/extension_messages.h" | 38 #include "extensions/common/extension_messages.h" |
39 #include "extensions/common/extension_urls.h" | 39 #include "extensions/common/extension_urls.h" |
40 #include "extensions/common/feature_switch.h" | 40 #include "extensions/common/feature_switch.h" |
41 #include "extensions/common/manifest_handlers/background_info.h" | 41 #include "extensions/common/manifest_handlers/background_info.h" |
42 #include "ui/base/l10n/l10n_util.h" | 42 #include "ui/base/l10n/l10n_util.h" |
43 #include "ui/base/window_open_disposition.h" | 43 #include "ui/base/window_open_disposition.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 host_contents_.reset(WebContents::Create( | 133 host_contents_.reset(WebContents::Create( |
134 WebContents::CreateParams(browser_context_, site_instance))), | 134 WebContents::CreateParams(browser_context_, site_instance))), |
135 content::WebContentsObserver::Observe(host_contents_.get()); | 135 content::WebContentsObserver::Observe(host_contents_.get()); |
136 host_contents_->SetDelegate(this); | 136 host_contents_->SetDelegate(this); |
137 SetViewType(host_contents_.get(), host_type); | 137 SetViewType(host_contents_.get(), host_type); |
138 | 138 |
139 render_view_host_ = host_contents_->GetRenderViewHost(); | 139 render_view_host_ = host_contents_->GetRenderViewHost(); |
140 | 140 |
141 // Listen for when an extension is unloaded from the same profile, as it may | 141 // Listen for when an extension is unloaded from the same profile, as it may |
142 // be the same extension that this points to. | 142 // be the same extension that this points to. |
143 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 143 registrar_.Add(this, |
| 144 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
144 content::Source<BrowserContext>(browser_context_)); | 145 content::Source<BrowserContext>(browser_context_)); |
145 | 146 |
146 // Set up web contents observers and pref observers. | 147 // Set up web contents observers and pref observers. |
147 delegate_->OnExtensionHostCreated(host_contents()); | 148 delegate_->OnExtensionHostCreated(host_contents()); |
148 } | 149 } |
149 | 150 |
150 ExtensionHost::~ExtensionHost() { | 151 ExtensionHost::~ExtensionHost() { |
151 if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && | 152 if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && |
152 extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) { | 153 extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) { |
153 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime", | 154 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime", |
154 since_created_.Elapsed()); | 155 since_created_.Elapsed()); |
155 } | 156 } |
156 content::NotificationService::current()->Notify( | 157 content::NotificationService::current()->Notify( |
157 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 158 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
158 content::Source<BrowserContext>(browser_context_), | 159 content::Source<BrowserContext>(browser_context_), |
159 content::Details<ExtensionHost>(this)); | 160 content::Details<ExtensionHost>(this)); |
160 ProcessCreationQueue::GetInstance()->Remove(this); | 161 ProcessCreationQueue::GetInstance()->Remove(this); |
161 } | 162 } |
162 | 163 |
163 content::RenderProcessHost* ExtensionHost::render_process_host() const { | 164 content::RenderProcessHost* ExtensionHost::render_process_host() const { |
164 return render_view_host()->GetProcess(); | 165 return render_view_host()->GetProcess(); |
165 } | 166 } |
166 | 167 |
167 RenderViewHost* ExtensionHost::render_view_host() const { | 168 RenderViewHost* ExtensionHost::render_view_host() const { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 std::string()); | 204 std::string()); |
204 } | 205 } |
205 | 206 |
206 bool ExtensionHost::IsBackgroundPage() const { | 207 bool ExtensionHost::IsBackgroundPage() const { |
207 DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 208 DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
208 return true; | 209 return true; |
209 } | 210 } |
210 | 211 |
211 void ExtensionHost::Close() { | 212 void ExtensionHost::Close() { |
212 content::NotificationService::current()->Notify( | 213 content::NotificationService::current()->Notify( |
213 chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 214 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
214 content::Source<BrowserContext>(browser_context_), | 215 content::Source<BrowserContext>(browser_context_), |
215 content::Details<ExtensionHost>(this)); | 216 content::Details<ExtensionHost>(this)); |
216 } | 217 } |
217 | 218 |
218 void ExtensionHost::Observe(int type, | 219 void ExtensionHost::Observe(int type, |
219 const content::NotificationSource& source, | 220 const content::NotificationSource& source, |
220 const content::NotificationDetails& details) { | 221 const content::NotificationDetails& details) { |
221 switch (type) { | 222 switch (type) { |
222 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: | 223 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: |
223 // The extension object will be deleted after this notification has been | 224 // The extension object will be deleted after this notification has been |
224 // sent. NULL it out so that dirty pointer issues don't arise in cases | 225 // sent. NULL it out so that dirty pointer issues don't arise in cases |
225 // when multiple ExtensionHost objects pointing to the same Extension are | 226 // when multiple ExtensionHost objects pointing to the same Extension are |
226 // present. | 227 // present. |
227 if (extension_ == content::Details<UnloadedExtensionInfo>(details)-> | 228 if (extension_ == content::Details<UnloadedExtensionInfo>(details)-> |
228 extension) { | 229 extension) { |
229 extension_ = NULL; | 230 extension_ = NULL; |
230 } | 231 } |
231 break; | 232 break; |
232 default: | 233 default: |
(...skipping 16 matching lines...) Expand all Loading... |
249 // is unloaded, and any other host that pointed to that extension will have | 250 // is unloaded, and any other host that pointed to that extension will have |
250 // its pointer to it NULLed out so that any attempt to unload a dirty pointer | 251 // its pointer to it NULLed out so that any attempt to unload a dirty pointer |
251 // will be averted. | 252 // will be averted. |
252 if (!extension_) | 253 if (!extension_) |
253 return; | 254 return; |
254 | 255 |
255 // TODO(aa): This is suspicious. There can be multiple views in an extension, | 256 // TODO(aa): This is suspicious. There can be multiple views in an extension, |
256 // and they aren't all going to use ExtensionHost. This should be in someplace | 257 // and they aren't all going to use ExtensionHost. This should be in someplace |
257 // more central, like EPM maybe. | 258 // more central, like EPM maybe. |
258 content::NotificationService::current()->Notify( | 259 content::NotificationService::current()->Notify( |
259 chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | 260 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, |
260 content::Source<BrowserContext>(browser_context_), | 261 content::Source<BrowserContext>(browser_context_), |
261 content::Details<ExtensionHost>(this)); | 262 content::Details<ExtensionHost>(this)); |
262 } | 263 } |
263 | 264 |
264 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { | 265 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { |
265 bool notify = !did_stop_loading_; | 266 bool notify = !did_stop_loading_; |
266 did_stop_loading_ = true; | 267 did_stop_loading_ = true; |
267 OnDidStopLoading(); | 268 OnDidStopLoading(); |
268 if (notify) { | 269 if (notify) { |
269 if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 270 if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
(...skipping 11 matching lines...) Expand all Loading... |
281 UMA_HISTOGRAM_TIMES("Extensions.PopupLoadTime", | 282 UMA_HISTOGRAM_TIMES("Extensions.PopupLoadTime", |
282 since_created_.Elapsed()); | 283 since_created_.Elapsed()); |
283 } else if (extension_host_type_ == VIEW_TYPE_EXTENSION_INFOBAR) { | 284 } else if (extension_host_type_ == VIEW_TYPE_EXTENSION_INFOBAR) { |
284 UMA_HISTOGRAM_TIMES("Extensions.InfobarLoadTime", | 285 UMA_HISTOGRAM_TIMES("Extensions.InfobarLoadTime", |
285 since_created_.Elapsed()); | 286 since_created_.Elapsed()); |
286 } | 287 } |
287 | 288 |
288 // Send the notification last, because it might result in this being | 289 // Send the notification last, because it might result in this being |
289 // deleted. | 290 // deleted. |
290 content::NotificationService::current()->Notify( | 291 content::NotificationService::current()->Notify( |
291 chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 292 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
292 content::Source<BrowserContext>(browser_context_), | 293 content::Source<BrowserContext>(browser_context_), |
293 content::Details<ExtensionHost>(this)); | 294 content::Details<ExtensionHost>(this)); |
294 } | 295 } |
295 } | 296 } |
296 | 297 |
297 void ExtensionHost::OnDidStopLoading() { | 298 void ExtensionHost::OnDidStopLoading() { |
298 DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 299 DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
299 // Nothing to do for background pages. | 300 // Nothing to do for background pages. |
300 } | 301 } |
301 | 302 |
302 void ExtensionHost::DocumentAvailableInMainFrame() { | 303 void ExtensionHost::DocumentAvailableInMainFrame() { |
303 // If the document has already been marked as available for this host, then | 304 // If the document has already been marked as available for this host, then |
304 // bail. No need for the redundant setup. http://crbug.com/31170 | 305 // bail. No need for the redundant setup. http://crbug.com/31170 |
305 if (document_element_available_) | 306 if (document_element_available_) |
306 return; | 307 return; |
307 document_element_available_ = true; | 308 document_element_available_ = true; |
308 OnDocumentAvailable(); | 309 OnDocumentAvailable(); |
309 } | 310 } |
310 | 311 |
311 void ExtensionHost::OnDocumentAvailable() { | 312 void ExtensionHost::OnDocumentAvailable() { |
312 DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 313 DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
313 ExtensionSystem::Get(browser_context_) | 314 ExtensionSystem::Get(browser_context_) |
314 ->runtime_data() | 315 ->runtime_data() |
315 ->SetBackgroundPageReady(extension_, true); | 316 ->SetBackgroundPageReady(extension_, true); |
316 content::NotificationService::current()->Notify( | 317 content::NotificationService::current()->Notify( |
317 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, | 318 extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, |
318 content::Source<const Extension>(extension_), | 319 content::Source<const Extension>(extension_), |
319 content::NotificationService::NoDetails()); | 320 content::NotificationService::NoDetails()); |
320 } | 321 } |
321 | 322 |
322 void ExtensionHost::CloseContents(WebContents* contents) { | 323 void ExtensionHost::CloseContents(WebContents* contents) { |
323 Close(); | 324 Close(); |
324 } | 325 } |
325 | 326 |
326 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { | 327 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { |
327 bool handled = true; | 328 bool handled = true; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 } | 408 } |
408 } | 409 } |
409 } | 410 } |
410 | 411 |
411 delegate_->CreateTab( | 412 delegate_->CreateTab( |
412 new_contents, extension_id_, disposition, initial_pos, user_gesture); | 413 new_contents, extension_id_, disposition, initial_pos, user_gesture); |
413 } | 414 } |
414 | 415 |
415 void ExtensionHost::RenderViewReady() { | 416 void ExtensionHost::RenderViewReady() { |
416 content::NotificationService::current()->Notify( | 417 content::NotificationService::current()->Notify( |
417 chrome::NOTIFICATION_EXTENSION_HOST_CREATED, | 418 extensions::NOTIFICATION_EXTENSION_HOST_CREATED, |
418 content::Source<BrowserContext>(browser_context_), | 419 content::Source<BrowserContext>(browser_context_), |
419 content::Details<ExtensionHost>(this)); | 420 content::Details<ExtensionHost>(this)); |
420 } | 421 } |
421 | 422 |
422 void ExtensionHost::RequestMediaAccessPermission( | 423 void ExtensionHost::RequestMediaAccessPermission( |
423 content::WebContents* web_contents, | 424 content::WebContents* web_contents, |
424 const content::MediaStreamRequest& request, | 425 const content::MediaStreamRequest& request, |
425 const content::MediaResponseCallback& callback) { | 426 const content::MediaResponseCallback& callback) { |
426 delegate_->ProcessMediaAccessRequest( | 427 delegate_->ProcessMediaAccessRequest( |
427 web_contents, request, callback, extension()); | 428 web_contents, request, callback, extension()); |
428 } | 429 } |
429 | 430 |
430 bool ExtensionHost::IsNeverVisible(content::WebContents* web_contents) { | 431 bool ExtensionHost::IsNeverVisible(content::WebContents* web_contents) { |
431 ViewType view_type = extensions::GetViewType(web_contents); | 432 ViewType view_type = extensions::GetViewType(web_contents); |
432 return view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; | 433 return view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; |
433 } | 434 } |
434 | 435 |
435 } // namespace extensions | 436 } // namespace extensions |
OLD | NEW |