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.
5 #include "chrome/browser/extensions/navigation_observer.h"
7 #include "chrome/browser/extensions/extension_install_ui.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_registry.h"
15 #include "extensions/browser/extension_system.h"
17 using content::NavigationController
;
18 using content::NavigationEntry
;
20 namespace extensions
{
22 NavigationObserver::NavigationObserver(Profile
* profile
) : profile_(profile
) {
23 RegisterForNotifications();
26 NavigationObserver::~NavigationObserver() {}
28 void NavigationObserver::Observe(int type
,
29 const content::NotificationSource
& source
,
30 const content::NotificationDetails
& details
) {
31 if (type
!= content::NOTIFICATION_NAV_ENTRY_COMMITTED
) {
36 NavigationController
* controller
=
37 content::Source
<NavigationController
>(source
).ptr();
38 if (!profile_
->IsSameProfile(
39 Profile::FromBrowserContext(controller
->GetBrowserContext())))
42 PromptToEnableExtensionIfNecessary(controller
);
45 void NavigationObserver::RegisterForNotifications() {
46 registrar_
.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
47 content::NotificationService::AllSources());
50 void NavigationObserver::PromptToEnableExtensionIfNecessary(
51 NavigationController
* nav_controller
) {
52 // Bail out if we're already running a prompt.
53 if (!in_progress_prompt_extension_id_
.empty())
56 NavigationEntry
* nav_entry
= nav_controller
->GetVisibleEntry();
60 ExtensionRegistry
* registry
= extensions::ExtensionRegistry::Get(profile_
);
61 const Extension
* extension
=
62 registry
->disabled_extensions().GetExtensionOrAppByURL(
67 // Try not to repeatedly prompt the user about the same extension.
68 if (prompted_extensions_
.find(extension
->id()) != prompted_extensions_
.end())
70 prompted_extensions_
.insert(extension
->id());
72 ExtensionService
* extension_service
=
73 extensions::ExtensionSystem::Get(profile_
)->extension_service();
74 ExtensionPrefs
* extension_prefs
= extension_service
->extension_prefs();
75 if (extension_prefs
->DidExtensionEscalatePermissions(extension
->id())) {
76 // Keep track of the extension id and nav controller we're prompting for.
77 // These must be reset in InstallUIProceed and InstallUIAbort.
78 in_progress_prompt_extension_id_
= extension
->id();
79 in_progress_prompt_navigation_controller_
= nav_controller
;
81 extension_install_prompt_
.reset(
82 new ExtensionInstallPrompt(nav_controller
->GetWebContents()));
83 extension_install_prompt_
->ConfirmReEnable(this, extension
);
87 void NavigationObserver::InstallUIProceed() {
88 ExtensionService
* extension_service
=
89 extensions::ExtensionSystem::Get(profile_
)->extension_service();
90 const Extension
* extension
= extension_service
->GetExtensionById(
91 in_progress_prompt_extension_id_
, true);
92 NavigationController
* nav_controller
=
93 in_progress_prompt_navigation_controller_
;
95 CHECK(nav_controller
);
97 in_progress_prompt_extension_id_
= "";
98 in_progress_prompt_navigation_controller_
= NULL
;
99 extension_install_prompt_
.reset();
101 // Grant permissions, re-enable the extension, and then reload the tab.
102 extension_service
->GrantPermissionsAndEnableExtension(extension
);
103 nav_controller
->Reload(true);
106 void NavigationObserver::InstallUIAbort(bool user_initiated
) {
107 ExtensionService
* extension_service
=
108 extensions::ExtensionSystem::Get(profile_
)->extension_service();
109 const Extension
* extension
= extension_service
->GetExtensionById(
110 in_progress_prompt_extension_id_
, true);
112 in_progress_prompt_extension_id_
= "";
113 in_progress_prompt_navigation_controller_
= NULL
;
114 extension_install_prompt_
.reset();
116 std::string histogram_name
= user_initiated
?
117 "Extensions.Permissions_ReEnableCancel" :
118 "Extensions.Permissions_ReEnableAbort";
119 ExtensionService::RecordPermissionMessagesHistogram(
120 extension
, histogram_name
.c_str());
123 } // namespace extensions