Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / extensions / navigation_observer.cc
blobac19ff628e5c5c8c26947889f3083a4cb9242040
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_prefs.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_system.h"
18 using content::NavigationController;
19 using content::NavigationEntry;
21 namespace extensions {
23 NavigationObserver::NavigationObserver(Profile* profile) : profile_(profile) {
24 RegisterForNotifications();
27 NavigationObserver::~NavigationObserver() {}
29 void NavigationObserver::Observe(int type,
30 const content::NotificationSource& source,
31 const content::NotificationDetails& details) {
32 if (type != content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
33 NOTREACHED();
34 return;
37 NavigationController* controller =
38 content::Source<NavigationController>(source).ptr();
39 if (!profile_->IsSameProfile(
40 Profile::FromBrowserContext(controller->GetBrowserContext())))
41 return;
43 PromptToEnableExtensionIfNecessary(controller);
46 void NavigationObserver::RegisterForNotifications() {
47 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
48 content::NotificationService::AllSources());
51 void NavigationObserver::PromptToEnableExtensionIfNecessary(
52 NavigationController* nav_controller) {
53 // Bail out if we're already running a prompt.
54 if (!in_progress_prompt_extension_id_.empty())
55 return;
57 NavigationEntry* nav_entry = nav_controller->GetVisibleEntry();
58 if (!nav_entry)
59 return;
61 ExtensionRegistry* registry = extensions::ExtensionRegistry::Get(profile_);
62 const Extension* extension =
63 registry->disabled_extensions().GetExtensionOrAppByURL(
64 nav_entry->GetURL());
65 if (!extension)
66 return;
68 // Try not to repeatedly prompt the user about the same extension.
69 if (prompted_extensions_.find(extension->id()) != prompted_extensions_.end())
70 return;
71 prompted_extensions_.insert(extension->id());
73 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
74 if (extension_prefs->DidExtensionEscalatePermissions(extension->id())) {
75 // Keep track of the extension id and nav controller we're prompting for.
76 // These must be reset in InstallUIProceed and InstallUIAbort.
77 in_progress_prompt_extension_id_ = extension->id();
78 in_progress_prompt_navigation_controller_ = nav_controller;
80 extension_install_prompt_.reset(
81 new ExtensionInstallPrompt(nav_controller->GetWebContents()));
82 extension_install_prompt_->ConfirmReEnable(this, extension);
86 void NavigationObserver::InstallUIProceed() {
87 ExtensionService* extension_service =
88 extensions::ExtensionSystem::Get(profile_)->extension_service();
89 const Extension* extension = extension_service->GetExtensionById(
90 in_progress_prompt_extension_id_, true);
91 NavigationController* nav_controller =
92 in_progress_prompt_navigation_controller_;
93 CHECK(extension);
94 CHECK(nav_controller);
96 in_progress_prompt_extension_id_ = "";
97 in_progress_prompt_navigation_controller_ = NULL;
98 extension_install_prompt_.reset();
100 // Grant permissions, re-enable the extension, and then reload the tab.
101 extension_service->GrantPermissionsAndEnableExtension(extension);
102 nav_controller->Reload(true);
105 void NavigationObserver::InstallUIAbort(bool user_initiated) {
106 ExtensionService* extension_service =
107 extensions::ExtensionSystem::Get(profile_)->extension_service();
108 const Extension* extension = extension_service->GetExtensionById(
109 in_progress_prompt_extension_id_, true);
111 in_progress_prompt_extension_id_ = "";
112 in_progress_prompt_navigation_controller_ = NULL;
113 extension_install_prompt_.reset();
115 std::string histogram_name = user_initiated
116 ? "Extensions.Permissions_ReEnableCancel2"
117 : "Extensions.Permissions_ReEnableAbort2";
118 ExtensionService::RecordPermissionMessagesHistogram(
119 extension, histogram_name.c_str());
122 } // namespace extensions