| 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 since_created_.Elapsed()); | 163 since_created_.Elapsed()); |
| 164 } | 164 } |
| 165 content::NotificationService::current()->Notify( | 165 content::NotificationService::current()->Notify( |
| 166 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 166 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 167 content::Source<Profile>(profile_), | 167 content::Source<Profile>(profile_), |
| 168 content::Details<ExtensionHost>(this)); | 168 content::Details<ExtensionHost>(this)); |
| 169 ProcessCreationQueue::GetInstance()->Remove(this); | 169 ProcessCreationQueue::GetInstance()->Remove(this); |
| 170 } | 170 } |
| 171 | 171 |
| 172 void ExtensionHost::CreateView(Browser* browser) { | 172 void ExtensionHost::CreateView(Browser* browser) { |
| 173 #if defined(TOOLKIT_VIEWS) | 173 extension_view_.reset(ExtensionView::Create(this, browser)); |
| 174 view_.reset(new ExtensionViewViews(this, browser)); | |
| 175 // We own |view_|, so don't auto delete when it's removed from the view | |
| 176 // hierarchy. | |
| 177 view_->set_owned_by_client(); | |
| 178 #elif defined(OS_MACOSX) | |
| 179 view_.reset(new ExtensionViewMac(this, browser)); | |
| 180 view_->Init(); | |
| 181 #elif defined(TOOLKIT_GTK) | |
| 182 view_.reset(new ExtensionViewGtk(this, browser)); | |
| 183 view_->Init(); | |
| 184 #else | |
| 185 // TODO(port) | |
| 186 NOTREACHED(); | |
| 187 #endif | |
| 188 } | 174 } |
| 189 | 175 |
| 190 WebContents* ExtensionHost::GetAssociatedWebContents() const { | 176 WebContents* ExtensionHost::GetAssociatedWebContents() const { |
| 191 return associated_web_contents_; | 177 return associated_web_contents_; |
| 192 } | 178 } |
| 193 | 179 |
| 194 void ExtensionHost::SetAssociatedWebContents( | 180 void ExtensionHost::SetAssociatedWebContents( |
| 195 content::WebContents* web_contents) { | 181 content::WebContents* web_contents) { |
| 196 associated_web_contents_ = web_contents; | 182 associated_web_contents_ = web_contents; |
| 197 if (web_contents) { | 183 if (web_contents) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 227 void ExtensionHost::CreateRenderViewNow() { | 213 void ExtensionHost::CreateRenderViewNow() { |
| 228 LoadInitialURL(); | 214 LoadInitialURL(); |
| 229 if (is_background_page()) { | 215 if (is_background_page()) { |
| 230 DCHECK(IsRenderViewLive()); | 216 DCHECK(IsRenderViewLive()); |
| 231 profile_->GetExtensionService()->DidCreateRenderViewForBackgroundPage(this); | 217 profile_->GetExtensionService()->DidCreateRenderViewForBackgroundPage(this); |
| 232 } | 218 } |
| 233 } | 219 } |
| 234 | 220 |
| 235 extensions::WindowController* | 221 extensions::WindowController* |
| 236 ExtensionHost::GetExtensionWindowController() const { | 222 ExtensionHost::GetExtensionWindowController() const { |
| 237 return view() && view()->browser() ? | 223 return extension_view() && extension_view()->browser() ? |
| 238 view()->browser()->extension_window_controller() : NULL; | 224 extension_view()->browser()->extension_window_controller() : NULL; |
| 239 } | 225 } |
| 240 | 226 |
| 241 const GURL& ExtensionHost::GetURL() const { | 227 const GURL& ExtensionHost::GetURL() const { |
| 242 return host_contents()->GetURL(); | 228 return host_contents()->GetURL(); |
| 243 } | 229 } |
| 244 | 230 |
| 245 void ExtensionHost::LoadInitialURL() { | 231 void ExtensionHost::LoadInitialURL() { |
| 246 if (!is_background_page() && | 232 if (!is_background_page() && |
| 247 !profile_->GetExtensionService()->IsBackgroundPageReady(extension_)) { | 233 !profile_->GetExtensionService()->IsBackgroundPageReady(extension_)) { |
| 248 // Make sure the background page loads before any others. | 234 // Make sure the background page loads before any others. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 } | 275 } |
| 290 break; | 276 break; |
| 291 default: | 277 default: |
| 292 NOTREACHED() << "Unexpected notification sent."; | 278 NOTREACHED() << "Unexpected notification sent."; |
| 293 break; | 279 break; |
| 294 } | 280 } |
| 295 } | 281 } |
| 296 | 282 |
| 297 void ExtensionHost::ResizeDueToAutoResize(WebContents* source, | 283 void ExtensionHost::ResizeDueToAutoResize(WebContents* source, |
| 298 const gfx::Size& new_size) { | 284 const gfx::Size& new_size) { |
| 299 if (view()) | 285 if (extension_view()) |
| 300 view()->ResizeDueToAutoResize(new_size); | 286 extension_view()->ResizeDueToAutoResize(new_size); |
| 301 } | 287 } |
| 302 | 288 |
| 303 void ExtensionHost::RenderViewGone(base::TerminationStatus status) { | 289 void ExtensionHost::RenderViewGone(base::TerminationStatus status) { |
| 304 // During browser shutdown, we may use sudden termination on an extension | 290 // During browser shutdown, we may use sudden termination on an extension |
| 305 // process, so it is expected to lose our connection to the render view. | 291 // process, so it is expected to lose our connection to the render view. |
| 306 // Do nothing. | 292 // Do nothing. |
| 307 if (browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID) | 293 if (browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID) |
| 308 return; | 294 return; |
| 309 | 295 |
| 310 // In certain cases, multiple ExtensionHost objects may have pointed to | 296 // In certain cases, multiple ExtensionHost objects may have pointed to |
| (...skipping 25 matching lines...) Expand all Loading... |
| 336 } | 322 } |
| 337 | 323 |
| 338 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { | 324 void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { |
| 339 bool notify = !did_stop_loading_; | 325 bool notify = !did_stop_loading_; |
| 340 did_stop_loading_ = true; | 326 did_stop_loading_ = true; |
| 341 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP || | 327 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP || |
| 342 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_DIALOG || | 328 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_DIALOG || |
| 343 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || | 329 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || |
| 344 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { | 330 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { |
| 345 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) | 331 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) |
| 346 if (view()) | 332 if (extension_view()) |
| 347 view()->DidStopLoading(); | 333 extension_view()->DidStopLoading(); |
| 348 #endif | 334 #endif |
| 349 } | 335 } |
| 350 if (notify) { | 336 if (notify) { |
| 351 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 337 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
| 352 if (extension_ && extension_->has_lazy_background_page()) { | 338 if (extension_ && extension_->has_lazy_background_page()) { |
| 353 UMA_HISTOGRAM_TIMES("Extensions.EventPageLoadTime", | 339 UMA_HISTOGRAM_TIMES("Extensions.EventPageLoadTime", |
| 354 since_created_.Elapsed()); | 340 since_created_.Elapsed()); |
| 355 } else { | 341 } else { |
| 356 UMA_HISTOGRAM_TIMES("Extensions.BackgroundPageLoadTime", | 342 UMA_HISTOGRAM_TIMES("Extensions.BackgroundPageLoadTime", |
| 357 since_created_.Elapsed()); | 343 since_created_.Elapsed()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || | 392 extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_INFOBAR || |
| 407 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { | 393 extension_host_type_ == chrome::VIEW_TYPE_PANEL) { |
| 408 Close(); | 394 Close(); |
| 409 } | 395 } |
| 410 } | 396 } |
| 411 | 397 |
| 412 void ExtensionHost::OnStartDownload( | 398 void ExtensionHost::OnStartDownload( |
| 413 content::WebContents* source, content::DownloadItem* download) { | 399 content::WebContents* source, content::DownloadItem* download) { |
| 414 // If |source| is in the context of a Browser, show the DownloadShelf on that | 400 // If |source| is in the context of a Browser, show the DownloadShelf on that |
| 415 // Browser. | 401 // Browser. |
| 416 if (!view() || !view()->browser()) | 402 if (!extension_view() || !extension_view()->browser()) |
| 417 return; | 403 return; |
| 418 static_cast<content::WebContentsDelegate*>(view()->browser())-> | 404 static_cast<content::WebContentsDelegate*>(extension_view()->browser())-> |
| 419 OnStartDownload(source, download); | 405 OnStartDownload(source, download); |
| 420 } | 406 } |
| 421 | 407 |
| 422 void ExtensionHost::WebIntentDispatch( | 408 void ExtensionHost::WebIntentDispatch( |
| 423 content::WebContents* web_contents, | 409 content::WebContents* web_contents, |
| 424 content::WebIntentsDispatcher* intents_dispatcher) { | 410 content::WebIntentsDispatcher* intents_dispatcher) { |
| 425 #if !defined(OS_ANDROID) | 411 #if !defined(OS_ANDROID) |
| 426 scoped_ptr<content::WebIntentsDispatcher> dispatcher(intents_dispatcher); | 412 scoped_ptr<content::WebIntentsDispatcher> dispatcher(intents_dispatcher); |
| 427 | 413 |
| 428 Browser* browser = view() ? view()->browser() | 414 Browser* browser = extension_view() ? extension_view()->browser() |
| 429 : browser::FindBrowserWithWebContents(web_contents); | 415 : browser::FindBrowserWithWebContents(web_contents); |
| 430 | 416 |
| 431 // For background scripts/pages, there will be no view(). In this case, we | 417 // For background scripts/pages, there will be no extension_view(). In this |
| 432 // want to treat the intent as a browser-initiated one and deliver it into the | 418 // case, we want to treat the intent as a browser-initiated one and deliver it |
| 433 // current browser. It probably came from a context menu click or similar. | 419 // into the current browser. It probably came from a context menu click or |
| 420 // similar. |
| 434 if (!browser) | 421 if (!browser) |
| 435 browser = web_intents::GetBrowserForBackgroundWebIntentDelivery(profile()); | 422 browser = web_intents::GetBrowserForBackgroundWebIntentDelivery(profile()); |
| 436 | 423 |
| 437 if (browser) { | 424 if (browser) { |
| 438 static_cast<WebContentsDelegate*>(browser)-> | 425 static_cast<WebContentsDelegate*>(browser)-> |
| 439 WebIntentDispatch(NULL, dispatcher.release()); | 426 WebIntentDispatch(NULL, dispatcher.release()); |
| 440 } | 427 } |
| 441 #endif | 428 #endif |
| 442 } | 429 } |
| 443 | 430 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 461 switch (params.disposition) { | 448 switch (params.disposition) { |
| 462 case SINGLETON_TAB: | 449 case SINGLETON_TAB: |
| 463 case NEW_FOREGROUND_TAB: | 450 case NEW_FOREGROUND_TAB: |
| 464 case NEW_BACKGROUND_TAB: | 451 case NEW_BACKGROUND_TAB: |
| 465 case NEW_POPUP: | 452 case NEW_POPUP: |
| 466 case NEW_WINDOW: | 453 case NEW_WINDOW: |
| 467 case SAVE_TO_DISK: | 454 case SAVE_TO_DISK: |
| 468 case OFF_THE_RECORD: { | 455 case OFF_THE_RECORD: { |
| 469 // Only allow these from hosts that are bound to a browser (e.g. popups). | 456 // Only allow these from hosts that are bound to a browser (e.g. popups). |
| 470 // Otherwise they are not driven by a user gesture. | 457 // Otherwise they are not driven by a user gesture. |
| 471 Browser* browser = view() ? view()->browser() : NULL; | 458 Browser* browser = extension_view() ? extension_view()->browser() : NULL; |
| 472 return browser ? browser->OpenURL(params) : NULL; | 459 return browser ? browser->OpenURL(params) : NULL; |
| 473 } | 460 } |
| 474 default: | 461 default: |
| 475 return NULL; | 462 return NULL; |
| 476 } | 463 } |
| 477 } | 464 } |
| 478 | 465 |
| 479 bool ExtensionHost::PreHandleKeyboardEvent(WebContents* source, | 466 bool ExtensionHost::PreHandleKeyboardEvent(WebContents* source, |
| 480 const NativeWebKeyboardEvent& event, | 467 const NativeWebKeyboardEvent& event, |
| 481 bool* is_keyboard_shortcut) { | 468 bool* is_keyboard_shortcut) { |
| 482 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP && | 469 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP && |
| 483 event.type == NativeWebKeyboardEvent::RawKeyDown && | 470 event.type == NativeWebKeyboardEvent::RawKeyDown && |
| 484 event.windowsKeyCode == ui::VKEY_ESCAPE) { | 471 event.windowsKeyCode == ui::VKEY_ESCAPE) { |
| 485 DCHECK(is_keyboard_shortcut != NULL); | 472 DCHECK(is_keyboard_shortcut != NULL); |
| 486 *is_keyboard_shortcut = true; | 473 *is_keyboard_shortcut = true; |
| 487 return false; | 474 return false; |
| 488 } | 475 } |
| 489 | 476 |
| 490 // Handle higher priority browser shortcuts such as Ctrl-w. | 477 // Handle higher priority browser shortcuts such as Ctrl-w. |
| 491 Browser* browser = view() ? view()->browser() : NULL; | 478 Browser* browser = extension_view() ? extension_view()->browser() : NULL; |
| 492 if (browser) | 479 if (browser) |
| 493 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut); | 480 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut); |
| 494 | 481 |
| 495 *is_keyboard_shortcut = false; | 482 *is_keyboard_shortcut = false; |
| 496 return false; | 483 return false; |
| 497 } | 484 } |
| 498 | 485 |
| 499 void ExtensionHost::HandleKeyboardEvent(WebContents* source, | 486 void ExtensionHost::HandleKeyboardEvent(WebContents* source, |
| 500 const NativeWebKeyboardEvent& event) { | 487 const NativeWebKeyboardEvent& event) { |
| 501 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP) { | 488 if (extension_host_type_ == chrome::VIEW_TYPE_EXTENSION_POPUP) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 ExtensionProcessManager* pm = | 531 ExtensionProcessManager* pm = |
| 545 ExtensionSystem::Get(profile_)->process_manager(); | 532 ExtensionSystem::Get(profile_)->process_manager(); |
| 546 if (pm) | 533 if (pm) |
| 547 pm->DecrementLazyKeepaliveCount(extension()); | 534 pm->DecrementLazyKeepaliveCount(extension()); |
| 548 } | 535 } |
| 549 | 536 |
| 550 void ExtensionHost::UnhandledKeyboardEvent( | 537 void ExtensionHost::UnhandledKeyboardEvent( |
| 551 WebContents* source, | 538 WebContents* source, |
| 552 const content::NativeWebKeyboardEvent& event) { | 539 const content::NativeWebKeyboardEvent& event) { |
| 553 // Handle lower priority browser shortcuts such as Ctrl-f. | 540 // Handle lower priority browser shortcuts such as Ctrl-f. |
| 554 Browser* browser = view() ? view()->browser() : NULL; | 541 Browser* browser = extension_view() ? extension_view()->browser() : NULL; |
| 555 if (browser) | 542 if (browser) |
| 556 return browser->HandleKeyboardEvent(source, event); | 543 return browser->HandleKeyboardEvent(source, event); |
| 557 } | 544 } |
| 558 | 545 |
| 559 void ExtensionHost::RenderViewCreated(RenderViewHost* render_view_host) { | 546 void ExtensionHost::RenderViewCreated(RenderViewHost* render_view_host) { |
| 560 render_view_host_ = render_view_host; | 547 render_view_host_ = render_view_host; |
| 561 | 548 |
| 562 if (view()) | 549 if (extension_view()) |
| 563 view()->RenderViewCreated(); | 550 extension_view()->RenderViewCreated(); |
| 564 | 551 |
| 565 // If the host is bound to a window, then extract its id. Extensions hosted | 552 // If the host is bound to a window, then extract its id. Extensions hosted |
| 566 // in ExternalTabContainer objects may not have an associated window. | 553 // in ExternalTabContainer objects may not have an associated window. |
| 567 extensions::WindowController* window = GetExtensionWindowController(); | 554 extensions::WindowController* window = GetExtensionWindowController(); |
| 568 if (window) { | 555 if (window) { |
| 569 render_view_host->Send(new ExtensionMsg_UpdateBrowserWindowId( | 556 render_view_host->Send(new ExtensionMsg_UpdateBrowserWindowId( |
| 570 render_view_host->GetRoutingID(), window->GetWindowId())); | 557 render_view_host->GetRoutingID(), window->GetWindowId())); |
| 571 } | 558 } |
| 572 } | 559 } |
| 573 | 560 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 } | 611 } |
| 625 | 612 |
| 626 void ExtensionHost::RenderViewReady() { | 613 void ExtensionHost::RenderViewReady() { |
| 627 content::NotificationService::current()->Notify( | 614 content::NotificationService::current()->Notify( |
| 628 chrome::NOTIFICATION_EXTENSION_HOST_CREATED, | 615 chrome::NOTIFICATION_EXTENSION_HOST_CREATED, |
| 629 content::Source<Profile>(profile_), | 616 content::Source<Profile>(profile_), |
| 630 content::Details<ExtensionHost>(this)); | 617 content::Details<ExtensionHost>(this)); |
| 631 } | 618 } |
| 632 | 619 |
| 633 } // namespace extensions | 620 } // namespace extensions |
| OLD | NEW |