[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 5 | #include "chrome/browser/extensions/navigation_observer.h" |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 6 | |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 7 | #include "chrome/browser/extensions/extension_service.h" |
| 8 | #include "chrome/browser/profiles/profile.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 9 | #include "content/public/browser/navigation_controller.h" |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 10 | #include "content/public/browser/navigation_entry.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 11 | #include "content/public/browser/notification_service.h" |
[email protected] | c333e79 | 2012-01-06 16:57:39 | [diff] [blame] | 12 | #include "content/public/browser/notification_types.h" |
[email protected] | dccba4f8 | 2014-05-29 00:52:56 | [diff] [blame] | 13 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 284ffac | 2014-02-12 01:08:58 | [diff] [blame] | 14 | #include "extensions/browser/extension_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 15 | #include "extensions/browser/extension_system.h" |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 16 | |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 17 | using content::NavigationController; |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 18 | using content::NavigationEntry; |
| 19 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 20 | namespace extensions { |
| 21 | |
| 22 | NavigationObserver::NavigationObserver(Profile* profile) : profile_(profile) { |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 23 | RegisterForNotifications(); |
| 24 | } |
| 25 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 26 | NavigationObserver::~NavigationObserver() {} |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 27 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 28 | void NavigationObserver::Observe(int type, |
| 29 | const content::NotificationSource& source, |
| 30 | const content::NotificationDetails& details) { |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 31 | if (type != content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
| 32 | NOTREACHED(); |
| 33 | return; |
| 34 | } |
| 35 | |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 36 | NavigationController* controller = |
| 37 | content::Source<NavigationController>(source).ptr(); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 38 | if (!profile_->IsSameProfile( |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 39 | Profile::FromBrowserContext(controller->GetBrowserContext()))) |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 40 | return; |
| 41 | |
| 42 | PromptToEnableExtensionIfNecessary(controller); |
| 43 | } |
| 44 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 45 | void NavigationObserver::RegisterForNotifications() { |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 46 | registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 47 | content::NotificationService::AllSources()); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 50 | void NavigationObserver::PromptToEnableExtensionIfNecessary( |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 51 | NavigationController* nav_controller) { |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 52 | // Bail out if we're already running a prompt. |
| 53 | if (!in_progress_prompt_extension_id_.empty()) |
| 54 | return; |
| 55 | |
[email protected] | afe9aba | 2013-08-16 20:31:34 | [diff] [blame] | 56 | NavigationEntry* nav_entry = nav_controller->GetVisibleEntry(); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 57 | if (!nav_entry) |
| 58 | return; |
| 59 | |
[email protected] | 284ffac | 2014-02-12 01:08:58 | [diff] [blame] | 60 | ExtensionRegistry* registry = extensions::ExtensionRegistry::Get(profile_); |
| 61 | const Extension* extension = |
| 62 | registry->disabled_extensions().GetExtensionOrAppByURL( |
| 63 | nav_entry->GetURL()); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 64 | if (!extension) |
| 65 | return; |
| 66 | |
| 67 | // Try not to repeatedly prompt the user about the same extension. |
| 68 | if (prompted_extensions_.find(extension->id()) != prompted_extensions_.end()) |
| 69 | return; |
| 70 | prompted_extensions_.insert(extension->id()); |
| 71 | |
[email protected] | 7c82539c | 2014-02-19 06:09:17 | [diff] [blame] | 72 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 73 | if (extension_prefs->DidExtensionEscalatePermissions(extension->id())) { |
| 74 | // Keep track of the extension id and nav controller we're prompting for. |
| 75 | // These must be reset in InstallUIProceed and InstallUIAbort. |
| 76 | in_progress_prompt_extension_id_ = extension->id(); |
| 77 | in_progress_prompt_navigation_controller_ = nav_controller; |
| 78 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 79 | extension_install_prompt_.reset( |
[email protected] | 91e51d61 | 2012-10-21 23:03:05 | [diff] [blame] | 80 | new ExtensionInstallPrompt(nav_controller->GetWebContents())); |
rdevlin.cronin | f84cab7 | 2015-12-12 03:45:23 | [diff] [blame^] | 81 | ExtensionInstallPrompt::PromptType type = |
| 82 | ExtensionInstallPrompt::GetReEnablePromptTypeForExtension(profile_, |
| 83 | extension); |
| 84 | extension_install_prompt_->ShowDialog( |
| 85 | this, extension, nullptr, new ExtensionInstallPrompt::Prompt(type), |
| 86 | ExtensionInstallPrompt::GetDefaultShowDialogCallback()); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 90 | void NavigationObserver::InstallUIProceed() { |
[email protected] | 06bdd2b | 2012-11-30 18:47:13 | [diff] [blame] | 91 | ExtensionService* extension_service = |
| 92 | extensions::ExtensionSystem::Get(profile_)->extension_service(); |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 93 | const Extension* extension = extension_service->GetExtensionById( |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 94 | in_progress_prompt_extension_id_, true); |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 95 | NavigationController* nav_controller = |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 96 | in_progress_prompt_navigation_controller_; |
| 97 | CHECK(extension); |
| 98 | CHECK(nav_controller); |
| 99 | |
| 100 | in_progress_prompt_extension_id_ = ""; |
| 101 | in_progress_prompt_navigation_controller_ = NULL; |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 102 | extension_install_prompt_.reset(); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 103 | |
| 104 | // Grant permissions, re-enable the extension, and then reload the tab. |
[email protected] | 009633c | 2013-03-07 22:08:28 | [diff] [blame] | 105 | extension_service->GrantPermissionsAndEnableExtension(extension); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 106 | nav_controller->Reload(true); |
| 107 | } |
| 108 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 109 | void NavigationObserver::InstallUIAbort(bool user_initiated) { |
[email protected] | 06bdd2b | 2012-11-30 18:47:13 | [diff] [blame] | 110 | ExtensionService* extension_service = |
| 111 | extensions::ExtensionSystem::Get(profile_)->extension_service(); |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 112 | const Extension* extension = extension_service->GetExtensionById( |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 113 | in_progress_prompt_extension_id_, true); |
| 114 | |
| 115 | in_progress_prompt_extension_id_ = ""; |
| 116 | in_progress_prompt_navigation_controller_ = NULL; |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 117 | extension_install_prompt_.reset(); |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 118 | |
treib | 2e0517f9 | 2015-05-07 23:15:36 | [diff] [blame] | 119 | std::string histogram_name = user_initiated ? "ReEnableCancel" |
| 120 | : "ReEnableAbort"; |
[email protected] | f9c292b3 | 2011-09-13 16:14:05 | [diff] [blame] | 121 | ExtensionService::RecordPermissionMessagesHistogram( |
| 122 | extension, histogram_name.c_str()); |
| 123 | } |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 124 | |
| 125 | } // namespace extensions |