Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_host.h" | 5 #include "chrome/browser/extensions/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/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 bool pending_create_; | 121 bool pending_create_; |
| 122 base::WeakPtrFactory<ProcessCreationQueue> ptr_factory_; | 122 base::WeakPtrFactory<ProcessCreationQueue> ptr_factory_; |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 //////////////// | 125 //////////////// |
| 126 // ExtensionHost | 126 // ExtensionHost |
| 127 | 127 |
| 128 ExtensionHost::ExtensionHost(const Extension* extension, | 128 ExtensionHost::ExtensionHost(const Extension* extension, |
| 129 SiteInstance* site_instance, | 129 SiteInstance* site_instance, |
| 130 const GURL& url, | 130 const GURL& url, |
| 131 chrome::ViewType host_type) | 131 extensions::ViewType host_type) |
|
Yoyo Zhou
2013/04/01 17:36:51
''
| |
| 132 : extension_(extension), | 132 : extension_(extension), |
| 133 extension_id_(extension->id()), | 133 extension_id_(extension->id()), |
| 134 profile_(Profile::FromBrowserContext( | 134 profile_(Profile::FromBrowserContext( |
| 135 site_instance->GetBrowserContext())), | 135 site_instance->GetBrowserContext())), |
| 136 render_view_host_(NULL), | 136 render_view_host_(NULL), |
| 137 did_stop_loading_(false), | 137 did_stop_loading_(false), |
| 138 document_element_available_(false), | 138 document_element_available_(false), |
| 139 initial_url_(url), | 139 initial_url_(url), |
| 140 ALLOW_THIS_IN_INITIALIZER_LIST( | 140 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 141 extension_function_dispatcher_(profile_, this)), | 141 extension_function_dispatcher_(profile_, this)), |
| 142 extension_host_type_(host_type), | 142 extension_host_type_(host_type), |
| 143 associated_web_contents_(NULL) { | 143 associated_web_contents_(NULL) { |
| 144 host_contents_.reset(WebContents::Create( | 144 host_contents_.reset(WebContents::Create( |
| 145 WebContents::CreateParams(profile_, site_instance))), | 145 WebContents::CreateParams(profile_, site_instance))), |
| 146 content::WebContentsObserver::Observe(host_contents_.get()); | 146 content::WebContentsObserver::Observe(host_contents_.get()); |
| 147 host_contents_->SetDelegate(this); | 147 host_contents_->SetDelegate(this); |
| 148 chrome::SetViewType(host_contents_.get(), host_type); | 148 chrome::SetViewType(host_contents_.get(), host_type); |
| 149 | 149 |
| 150 PrefsTabHelper::CreateForWebContents(host_contents()); | 150 PrefsTabHelper::CreateForWebContents(host_contents()); |
| 151 | 151 |
| 152 render_view_host_ = host_contents_->GetRenderViewHost(); | 152 render_view_host_ = host_contents_->GetRenderViewHost(); |
| 153 | 153 |
| 154 // Listen for when an extension is unloaded from the same profile, as it may | 154 // Listen for when an extension is unloaded from the same profile, as it may |
| 155 // be the same extension that this points to. | 155 // be the same extension that this points to. |
| 156 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 156 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 157 content::Source<Profile>(profile_)); | 157 content::Source<Profile>(profile_)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 ExtensionHost::~ExtensionHost() { | 160 ExtensionHost::~ExtensionHost() { |
| 161 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && | 161 if (extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE && |
| 162 extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) { | 162 extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) { |
| 163 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime", | 163 UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime", |
| 164 since_created_.Elapsed()); | 164 since_created_.Elapsed()); |
| 165 } | 165 } |
| 166 // Clear the extension's renderer process out from any WebContents | 166 // Clear the extension's renderer process out from any WebContents |
| 167 // that might have it in their back history. | 167 // that might have it in their back history. |
| 168 // TODO(jyasskin): Remove this hack by making extension reloading | 168 // TODO(jyasskin): Remove this hack by making extension reloading |
| 169 // wait for renderer shutdown. | 169 // wait for renderer shutdown. |
| 170 content::NotificationService::current()->Notify( | 170 content::NotificationService::current()->Notify( |
| 171 content::NOTIFICATION_RENDERER_PROCESS_CLOSING, | 171 content::NOTIFICATION_RENDERER_PROCESS_CLOSING, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 static const base::StringPiece css( | 343 static const base::StringPiece css( |
| 344 ResourceBundle::GetSharedInstance().GetRawDataResource( | 344 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 345 IDR_EXTENSIONS_INFOBAR_CSS)); | 345 IDR_EXTENSIONS_INFOBAR_CSS)); |
| 346 | 346 |
| 347 render_view_host()->InsertCSS(string16(), css.as_string()); | 347 render_view_host()->InsertCSS(string16(), css.as_string()); |
| 348 } | 348 } |
| 349 | 349 |
| 350 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { | 350 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { |
| 351 bool notify = !did_stop_loading_; | 351 bool notify = !did_stop_loading_; |
| 352 did_stop_loading_ = true; | 352 did_stop_loading_ = true; |
| 353 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP || | 353 if (extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_POPUP || |
| 354 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_DIALOG || | 354 extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_DIALOG || |
| 355 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || | 355 extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_INFOBAR || |
| 356 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { | 356 extension_host_type_ == extensions::VIEW_TYPE_PANEL) { |
| 357 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) | 357 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) |
| 358 if (view()) | 358 if (view()) |
| 359 view()->DidStopLoading(); | 359 view()->DidStopLoading(); |
| 360 #endif | 360 #endif |
| 361 } | 361 } |
| 362 if (notify) { | 362 if (notify) { |
| 363 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 363 if (extension_host_type_ == |
| 364 extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | |
| 364 if (extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) { | 365 if (extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) { |
| 365 UMA_HISTOGRAM_TIMES("Extensions.EventPageLoadTime", | 366 UMA_HISTOGRAM_TIMES("Extensions.EventPageLoadTime", |
| 366 since_created_.Elapsed()); | 367 since_created_.Elapsed()); |
| 367 } else { | 368 } else { |
| 368 UMA_HISTOGRAM_TIMES("Extensions.BackgroundPageLoadTime", | 369 UMA_HISTOGRAM_TIMES("Extensions.BackgroundPageLoadTime", |
| 369 since_created_.Elapsed()); | 370 since_created_.Elapsed()); |
| 370 } | 371 } |
| 371 } else if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_DIALOG) { | 372 } else if (extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_DIALOG) { |
| 372 UMA_HISTOGRAM_TIMES("Extensions.DialogLoadTime", | 373 UMA_HISTOGRAM_TIMES("Extensions.DialogLoadTime", |
| 373 since_created_.Elapsed()); | 374 since_created_.Elapsed()); |
| 374 } else if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP) { | 375 } else if (extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_POPUP) { |
| 375 UMA_HISTOGRAM_TIMES("Extensions.PopupLoadTime", | 376 UMA_HISTOGRAM_TIMES("Extensions.PopupLoadTime", |
| 376 since_created_.Elapsed()); | 377 since_created_.Elapsed()); |
| 377 } else if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR) { | 378 } else if (extension_host_type_ == |
| 379 extensions::VIEW_TYPE_EXTENSION_INFOBAR) { | |
| 378 UMA_HISTOGRAM_TIMES("Extensions.InfobarLoadTime", | 380 UMA_HISTOGRAM_TIMES("Extensions.InfobarLoadTime", |
| 379 since_created_.Elapsed()); | 381 since_created_.Elapsed()); |
| 380 } else if (extension_host_type_ == chrome::VIEW_TYPE_PANEL) { | 382 } else if (extension_host_type_ == extensions::VIEW_TYPE_PANEL) { |
| 381 UMA_HISTOGRAM_TIMES("Extensions.PanelLoadTime", since_created_.Elapsed()); | 383 UMA_HISTOGRAM_TIMES("Extensions.PanelLoadTime", since_created_.Elapsed()); |
| 382 } | 384 } |
| 383 | 385 |
| 384 // Send the notification last, because it might result in this being | 386 // Send the notification last, because it might result in this being |
| 385 // deleted. | 387 // deleted. |
| 386 content::NotificationService::current()->Notify( | 388 content::NotificationService::current()->Notify( |
| 387 chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 389 chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| 388 content::Source<Profile>(profile_), | 390 content::Source<Profile>(profile_), |
| 389 content::Details<ExtensionHost>(this)); | 391 content::Details<ExtensionHost>(this)); |
| 390 } | 392 } |
| 391 } | 393 } |
| 392 | 394 |
| 393 void ExtensionHost::DocumentAvailableInMainFrame() { | 395 void ExtensionHost::DocumentAvailableInMainFrame() { |
| 394 // If the document has already been marked as available for this host, then | 396 // If the document has already been marked as available for this host, then |
| 395 // bail. No need for the redundant setup. http://crbug.com/31170 | 397 // bail. No need for the redundant setup. http://crbug.com/31170 |
| 396 if (document_element_available_) | 398 if (document_element_available_) |
| 397 return; | 399 return; |
| 398 | 400 |
| 399 document_element_available_ = true; | 401 document_element_available_ = true; |
| 400 if (is_background_page()) { | 402 if (is_background_page()) { |
| 401 extensions::ExtensionSystem::Get(profile_)->extension_service()-> | 403 extensions::ExtensionSystem::Get(profile_)->extension_service()-> |
| 402 SetBackgroundPageReady(extension_); | 404 SetBackgroundPageReady(extension_); |
| 403 } else { | 405 } else { |
| 404 switch (extension_host_type_) { | 406 switch (extension_host_type_) { |
| 405 case chrome::VIEW_TYPE_EXTENSION_INFOBAR: | 407 case extensions::VIEW_TYPE_EXTENSION_INFOBAR: |
| 406 InsertInfobarCSS(); | 408 InsertInfobarCSS(); |
| 407 break; | 409 break; |
| 408 default: | 410 default: |
| 409 break; // No style sheet for other types, at the moment. | 411 break; // No style sheet for other types, at the moment. |
| 410 } | 412 } |
| 411 } | 413 } |
| 412 } | 414 } |
| 413 | 415 |
| 414 void ExtensionHost::CloseContents(WebContents* contents) { | 416 void ExtensionHost::CloseContents(WebContents* contents) { |
| 415 // TODO(mpcomplete): is this check really necessary? | 417 // TODO(mpcomplete): is this check really necessary? |
| 416 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP || | 418 if (extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_POPUP || |
| 417 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_DIALOG || | 419 extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_DIALOG || |
| 418 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE || | 420 extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE || |
| 419 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || | 421 extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_INFOBAR || |
| 420 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { | 422 extension_host_type_ == extensions::VIEW_TYPE_PANEL) { |
| 421 Close(); | 423 Close(); |
| 422 } | 424 } |
| 423 } | 425 } |
| 424 | 426 |
| 425 void ExtensionHost::WillRunJavaScriptDialog() { | 427 void ExtensionHost::WillRunJavaScriptDialog() { |
| 426 ExtensionProcessManager* pm = | 428 ExtensionProcessManager* pm = |
| 427 ExtensionSystem::Get(profile_)->process_manager(); | 429 ExtensionSystem::Get(profile_)->process_manager(); |
| 428 if (pm) | 430 if (pm) |
| 429 pm->IncrementLazyKeepaliveCount(extension()); | 431 pm->IncrementLazyKeepaliveCount(extension()); |
| 430 } | 432 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 453 return browser ? browser->OpenURL(params) : NULL; | 455 return browser ? browser->OpenURL(params) : NULL; |
| 454 } | 456 } |
| 455 default: | 457 default: |
| 456 return NULL; | 458 return NULL; |
| 457 } | 459 } |
| 458 } | 460 } |
| 459 | 461 |
| 460 bool ExtensionHost::PreHandleKeyboardEvent(WebContents* source, | 462 bool ExtensionHost::PreHandleKeyboardEvent(WebContents* source, |
| 461 const NativeWebKeyboardEvent& event, | 463 const NativeWebKeyboardEvent& event, |
| 462 bool* is_keyboard_shortcut) { | 464 bool* is_keyboard_shortcut) { |
| 463 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP && | 465 if (extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_POPUP && |
| 464 event.type == NativeWebKeyboardEvent::RawKeyDown && | 466 event.type == NativeWebKeyboardEvent::RawKeyDown && |
| 465 event.windowsKeyCode == ui::VKEY_ESCAPE) { | 467 event.windowsKeyCode == ui::VKEY_ESCAPE) { |
| 466 DCHECK(is_keyboard_shortcut != NULL); | 468 DCHECK(is_keyboard_shortcut != NULL); |
| 467 *is_keyboard_shortcut = true; | 469 *is_keyboard_shortcut = true; |
| 468 return false; | 470 return false; |
| 469 } | 471 } |
| 470 | 472 |
| 471 // Handle higher priority browser shortcuts such as Ctrl-w. | 473 // Handle higher priority browser shortcuts such as Ctrl-w. |
| 472 Browser* browser = view() ? view()->browser() : NULL; | 474 Browser* browser = view() ? view()->browser() : NULL; |
| 473 if (browser) | 475 if (browser) |
| 474 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut); | 476 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut); |
| 475 | 477 |
| 476 *is_keyboard_shortcut = false; | 478 *is_keyboard_shortcut = false; |
| 477 return false; | 479 return false; |
| 478 } | 480 } |
| 479 | 481 |
| 480 void ExtensionHost::HandleKeyboardEvent(WebContents* source, | 482 void ExtensionHost::HandleKeyboardEvent(WebContents* source, |
| 481 const NativeWebKeyboardEvent& event) { | 483 const NativeWebKeyboardEvent& event) { |
| 482 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP) { | 484 if (extension_host_type_ == extensions::VIEW_TYPE_EXTENSION_POPUP) { |
| 483 if (event.type == NativeWebKeyboardEvent::RawKeyDown && | 485 if (event.type == NativeWebKeyboardEvent::RawKeyDown && |
| 484 event.windowsKeyCode == ui::VKEY_ESCAPE) { | 486 event.windowsKeyCode == ui::VKEY_ESCAPE) { |
| 485 Close(); | 487 Close(); |
| 486 return; | 488 return; |
| 487 } | 489 } |
| 488 } | 490 } |
| 489 UnhandledKeyboardEvent(source, event); | 491 UnhandledKeyboardEvent(source, event); |
| 490 } | 492 } |
| 491 | 493 |
| 492 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { | 494 bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 633 content::IsAudioMediaType(request.audio_type), | 635 content::IsAudioMediaType(request.audio_type), |
| 634 content::IsVideoMediaType(request.video_type), | 636 content::IsVideoMediaType(request.video_type), |
| 635 &devices); | 637 &devices); |
| 636 | 638 |
| 637 // For tab capture device, we require the tabCapture permission. | 639 // For tab capture device, we require the tabCapture permission. |
| 638 RequestMediaAccessPermissionHelper::AuthorizeRequest( | 640 RequestMediaAccessPermissionHelper::AuthorizeRequest( |
| 639 devices, request, callback, extension(), false); | 641 devices, request, callback, extension(), false); |
| 640 } | 642 } |
| 641 | 643 |
| 642 } // namespace extensions | 644 } // namespace extensions |
| OLD | NEW |