1 // Copyright 2014 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/external_install_error.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/extensions/extension_install_prompt_show_params.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/external_install_manager.h"
13 #include "chrome/browser/extensions/webstore_data_fetcher.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/global_error/global_error.h"
18 #include "chrome/browser/ui/global_error/global_error_service.h"
19 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/browser/uninstall_reason.h"
25 #include "extensions/common/constants.h"
26 #include "extensions/common/extension.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/gfx/image/image.h"
29 #include "ui/gfx/image/image_skia_operations.h"
31 namespace extensions
{
35 // Return the menu label for a global error.
36 base::string16
GetMenuItemLabel(const Extension
* extension
) {
38 return base::string16();
41 if (extension
->is_app())
42 id
= IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_APP
;
43 else if (extension
->is_theme())
44 id
= IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_THEME
;
46 id
= IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_EXTENSION
;
48 return l10n_util::GetStringFUTF16(id
, base::UTF8ToUTF16(extension
->name()));
51 // A global error that spawns a dialog when the menu item is clicked.
52 class ExternalInstallMenuAlert
: public GlobalError
{
54 explicit ExternalInstallMenuAlert(ExternalInstallError
* error
);
55 ~ExternalInstallMenuAlert() override
;
58 // GlobalError implementation.
59 Severity
GetSeverity() override
;
60 bool HasMenuItem() override
;
61 int MenuItemCommandID() override
;
62 base::string16
MenuItemLabel() override
;
63 void ExecuteMenuItem(Browser
* browser
) override
;
64 bool HasBubbleView() override
;
65 bool HasShownBubbleView() override
;
66 void ShowBubbleView(Browser
* browser
) override
;
67 GlobalErrorBubbleViewBase
* GetBubbleView() override
;
69 // The owning ExternalInstallError.
70 ExternalInstallError
* error_
;
72 DISALLOW_COPY_AND_ASSIGN(ExternalInstallMenuAlert
);
75 // A global error that spawns a bubble when the menu item is clicked.
76 class ExternalInstallBubbleAlert
: public GlobalErrorWithStandardBubble
{
78 explicit ExternalInstallBubbleAlert(ExternalInstallError
* error
,
79 ExtensionInstallPrompt::Prompt
* prompt
);
80 ~ExternalInstallBubbleAlert() override
;
83 // GlobalError implementation.
84 Severity
GetSeverity() override
;
85 bool HasMenuItem() override
;
86 int MenuItemCommandID() override
;
87 base::string16
MenuItemLabel() override
;
88 void ExecuteMenuItem(Browser
* browser
) override
;
90 // GlobalErrorWithStandardBubble implementation.
91 gfx::Image
GetBubbleViewIcon() override
;
92 base::string16
GetBubbleViewTitle() override
;
93 std::vector
<base::string16
> GetBubbleViewMessages() override
;
94 base::string16
GetBubbleViewAcceptButtonLabel() override
;
95 base::string16
GetBubbleViewCancelButtonLabel() override
;
96 void OnBubbleViewDidClose(Browser
* browser
) override
;
97 void BubbleViewAcceptButtonPressed(Browser
* browser
) override
;
98 void BubbleViewCancelButtonPressed(Browser
* browser
) override
;
100 // The owning ExternalInstallError.
101 ExternalInstallError
* error_
;
103 // The Prompt with all information, which we then use to populate the bubble.
104 ExtensionInstallPrompt::Prompt
* prompt_
;
106 DISALLOW_COPY_AND_ASSIGN(ExternalInstallBubbleAlert
);
109 ////////////////////////////////////////////////////////////////////////////////
110 // ExternalInstallMenuAlert
112 ExternalInstallMenuAlert::ExternalInstallMenuAlert(ExternalInstallError
* error
)
116 ExternalInstallMenuAlert::~ExternalInstallMenuAlert() {
119 GlobalError::Severity
ExternalInstallMenuAlert::GetSeverity() {
123 bool ExternalInstallMenuAlert::HasMenuItem() {
127 int ExternalInstallMenuAlert::MenuItemCommandID() {
128 return IDC_EXTERNAL_EXTENSION_ALERT
;
131 base::string16
ExternalInstallMenuAlert::MenuItemLabel() {
132 return GetMenuItemLabel(error_
->GetExtension());
135 void ExternalInstallMenuAlert::ExecuteMenuItem(Browser
* browser
) {
136 error_
->ShowDialog(browser
);
139 bool ExternalInstallMenuAlert::HasBubbleView() {
143 bool ExternalInstallMenuAlert::HasShownBubbleView() {
148 void ExternalInstallMenuAlert::ShowBubbleView(Browser
* browser
) {
152 GlobalErrorBubbleViewBase
* ExternalInstallMenuAlert::GetBubbleView() {
156 ////////////////////////////////////////////////////////////////////////////////
157 // ExternalInstallBubbleAlert
159 ExternalInstallBubbleAlert::ExternalInstallBubbleAlert(
160 ExternalInstallError
* error
,
161 ExtensionInstallPrompt::Prompt
* prompt
)
162 : error_(error
), prompt_(prompt
) {
167 ExternalInstallBubbleAlert::~ExternalInstallBubbleAlert() {
170 GlobalError::Severity
ExternalInstallBubbleAlert::GetSeverity() {
174 bool ExternalInstallBubbleAlert::HasMenuItem() {
178 int ExternalInstallBubbleAlert::MenuItemCommandID() {
179 return IDC_EXTERNAL_EXTENSION_ALERT
;
182 base::string16
ExternalInstallBubbleAlert::MenuItemLabel() {
183 return GetMenuItemLabel(error_
->GetExtension());
186 void ExternalInstallBubbleAlert::ExecuteMenuItem(Browser
* browser
) {
187 ShowBubbleView(browser
);
190 gfx::Image
ExternalInstallBubbleAlert::GetBubbleViewIcon() {
191 if (prompt_
->icon().IsEmpty())
192 return GlobalErrorWithStandardBubble::GetBubbleViewIcon();
193 // Scale icon to a reasonable size.
194 return gfx::Image(gfx::ImageSkiaOperations::CreateResizedImage(
195 *prompt_
->icon().ToImageSkia(),
196 skia::ImageOperations::RESIZE_BEST
,
197 gfx::Size(extension_misc::EXTENSION_ICON_SMALL
,
198 extension_misc::EXTENSION_ICON_SMALL
)));
201 base::string16
ExternalInstallBubbleAlert::GetBubbleViewTitle() {
202 return prompt_
->GetDialogTitle();
205 std::vector
<base::string16
>
206 ExternalInstallBubbleAlert::GetBubbleViewMessages() {
207 ExtensionInstallPrompt::PermissionsType regular_permissions
=
208 ExtensionInstallPrompt::PermissionsType::REGULAR_PERMISSIONS
;
209 ExtensionInstallPrompt::PermissionsType withheld_permissions
=
210 ExtensionInstallPrompt::PermissionsType::WITHHELD_PERMISSIONS
;
212 std::vector
<base::string16
> messages
;
213 if (prompt_
->GetPermissionCount(regular_permissions
)) {
214 messages
.push_back(prompt_
->GetPermissionsHeading(regular_permissions
));
215 for (size_t i
= 0; i
< prompt_
->GetPermissionCount(regular_permissions
);
217 messages
.push_back(l10n_util::GetStringFUTF16(
218 IDS_EXTENSION_PERMISSION_LINE
,
219 prompt_
->GetPermission(i
, regular_permissions
)));
222 if (prompt_
->GetPermissionCount(withheld_permissions
)) {
223 messages
.push_back(prompt_
->GetPermissionsHeading(withheld_permissions
));
224 for (size_t i
= 0; i
< prompt_
->GetPermissionCount(withheld_permissions
);
226 messages
.push_back(l10n_util::GetStringFUTF16(
227 IDS_EXTENSION_PERMISSION_LINE
,
228 prompt_
->GetPermission(i
, withheld_permissions
)));
231 // TODO(yoz): OAuth issue advice?
235 base::string16
ExternalInstallBubbleAlert::GetBubbleViewAcceptButtonLabel() {
236 return prompt_
->GetAcceptButtonLabel();
239 base::string16
ExternalInstallBubbleAlert::GetBubbleViewCancelButtonLabel() {
240 return prompt_
->GetAbortButtonLabel();
243 void ExternalInstallBubbleAlert::OnBubbleViewDidClose(Browser
* browser
) {
246 void ExternalInstallBubbleAlert::BubbleViewAcceptButtonPressed(
248 error_
->InstallUIProceed();
251 void ExternalInstallBubbleAlert::BubbleViewCancelButtonPressed(
253 error_
->InstallUIAbort(true);
258 ////////////////////////////////////////////////////////////////////////////////
259 // ExternalInstallError
261 ExternalInstallError::ExternalInstallError(
262 content::BrowserContext
* browser_context
,
263 const std::string
& extension_id
,
264 AlertType alert_type
,
265 ExternalInstallManager
* manager
)
266 : browser_context_(browser_context
),
267 extension_id_(extension_id
),
268 alert_type_(alert_type
),
270 error_service_(GlobalErrorServiceFactory::GetForProfile(
271 Profile::FromBrowserContext(browser_context_
))),
272 weak_factory_(this) {
273 prompt_
= new ExtensionInstallPrompt::Prompt(
274 ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT
);
276 webstore_data_fetcher_
.reset(new WebstoreDataFetcher(
277 this, browser_context_
->GetRequestContext(), GURL(), extension_id_
));
278 webstore_data_fetcher_
->Start();
281 ExternalInstallError::~ExternalInstallError() {
282 if (global_error_
.get())
283 error_service_
->RemoveGlobalError(global_error_
.get());
286 void ExternalInstallError::InstallUIProceed() {
287 const Extension
* extension
= GetExtension();
289 ExtensionSystem::Get(browser_context_
)
290 ->extension_service()
291 ->GrantPermissionsAndEnableExtension(extension
);
292 // Since the manager listens for the extension to be loaded, this will
293 // remove the error...
295 // ... Otherwise we have to do it explicitly.
296 manager_
->RemoveExternalInstallError();
300 void ExternalInstallError::InstallUIAbort(bool user_initiated
) {
301 if (user_initiated
&& GetExtension()) {
302 ExtensionSystem::Get(browser_context_
)
303 ->extension_service()
304 ->UninstallExtension(extension_id_
,
305 extensions::UNINSTALL_REASON_INSTALL_CANCELED
,
306 base::Bind(&base::DoNothing
),
307 NULL
); // Ignore error.
308 // Since the manager listens for the extension to be removed, this will
309 // remove the error...
311 // ... Otherwise we have to do it explicitly.
312 manager_
->RemoveExternalInstallError();
316 void ExternalInstallError::ShowDialog(Browser
* browser
) {
317 DCHECK(install_ui_
.get());
318 DCHECK(prompt_
.get());
320 content::WebContents
* web_contents
= NULL
;
321 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
322 install_ui_show_params_
.reset(
323 new ExtensionInstallPromptShowParams(web_contents
));
324 ExtensionInstallPrompt::GetDefaultShowDialogCallback().Run(
325 install_ui_show_params_
.get(), this, prompt_
);
328 const Extension
* ExternalInstallError::GetExtension() const {
329 return ExtensionRegistry::Get(browser_context_
)
330 ->GetExtensionById(extension_id_
, ExtensionRegistry::EVERYTHING
);
333 void ExternalInstallError::OnWebstoreRequestFailure() {
337 void ExternalInstallError::OnWebstoreResponseParseSuccess(
338 scoped_ptr
<base::DictionaryValue
> webstore_data
) {
339 std::string localized_user_count
;
340 double average_rating
= 0;
341 int rating_count
= 0;
342 if (!webstore_data
->GetString(kUsersKey
, &localized_user_count
) ||
343 !webstore_data
->GetDouble(kAverageRatingKey
, &average_rating
) ||
344 !webstore_data
->GetInteger(kRatingCountKey
, &rating_count
)) {
345 // If we don't get a valid webstore response, short circuit, and continue
346 // to show a prompt without webstore data.
351 bool show_user_count
= true;
352 webstore_data
->GetBoolean(kShowUserCountKey
, &show_user_count
);
354 prompt_
->SetWebstoreData(
355 localized_user_count
, show_user_count
, average_rating
, rating_count
);
359 void ExternalInstallError::OnWebstoreResponseParseFailure(
360 const std::string
& error
) {
364 void ExternalInstallError::OnFetchComplete() {
365 // Create a new ExtensionInstallPrompt. We pass in NULL for the UI
366 // components because we display at a later point, and don't want
367 // to pass ones which may be invalidated.
369 new ExtensionInstallPrompt(Profile::FromBrowserContext(browser_context_
),
370 NULL
)); // NULL native window.
372 install_ui_
->ConfirmExternalInstall(
375 base::Bind(&ExternalInstallError::OnDialogReady
,
376 weak_factory_
.GetWeakPtr()),
380 void ExternalInstallError::OnDialogReady(
381 ExtensionInstallPromptShowParams
* show_params
,
382 ExtensionInstallPrompt::Delegate
* prompt_delegate
,
383 scoped_refptr
<ExtensionInstallPrompt::Prompt
> prompt
) {
384 DCHECK_EQ(this, prompt_delegate
);
387 if (alert_type_
== BUBBLE_ALERT
) {
388 global_error_
.reset(new ExternalInstallBubbleAlert(this, prompt_
.get()));
389 error_service_
->AddGlobalError(global_error_
.get());
392 chrome::FindTabbedBrowser(Profile::FromBrowserContext(browser_context_
),
394 chrome::GetActiveDesktop());
396 global_error_
->ShowBubbleView(browser
);
398 DCHECK(alert_type_
== MENU_ALERT
);
399 global_error_
.reset(new ExternalInstallMenuAlert(this));
400 error_service_
->AddGlobalError(global_error_
.get());
404 } // namespace extensions