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/tab_helper.h" | 5 #include "chrome/browser/extensions/tab_helper.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/extensions/activity_log/activity_log.h" | 12 #include "chrome/browser/extensions/activity_log/activity_log.h" |
13 #include "chrome/browser/extensions/api/declarative_content/chrome_content_rules
_registry.h" | 13 #include "chrome/browser/extensions/api/declarative_content/chrome_content_rules
_registry.h" |
14 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 14 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
15 #include "chrome/browser/extensions/api/webstore/webstore_api.h" | |
16 #include "chrome/browser/extensions/bookmark_app_helper.h" | 15 #include "chrome/browser/extensions/bookmark_app_helper.h" |
17 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | 16 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
18 #include "chrome/browser/extensions/extension_action_runner.h" | 17 #include "chrome/browser/extensions/extension_action_runner.h" |
19 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
20 #include "chrome/browser/extensions/extension_util.h" | 19 #include "chrome/browser/extensions/extension_util.h" |
| 20 #include "chrome/browser/extensions/install_observer.h" |
| 21 #include "chrome/browser/extensions/install_tracker.h" |
| 22 #include "chrome/browser/extensions/install_tracker_factory.h" |
21 #include "chrome/browser/extensions/location_bar_controller.h" | 23 #include "chrome/browser/extensions/location_bar_controller.h" |
22 #include "chrome/browser/extensions/webstore_inline_installer.h" | 24 #include "chrome/browser/extensions/webstore_inline_installer.h" |
23 #include "chrome/browser/extensions/webstore_inline_installer_factory.h" | 25 #include "chrome/browser/extensions/webstore_inline_installer_factory.h" |
24 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/sessions/session_tab_helper.h" | 27 #include "chrome/browser/sessions/session_tab_helper.h" |
26 #include "chrome/browser/shell_integration.h" | 28 #include "chrome/browser/shell_integration.h" |
27 #include "chrome/browser/ui/browser_commands.h" | 29 #include "chrome/browser/ui/browser_commands.h" |
28 #include "chrome/browser/ui/browser_dialogs.h" | 30 #include "chrome/browser/ui/browser_dialogs.h" |
29 #include "chrome/browser/ui/browser_finder.h" | 31 #include "chrome/browser/ui/browser_finder.h" |
30 #include "chrome/browser/web_applications/web_app.h" | 32 #include "chrome/browser/web_applications/web_app.h" |
| 33 #include "chrome/common/extensions/api/webstore/webstore_api_constants.h" |
31 #include "chrome/common/extensions/chrome_extension_messages.h" | 34 #include "chrome/common/extensions/chrome_extension_messages.h" |
32 #include "chrome/common/extensions/extension_constants.h" | 35 #include "chrome/common/extensions/extension_constants.h" |
33 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 36 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
34 #include "chrome/common/render_messages.h" | 37 #include "chrome/common/render_messages.h" |
35 #include "chrome/common/url_constants.h" | 38 #include "chrome/common/url_constants.h" |
36 #include "content/public/browser/invalidate_type.h" | 39 #include "content/public/browser/invalidate_type.h" |
37 #include "content/public/browser/navigation_controller.h" | 40 #include "content/public/browser/navigation_controller.h" |
38 #include "content/public/browser/navigation_details.h" | 41 #include "content/public/browser/navigation_details.h" |
39 #include "content/public/browser/navigation_entry.h" | 42 #include "content/public/browser/navigation_entry.h" |
40 #include "content/public/browser/notification_service.h" | 43 #include "content/public/browser/notification_service.h" |
(...skipping 24 matching lines...) Expand all Loading... |
65 #endif | 68 #endif |
66 | 69 |
67 using content::NavigationController; | 70 using content::NavigationController; |
68 using content::NavigationEntry; | 71 using content::NavigationEntry; |
69 using content::WebContents; | 72 using content::WebContents; |
70 | 73 |
71 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::TabHelper); | 74 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::TabHelper); |
72 | 75 |
73 namespace extensions { | 76 namespace extensions { |
74 | 77 |
| 78 // A helper class to watch the progress of inline installation and update the |
| 79 // renderer. Owned by the TabHelper. |
| 80 class TabHelper::InlineInstallObserver : public InstallObserver { |
| 81 public: |
| 82 InlineInstallObserver(TabHelper* tab_helper, |
| 83 content::BrowserContext* browser_context, |
| 84 int routing_id, |
| 85 const std::string& extension_id, |
| 86 bool observe_download_progress, |
| 87 bool observe_install_stage) |
| 88 : tab_helper_(tab_helper), |
| 89 routing_id_(routing_id), |
| 90 extension_id_(extension_id), |
| 91 observe_download_progress_(observe_download_progress), |
| 92 observe_install_stage_(observe_install_stage), |
| 93 install_observer_(this) { |
| 94 DCHECK(tab_helper); |
| 95 DCHECK(observe_download_progress || observe_install_stage); |
| 96 InstallTracker* install_tracker = |
| 97 InstallTrackerFactory::GetForBrowserContext(browser_context); |
| 98 if (install_tracker) |
| 99 install_observer_.Add(install_tracker); |
| 100 } |
| 101 ~InlineInstallObserver() override {} |
| 102 |
| 103 private: |
| 104 // InstallObserver: |
| 105 void OnBeginExtensionDownload(const std::string& extension_id) override { |
| 106 SendInstallStageChangedMessage(extension_id, |
| 107 api::webstore::INSTALL_STAGE_DOWNLOADING); |
| 108 } |
| 109 void OnDownloadProgress(const std::string& extension_id, |
| 110 int percent_downloaded) override { |
| 111 if (observe_download_progress_ && extension_id == extension_id_) { |
| 112 tab_helper_->Send(new ExtensionMsg_InlineInstallDownloadProgress( |
| 113 routing_id_, percent_downloaded)); |
| 114 } |
| 115 } |
| 116 void OnBeginCrxInstall(const std::string& extension_id) override { |
| 117 SendInstallStageChangedMessage(extension_id, |
| 118 api::webstore::INSTALL_STAGE_INSTALLING); |
| 119 } |
| 120 void OnShutdown() override { install_observer_.RemoveAll(); } |
| 121 |
| 122 void SendInstallStageChangedMessage(const std::string& extension_id, |
| 123 api::webstore::InstallStage stage) { |
| 124 if (observe_install_stage_ && extension_id == extension_id_) { |
| 125 tab_helper_->Send( |
| 126 new ExtensionMsg_InlineInstallStageChanged(routing_id_, stage)); |
| 127 } |
| 128 } |
| 129 |
| 130 // The owning TabHelper (guaranteed to be valid). |
| 131 TabHelper* const tab_helper_; |
| 132 |
| 133 // The routing id to use in sending IPC updates. |
| 134 int routing_id_; |
| 135 |
| 136 // The id of the extension to observe. |
| 137 std::string extension_id_; |
| 138 |
| 139 // Whether or not to observe download/install progress. |
| 140 const bool observe_download_progress_; |
| 141 const bool observe_install_stage_; |
| 142 |
| 143 ScopedObserver<InstallTracker, InstallObserver> install_observer_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(InlineInstallObserver); |
| 146 }; |
| 147 |
75 TabHelper::TabHelper(content::WebContents* web_contents) | 148 TabHelper::TabHelper(content::WebContents* web_contents) |
76 : content::WebContentsObserver(web_contents), | 149 : content::WebContentsObserver(web_contents), |
77 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), | 150 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), |
78 extension_app_(NULL), | 151 extension_app_(NULL), |
79 pending_web_app_action_(NONE), | 152 pending_web_app_action_(NONE), |
80 last_committed_nav_entry_unique_id_(0), | 153 last_committed_nav_entry_unique_id_(0), |
81 update_shortcut_on_load_complete_(false), | 154 update_shortcut_on_load_complete_(false), |
82 script_executor_( | 155 script_executor_( |
83 new ScriptExecutor(web_contents, &script_execution_observers_)), | 156 new ScriptExecutor(web_contents, &script_execution_observers_)), |
84 location_bar_controller_(new LocationBarController(web_contents)), | 157 location_bar_controller_(new LocationBarController(web_contents)), |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 const GURL& requestor_url, | 435 const GURL& requestor_url, |
363 int listeners_mask) { | 436 int listeners_mask) { |
364 // Check that the listener is reasonable. We should never get anything other | 437 // Check that the listener is reasonable. We should never get anything other |
365 // than an install stage listener, a download listener, or both. | 438 // than an install stage listener, a download listener, or both. |
366 if ((listeners_mask & ~(api::webstore::INSTALL_STAGE_LISTENER | | 439 if ((listeners_mask & ~(api::webstore::INSTALL_STAGE_LISTENER | |
367 api::webstore::DOWNLOAD_PROGRESS_LISTENER)) != 0 || | 440 api::webstore::DOWNLOAD_PROGRESS_LISTENER)) != 0 || |
368 requestor_url.is_empty()) { | 441 requestor_url.is_empty()) { |
369 NOTREACHED(); | 442 NOTREACHED(); |
370 return; | 443 return; |
371 } | 444 } |
| 445 |
| 446 if (pending_inline_installations_.count(webstore_item_id) != 0) { |
| 447 Send(new ExtensionMsg_InlineWebstoreInstallResponse( |
| 448 return_route_id, install_id, false, |
| 449 webstore_install::kInstallInProgressError, |
| 450 webstore_install::INSTALL_IN_PROGRESS)); |
| 451 return; |
| 452 } |
| 453 |
| 454 pending_inline_installations_.insert(webstore_item_id); |
372 // Inform the Webstore API that an inline install is happening, in case the | 455 // Inform the Webstore API that an inline install is happening, in case the |
373 // page requested status updates. | 456 // page requested status updates. |
374 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); | 457 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
375 if (registry->disabled_extensions().Contains(webstore_item_id) && | 458 if (registry->disabled_extensions().Contains(webstore_item_id) && |
376 (ExtensionPrefs::Get(profile_)->GetDisableReasons(webstore_item_id) & | 459 (ExtensionPrefs::Get(profile_)->GetDisableReasons(webstore_item_id) & |
377 Extension::DISABLE_PERMISSIONS_INCREASE) != 0) { | 460 Extension::DISABLE_PERMISSIONS_INCREASE) != 0) { |
378 // The extension was disabled due to permissions increase. Prompt for | 461 // The extension was disabled due to permissions increase. Prompt for |
379 // re-enable. | 462 // re-enable. |
380 // TODO(devlin): We should also prompt for re-enable for other reasons, | 463 // TODO(devlin): We should also prompt for re-enable for other reasons, |
381 // like user-disabled. | 464 // like user-disabled. |
382 // For clarity, explicitly end any prior reenable process. | 465 // For clarity, explicitly end any prior reenable process. |
383 extension_reenabler_.reset(); | 466 extension_reenabler_.reset(); |
384 extension_reenabler_ = ExtensionReenabler::PromptForReenable( | 467 extension_reenabler_ = ExtensionReenabler::PromptForReenable( |
385 registry->disabled_extensions().GetByID(webstore_item_id), | 468 registry->disabled_extensions().GetByID(webstore_item_id), profile_, |
386 profile_, | 469 web_contents(), requestor_url, |
387 web_contents(), | |
388 requestor_url, | |
389 base::Bind(&TabHelper::OnReenableComplete, | 470 base::Bind(&TabHelper::OnReenableComplete, |
390 weak_ptr_factory_.GetWeakPtr(), | 471 weak_ptr_factory_.GetWeakPtr(), install_id, |
391 install_id, | 472 return_route_id, webstore_item_id)); |
392 return_route_id)); | |
393 } else { | 473 } else { |
394 // TODO(devlin): We should adddress the case of the extension already | 474 // TODO(devlin): We should adddress the case of the extension already |
395 // being installed and enabled. | 475 // being installed and enabled. |
396 WebstoreAPI::Get(profile_)->OnInlineInstallStart( | 476 bool observe_download_progress = |
397 return_route_id, this, webstore_item_id, listeners_mask); | 477 (listeners_mask & api::webstore::DOWNLOAD_PROGRESS_LISTENER) != 0; |
| 478 bool observe_install_stage = |
| 479 (listeners_mask & api::webstore::INSTALL_STAGE_LISTENER) != 0; |
| 480 if (observe_install_stage || observe_download_progress) { |
| 481 DCHECK_EQ(0u, install_observers_.count(webstore_item_id)); |
| 482 install_observers_[webstore_item_id] = |
| 483 base::MakeUnique<InlineInstallObserver>( |
| 484 this, web_contents()->GetBrowserContext(), return_route_id, |
| 485 webstore_item_id, observe_download_progress, |
| 486 observe_install_stage); |
| 487 } |
398 | 488 |
399 WebstoreStandaloneInstaller::Callback callback = | 489 WebstoreStandaloneInstaller::Callback callback = base::Bind( |
400 base::Bind(&TabHelper::OnInlineInstallComplete, | 490 &TabHelper::OnInlineInstallComplete, weak_ptr_factory_.GetWeakPtr(), |
401 base::Unretained(this), | 491 install_id, return_route_id, webstore_item_id); |
402 install_id, | |
403 return_route_id); | |
404 scoped_refptr<WebstoreInlineInstaller> installer( | 492 scoped_refptr<WebstoreInlineInstaller> installer( |
405 webstore_inline_installer_factory_->CreateInstaller( | 493 webstore_inline_installer_factory_->CreateInstaller( |
406 web_contents(), host, webstore_item_id, requestor_url, callback)); | 494 web_contents(), host, webstore_item_id, requestor_url, callback)); |
407 installer->BeginInstall(); | 495 installer->BeginInstall(); |
408 } | 496 } |
409 } | 497 } |
410 | 498 |
411 void TabHelper::OnGetAppInstallState(content::RenderFrameHost* host, | 499 void TabHelper::OnGetAppInstallState(content::RenderFrameHost* host, |
412 const GURL& requestor_url, | 500 const GURL& requestor_url, |
413 int return_route_id, | 501 int return_route_id, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | 575 web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); |
488 } | 576 } |
489 } | 577 } |
490 | 578 |
491 WindowController* TabHelper::GetExtensionWindowController() const { | 579 WindowController* TabHelper::GetExtensionWindowController() const { |
492 return ExtensionTabUtil::GetWindowControllerOfTab(web_contents()); | 580 return ExtensionTabUtil::GetWindowControllerOfTab(web_contents()); |
493 } | 581 } |
494 | 582 |
495 void TabHelper::OnReenableComplete(int install_id, | 583 void TabHelper::OnReenableComplete(int install_id, |
496 int return_route_id, | 584 int return_route_id, |
| 585 const std::string& extension_id, |
497 ExtensionReenabler::ReenableResult result) { | 586 ExtensionReenabler::ReenableResult result) { |
498 extension_reenabler_.reset(); | |
499 // Map the re-enable results to webstore-install results. | 587 // Map the re-enable results to webstore-install results. |
500 webstore_install::Result webstore_result = webstore_install::SUCCESS; | 588 webstore_install::Result webstore_result = webstore_install::SUCCESS; |
501 std::string error; | 589 std::string error; |
502 switch (result) { | 590 switch (result) { |
503 case ExtensionReenabler::REENABLE_SUCCESS: | 591 case ExtensionReenabler::REENABLE_SUCCESS: |
504 break; // already set | 592 break; // already set |
505 case ExtensionReenabler::USER_CANCELED: | 593 case ExtensionReenabler::USER_CANCELED: |
506 webstore_result = webstore_install::USER_CANCELLED; | 594 webstore_result = webstore_install::USER_CANCELLED; |
507 error = "User canceled install."; | 595 error = "User canceled install."; |
508 break; | 596 break; |
509 case ExtensionReenabler::NOT_ALLOWED: | 597 case ExtensionReenabler::NOT_ALLOWED: |
510 webstore_result = webstore_install::NOT_PERMITTED; | 598 webstore_result = webstore_install::NOT_PERMITTED; |
511 error = "Install not permitted."; | 599 error = "Install not permitted."; |
512 break; | 600 break; |
513 case ExtensionReenabler::ABORTED: | 601 case ExtensionReenabler::ABORTED: |
514 webstore_result = webstore_install::ABORTED; | 602 webstore_result = webstore_install::ABORTED; |
515 error = "Aborted due to tab closing."; | 603 error = "Aborted due to tab closing."; |
516 break; | 604 break; |
517 } | 605 } |
518 | 606 |
519 OnInlineInstallComplete(install_id, | 607 OnInlineInstallComplete(install_id, return_route_id, extension_id, |
520 return_route_id, | 608 result == ExtensionReenabler::REENABLE_SUCCESS, error, |
521 result == ExtensionReenabler::REENABLE_SUCCESS, | |
522 error, | |
523 webstore_result); | 609 webstore_result); |
| 610 // Note: ExtensionReenabler contained the callback with the curried-in |
| 611 // |extension_id|; delete it last. |
| 612 extension_reenabler_.reset(); |
524 } | 613 } |
525 | 614 |
526 void TabHelper::OnInlineInstallComplete(int install_id, | 615 void TabHelper::OnInlineInstallComplete(int install_id, |
527 int return_route_id, | 616 int return_route_id, |
| 617 const std::string& extension_id, |
528 bool success, | 618 bool success, |
529 const std::string& error, | 619 const std::string& error, |
530 webstore_install::Result result) { | 620 webstore_install::Result result) { |
| 621 DCHECK_EQ(1u, pending_inline_installations_.count(extension_id)); |
| 622 pending_inline_installations_.erase(extension_id); |
| 623 install_observers_.erase(extension_id); |
531 Send(new ExtensionMsg_InlineWebstoreInstallResponse( | 624 Send(new ExtensionMsg_InlineWebstoreInstallResponse( |
532 return_route_id, | 625 return_route_id, |
533 install_id, | 626 install_id, |
534 success, | 627 success, |
535 success ? std::string() : error, | 628 success ? std::string() : error, |
536 result)); | 629 result)); |
537 } | 630 } |
538 | 631 |
539 WebContents* TabHelper::GetAssociatedWebContents() const { | 632 WebContents* TabHelper::GetAssociatedWebContents() const { |
540 return web_contents(); | 633 return web_contents(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 } | 673 } |
581 } | 674 } |
582 | 675 |
583 void TabHelper::SetTabId(content::RenderFrameHost* render_frame_host) { | 676 void TabHelper::SetTabId(content::RenderFrameHost* render_frame_host) { |
584 render_frame_host->Send( | 677 render_frame_host->Send( |
585 new ExtensionMsg_SetTabId(render_frame_host->GetRoutingID(), | 678 new ExtensionMsg_SetTabId(render_frame_host->GetRoutingID(), |
586 SessionTabHelper::IdForTab(web_contents()))); | 679 SessionTabHelper::IdForTab(web_contents()))); |
587 } | 680 } |
588 | 681 |
589 } // namespace extensions | 682 } // namespace extensions |
OLD | NEW |