Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/navigation_observer.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "chrome/browser/extensions/extension_service.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "content/public/browser/navigation_controller.h" | |
| 11 #include "content/public/browser/navigation_entry.h" | |
| 12 #include "content/public/browser/notification_service.h" | |
| 13 #include "content/public/browser/notification_types.h" | |
| 14 #include "extensions/browser/extension_prefs.h" | |
| 15 #include "extensions/browser/extension_registry.h" | |
| 16 #include "extensions/browser/extension_system.h" | |
| 17 | |
| 18 using content::NavigationController; | |
| 19 using content::NavigationEntry; | |
| 20 | |
| 21 namespace extensions { | |
| 22 | |
| 23 NavigationObserver::NavigationObserver(Profile* profile) | |
| 24 : profile_(profile), | |
| 25 extension_registry_observer_(this), | |
| 26 weak_factory_(this) { | |
| 27 RegisterForNotifications(); | |
| 28 extension_registry_observer_.Add(ExtensionRegistry::Get(profile)); | |
| 29 } | |
| 30 | |
| 31 NavigationObserver::~NavigationObserver() {} | |
| 32 | |
| 33 void NavigationObserver::Observe(int type, | |
| 34 const content::NotificationSource& source, | |
| 35 const content::NotificationDetails& details) { | |
| 36 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | |
| 37 | |
| 38 NavigationController* controller = | |
| 39 content::Source<NavigationController>(source).ptr(); | |
| 40 if (!profile_->IsSameProfile( | |
| 41 Profile::FromBrowserContext(controller->GetBrowserContext()))) | |
| 42 return; | |
| 43 | |
| 44 PromptToEnableExtensionIfNecessary(controller); | |
| 45 } | |
| 46 | |
| 47 void NavigationObserver::RegisterForNotifications() { | |
| 48 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 49 content::NotificationService::AllSources()); | |
| 50 } | |
| 51 | |
| 52 void NavigationObserver::PromptToEnableExtensionIfNecessary( | |
| 53 NavigationController* nav_controller) { | |
| 54 // Bail out if we're already running a prompt. | |
| 55 if (!in_progress_prompt_extension_id_.empty()) | |
| 56 return; | |
| 57 | |
| 58 NavigationEntry* nav_entry = nav_controller->GetVisibleEntry(); | |
| 59 if (!nav_entry) | |
| 60 return; | |
| 61 | |
| 62 ExtensionRegistry* registry = extensions::ExtensionRegistry::Get(profile_); | |
| 63 const Extension* extension = | |
| 64 registry->disabled_extensions().GetExtensionOrAppByURL( | |
| 65 nav_entry->GetURL()); | |
| 66 if (!extension) | |
| 67 return; | |
| 68 | |
| 69 // Try not to repeatedly prompt the user about the same extension. | |
| 70 if (prompted_extensions_.find(extension->id()) != prompted_extensions_.end()) | |
| 71 return; | |
| 72 prompted_extensions_.insert(extension->id()); | |
| 73 | |
| 74 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | |
| 75 if (extension_prefs->DidExtensionEscalatePermissions(extension->id())) { | |
|
lazyboy
2017/01/11 00:33:30
So the re-enable prompt we were thinking of probab
Devlin
2017/01/12 17:25:44
Ah, you're quite right. Let's keep it for chrome-
| |
| 76 // Keep track of the extension id and nav controller we're prompting for. | |
| 77 // These must be reset in OnInstallPromptDone. | |
| 78 in_progress_prompt_extension_id_ = extension->id(); | |
| 79 in_progress_prompt_navigation_controller_ = nav_controller; | |
| 80 | |
| 81 extension_install_prompt_.reset( | |
| 82 new ExtensionInstallPrompt(nav_controller->GetWebContents())); | |
| 83 ExtensionInstallPrompt::PromptType type = | |
| 84 ExtensionInstallPrompt::GetReEnablePromptTypeForExtension(profile_, | |
| 85 extension); | |
| 86 extension_install_prompt_->ShowDialog( | |
| 87 base::Bind(&NavigationObserver::OnInstallPromptDone, | |
| 88 weak_factory_.GetWeakPtr()), | |
| 89 extension, nullptr, | |
| 90 base::MakeUnique<ExtensionInstallPrompt::Prompt>(type), | |
| 91 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void NavigationObserver::OnInstallPromptDone( | |
| 96 ExtensionInstallPrompt::Result result) { | |
| 97 // The extension was already uninstalled. | |
| 98 if (in_progress_prompt_extension_id_.empty()) | |
| 99 return; | |
| 100 | |
| 101 ExtensionService* extension_service = | |
| 102 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
| 103 const Extension* extension = extension_service->GetExtensionById( | |
| 104 in_progress_prompt_extension_id_, true); | |
| 105 CHECK(extension); | |
| 106 | |
| 107 if (result == ExtensionInstallPrompt::Result::ACCEPTED) { | |
| 108 NavigationController* nav_controller = | |
| 109 in_progress_prompt_navigation_controller_; | |
| 110 CHECK(nav_controller); | |
| 111 | |
| 112 // Grant permissions, re-enable the extension, and then reload the tab. | |
| 113 extension_service->GrantPermissionsAndEnableExtension(extension); | |
| 114 nav_controller->Reload(content::ReloadType::NORMAL, true); | |
| 115 } else { | |
| 116 std::string histogram_name = | |
| 117 result == ExtensionInstallPrompt::Result::USER_CANCELED | |
| 118 ? "ReEnableCancel" | |
| 119 : "ReEnableAbort"; | |
| 120 ExtensionService::RecordPermissionMessagesHistogram(extension, | |
| 121 histogram_name.c_str()); | |
| 122 } | |
| 123 | |
| 124 in_progress_prompt_extension_id_ = std::string(); | |
| 125 in_progress_prompt_navigation_controller_ = nullptr; | |
| 126 extension_install_prompt_.reset(); | |
| 127 } | |
| 128 | |
| 129 void NavigationObserver::OnExtensionUninstalled( | |
| 130 content::BrowserContext* browser_context, | |
| 131 const Extension* extension, | |
| 132 UninstallReason reason) { | |
| 133 if (in_progress_prompt_extension_id_.empty() || | |
| 134 in_progress_prompt_extension_id_ != extension->id()) { | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 in_progress_prompt_extension_id_ = std::string(); | |
| 139 in_progress_prompt_navigation_controller_ = nullptr; | |
| 140 extension_install_prompt_.reset(); | |
| 141 } | |
| 142 | |
| 143 } // namespace extensions | |
| OLD | NEW |