Temporarily re-enabling SizeAfterPrefChange test with traces (this time for Linux...
[chromium-blink-merge.git] / chrome / browser / ui / extensions / extension_enable_flow.cc
blob8b232873493deff02866eae3369cefd29854d098
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/ui/extensions/extension_enable_flow.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h"
12 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/extension_system.h"
19 using extensions::Extension;
21 ExtensionEnableFlow::ExtensionEnableFlow(Profile* profile,
22 const std::string& extension_id,
23 ExtensionEnableFlowDelegate* delegate)
24 : profile_(profile),
25 extension_id_(extension_id),
26 delegate_(delegate),
27 parent_contents_(NULL),
28 parent_window_(NULL),
29 extension_registry_observer_(this) {
32 ExtensionEnableFlow::~ExtensionEnableFlow() {
35 void ExtensionEnableFlow::StartForWebContents(
36 content::WebContents* parent_contents) {
37 parent_contents_ = parent_contents;
38 parent_window_ = NULL;
39 Run();
42 void ExtensionEnableFlow::StartForNativeWindow(
43 gfx::NativeWindow parent_window) {
44 parent_contents_ = NULL;
45 parent_window_ = parent_window;
46 Run();
49 void ExtensionEnableFlow::StartForCurrentlyNonexistentWindow(
50 base::Callback<gfx::NativeWindow(void)> window_getter) {
51 window_getter_ = window_getter;
52 Run();
55 void ExtensionEnableFlow::Run() {
56 ExtensionService* service =
57 extensions::ExtensionSystem::Get(profile_)->extension_service();
58 const Extension* extension = service->GetExtensionById(extension_id_, true);
59 if (!extension) {
60 extension = extensions::ExtensionRegistry::Get(profile_)->GetExtensionById(
61 extension_id_, extensions::ExtensionRegistry::TERMINATED);
62 // It's possible (though unlikely) the app could have been uninstalled since
63 // the user clicked on it.
64 if (!extension)
65 return;
66 // If the app was terminated, reload it first.
67 service->ReloadExtension(extension_id_);
69 // ReloadExtension reallocates the Extension object.
70 extension = service->GetExtensionById(extension_id_, true);
72 // |extension| could be NULL for asynchronous load, such as the case of
73 // an unpacked extension. Wait for the load to continue the flow.
74 if (!extension) {
75 StartObserving();
76 return;
80 CheckPermissionAndMaybePromptUser();
83 void ExtensionEnableFlow::CheckPermissionAndMaybePromptUser() {
84 ExtensionService* service =
85 extensions::ExtensionSystem::Get(profile_)->extension_service();
86 const Extension* extension = service->GetExtensionById(extension_id_, true);
87 if (!extension) {
88 delegate_->ExtensionEnableFlowAborted(false); // |delegate_| may delete us.
89 return;
92 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
93 if (!prefs->DidExtensionEscalatePermissions(extension_id_)) {
94 // Enable the extension immediately if its privileges weren't escalated.
95 // This is a no-op if the extension was previously terminated.
96 service->EnableExtension(extension_id_);
98 delegate_->ExtensionEnableFlowFinished(); // |delegate_| may delete us.
99 return;
102 CreatePrompt();
103 prompt_->ConfirmReEnable(this, extension);
106 void ExtensionEnableFlow::CreatePrompt() {
107 if (!window_getter_.is_null())
108 parent_window_ = window_getter_.Run();
109 prompt_.reset(parent_contents_ ?
110 new ExtensionInstallPrompt(parent_contents_) :
111 new ExtensionInstallPrompt(profile_, parent_window_, this));
114 void ExtensionEnableFlow::StartObserving() {
115 extension_registry_observer_.Add(
116 extensions::ExtensionRegistry::Get(profile_));
117 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
118 content::Source<Profile>(profile_));
119 registrar_.Add(this,
120 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
121 content::Source<Profile>(profile_));
124 void ExtensionEnableFlow::StopObserving() {
125 registrar_.RemoveAll();
128 void ExtensionEnableFlow::Observe(int type,
129 const content::NotificationSource& source,
130 const content::NotificationDetails& details) {
131 switch (type) {
132 case chrome::NOTIFICATION_EXTENSION_LOAD_ERROR: {
133 StopObserving();
134 delegate_->ExtensionEnableFlowAborted(false);
135 break;
137 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED: {
138 const Extension* extension =
139 content::Details<const Extension>(details).ptr();
140 if (extension->id() == extension_id_) {
141 StopObserving();
142 delegate_->ExtensionEnableFlowAborted(false);
144 break;
146 default:
147 NOTREACHED();
151 void ExtensionEnableFlow::OnExtensionLoaded(
152 content::BrowserContext* browser_context,
153 const Extension* extension) {
154 if (extension->id() == extension_id_) {
155 StopObserving();
156 CheckPermissionAndMaybePromptUser();
160 void ExtensionEnableFlow::InstallUIProceed() {
161 ExtensionService* service =
162 extensions::ExtensionSystem::Get(profile_)->extension_service();
164 // The extension can be uninstalled in another window while the UI was
165 // showing. Treat it as a cancellation and notify |delegate_|.
166 const Extension* extension = service->GetExtensionById(extension_id_, true);
167 if (!extension) {
168 delegate_->ExtensionEnableFlowAborted(true);
169 return;
172 service->GrantPermissionsAndEnableExtension(extension);
173 delegate_->ExtensionEnableFlowFinished(); // |delegate_| may delete us.
176 void ExtensionEnableFlow::InstallUIAbort(bool user_initiated) {
177 delegate_->ExtensionEnableFlowAborted(user_initiated);
178 // |delegate_| may delete us.
181 content::WebContents* ExtensionEnableFlow::OpenURL(
182 const content::OpenURLParams& params) {
183 chrome::ScopedTabbedBrowserDisplayer displayer(
184 profile_, chrome::GetActiveDesktop());
185 return displayer.browser()->OpenURL(params);