shell_extension_system.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2014 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 "extensions/shell/browser/shell_extension_system.h"
  5. #include <memory>
  6. #include <string>
  7. #include "apps/launcher.h"
  8. #include "base/bind.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "components/services/app_service/public/mojom/types.mojom-shared.h"
  12. #include "components/value_store/value_store_factory_impl.h"
  13. #include "content/public/browser/browser_context.h"
  14. #include "content/public/browser/browser_task_traits.h"
  15. #include "content/public/browser/browser_thread.h"
  16. #include "extensions/browser/api/app_runtime/app_runtime_api.h"
  17. #include "extensions/browser/extension_registry.h"
  18. #include "extensions/browser/info_map.h"
  19. #include "extensions/browser/null_app_sorting.h"
  20. #include "extensions/browser/quota_service.h"
  21. #include "extensions/browser/service_worker_manager.h"
  22. #include "extensions/browser/user_script_manager.h"
  23. #include "extensions/common/constants.h"
  24. #include "extensions/common/file_util.h"
  25. #include "extensions/shell/browser/shell_extension_loader.h"
  26. using content::BrowserContext;
  27. namespace extensions {
  28. ShellExtensionSystem::ShellExtensionSystem(BrowserContext* browser_context)
  29. : browser_context_(browser_context),
  30. store_factory_(
  31. new value_store::ValueStoreFactoryImpl(browser_context->GetPath())) {}
  32. ShellExtensionSystem::~ShellExtensionSystem() = default;
  33. const Extension* ShellExtensionSystem::LoadExtension(
  34. const base::FilePath& extension_dir) {
  35. return extension_loader_->LoadExtension(extension_dir);
  36. }
  37. const Extension* ShellExtensionSystem::LoadApp(const base::FilePath& app_dir) {
  38. return LoadExtension(app_dir);
  39. }
  40. void ShellExtensionSystem::FinishInitialization() {
  41. // Inform the rest of the extensions system to start.
  42. ready_.Signal();
  43. }
  44. void ShellExtensionSystem::LaunchApp(const ExtensionId& extension_id) {
  45. // Send the onLaunched event.
  46. DCHECK(ExtensionRegistry::Get(browser_context_)
  47. ->enabled_extensions()
  48. .Contains(extension_id));
  49. const Extension* extension = ExtensionRegistry::Get(browser_context_)
  50. ->enabled_extensions()
  51. .GetByID(extension_id);
  52. apps::LaunchPlatformApp(browser_context_, extension,
  53. AppLaunchSource::kSourceUntracked);
  54. }
  55. void ShellExtensionSystem::ReloadExtension(const ExtensionId& extension_id) {
  56. extension_loader_->ReloadExtension(extension_id);
  57. }
  58. void ShellExtensionSystem::Shutdown() {
  59. extension_loader_.reset();
  60. }
  61. void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
  62. service_worker_manager_ =
  63. std::make_unique<ServiceWorkerManager>(browser_context_);
  64. quota_service_ = std::make_unique<QuotaService>();
  65. app_sorting_ = std::make_unique<NullAppSorting>();
  66. extension_loader_ = std::make_unique<ShellExtensionLoader>(browser_context_);
  67. user_script_manager_ = std::make_unique<UserScriptManager>(browser_context_);
  68. }
  69. ExtensionService* ShellExtensionSystem::extension_service() {
  70. return nullptr;
  71. }
  72. ManagementPolicy* ShellExtensionSystem::management_policy() {
  73. return nullptr;
  74. }
  75. ServiceWorkerManager* ShellExtensionSystem::service_worker_manager() {
  76. return service_worker_manager_.get();
  77. }
  78. UserScriptManager* ShellExtensionSystem::user_script_manager() {
  79. return user_script_manager_.get();
  80. }
  81. StateStore* ShellExtensionSystem::state_store() {
  82. return nullptr;
  83. }
  84. StateStore* ShellExtensionSystem::rules_store() {
  85. return nullptr;
  86. }
  87. StateStore* ShellExtensionSystem::dynamic_user_scripts_store() {
  88. return nullptr;
  89. }
  90. scoped_refptr<value_store::ValueStoreFactory>
  91. ShellExtensionSystem::store_factory() {
  92. return store_factory_;
  93. }
  94. InfoMap* ShellExtensionSystem::info_map() {
  95. if (!info_map_.get())
  96. info_map_ = new InfoMap;
  97. return info_map_.get();
  98. }
  99. QuotaService* ShellExtensionSystem::quota_service() {
  100. return quota_service_.get();
  101. }
  102. AppSorting* ShellExtensionSystem::app_sorting() {
  103. return app_sorting_.get();
  104. }
  105. void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
  106. const Extension* extension,
  107. base::OnceClosure callback) {
  108. content::GetIOThreadTaskRunner({})->PostTaskAndReply(
  109. FROM_HERE,
  110. base::BindOnce(&InfoMap::AddExtension, info_map(),
  111. base::RetainedRef(extension), base::Time::Now(), false,
  112. false),
  113. std::move(callback));
  114. }
  115. void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
  116. const std::string& extension_id) {}
  117. const base::OneShotEvent& ShellExtensionSystem::ready() const {
  118. return ready_;
  119. }
  120. bool ShellExtensionSystem::is_ready() const {
  121. return ready_.is_signaled();
  122. }
  123. ContentVerifier* ShellExtensionSystem::content_verifier() {
  124. return nullptr;
  125. }
  126. std::unique_ptr<ExtensionSet> ShellExtensionSystem::GetDependentExtensions(
  127. const Extension* extension) {
  128. return std::make_unique<ExtensionSet>();
  129. }
  130. void ShellExtensionSystem::InstallUpdate(
  131. const std::string& extension_id,
  132. const std::string& public_key,
  133. const base::FilePath& temp_dir,
  134. bool install_immediately,
  135. InstallUpdateCallback install_update_callback) {
  136. NOTREACHED();
  137. base::DeletePathRecursively(temp_dir);
  138. }
  139. void ShellExtensionSystem::PerformActionBasedOnOmahaAttributes(
  140. const std::string& extension_id,
  141. const base::Value& attributes) {
  142. NOTREACHED();
  143. }
  144. bool ShellExtensionSystem::FinishDelayedInstallationIfReady(
  145. const std::string& extension_id,
  146. bool install_immediately) {
  147. NOTREACHED();
  148. return false;
  149. }
  150. void ShellExtensionSystem::OnExtensionRegisteredWithRequestContexts(
  151. scoped_refptr<Extension> extension) {
  152. ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_);
  153. registry->AddReady(extension);
  154. registry->TriggerOnReady(extension.get());
  155. }
  156. } // namespace extensions