add-flag-to-configure-extension-downloading.patch 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Add extension-mime-request-handling chrome://flag to tweak the behavior of
  2. # extension MIME types
  3. --- a/chrome/browser/about_flags.cc
  4. +++ b/chrome/browser/about_flags.cc
  5. @@ -249,6 +249,16 @@ const unsigned kOsDesktop = kOsMac | kOs
  6. const unsigned kOsAura = kOsWin | kOsLinux | kOsCrOS;
  7. #endif // USE_AURA || OS_ANDROID
  8. +const FeatureEntry::Choice kExtensionHandlingChoices[] = {
  9. + {flags_ui::kGenericExperimentChoiceDefault, "", ""},
  10. + {"Download as regular file",
  11. + "extension-mime-request-handling",
  12. + "download-as-regular-file"},
  13. + {"Always prompt for install",
  14. + "extension-mime-request-handling",
  15. + "always-prompt-for-install"},
  16. +};
  17. +
  18. const FeatureEntry::Choice kTouchEventFeatureDetectionChoices[] = {
  19. {flags_ui::kGenericExperimentChoiceDisabled, "", ""},
  20. {flags_ui::kGenericExperimentChoiceEnabled,
  21. @@ -1752,6 +1762,10 @@ const FeatureEntry::Choice kWebOtpBacken
  22. //
  23. // When adding a new choice, add it to the end of the list.
  24. const FeatureEntry kFeatureEntries[] = {
  25. + {"extension-mime-request-handling",
  26. + "Handling of extension MIME type requests",
  27. + "Used when deciding how to handle a request for a CRX or User Script MIME type",
  28. + kOsAll, MULTI_VALUE_TYPE(kExtensionHandlingChoices)},
  29. {"enable-stacked-tab-strip",
  30. "Enable stacking in tab strip",
  31. "Forces tabs to be stacked in the tab strip. Otherwise, they follow default behavior.",
  32. --- a/chrome/browser/download/download_crx_util.cc
  33. +++ b/chrome/browser/download/download_crx_util.cc
  34. @@ -6,6 +6,7 @@
  35. #include "chrome/browser/download/download_crx_util.h"
  36. +#include "base/command_line.h"
  37. #include "chrome/browser/chrome_notification_types.h"
  38. #include "chrome/browser/extensions/crx_installer.h"
  39. #include "chrome/browser/extensions/extension_install_prompt.h"
  40. @@ -126,6 +127,14 @@ scoped_refptr<extensions::CrxInstaller>
  41. return installer;
  42. }
  43. +bool ShouldDownloadAsRegularFile() {
  44. + const base::CommandLine& command_line =
  45. + *base::CommandLine::ForCurrentProcess();
  46. + return command_line.HasSwitch("extension-mime-request-handling") &&
  47. + command_line.GetSwitchValueASCII("extension-mime-request-handling") ==
  48. + "download-as-regular-file";
  49. +}
  50. +
  51. bool IsExtensionDownload(const DownloadItem& download_item) {
  52. if (download_item.GetTargetDisposition() ==
  53. DownloadItem::TARGET_DISPOSITION_PROMPT)
  54. @@ -134,7 +143,7 @@ bool IsExtensionDownload(const DownloadI
  55. if (download_item.GetMimeType() == extensions::Extension::kMimeType ||
  56. extensions::UserScript::IsURLUserScript(download_item.GetURL(),
  57. download_item.GetMimeType())) {
  58. - return true;
  59. + return !ShouldDownloadAsRegularFile();
  60. } else {
  61. return false;
  62. }
  63. --- a/chrome/browser/download/download_crx_util.h
  64. +++ b/chrome/browser/download/download_crx_util.h
  65. @@ -43,6 +43,10 @@ scoped_refptr<extensions::CrxInstaller>
  66. Profile* profile,
  67. const download::DownloadItem& download_item);
  68. +// Returns true if the user wants all extensions to be downloaded as regular
  69. +// files.
  70. +bool ShouldDownloadAsRegularFile();
  71. +
  72. // Returns true if this is an extension download. This also considers user
  73. // scripts to be extension downloads, since we convert those automatically.
  74. bool IsExtensionDownload(const download::DownloadItem& download_item);
  75. --- a/chrome/browser/download/download_target_determiner.cc
  76. +++ b/chrome/browser/download/download_target_determiner.cc
  77. @@ -1030,10 +1030,12 @@ DownloadConfirmationReason DownloadTarge
  78. return DownloadConfirmationReason::SAVE_AS;
  79. #if BUILDFLAG(ENABLE_EXTENSIONS)
  80. - // Don't prompt for extension downloads if the installation site is white
  81. - // listed.
  82. - if (download_crx_util::IsTrustedExtensionDownload(GetProfile(), *download_))
  83. - return DownloadConfirmationReason::NONE;
  84. + if (!download_crx_util::ShouldDownloadAsRegularFile()) {
  85. + // Don't prompt for extension downloads.
  86. + if (download_crx_util::IsTrustedExtensionDownload(GetProfile(), *download_) ||
  87. + filename.MatchesExtension(extensions::kExtensionFileExtension))
  88. + return DownloadConfirmationReason::NONE;
  89. + }
  90. #endif
  91. // Don't prompt for file types that are marked for opening automatically.
  92. --- a/chrome/browser/extensions/extension_management.cc
  93. +++ b/chrome/browser/extensions/extension_management.cc
  94. @@ -8,6 +8,7 @@
  95. #include "base/bind.h"
  96. #include "base/bind_helpers.h"
  97. +#include "base/command_line.h"
  98. #include "base/logging.h"
  99. #include "base/metrics/histogram_macros.h"
  100. #include "base/stl_util.h"
  101. @@ -203,6 +204,13 @@ bool ExtensionManagement::IsInstallation
  102. bool ExtensionManagement::IsOffstoreInstallAllowed(
  103. const GURL& url,
  104. const GURL& referrer_url) const {
  105. + const base::CommandLine& command_line =
  106. + *base::CommandLine::ForCurrentProcess();
  107. + if (command_line.HasSwitch("extension-mime-request-handling") &&
  108. + command_line.GetSwitchValueASCII("extension-mime-request-handling") ==
  109. + "always-prompt-for-install") {
  110. + return true;
  111. + }
  112. // No allowed install sites specified, disallow by default.
  113. if (!global_settings_->has_restricted_install_sources)
  114. return false;