Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: chrome/browser/extensions/external_install_ui.cc

Issue 145153002: Make sideloaded (externally installed) extensions display webstore info (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ChromeOS fix Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/external_install_ui.h" 5 #include "chrome/browser/extensions/external_install_ui.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/app/chrome_command_ids.h" 16 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/chrome_notification_types.h" 17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/extensions/extension_install_prompt.h" 18 #include "chrome/browser/extensions/extension_install_prompt.h"
19 #include "chrome/browser/extensions/extension_install_ui.h" 19 #include "chrome/browser/extensions/extension_install_ui.h"
20 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_uninstall_dialog.h" 21 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
22 #include "chrome/browser/extensions/webstore_data_fetcher.h"
23 #include "chrome/browser/extensions/webstore_data_fetcher_delegate.h"
22 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h" 25 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_finder.h" 26 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/global_error/global_error.h" 27 #include "chrome/browser/ui/global_error/global_error.h"
26 #include "chrome/browser/ui/global_error/global_error_service.h" 28 #include "chrome/browser/ui/global_error/global_error_service.h"
27 #include "chrome/browser/ui/global_error/global_error_service_factory.h" 29 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
28 #include "chrome/browser/ui/host_desktop.h" 30 #include "chrome/browser/ui/host_desktop.h"
29 #include "chrome/common/extensions/extension_constants.h" 31 #include "chrome/common/extensions/extension_constants.h"
30 #include "chrome/common/extensions/manifest_url_handler.h" 32 #include "chrome/common/extensions/manifest_url_handler.h"
31 #include "content/public/browser/notification_details.h" 33 #include "content/public/browser/notification_details.h"
(...skipping 17 matching lines...) Expand all
49 bool UseBubbleInstall(const Extension* extension, bool is_new_profile) { 51 bool UseBubbleInstall(const Extension* extension, bool is_new_profile) {
50 return ManifestURL::UpdatesFromGallery(extension) && !is_new_profile; 52 return ManifestURL::UpdatesFromGallery(extension) && !is_new_profile;
51 } 53 }
52 54
53 } // namespace 55 } // namespace
54 56
55 static const int kMenuCommandId = IDC_EXTERNAL_EXTENSION_ALERT; 57 static const int kMenuCommandId = IDC_EXTERNAL_EXTENSION_ALERT;
56 58
57 class ExternalInstallGlobalError; 59 class ExternalInstallGlobalError;
58 60
59 // TODO(mpcomplete): Get rid of the refcounting on this class, or document 61 // This class is refcounted to stay alive while we try and pull webstore data.
60 // why it's necessary. Will do after refactoring to merge back with
61 // ExtensionDisabledDialogDelegate.
62 class ExternalInstallDialogDelegate 62 class ExternalInstallDialogDelegate
63 : public ExtensionInstallPrompt::Delegate, 63 : public ExtensionInstallPrompt::Delegate,
64 public WebstoreDataFetcherDelegate,
64 public base::RefCountedThreadSafe<ExternalInstallDialogDelegate> { 65 public base::RefCountedThreadSafe<ExternalInstallDialogDelegate> {
65 public: 66 public:
66 ExternalInstallDialogDelegate(Browser* browser, 67 ExternalInstallDialogDelegate(Browser* browser,
67 ExtensionService* service, 68 ExtensionService* service,
68 const Extension* extension, 69 const Extension* extension,
69 bool use_global_error); 70 bool use_global_error);
70 71
71 Browser* browser() { return browser_; } 72 Browser* browser() { return browser_; }
72 73
73 private: 74 private:
74 friend class base::RefCountedThreadSafe<ExternalInstallDialogDelegate>; 75 friend class base::RefCountedThreadSafe<ExternalInstallDialogDelegate>;
75 friend class ExternalInstallGlobalError; 76 friend class ExternalInstallGlobalError;
76 77
77 virtual ~ExternalInstallDialogDelegate(); 78 virtual ~ExternalInstallDialogDelegate();
78 79
79 // ExtensionInstallPrompt::Delegate: 80 // ExtensionInstallPrompt::Delegate:
80 virtual void InstallUIProceed() OVERRIDE; 81 virtual void InstallUIProceed() OVERRIDE;
81 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; 82 virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
82 83
84 // WebstoreDataFetcherDelegate:
85 virtual void OnWebstoreRequestFailure() OVERRIDE;
86 virtual void OnWebstoreResponseParseSuccess(
87 scoped_ptr<base::DictionaryValue> webstore_data) OVERRIDE;
88 virtual void OnWebstoreResponseParseFailure(
89 const std::string& error) OVERRIDE;
90
91 // Show the install dialog to the user.
92 void ShowInstallUI();
93
83 // The UI for showing the install dialog when enabling. 94 // The UI for showing the install dialog when enabling.
84 scoped_ptr<ExtensionInstallPrompt> install_ui_; 95 scoped_ptr<ExtensionInstallPrompt> install_ui_;
96 scoped_ptr<ExtensionInstallPrompt::Prompt> prompt_;
85 97
86 Browser* browser_; 98 Browser* browser_;
87 base::WeakPtr<ExtensionService> service_weak_; 99 base::WeakPtr<ExtensionService> service_weak_;
88 const std::string extension_id_; 100 scoped_ptr<WebstoreDataFetcher> webstore_data_fetcher_;
101 std::string extension_id_;
102 bool use_global_error_;
103
104 DISALLOW_COPY_AND_ASSIGN(ExternalInstallDialogDelegate);
89 }; 105 };
90 106
91 // Only shows a menu item, no bubble. Clicking the menu item shows 107 // Only shows a menu item, no bubble. Clicking the menu item shows
92 // an external install dialog. 108 // an external install dialog.
93 class ExternalInstallMenuAlert : public GlobalErrorWithStandardBubble, 109 class ExternalInstallMenuAlert : public GlobalErrorWithStandardBubble,
94 public content::NotificationObserver { 110 public content::NotificationObserver {
95 public: 111 public:
96 ExternalInstallMenuAlert(ExtensionService* service, 112 ExternalInstallMenuAlert(ExtensionService* service,
97 const Extension* extension); 113 const Extension* extension);
98 virtual ~ExternalInstallMenuAlert(); 114 virtual ~ExternalInstallMenuAlert();
99 115
100 const Extension* extension() const { return extension_; }
101
102 // GlobalError implementation. 116 // GlobalError implementation.
103 virtual Severity GetSeverity() OVERRIDE; 117 virtual Severity GetSeverity() OVERRIDE;
104 virtual bool HasMenuItem() OVERRIDE; 118 virtual bool HasMenuItem() OVERRIDE;
105 virtual int MenuItemCommandID() OVERRIDE; 119 virtual int MenuItemCommandID() OVERRIDE;
106 virtual base::string16 MenuItemLabel() OVERRIDE; 120 virtual base::string16 MenuItemLabel() OVERRIDE;
107 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE; 121 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE;
108 virtual bool HasBubbleView() OVERRIDE; 122 virtual bool HasBubbleView() OVERRIDE;
109 virtual base::string16 GetBubbleViewTitle() OVERRIDE; 123 virtual base::string16 GetBubbleViewTitle() OVERRIDE;
110 virtual std::vector<base::string16> GetBubbleViewMessages() OVERRIDE; 124 virtual std::vector<base::string16> GetBubbleViewMessages() OVERRIDE;
111 virtual base::string16 GetBubbleViewAcceptButtonLabel() OVERRIDE; 125 virtual base::string16 GetBubbleViewAcceptButtonLabel() OVERRIDE;
112 virtual base::string16 GetBubbleViewCancelButtonLabel() OVERRIDE; 126 virtual base::string16 GetBubbleViewCancelButtonLabel() OVERRIDE;
113 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE; 127 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE;
114 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE; 128 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE;
115 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE; 129 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE;
116 130
117 // content::NotificationObserver implementation. 131 // content::NotificationObserver implementation.
118 virtual void Observe(int type, 132 virtual void Observe(int type,
119 const content::NotificationSource& source, 133 const content::NotificationSource& source,
120 const content::NotificationDetails& details) OVERRIDE; 134 const content::NotificationDetails& details) OVERRIDE;
121 135
122 protected: 136 protected:
123 ExtensionService* service_; 137 ExtensionService* service_;
124 const Extension* extension_; 138 const Extension* extension_;
125 content::NotificationRegistrar registrar_; 139 content::NotificationRegistrar registrar_;
140
141 private:
142 DISALLOW_COPY_AND_ASSIGN(ExternalInstallMenuAlert);
126 }; 143 };
127 144
128 // Shows a menu item and a global error bubble, replacing the install dialog. 145 // Shows a menu item and a global error bubble, replacing the install dialog.
129 class ExternalInstallGlobalError : public ExternalInstallMenuAlert { 146 class ExternalInstallGlobalError : public ExternalInstallMenuAlert {
130 public: 147 public:
131 ExternalInstallGlobalError(ExtensionService* service, 148 ExternalInstallGlobalError(ExtensionService* service,
132 const Extension* extension, 149 const Extension* extension,
133 ExternalInstallDialogDelegate* delegate, 150 ExternalInstallDialogDelegate* delegate,
134 const ExtensionInstallPrompt::Prompt& prompt); 151 const ExtensionInstallPrompt::Prompt& prompt);
135 virtual ~ExternalInstallGlobalError(); 152 virtual ~ExternalInstallGlobalError();
136 153
137 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE; 154 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE;
138 virtual bool HasBubbleView() OVERRIDE; 155 virtual bool HasBubbleView() OVERRIDE;
139 virtual gfx::Image GetBubbleViewIcon() OVERRIDE; 156 virtual gfx::Image GetBubbleViewIcon() OVERRIDE;
140 virtual base::string16 GetBubbleViewTitle() OVERRIDE; 157 virtual base::string16 GetBubbleViewTitle() OVERRIDE;
141 virtual std::vector<base::string16> GetBubbleViewMessages() OVERRIDE; 158 virtual std::vector<base::string16> GetBubbleViewMessages() OVERRIDE;
142 virtual base::string16 GetBubbleViewAcceptButtonLabel() OVERRIDE; 159 virtual base::string16 GetBubbleViewAcceptButtonLabel() OVERRIDE;
143 virtual base::string16 GetBubbleViewCancelButtonLabel() OVERRIDE; 160 virtual base::string16 GetBubbleViewCancelButtonLabel() OVERRIDE;
144 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE; 161 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE;
145 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE; 162 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE;
146 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE; 163 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE;
147 164
148 protected: 165 protected:
149 // Ref-counted, but needs to be disposed of if we are dismissed without 166 // Ref-counted, but needs to be disposed of if we are dismissed without
150 // having been clicked (perhaps because the user enabled the extension 167 // having been clicked (perhaps because the user enabled the extension
151 // manually). 168 // manually).
152 ExternalInstallDialogDelegate* delegate_; 169 ExternalInstallDialogDelegate* delegate_;
153 const ExtensionInstallPrompt::Prompt* prompt_; 170 const ExtensionInstallPrompt::Prompt* prompt_;
171
172 private:
173 DISALLOW_COPY_AND_ASSIGN(ExternalInstallGlobalError);
154 }; 174 };
155 175
156 static void CreateExternalInstallGlobalError( 176 static void CreateExternalInstallGlobalError(
157 base::WeakPtr<ExtensionService> service, 177 base::WeakPtr<ExtensionService> service,
158 const std::string& extension_id, 178 const std::string& extension_id,
159 const ExtensionInstallPrompt::ShowParams& show_params, 179 const ExtensionInstallPrompt::ShowParams& show_params,
160 ExtensionInstallPrompt::Delegate* prompt_delegate, 180 ExtensionInstallPrompt::Delegate* prompt_delegate,
161 const ExtensionInstallPrompt::Prompt& prompt) { 181 const ExtensionInstallPrompt::Prompt& prompt) {
162 if (!service.get()) 182 if (!service.get())
163 return; 183 return;
(...skipping 25 matching lines...) Expand all
189 209
190 // ExternalInstallDialogDelegate -------------------------------------------- 210 // ExternalInstallDialogDelegate --------------------------------------------
191 211
192 ExternalInstallDialogDelegate::ExternalInstallDialogDelegate( 212 ExternalInstallDialogDelegate::ExternalInstallDialogDelegate(
193 Browser* browser, 213 Browser* browser,
194 ExtensionService* service, 214 ExtensionService* service,
195 const Extension* extension, 215 const Extension* extension,
196 bool use_global_error) 216 bool use_global_error)
197 : browser_(browser), 217 : browser_(browser),
198 service_weak_(service->AsWeakPtr()), 218 service_weak_(service->AsWeakPtr()),
199 extension_id_(extension->id()) { 219 extension_id_(extension->id()),
220 use_global_error_(use_global_error) {
200 AddRef(); // Balanced in Proceed or Abort. 221 AddRef(); // Balanced in Proceed or Abort.
201 222
223 prompt_.reset(new ExtensionInstallPrompt::Prompt(
224 ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT));
225
226 // If we don't have a browser, we can't go to the webstore to fetch data.
227 // This should only happen in tests.
228 if (!browser) {
229 ShowInstallUI();
230 return;
231 }
232
233 webstore_data_fetcher_.reset(new WebstoreDataFetcher(
234 this,
235 browser->profile()->GetRequestContext(),
236 GURL::EmptyGURL(),
237 extension->id()));
238 webstore_data_fetcher_->Start();
239 }
240
241 void ExternalInstallDialogDelegate::OnWebstoreRequestFailure() {
242 ShowInstallUI();
243 }
244
245 void ExternalInstallDialogDelegate::OnWebstoreResponseParseSuccess(
246 scoped_ptr<base::DictionaryValue> webstore_data) {
247 std::string localized_user_count;
248 double average_rating;
249 int rating_count;
250 if (!webstore_data->GetString(kUsersKey, &localized_user_count) ||
251 !webstore_data->GetDouble(kAverageRatingKey, &average_rating) ||
252 !webstore_data->GetInteger(kRatingCountKey, &rating_count)) {
253 // If we don't get a valid webstore response, short circuit, and continue
254 // to show a prompt without webstore data.
255 ShowInstallUI();
256 return;
257 }
258
259 bool show_user_count = true;
260 webstore_data->GetBoolean(kShowUserCountKey, &show_user_count);
261
262 prompt_->SetWebstoreData(localized_user_count,
263 show_user_count,
264 average_rating,
265 rating_count);
266
267 ShowInstallUI();
268 }
269
270 void ExternalInstallDialogDelegate::OnWebstoreResponseParseFailure(
271 const std::string& error) {
272 ShowInstallUI();
273 }
274
275 void ExternalInstallDialogDelegate::ShowInstallUI() {
276 const Extension* extension = NULL;
277 if (!service_weak_.get() ||
278 !(extension = service_weak_->GetInstalledExtension(extension_id_))) {
279 return;
280 }
202 install_ui_.reset( 281 install_ui_.reset(
203 ExtensionInstallUI::CreateInstallPromptWithBrowser(browser)); 282 ExtensionInstallUI::CreateInstallPromptWithBrowser(browser_));
204 283
205 const ExtensionInstallPrompt::ShowDialogCallback callback = 284 const ExtensionInstallPrompt::ShowDialogCallback callback =
206 use_global_error ? 285 use_global_error_ ?
207 base::Bind(&CreateExternalInstallGlobalError, 286 base::Bind(&CreateExternalInstallGlobalError,
208 service_weak_, extension_id_) : 287 service_weak_,
209 ExtensionInstallPrompt::GetDefaultShowDialogCallback(); 288 extension_id_) :
210 install_ui_->ConfirmExternalInstall(this, extension, callback); 289 ExtensionInstallPrompt::GetDefaultShowDialogCallback();
290
291 install_ui_->ConfirmExternalInstall(this, extension, callback, *prompt_);
211 } 292 }
212 293
213 ExternalInstallDialogDelegate::~ExternalInstallDialogDelegate() { 294 ExternalInstallDialogDelegate::~ExternalInstallDialogDelegate() {
214 } 295 }
215 296
216 void ExternalInstallDialogDelegate::InstallUIProceed() { 297 void ExternalInstallDialogDelegate::InstallUIProceed() {
217 if (!service_weak_.get()) 298 const Extension* extension = NULL;
299 if (!service_weak_.get() ||
300 !(extension = service_weak_->GetInstalledExtension(extension_id_))) {
218 return; 301 return;
219 const Extension* extension = 302 }
220 service_weak_->GetInstalledExtension(extension_id_);
221 if (!extension)
222 return;
223 service_weak_->GrantPermissionsAndEnableExtension(extension); 303 service_weak_->GrantPermissionsAndEnableExtension(extension);
224 Release(); 304 Release();
225 } 305 }
226 306
227 void ExternalInstallDialogDelegate::InstallUIAbort(bool user_initiated) { 307 void ExternalInstallDialogDelegate::InstallUIAbort(bool user_initiated) {
228 if (!service_weak_.get()) 308 const Extension* extension = NULL;
309 if (!service_weak_.get() ||
310 !(extension = service_weak_->GetInstalledExtension(extension_id_))) {
229 return; 311 return;
230 const Extension* extension = 312 }
231 service_weak_->GetInstalledExtension(extension_id_);
232 if (!extension)
233 return;
234 service_weak_->UninstallExtension(extension_id_, false, NULL); 313 service_weak_->UninstallExtension(extension_id_, false, NULL);
235 Release(); 314 Release();
236 } 315 }
237 316
238 // ExternalInstallMenuAlert ------------------------------------------------- 317 // ExternalInstallMenuAlert -------------------------------------------------
239 318
240 ExternalInstallMenuAlert::ExternalInstallMenuAlert( 319 ExternalInstallMenuAlert::ExternalInstallMenuAlert(
241 ExtensionService* service, 320 ExtensionService* service,
242 const Extension* extension) 321 const Extension* extension)
243 : service_(service), 322 : service_(service),
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 533
455 bool HasExternalInstallBubble(ExtensionService* service) { 534 bool HasExternalInstallBubble(ExtensionService* service) {
456 GlobalErrorService* error_service = 535 GlobalErrorService* error_service =
457 GlobalErrorServiceFactory::GetForProfile(service->profile()); 536 GlobalErrorServiceFactory::GetForProfile(service->profile());
458 GlobalError* error = error_service->GetGlobalErrorByMenuItemCommandID( 537 GlobalError* error = error_service->GetGlobalErrorByMenuItemCommandID(
459 kMenuCommandId); 538 kMenuCommandId);
460 return error && error->HasBubbleView(); 539 return error && error->HasBubbleView();
461 } 540 }
462 541
463 } // namespace extensions 542 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_install_prompt.cc ('k') | chrome/browser/extensions/webstore_data_fetcher_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698