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