electron_extension_loader.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2018 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.
  4. #include "shell/browser/extensions/electron_extension_loader.h"
  5. #include <utility>
  6. #include "base/auto_reset.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/functional/bind.h"
  10. #include "base/logging.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/task/sequenced_task_runner.h"
  13. #include "base/threading/thread_restrictions.h"
  14. #include "base/time/time.h"
  15. #include "extensions/browser/extension_file_task_runner.h"
  16. #include "extensions/browser/extension_prefs.h"
  17. #include "extensions/browser/extension_registry.h"
  18. #include "extensions/browser/pref_names.h"
  19. #include "extensions/common/error_utils.h"
  20. #include "extensions/common/file_util.h"
  21. #include "extensions/common/manifest_constants.h"
  22. namespace extensions {
  23. using LoadErrorBehavior = ExtensionRegistrar::LoadErrorBehavior;
  24. namespace {
  25. std::pair<scoped_refptr<const Extension>, std::string> LoadUnpacked(
  26. const base::FilePath& extension_dir,
  27. int load_flags) {
  28. // app_shell only supports unpacked extensions.
  29. // NOTE: If you add packed extension support consider removing the flag
  30. // FOLLOW_SYMLINKS_ANYWHERE below. Packed extensions should not have symlinks.
  31. if (!base::DirectoryExists(extension_dir)) {
  32. std::string err = "Extension directory not found: " +
  33. base::UTF16ToUTF8(extension_dir.LossyDisplayName());
  34. return std::make_pair(nullptr, err);
  35. }
  36. // remove _metadata folder. Otherwise, the following warning will be thrown
  37. // Cannot load extension with file or directory name _metadata.
  38. // Filenames starting with "_" are reserved for use by the system.
  39. // see: https://bugs.chromium.org/p/chromium/issues/detail?id=377278
  40. base::FilePath metadata_dir = extension_dir.Append(kMetadataFolder);
  41. if (base::DirectoryExists(metadata_dir)) {
  42. base::DeletePathRecursively(metadata_dir);
  43. }
  44. std::string load_error;
  45. scoped_refptr<Extension> extension = file_util::LoadExtension(
  46. extension_dir, extensions::mojom::ManifestLocation::kCommandLine,
  47. load_flags, &load_error);
  48. if (!extension.get()) {
  49. std::string err = "Loading extension at " +
  50. base::UTF16ToUTF8(extension_dir.LossyDisplayName()) +
  51. " failed with: " + load_error;
  52. return std::make_pair(nullptr, err);
  53. }
  54. std::string warnings;
  55. // Log warnings.
  56. if (!extension->install_warnings().empty()) {
  57. std::string warning_prefix =
  58. "Warnings loading extension at " +
  59. base::UTF16ToUTF8(extension_dir.LossyDisplayName());
  60. for (const auto& warning : extension->install_warnings()) {
  61. std::string unrecognized_manifest_error = ErrorUtils::FormatErrorMessage(
  62. manifest_errors::kUnrecognizedManifestKey, warning.key);
  63. if (warning.message == unrecognized_manifest_error) {
  64. // filter kUnrecognizedManifestKey error. This error does not have any
  65. // impact e.g: Unrecognized manifest key 'minimum_chrome_version' etc.
  66. LOG(WARNING) << warning_prefix << ": " << warning.message;
  67. } else {
  68. warnings += " " + warning.message + "\n";
  69. }
  70. }
  71. if (warnings != "") {
  72. warnings = warning_prefix + ":\n" + warnings;
  73. }
  74. }
  75. return std::make_pair(extension, warnings);
  76. }
  77. } // namespace
  78. ElectronExtensionLoader::ElectronExtensionLoader(
  79. content::BrowserContext* browser_context)
  80. : browser_context_(browser_context),
  81. extension_registrar_(ExtensionRegistrar::Get(browser_context)) {
  82. extension_registrar_->SetDelegate(this);
  83. }
  84. ElectronExtensionLoader::~ElectronExtensionLoader() = default;
  85. void ElectronExtensionLoader::LoadExtension(
  86. const base::FilePath& extension_dir,
  87. int load_flags,
  88. base::OnceCallback<void(const Extension*, const std::string&)> cb) {
  89. GetExtensionFileTaskRunner()->PostTaskAndReplyWithResult(
  90. FROM_HERE, base::BindOnce(&LoadUnpacked, extension_dir, load_flags),
  91. base::BindOnce(&ElectronExtensionLoader::FinishExtensionLoad,
  92. weak_factory_.GetWeakPtr(), std::move(cb)));
  93. }
  94. void ElectronExtensionLoader::ReloadExtension(const ExtensionId& extension_id) {
  95. const Extension* extension = ExtensionRegistry::Get(browser_context_)
  96. ->GetInstalledExtension(extension_id);
  97. // We shouldn't be trying to reload extensions that haven't been added.
  98. DCHECK(extension);
  99. // This should always start false since it's only set here, or in
  100. // LoadExtensionForReload() as a result of the call below.
  101. DCHECK_EQ(false, did_schedule_reload_);
  102. base::AutoReset<bool> reset_did_schedule_reload(&did_schedule_reload_, false);
  103. extension_registrar_->ReloadExtension(extension_id,
  104. LoadErrorBehavior::kQuiet);
  105. if (did_schedule_reload_)
  106. return;
  107. }
  108. void ElectronExtensionLoader::UnloadExtension(
  109. const ExtensionId& extension_id,
  110. extensions::UnloadedExtensionReason reason) {
  111. extension_registrar_->RemoveExtension(extension_id, reason);
  112. }
  113. void ElectronExtensionLoader::FinishExtensionLoad(
  114. base::OnceCallback<void(const Extension*, const std::string&)> cb,
  115. std::pair<scoped_refptr<const Extension>, std::string> result) {
  116. scoped_refptr<const Extension> extension = result.first;
  117. if (extension) {
  118. extension_registrar_->AddExtension(extension);
  119. // Write extension install time to ExtensionPrefs. This is required by
  120. // WebRequestAPI which calls extensions::ExtensionPrefs::GetInstallTime.
  121. //
  122. // Implementation for writing the pref was based on
  123. // PreferenceAPIBase::SetExtensionControlledPref.
  124. {
  125. ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser_context_);
  126. ExtensionPrefs::ScopedDictionaryUpdate update(
  127. extension_prefs, extension.get()->id(),
  128. extensions::pref_names::kPrefPreferences);
  129. auto preference = update.Create();
  130. const int64_t now_usec =
  131. base::Time::Now().since_origin().InMicroseconds();
  132. preference->SetString("install_time", base::NumberToString(now_usec));
  133. }
  134. }
  135. std::move(cb).Run(extension.get(), result.second);
  136. }
  137. void ElectronExtensionLoader::FinishExtensionReload(
  138. const ExtensionId& old_extension_id,
  139. std::pair<scoped_refptr<const Extension>, std::string> result) {
  140. scoped_refptr<const Extension> extension = result.first;
  141. if (extension) {
  142. extension_registrar_->AddExtension(extension);
  143. }
  144. }
  145. void ElectronExtensionLoader::PreAddExtension(const Extension* extension,
  146. const Extension* old_extension) {
  147. if (old_extension)
  148. return;
  149. // The extension might be disabled if a previous reload attempt failed. In
  150. // that case, we want to remove that disable reason.
  151. ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser_context_);
  152. extension_prefs->RemoveDisableReason(extension->id(),
  153. disable_reason::DISABLE_RELOAD);
  154. }
  155. void ElectronExtensionLoader::PostActivateExtension(
  156. scoped_refptr<const Extension> extension) {}
  157. void ElectronExtensionLoader::PostDeactivateExtension(
  158. scoped_refptr<const Extension> extension) {}
  159. void ElectronExtensionLoader::PreUninstallExtension(
  160. scoped_refptr<const Extension> extension) {}
  161. void ElectronExtensionLoader::PostUninstallExtension(
  162. scoped_refptr<const Extension> extension,
  163. base::OnceClosure done_callback) {}
  164. void ElectronExtensionLoader::PostNotifyUninstallExtension(
  165. scoped_refptr<const Extension> extension) {}
  166. void ElectronExtensionLoader::LoadExtensionForReload(
  167. const ExtensionId& extension_id,
  168. const base::FilePath& path,
  169. LoadErrorBehavior load_error_behavior) {
  170. CHECK(!path.empty());
  171. // TODO(nornagon): we should save whether file access was granted
  172. // when loading this extension and retain it here. As is, reloading an
  173. // extension will cause the file access permission to be dropped.
  174. int load_flags = Extension::FOLLOW_SYMLINKS_ANYWHERE;
  175. GetExtensionFileTaskRunner()->PostTaskAndReplyWithResult(
  176. FROM_HERE, base::BindOnce(&LoadUnpacked, path, load_flags),
  177. base::BindOnce(&ElectronExtensionLoader::FinishExtensionReload,
  178. weak_factory_.GetWeakPtr(), extension_id));
  179. did_schedule_reload_ = true;
  180. }
  181. void ElectronExtensionLoader::ShowExtensionDisabledError(
  182. const Extension* extension,
  183. bool is_remote_install) {}
  184. void ElectronExtensionLoader::FinishDelayedInstallationsIfAny() {}
  185. bool ElectronExtensionLoader::CanAddExtension(const Extension* extension) {
  186. return true;
  187. }
  188. bool ElectronExtensionLoader::CanEnableExtension(const Extension* extension) {
  189. return true;
  190. }
  191. bool ElectronExtensionLoader::CanDisableExtension(const Extension* extension) {
  192. // Extensions cannot be disabled by the user.
  193. return false;
  194. }
  195. bool ElectronExtensionLoader::ShouldBlockExtension(const Extension* extension) {
  196. return false;
  197. }
  198. void ElectronExtensionLoader::GrantActivePermissions(
  199. const Extension* extension) {
  200. NOTIMPLEMENTED();
  201. }
  202. } // namespace extensions