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

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

Issue 11087071: Making ShowExtensionInstallDialog a callback (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 8 years, 2 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 | Annotate | Revision Log
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/extension_install_prompt.h" 5 #include "chrome/browser/extensions/extension_install_prompt.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 0, // Bundle installs don't show OAuth permissions. 88 0, // Bundle installs don't show OAuth permissions.
89 IDS_EXTENSION_PROMPT_OAUTH_REENABLE_HEADER, 89 IDS_EXTENSION_PROMPT_OAUTH_REENABLE_HEADER,
90 IDS_EXTENSION_PROMPT_OAUTH_PERMISSIONS_HEADER, 90 IDS_EXTENSION_PROMPT_OAUTH_PERMISSIONS_HEADER,
91 }; 91 };
92 92
93 namespace { 93 namespace {
94 94
95 // Size of extension icon in top left of dialog. 95 // Size of extension icon in top left of dialog.
96 const int kIconSize = 69; 96 const int kIconSize = 69;
97 97
98 // A flag used for SetExtensionInstallDialogAutoConfirmForTests
99 enum AutoConfirmForTest {
100 DO_NOT_SKIP = 0,
101 PROCEED,
102 ABORT
103 };
104
98 // Returns pixel size under maximal scale factor for the icon whose device 105 // Returns pixel size under maximal scale factor for the icon whose device
99 // independent size is |size_in_dip| 106 // independent size is |size_in_dip|
100 int GetSizeForMaxScaleFactor(int size_in_dip) { 107 int GetSizeForMaxScaleFactor(int size_in_dip) {
101 std::vector<ui::ScaleFactor> supported_scale_factors = 108 std::vector<ui::ScaleFactor> supported_scale_factors =
102 ui::GetSupportedScaleFactors(); 109 ui::GetSupportedScaleFactors();
103 // Scale factors are in ascending order, so the last one is the one we need. 110 // Scale factors are in ascending order, so the last one is the one we need.
104 ui::ScaleFactor max_scale_factor = supported_scale_factors.back(); 111 ui::ScaleFactor max_scale_factor = supported_scale_factors.back();
105 float max_scale_factor_scale = ui::GetScaleFactorScale(max_scale_factor); 112 float max_scale_factor_scale = ui::GetScaleFactorScale(max_scale_factor);
106 113
107 return static_cast<int>(size_in_dip * max_scale_factor_scale); 114 return static_cast<int>(size_in_dip * max_scale_factor_scale);
108 } 115 }
109 116
110 // Returns bitmap for the default icon with size equal to the default icon's 117 // Returns bitmap for the default icon with size equal to the default icon's
111 // pixel size under maximal supported scale factor. 118 // pixel size under maximal supported scale factor.
112 SkBitmap GetDefaultIconBitmapForMaxScaleFactor(bool is_app) { 119 SkBitmap GetDefaultIconBitmapForMaxScaleFactor(bool is_app) {
113 std::vector<ui::ScaleFactor> supported_scale_factors = 120 std::vector<ui::ScaleFactor> supported_scale_factors =
114 ui::GetSupportedScaleFactors(); 121 ui::GetSupportedScaleFactors();
115 // Scale factors are in ascending order, so the last one is the one we need. 122 // Scale factors are in ascending order, so the last one is the one we need.
116 ui::ScaleFactor max_scale_factor = 123 ui::ScaleFactor max_scale_factor =
117 supported_scale_factors[supported_scale_factors.size() - 1]; 124 supported_scale_factors[supported_scale_factors.size() - 1];
118 125
119 return Extension::GetDefaultIcon(is_app). 126 return Extension::GetDefaultIcon(is_app).
120 GetRepresentation(max_scale_factor).sk_bitmap(); 127 GetRepresentation(max_scale_factor).sk_bitmap();
121 } 128 }
122 129
130 void AutoConfirmTask(ExtensionInstallPrompt::Delegate* delegate, bool proceed) {
131 if (proceed)
132 delegate->InstallUIProceed();
133 else
134 delegate->InstallUIAbort(true);
135 }
136
137 void DoAutoConfirm(AutoConfirmForTest setting,
138 ExtensionInstallPrompt::Delegate* delegate) {
139 bool proceed = (setting == PROCEED);
140 // We use PostTask instead of calling the delegate directly here, because in
141 // the real implementations it's highly likely the message loop will be
142 // pumping a few times before the user clicks accept or cancel.
143 MessageLoop::current()->PostTask(
144 FROM_HERE,
145 base::Bind(&AutoConfirmTask, delegate, proceed));
146 }
147
148 AutoConfirmForTest CheckAutoConfirmCommandLineSwitch() {
149 const CommandLine* cmdline = CommandLine::ForCurrentProcess();
150 if (!cmdline->HasSwitch(switches::kAppsGalleryInstallAutoConfirmForTests))
151 return DO_NOT_SKIP;
152 std::string value = cmdline->GetSwitchValueASCII(
153 switches::kAppsGalleryInstallAutoConfirmForTests);
154 if (value == "accept")
155 return PROCEED;
156 else if (value == "cancel")
157 return ABORT;
158 else
159 NOTREACHED();
160 return DO_NOT_SKIP;
161 }
162
123 } // namespace 163 } // namespace
124 164
125 ExtensionInstallPrompt::Prompt::Prompt(Profile* profile, PromptType type) 165 ExtensionInstallPrompt::Prompt::Prompt(Profile* profile, PromptType type)
126 : type_(type), 166 : type_(type),
127 extension_(NULL), 167 extension_(NULL),
128 bundle_(NULL), 168 bundle_(NULL),
129 average_rating_(0.0), 169 average_rating_(0.0),
130 rating_count_(0), 170 rating_count_(0),
131 profile_(profile) { 171 profile_(profile) {
132 } 172 }
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 extension_ = extension; 381 extension_ = extension;
342 permissions_ = extension->GetActivePermissions(); 382 permissions_ = extension->GetActivePermissions();
343 delegate_ = delegate; 383 delegate_ = delegate;
344 prompt_ = prompt; 384 prompt_ = prompt;
345 prompt_type_ = prompt.type(); 385 prompt_type_ = prompt.type();
346 386
347 SetIcon(icon); 387 SetIcon(icon);
348 FetchOAuthIssueAdviceIfNeeded(); 388 FetchOAuthIssueAdviceIfNeeded();
349 } 389 }
350 390
351 void ExtensionInstallPrompt::ConfirmWebstoreInstall(Delegate* delegate, 391 void ExtensionInstallPrompt::ConfirmWebstoreInstall(
352 const Extension* extension, 392 Delegate* delegate,
353 const SkBitmap* icon) { 393 const Extension* extension,
394 const SkBitmap* icon,
395 scoped_refptr<ExtensionInstallDialog> dialog) {
354 // SetIcon requires |extension_| to be set. ConfirmInstall will setup the 396 // SetIcon requires |extension_| to be set. ConfirmInstall will setup the
355 // remaining fields. 397 // remaining fields.
356 extension_ = extension; 398 extension_ = extension;
357 SetIcon(icon); 399 SetIcon(icon);
358 ConfirmInstall(delegate, extension); 400 ConfirmInstall(delegate, extension, dialog);
359 } 401 }
360 402
361 void ExtensionInstallPrompt::ConfirmInstall(Delegate* delegate, 403 void ExtensionInstallPrompt::ConfirmInstall(
362 const Extension* extension) { 404 Delegate* delegate,
405 const Extension* extension,
406 scoped_refptr<ExtensionInstallDialog> dialog) {
363 DCHECK(ui_loop_ == MessageLoop::current()); 407 DCHECK(ui_loop_ == MessageLoop::current());
364 extension_ = extension; 408 extension_ = extension;
365 permissions_ = extension->GetActivePermissions(); 409 permissions_ = extension->GetActivePermissions();
366 delegate_ = delegate; 410 delegate_ = delegate;
367 prompt_type_ = INSTALL_PROMPT; 411 prompt_type_ = INSTALL_PROMPT;
412 dialog_ = dialog;
368 413
369 // We special-case themes to not show any confirm UI. Instead they are 414 // We special-case themes to not show any confirm UI. Instead they are
370 // immediately installed, and then we show an infobar (see OnInstallSuccess) 415 // immediately installed, and then we show an infobar (see OnInstallSuccess)
371 // to allow the user to revert if they don't like it. 416 // to allow the user to revert if they don't like it.
372 // 417 //
373 // We don't do this in the case where off-store extension installs are 418 // We don't do this in the case where off-store extension installs are
374 // disabled because in that case, we don't show the dangerous download UI, so 419 // disabled because in that case, we don't show the dangerous download UI, so
375 // we need the UI confirmation. 420 // we need the UI confirmation.
376 if (extension->is_theme()) { 421 if (extension->is_theme()) {
377 if (extension->from_webstore() || 422 if (extension->from_webstore() ||
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 prompt_.SetPermissions(permissions_->GetWarningMessages(extension_type)); 575 prompt_.SetPermissions(permissions_->GetWarningMessages(extension_type));
531 } 576 }
532 577
533 switch (prompt_type_) { 578 switch (prompt_type_) {
534 case PERMISSIONS_PROMPT: 579 case PERMISSIONS_PROMPT:
535 case RE_ENABLE_PROMPT: 580 case RE_ENABLE_PROMPT:
536 case INLINE_INSTALL_PROMPT: 581 case INLINE_INSTALL_PROMPT:
537 case INSTALL_PROMPT: { 582 case INSTALL_PROMPT: {
538 prompt_.set_extension(extension_); 583 prompt_.set_extension(extension_);
539 prompt_.set_icon(gfx::Image(icon_)); 584 prompt_.set_icon(gfx::Image(icon_));
540 ShowExtensionInstallDialog(parent_, navigator_, delegate_, prompt_);
541 break; 585 break;
542 } 586 }
543 case BUNDLE_INSTALL_PROMPT: { 587 case BUNDLE_INSTALL_PROMPT: {
544 prompt_.set_bundle(bundle_); 588 prompt_.set_bundle(bundle_);
545 ShowExtensionInstallDialog(parent_, navigator_, delegate_, prompt_);
546 break; 589 break;
547 } 590 }
548 default: 591 default:
549 NOTREACHED() << "Unknown message"; 592 NOTREACHED() << "Unknown message";
550 break; 593 return;
594 }
595
596 AutoConfirmForTest auto_confirm = CheckAutoConfirmCommandLineSwitch();
597 if (auto_confirm != DO_NOT_SKIP) {
598 DoAutoConfirm(auto_confirm, delegate_);
599 return;
600 }
601
602 if (dialog_.get()) {
603 dialog_->ShowExtensionInstallDialog(
604 parent_, navigator_, delegate_, prompt_);
605 } else {
606 scoped_refptr<ExtensionInstallDialog> dialog(
607 ExtensionInstallDialog::CreateDefaultImpl());
608 dialog->ShowExtensionInstallDialog(
609 parent_, navigator_, delegate_, prompt_);
551 } 610 }
552 } 611 }
553 612
554 namespace chrome { 613 namespace chrome {
555 614
556 ExtensionInstallPrompt* CreateExtensionInstallPromptWithBrowser( 615 ExtensionInstallPrompt* CreateExtensionInstallPromptWithBrowser(
557 Browser* browser) { 616 Browser* browser) {
558 // |browser| can be NULL in unit tests. 617 // |browser| can be NULL in unit tests.
559 if (!browser) 618 if (!browser)
560 return new ExtensionInstallPrompt(NULL, NULL, NULL); 619 return new ExtensionInstallPrompt(NULL, NULL, NULL);
561 gfx::NativeWindow parent = 620 gfx::NativeWindow parent =
562 browser->window() ? browser->window()->GetNativeWindow() : NULL; 621 browser->window() ? browser->window()->GetNativeWindow() : NULL;
563 return new ExtensionInstallPrompt(parent, browser, browser->profile()); 622 return new ExtensionInstallPrompt(parent, browser, browser->profile());
564 } 623 }
565 624
566 } // namespace chrome 625 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698