• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
6 
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"
18 
19 using extensions::Extension;
20 
ExtensionEnableFlow(Profile * profile,const std::string & extension_id,ExtensionEnableFlowDelegate * delegate)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) {
30 }
31 
~ExtensionEnableFlow()32 ExtensionEnableFlow::~ExtensionEnableFlow() {
33 }
34 
StartForWebContents(content::WebContents * parent_contents)35 void ExtensionEnableFlow::StartForWebContents(
36     content::WebContents* parent_contents) {
37   parent_contents_ = parent_contents;
38   parent_window_ = NULL;
39   Run();
40 }
41 
StartForNativeWindow(gfx::NativeWindow parent_window)42 void ExtensionEnableFlow::StartForNativeWindow(
43     gfx::NativeWindow parent_window) {
44   parent_contents_ = NULL;
45   parent_window_ = parent_window;
46   Run();
47 }
48 
StartForCurrentlyNonexistentWindow(base::Callback<gfx::NativeWindow (void)> window_getter)49 void ExtensionEnableFlow::StartForCurrentlyNonexistentWindow(
50     base::Callback<gfx::NativeWindow(void)> window_getter) {
51   window_getter_ = window_getter;
52   Run();
53 }
54 
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_);
68 
69     // ReloadExtension reallocates the Extension object.
70     extension = service->GetExtensionById(extension_id_, true);
71 
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;
77     }
78   }
79 
80   CheckPermissionAndMaybePromptUser();
81 }
82 
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;
90   }
91 
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_);
97 
98     delegate_->ExtensionEnableFlowFinished();  // |delegate_| may delete us.
99     return;
100   }
101 
102   CreatePrompt();
103   prompt_->ConfirmReEnable(this, extension);
104 }
105 
CreatePrompt()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));
112 }
113 
StartObserving()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 }
120 
StopObserving()121 void ExtensionEnableFlow::StopObserving() {
122   registrar_.RemoveAll();
123   extension_registry_observer_.RemoveAll();
124 }
125 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)126 void ExtensionEnableFlow::Observe(int type,
127                                   const content::NotificationSource& source,
128                                   const content::NotificationDetails& details) {
129   DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_LOAD_ERROR, type);
130   StopObserving();
131   delegate_->ExtensionEnableFlowAborted(false);
132 }
133 
OnExtensionLoaded(content::BrowserContext * browser_context,const Extension * extension)134 void ExtensionEnableFlow::OnExtensionLoaded(
135     content::BrowserContext* browser_context,
136     const Extension* extension) {
137   if (extension->id() == extension_id_) {
138     StopObserving();
139     CheckPermissionAndMaybePromptUser();
140   }
141 }
142 
OnExtensionUninstalled(content::BrowserContext * browser_context,const Extension * extension)143 void ExtensionEnableFlow::OnExtensionUninstalled(
144     content::BrowserContext* browser_context,
145     const Extension* extension) {
146   if (extension->id() == extension_id_) {
147     StopObserving();
148     delegate_->ExtensionEnableFlowAborted(false);
149   }
150 }
151 
InstallUIProceed()152 void ExtensionEnableFlow::InstallUIProceed() {
153   ExtensionService* service =
154       extensions::ExtensionSystem::Get(profile_)->extension_service();
155 
156   // The extension can be uninstalled in another window while the UI was
157   // showing. Treat it as a cancellation and notify |delegate_|.
158   const Extension* extension = service->GetExtensionById(extension_id_, true);
159   if (!extension) {
160     delegate_->ExtensionEnableFlowAborted(true);
161     return;
162   }
163 
164   service->GrantPermissionsAndEnableExtension(extension);
165   delegate_->ExtensionEnableFlowFinished();  // |delegate_| may delete us.
166 }
167 
InstallUIAbort(bool user_initiated)168 void ExtensionEnableFlow::InstallUIAbort(bool user_initiated) {
169   delegate_->ExtensionEnableFlowAborted(user_initiated);
170   // |delegate_| may delete us.
171 }
172 
OpenURL(const content::OpenURLParams & params)173 content::WebContents* ExtensionEnableFlow::OpenURL(
174     const content::OpenURLParams& params) {
175   chrome::ScopedTabbedBrowserDisplayer displayer(
176       profile_, chrome::GetActiveDesktop());
177   return displayer.browser()->OpenURL(params);
178 }
179