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 messages
.push_back(prompt_
->GetHeading());
214 if (prompt_
->GetPermissionCount(regular_permissions
)) {
215 messages
.push_back(prompt_
->GetPermissionsHeading(regular_permissions
));
216 for (size_t i
= 0; i
< prompt_
->GetPermissionCount(regular_permissions
);
218 messages
.push_back(l10n_util::GetStringFUTF16(
219 IDS_EXTENSION_PERMISSION_LINE
,
220 prompt_
->GetPermission(i
, regular_permissions
)));
223 if (prompt_
->GetPermissionCount(withheld_permissions
)) {
224 messages
.push_back(prompt_
->GetPermissionsHeading(withheld_permissions
));
225 for (size_t i
= 0; i
< prompt_
->GetPermissionCount(withheld_permissions
);
227 messages
.push_back(l10n_util::GetStringFUTF16(
228 IDS_EXTENSION_PERMISSION_LINE
,
229 prompt_
->GetPermission(i
, withheld_permissions
)));
232 // TODO(yoz): OAuth issue advice?
236 base::string16
ExternalInstallBubbleAlert::GetBubbleViewAcceptButtonLabel() {
237 return prompt_
->GetAcceptButtonLabel();
240 base::string16
ExternalInstallBubbleAlert::GetBubbleViewCancelButtonLabel() {
241 return prompt_
->GetAbortButtonLabel();
244 void ExternalInstallBubbleAlert::OnBubbleViewDidClose(Browser
* browser
) {
247 void ExternalInstallBubbleAlert::BubbleViewAcceptButtonPressed(
249 error_
->InstallUIProceed();
252 void ExternalInstallBubbleAlert::BubbleViewCancelButtonPressed(
254 error_
->InstallUIAbort(true);
259 ////////////////////////////////////////////////////////////////////////////////
260 // ExternalInstallError
262 ExternalInstallError::ExternalInstallError(
263 content::BrowserContext
* browser_context
,
264 const std::string
& extension_id
,
265 AlertType alert_type
,
266 ExternalInstallManager
* manager
)
267 : browser_context_(browser_context
),
268 extension_id_(extension_id
),
269 alert_type_(alert_type
),
271 error_service_(GlobalErrorServiceFactory::GetForProfile(
272 Profile::FromBrowserContext(browser_context_
))),
273 weak_factory_(this) {
274 prompt_
= new ExtensionInstallPrompt::Prompt(
275 ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT
);
277 webstore_data_fetcher_
.reset(new WebstoreDataFetcher(
278 this, browser_context_
->GetRequestContext(), GURL(), extension_id_
));
279 webstore_data_fetcher_
->Start();
282 ExternalInstallError::~ExternalInstallError() {
283 if (global_error_
.get())
284 error_service_
->RemoveGlobalError(global_error_
.get());
287 void ExternalInstallError::InstallUIProceed() {
288 const Extension
* extension
= GetExtension();
290 ExtensionSystem::Get(browser_context_
)
291 ->extension_service()
292 ->GrantPermissionsAndEnableExtension(extension
);
293 // Since the manager listens for the extension to be loaded, this will
294 // remove the error...
296 // ... Otherwise we have to do it explicitly.
297 manager_
->RemoveExternalInstallError();
301 void ExternalInstallError::InstallUIAbort(bool user_initiated
) {
302 if (user_initiated
&& GetExtension()) {
303 ExtensionSystem::Get(browser_context_
)
304 ->extension_service()
305 ->UninstallExtension(extension_id_
,
306 extensions::UNINSTALL_REASON_INSTALL_CANCELED
,
307 base::Bind(&base::DoNothing
),
308 NULL
); // Ignore error.
309 // Since the manager listens for the extension to be removed, this will
310 // remove the error...
312 // ... Otherwise we have to do it explicitly.
313 manager_
->RemoveExternalInstallError();
317 void ExternalInstallError::ShowDialog(Browser
* browser
) {
318 DCHECK(install_ui_
.get());
319 DCHECK(prompt_
.get());
321 content::WebContents
* web_contents
= NULL
;
322 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
323 install_ui_show_params_
.reset(
324 new ExtensionInstallPromptShowParams(web_contents
));
325 ExtensionInstallPrompt::GetDefaultShowDialogCallback().Run(
326 install_ui_show_params_
.get(), this, prompt_
);
329 const Extension
* ExternalInstallError::GetExtension() const {
330 return ExtensionRegistry::Get(browser_context_
)
331 ->GetExtensionById(extension_id_
, ExtensionRegistry::EVERYTHING
);
334 void ExternalInstallError::OnWebstoreRequestFailure() {
338 void ExternalInstallError::OnWebstoreResponseParseSuccess(
339 scoped_ptr
<base::DictionaryValue
> webstore_data
) {
340 std::string localized_user_count
;
341 double average_rating
= 0;
342 int rating_count
= 0;
343 if (!webstore_data
->GetString(kUsersKey
, &localized_user_count
) ||
344 !webstore_data
->GetDouble(kAverageRatingKey
, &average_rating
) ||
345 !webstore_data
->GetInteger(kRatingCountKey
, &rating_count
)) {
346 // If we don't get a valid webstore response, short circuit, and continue
347 // to show a prompt without webstore data.
352 bool show_user_count
= true;
353 webstore_data
->GetBoolean(kShowUserCountKey
, &show_user_count
);
355 prompt_
->SetWebstoreData(
356 localized_user_count
, show_user_count
, average_rating
, rating_count
);
360 void ExternalInstallError::OnWebstoreResponseParseFailure(
361 const std::string
& error
) {
365 void ExternalInstallError::OnFetchComplete() {
366 // Create a new ExtensionInstallPrompt. We pass in NULL for the UI
367 // components because we display at a later point, and don't want
368 // to pass ones which may be invalidated.
370 new ExtensionInstallPrompt(Profile::FromBrowserContext(browser_context_
),
371 NULL
)); // NULL native window.
373 install_ui_
->ConfirmExternalInstall(
376 base::Bind(&ExternalInstallError::OnDialogReady
,
377 weak_factory_
.GetWeakPtr()),
381 void ExternalInstallError::OnDialogReady(
382 ExtensionInstallPromptShowParams
* show_params
,
383 ExtensionInstallPrompt::Delegate
* prompt_delegate
,
384 scoped_refptr
<ExtensionInstallPrompt::Prompt
> prompt
) {
385 DCHECK_EQ(this, prompt_delegate
);
388 if (alert_type_
== BUBBLE_ALERT
) {
389 global_error_
.reset(new ExternalInstallBubbleAlert(this, prompt_
.get()));
390 error_service_
->AddGlobalError(global_error_
.get());
393 chrome::FindTabbedBrowser(Profile::FromBrowserContext(browser_context_
),
395 chrome::GetActiveDesktop());
397 global_error_
->ShowBubbleView(browser
);
399 DCHECK(alert_type_
== MENU_ALERT
);
400 global_error_
.reset(new ExternalInstallMenuAlert(this));
401 error_service_
->AddGlobalError(global_error_
.get());
405 } // namespace extensions