| 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/navigation_observer.h" | 5 #include "chrome/browser/extensions/navigation_observer.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "content/public/browser/navigation_controller.h" | 9 #include "content/public/browser/navigation_controller.h" |
| 10 #include "content/public/browser/navigation_entry.h" | 10 #include "content/public/browser/navigation_entry.h" |
| 11 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/notification_types.h" | 12 #include "content/public/browser/notification_types.h" |
| 13 #include "extensions/browser/extension_prefs.h" | 13 #include "extensions/browser/extension_prefs.h" |
| 14 #include "extensions/browser/extension_registry.h" | 14 #include "extensions/browser/extension_registry.h" |
| 15 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
| 16 | 16 |
| 17 using content::NavigationController; | 17 using content::NavigationController; |
| 18 using content::NavigationEntry; | 18 using content::NavigationEntry; |
| 19 | 19 |
| 20 namespace extensions { | 20 namespace extensions { |
| 21 | 21 |
| 22 NavigationObserver::NavigationObserver(Profile* profile) : profile_(profile) { | 22 NavigationObserver::NavigationObserver(Profile* profile) |
| 23 : profile_(profile), weak_factory_(this) { |
| 23 RegisterForNotifications(); | 24 RegisterForNotifications(); |
| 24 } | 25 } |
| 25 | 26 |
| 26 NavigationObserver::~NavigationObserver() {} | 27 NavigationObserver::~NavigationObserver() {} |
| 27 | 28 |
| 28 void NavigationObserver::Observe(int type, | 29 void NavigationObserver::Observe(int type, |
| 29 const content::NotificationSource& source, | 30 const content::NotificationSource& source, |
| 30 const content::NotificationDetails& details) { | 31 const content::NotificationDetails& details) { |
| 31 if (type != content::NOTIFICATION_NAV_ENTRY_COMMITTED) { | 32 if (type != content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
| 32 NOTREACHED(); | 33 NOTREACHED(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 return; | 66 return; |
| 66 | 67 |
| 67 // Try not to repeatedly prompt the user about the same extension. | 68 // Try not to repeatedly prompt the user about the same extension. |
| 68 if (prompted_extensions_.find(extension->id()) != prompted_extensions_.end()) | 69 if (prompted_extensions_.find(extension->id()) != prompted_extensions_.end()) |
| 69 return; | 70 return; |
| 70 prompted_extensions_.insert(extension->id()); | 71 prompted_extensions_.insert(extension->id()); |
| 71 | 72 |
| 72 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 73 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
| 73 if (extension_prefs->DidExtensionEscalatePermissions(extension->id())) { | 74 if (extension_prefs->DidExtensionEscalatePermissions(extension->id())) { |
| 74 // Keep track of the extension id and nav controller we're prompting for. | 75 // Keep track of the extension id and nav controller we're prompting for. |
| 75 // These must be reset in InstallUIProceed and InstallUIAbort. | 76 // These must be reset in OnInstallPromptDone. |
| 76 in_progress_prompt_extension_id_ = extension->id(); | 77 in_progress_prompt_extension_id_ = extension->id(); |
| 77 in_progress_prompt_navigation_controller_ = nav_controller; | 78 in_progress_prompt_navigation_controller_ = nav_controller; |
| 78 | 79 |
| 79 extension_install_prompt_.reset( | 80 extension_install_prompt_.reset( |
| 80 new ExtensionInstallPrompt(nav_controller->GetWebContents())); | 81 new ExtensionInstallPrompt(nav_controller->GetWebContents())); |
| 81 ExtensionInstallPrompt::PromptType type = | 82 ExtensionInstallPrompt::PromptType type = |
| 82 ExtensionInstallPrompt::GetReEnablePromptTypeForExtension(profile_, | 83 ExtensionInstallPrompt::GetReEnablePromptTypeForExtension(profile_, |
| 83 extension); | 84 extension); |
| 84 extension_install_prompt_->ShowDialog( | 85 extension_install_prompt_->ShowDialog( |
| 85 this, extension, nullptr, | 86 base::Bind(&NavigationObserver::OnInstallPromptDone, |
| 87 weak_factory_.GetWeakPtr()), |
| 88 extension, nullptr, |
| 86 make_scoped_ptr(new ExtensionInstallPrompt::Prompt(type)), | 89 make_scoped_ptr(new ExtensionInstallPrompt::Prompt(type)), |
| 87 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | 90 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); |
| 88 } | 91 } |
| 89 } | 92 } |
| 90 | 93 |
| 91 void NavigationObserver::InstallUIProceed() { | 94 void NavigationObserver::OnInstallPromptDone( |
| 92 ExtensionService* extension_service = | 95 ExtensionInstallPrompt::Result result) { |
| 93 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
| 94 const Extension* extension = extension_service->GetExtensionById( | |
| 95 in_progress_prompt_extension_id_, true); | |
| 96 NavigationController* nav_controller = | |
| 97 in_progress_prompt_navigation_controller_; | |
| 98 CHECK(extension); | |
| 99 CHECK(nav_controller); | |
| 100 | |
| 101 in_progress_prompt_extension_id_ = ""; | |
| 102 in_progress_prompt_navigation_controller_ = NULL; | |
| 103 extension_install_prompt_.reset(); | |
| 104 | |
| 105 // Grant permissions, re-enable the extension, and then reload the tab. | |
| 106 extension_service->GrantPermissionsAndEnableExtension(extension); | |
| 107 nav_controller->Reload(true); | |
| 108 } | |
| 109 | |
| 110 void NavigationObserver::InstallUIAbort(bool user_initiated) { | |
| 111 ExtensionService* extension_service = | 96 ExtensionService* extension_service = |
| 112 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 97 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 113 const Extension* extension = extension_service->GetExtensionById( | 98 const Extension* extension = extension_service->GetExtensionById( |
| 114 in_progress_prompt_extension_id_, true); | 99 in_progress_prompt_extension_id_, true); |
| 115 | 100 |
| 116 in_progress_prompt_extension_id_ = ""; | 101 if (result == ExtensionInstallPrompt::Result::ACCEPTED) { |
| 117 in_progress_prompt_navigation_controller_ = NULL; | 102 NavigationController* nav_controller = |
| 103 in_progress_prompt_navigation_controller_; |
| 104 CHECK(extension); |
| 105 CHECK(nav_controller); |
| 106 |
| 107 // Grant permissions, re-enable the extension, and then reload the tab. |
| 108 extension_service->GrantPermissionsAndEnableExtension(extension); |
| 109 nav_controller->Reload(true); |
| 110 } else { |
| 111 std::string histogram_name = |
| 112 result == ExtensionInstallPrompt::Result::USER_CANCELED |
| 113 ? "ReEnableCancel" |
| 114 : "ReEnableAbort"; |
| 115 ExtensionService::RecordPermissionMessagesHistogram(extension, |
| 116 histogram_name.c_str()); |
| 117 } |
| 118 |
| 119 in_progress_prompt_extension_id_ = std::string(); |
| 120 in_progress_prompt_navigation_controller_ = nullptr; |
| 118 extension_install_prompt_.reset(); | 121 extension_install_prompt_.reset(); |
| 119 | |
| 120 std::string histogram_name = user_initiated ? "ReEnableCancel" | |
| 121 : "ReEnableAbort"; | |
| 122 ExtensionService::RecordPermissionMessagesHistogram( | |
| 123 extension, histogram_name.c_str()); | |
| 124 } | 122 } |
| 125 | 123 |
| 126 } // namespace extensions | 124 } // namespace extensions |
| OLD | NEW |