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

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

Powered by Google App Engine
This is Rietveld 408576698