1 // Copyright 2015 The Chromium Embedded Framework Authors. 2 // Portions copyright 2014 The Chromium Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 6 #ifndef CEF_LIBCEF_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_ 7 #define CEF_LIBCEF_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_ 8 9 #include <map> 10 #include <memory> 11 12 #include "include/cef_extension_handler.h" 13 #include "include/cef_request_context.h" 14 15 #include "base/compiler_specific.h" 16 #include "base/memory/weak_ptr.h" 17 #include "base/one_shot_event.h" 18 #include "extensions/browser/extension_system.h" 19 20 namespace base { 21 class DictionaryValue; 22 } 23 24 namespace content { 25 class BrowserContext; 26 } 27 28 namespace extensions { 29 30 class ExtensionRegistry; 31 class ProcessManager; 32 class RendererStartupHelper; 33 34 // Used to manage extensions. 35 class CefExtensionSystem : public ExtensionSystem { 36 public: 37 explicit CefExtensionSystem(content::BrowserContext* browser_context); 38 39 CefExtensionSystem(const CefExtensionSystem&) = delete; 40 CefExtensionSystem& operator=(const CefExtensionSystem&) = delete; 41 42 ~CefExtensionSystem() override; 43 44 // Initializes the extension system. 45 void Init(); 46 47 // Load an extension. For internal (built-in) extensions set |internal| to 48 // true and |loader_context| and |handler| to NULL. For external extensions 49 // set |internal| to false and |loader_context| must be the request context 50 // that loaded the extension. |handler| is optional for internal extensions 51 // and, if specified, will receive extension-related callbacks. 52 void LoadExtension(const base::FilePath& root_directory, 53 bool internal, 54 CefRefPtr<CefRequestContext> loader_context, 55 CefRefPtr<CefExtensionHandler> handler); 56 void LoadExtension(const std::string& manifest_contents, 57 const base::FilePath& root_directory, 58 bool internal, 59 CefRefPtr<CefRequestContext> loader_context, 60 CefRefPtr<CefExtensionHandler> handler); 61 void LoadExtension(std::unique_ptr<base::DictionaryValue> manifest, 62 const base::FilePath& root_directory, 63 bool internal, 64 CefRefPtr<CefRequestContext> loader_context, 65 CefRefPtr<CefExtensionHandler> handler); 66 67 // Unload the external extension identified by |extension_id|. 68 bool UnloadExtension(const std::string& extension_id); 69 70 // Returns true if an extension matching |extension_id| is loaded. 71 bool HasExtension(const std::string& extension_id) const; 72 73 // Returns the loaded extention matching |extension_id| or NULL if not found. 74 CefRefPtr<CefExtension> GetExtension(const std::string& extension_id) const; 75 76 using ExtensionMap = std::map<std::string, CefRefPtr<CefExtension>>; 77 78 // Returns the map of all loaded extensions. 79 ExtensionMap GetExtensions() const; 80 81 // Called when a request context is deleted. Unregisters any external 82 // extensions that were registered with this context. 83 void OnRequestContextDeleted(CefRequestContext* context); 84 85 // KeyedService implementation: 86 void Shutdown() override; 87 88 // ExtensionSystem implementation: 89 void InitForRegularProfile(bool extensions_enabled) override; 90 ExtensionService* extension_service() override; 91 ManagementPolicy* management_policy() override; 92 ServiceWorkerManager* service_worker_manager() override; 93 UserScriptManager* user_script_manager() override; 94 StateStore* state_store() override; 95 StateStore* rules_store() override; 96 StateStore* dynamic_user_scripts_store() override; 97 scoped_refptr<value_store::ValueStoreFactory> store_factory() override; 98 InfoMap* info_map() override; 99 QuotaService* quota_service() override; 100 AppSorting* app_sorting() override; 101 void RegisterExtensionWithRequestContexts( 102 const Extension* extension, 103 base::OnceClosure callback) override; 104 void UnregisterExtensionWithRequestContexts( 105 const std::string& extension_id, 106 const UnloadedExtensionReason reason) override; 107 const base::OneShotEvent& ready() const override; 108 bool is_ready() const override; 109 ContentVerifier* content_verifier() override; 110 std::unique_ptr<ExtensionSet> GetDependentExtensions( 111 const Extension* extension) override; 112 void InstallUpdate(const std::string& extension_id, 113 const std::string& public_key, 114 const base::FilePath& temp_dir, 115 bool install_immediately, 116 InstallUpdateCallback install_update_callback) override; 117 void PerformActionBasedOnOmahaAttributes( 118 const std::string& extension_id, 119 const base::Value& attributes) override; 120 bool FinishDelayedInstallationIfReady(const std::string& extension_id, 121 bool install_immediately) override; 122 initialized()123 bool initialized() const { return initialized_; } 124 125 private: 126 virtual void InitPrefs(); 127 128 // Information about a registered component extension. 129 struct ComponentExtensionInfo { 130 ComponentExtensionInfo(const base::DictionaryValue* manifest, 131 const base::FilePath& root_directory, 132 bool internal); 133 134 // The parsed contents of the extensions's manifest file. 135 const base::DictionaryValue* manifest; 136 137 // Directory where the extension is stored. 138 base::FilePath root_directory; 139 140 // True if the extension is an internal (built-in) component. 141 bool internal; 142 }; 143 144 scoped_refptr<const Extension> CreateExtension( 145 const ComponentExtensionInfo& info, 146 std::string* utf8_error); 147 148 // Loads a registered component extension. 149 const Extension* LoadExtension(const ComponentExtensionInfo& info, 150 CefRefPtr<CefRequestContext> loader_context, 151 CefRefPtr<CefExtensionHandler> handler); 152 153 // Unload the specified extension. 154 void UnloadExtension(const std::string& extension_id, 155 extensions::UnloadedExtensionReason reason); 156 157 // Handles sending notification that |extension| was loaded. 158 void NotifyExtensionLoaded(const Extension* extension); 159 160 // Handles sending notification that |extension| was unloaded. 161 void NotifyExtensionUnloaded(const Extension* extension, 162 UnloadedExtensionReason reason); 163 164 // Completes extension loading after URLRequestContexts have been updated 165 // on the IO thread. 166 void OnExtensionRegisteredWithRequestContexts( 167 scoped_refptr<const extensions::Extension> extension); 168 169 content::BrowserContext* browser_context_; // Not owned. 170 171 bool initialized_; 172 173 // Data to be accessed on the IO thread. Must outlive process_manager_. 174 scoped_refptr<InfoMap> info_map_; 175 176 std::unique_ptr<ServiceWorkerManager> service_worker_manager_; 177 std::unique_ptr<QuotaService> quota_service_; 178 std::unique_ptr<AppSorting> app_sorting_; 179 180 std::unique_ptr<StateStore> state_store_; 181 std::unique_ptr<StateStore> rules_store_; 182 scoped_refptr<value_store::ValueStoreFactory> store_factory_; 183 184 // Signaled when the extension system has completed its startup tasks. 185 base::OneShotEvent ready_; 186 187 // Sets of enabled/disabled/terminated/blacklisted extensions. Not owned. 188 ExtensionRegistry* registry_; 189 190 // The associated RendererStartupHelper. Guaranteed to outlive the 191 // ExtensionSystem, and thus us. 192 extensions::RendererStartupHelper* renderer_helper_; 193 194 // Map of extension ID to CEF extension object. 195 ExtensionMap extension_map_; 196 197 // Must be the last member. 198 base::WeakPtrFactory<CefExtensionSystem> weak_ptr_factory_; 199 }; 200 201 } // namespace extensions 202 203 #endif // CEF_LIBCEF_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_ 204