add-flag-to-configure-extension-downloading.patch 5.0 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. @@ -225,6 +225,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. @@ -1193,6 +1203,10 @@ const FeatureEntry::Choice kNotification
  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. @@ -120,6 +121,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. @@ -128,7 +137,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/extensions/extension_management.cc
  64. +++ b/chrome/browser/extensions/extension_management.cc
  65. @@ -8,6 +8,7 @@
  66. #include "base/bind.h"
  67. #include "base/bind_helpers.h"
  68. +#include "base/command_line.h"
  69. #include "base/logging.h"
  70. #include "base/metrics/histogram_macros.h"
  71. #include "base/stl_util.h"
  72. @@ -166,6 +167,13 @@ bool ExtensionManagement::IsInstallation
  73. bool ExtensionManagement::IsOffstoreInstallAllowed(
  74. const GURL& url,
  75. const GURL& referrer_url) const {
  76. + const base::CommandLine& command_line =
  77. + *base::CommandLine::ForCurrentProcess();
  78. + if (command_line.HasSwitch("extension-mime-request-handling") &&
  79. + command_line.GetSwitchValueASCII("extension-mime-request-handling") ==
  80. + "always-prompt-for-install") {
  81. + return true;
  82. + }
  83. // No allowed install sites specified, disallow by default.
  84. if (!global_settings_->has_restricted_install_sources)
  85. return false;
  86. --- a/chrome/browser/download/download_crx_util.h
  87. +++ b/chrome/browser/download/download_crx_util.h
  88. @@ -43,6 +43,10 @@ scoped_refptr<extensions::CrxInstaller>
  89. Profile* profile,
  90. const download::DownloadItem& download_item);
  91. +// Returns true if the user wants all extensions to be downloaded as regular
  92. +// files.
  93. +bool ShouldDownloadAsRegularFile();
  94. +
  95. // Returns true if this is an extension download. This also considers user
  96. // scripts to be extension downloads, since we convert those automatically.
  97. bool IsExtensionDownload(const download::DownloadItem& download_item);
  98. --- a/chrome/browser/download/download_target_determiner.cc
  99. +++ b/chrome/browser/download/download_target_determiner.cc
  100. @@ -1001,10 +1001,12 @@ DownloadConfirmationReason DownloadTarge
  101. return DownloadConfirmationReason::SAVE_AS;
  102. #if BUILDFLAG(ENABLE_EXTENSIONS)
  103. - // Don't prompt for extension downloads.
  104. - if (download_crx_util::IsExtensionDownload(*download_) ||
  105. - filename.MatchesExtension(extensions::kExtensionFileExtension))
  106. - return DownloadConfirmationReason::NONE;
  107. + if (!download_crx_util::ShouldDownloadAsRegularFile()) {
  108. + // Don't prompt for extension downloads.
  109. + if (download_crx_util::IsExtensionDownload(*download_) ||
  110. + filename.MatchesExtension(extensions::kExtensionFileExtension))
  111. + return DownloadConfirmationReason::NONE;
  112. + }
  113. #endif
  114. // Don't prompt for file types that are marked for opening automatically.