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_registry.h"
16 #include "extensions/browser/extension_system.h"
18 using extensions::Extension;
20 ExtensionEnableFlow::ExtensionEnableFlow(Profile* profile,
21 const std::string& extension_id,
22 ExtensionEnableFlowDelegate* delegate)
24 extension_id_(extension_id),
26 parent_contents_(NULL),
27 parent_window_(NULL) {
30 ExtensionEnableFlow::~ExtensionEnableFlow() {
33 void ExtensionEnableFlow::StartForWebContents(
34 content::WebContents* parent_contents) {
35 parent_contents_ = parent_contents;
36 parent_window_ = NULL;
40 void ExtensionEnableFlow::StartForNativeWindow(
41 gfx::NativeWindow parent_window) {
42 parent_contents_ = NULL;
43 parent_window_ = parent_window;
47 void ExtensionEnableFlow::StartForCurrentlyNonexistentWindow(
48 base::Callback<gfx::NativeWindow(void)> window_getter) {
49 window_getter_ = window_getter;
53 void ExtensionEnableFlow::Run() {
54 ExtensionService* service =
55 extensions::ExtensionSystem::Get(profile_)->extension_service();
56 const Extension* extension = service->GetExtensionById(extension_id_, true);
58 extension = extensions::ExtensionRegistry::Get(profile_)->GetExtensionById(
59 extension_id_, extensions::ExtensionRegistry::TERMINATED);
60 // It's possible (though unlikely) the app could have been uninstalled since
61 // the user clicked on it.
64 // If the app was terminated, reload it first.
65 service->ReloadExtension(extension_id_);
67 // ReloadExtension reallocates the Extension object.
68 extension = service->GetExtensionById(extension_id_, true);
70 // |extension| could be NULL for asynchronous load, such as the case of
71 // an unpacked extension. Wait for the load to continue the flow.
78 CheckPermissionAndMaybePromptUser();
81 void ExtensionEnableFlow::CheckPermissionAndMaybePromptUser() {
82 ExtensionService* service =
83 extensions::ExtensionSystem::Get(profile_)->extension_service();
84 const Extension* extension = service->GetExtensionById(extension_id_, true);
86 delegate_->ExtensionEnableFlowAborted(false); // |delegate_| may delete us.
90 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
91 if (!prefs->DidExtensionEscalatePermissions(extension_id_)) {
92 // Enable the extension immediately if its privileges weren't escalated.
93 // This is a no-op if the extension was previously terminated.
94 service->EnableExtension(extension_id_);
96 delegate_->ExtensionEnableFlowFinished(); // |delegate_| may delete us.
101 prompt_->ConfirmReEnable(this, extension);
104 void ExtensionEnableFlow::CreatePrompt() {
105 if (!window_getter_.is_null())
106 parent_window_ = window_getter_.Run();
107 prompt_.reset(parent_contents_ ?
108 new ExtensionInstallPrompt(parent_contents_) :
109 new ExtensionInstallPrompt(profile_, parent_window_, this));
112 void ExtensionEnableFlow::StartObserving() {
114 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
115 content::Source<Profile>(profile_));
116 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
117 content::Source<Profile>(profile_));
118 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
119 content::Source<Profile>(profile_));
122 void ExtensionEnableFlow::StopObserving() {
123 registrar_.RemoveAll();
126 void ExtensionEnableFlow::Observe(int type,
127 const content::NotificationSource& source,
128 const content::NotificationDetails& details) {
130 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
131 const Extension* extension =
132 content::Details<const Extension>(details).ptr();
133 if (extension->id() == extension_id_) {
135 CheckPermissionAndMaybePromptUser();
140 case chrome::NOTIFICATION_EXTENSION_LOAD_ERROR: {
142 delegate_->ExtensionEnableFlowAborted(false);
145 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
146 const Extension* extension =
147 content::Details<const Extension>(details).ptr();
148 if (extension->id() == extension_id_) {
150 delegate_->ExtensionEnableFlowAborted(false);
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);
168 delegate_->ExtensionEnableFlowAborted(true);
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);