Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / extensions / navigation_observer.cc
blob289a8b84377acb7acfb87ffeee1185310a4baef7
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_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "content/public/browser/navigation_controller.h"
10 #include "content/public/browser/navigation_entry.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/notification_types.h"
13 #include "extensions/browser/extension_prefs.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) {
32 NOTREACHED();
33 return;
36 NavigationController* controller =
37 content::Source<NavigationController>(source).ptr();
38 if (!profile_->IsSameProfile(
39 Profile::FromBrowserContext(controller->GetBrowserContext())))
40 return;
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())
54 return;
56 NavigationEntry* nav_entry = nav_controller->GetVisibleEntry();
57 if (!nav_entry)
58 return;
60 ExtensionRegistry* registry = extensions::ExtensionRegistry::Get(profile_);
61 const Extension* extension =
62 registry->disabled_extensions().GetExtensionOrAppByURL(
63 nav_entry->GetURL());
64 if (!extension)
65 return;
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());
72 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
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;
79 extension_install_prompt_.reset(
80 new ExtensionInstallPrompt(nav_controller->GetWebContents()));
81 extension_install_prompt_->ConfirmReEnable(this, extension);
85 void NavigationObserver::InstallUIProceed() {
86 ExtensionService* extension_service =
87 extensions::ExtensionSystem::Get(profile_)->extension_service();
88 const Extension* extension = extension_service->GetExtensionById(
89 in_progress_prompt_extension_id_, true);
90 NavigationController* nav_controller =
91 in_progress_prompt_navigation_controller_;
92 CHECK(extension);
93 CHECK(nav_controller);
95 in_progress_prompt_extension_id_ = "";
96 in_progress_prompt_navigation_controller_ = NULL;
97 extension_install_prompt_.reset();
99 // Grant permissions, re-enable the extension, and then reload the tab.
100 extension_service->GrantPermissionsAndEnableExtension(extension);
101 nav_controller->Reload(true);
104 void NavigationObserver::InstallUIAbort(bool user_initiated) {
105 ExtensionService* extension_service =
106 extensions::ExtensionSystem::Get(profile_)->extension_service();
107 const Extension* extension = extension_service->GetExtensionById(
108 in_progress_prompt_extension_id_, true);
110 in_progress_prompt_extension_id_ = "";
111 in_progress_prompt_navigation_controller_ = NULL;
112 extension_install_prompt_.reset();
114 std::string histogram_name = user_initiated ? "ReEnableCancel"
115 : "ReEnableAbort";
116 ExtensionService::RecordPermissionMessagesHistogram(
117 extension, histogram_name.c_str());
120 } // namespace extensions