| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/shared_memory.h" | |
| 14 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 15 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
| 16 #include "content/public/browser/notification_observer.h" | 15 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 16 #include "content/public/browser/notification_registrar.h" |
| 18 #include "extensions/common/host_id.h" | 17 #include "extensions/browser/extension_registry_observer.h" |
| 18 #include "extensions/common/extension.h" |
| 19 #include "extensions/common/extension_set.h" |
| 19 #include "extensions/common/user_script.h" | 20 #include "extensions/common/user_script.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class SharedMemory; | 23 class SharedMemory; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace content { | 26 namespace content { |
| 26 class BrowserContext; | 27 class BrowserContext; |
| 27 class RenderProcessHost; | 28 class RenderProcessHost; |
| 28 } | 29 } |
| 29 | 30 |
| 30 class Profile; | 31 class Profile; |
| 31 | 32 |
| 32 namespace extensions { | 33 namespace extensions { |
| 33 | 34 |
| 34 class ContentVerifier; | 35 class ContentVerifier; |
| 36 class ExtensionRegistry; |
| 37 |
| 38 typedef std::map<ExtensionId, ExtensionSet::ExtensionPathAndDefaultLocale> |
| 39 ExtensionsInfo; |
| 35 | 40 |
| 36 // Manages one "logical unit" of user scripts in shared memory by constructing a | 41 // Manages one "logical unit" of user scripts in shared memory by constructing a |
| 37 // new shared memory region when the set of scripts changes. Also notifies | 42 // new shared memory region when the set of scripts changes. Also notifies |
| 38 // renderers of new shared memory region when new renderers appear, or when | 43 // renderers of new shared memory region when new renderers appear, or when |
| 39 // script reloading completes. Script loading lives on the UI thread. Instances | 44 // script reloading completes. Script loading lives on the UI thread. Instances |
| 40 // of this class are embedded within classes with names ending in | 45 // of this class are embedded within classes with names ending in |
| 41 // UserScriptMaster. These "master" classes implement the strategy for which | 46 // UserScriptMaster. These "master" classes implement the strategy for which |
| 42 // scripts to load/unload on this logical unit of scripts. | 47 // scripts to load/unload on this logical unit of scripts. |
| 43 class UserScriptLoader : public content::NotificationObserver { | 48 class UserScriptLoader : public content::NotificationObserver, |
| 49 public ExtensionRegistryObserver { |
| 44 public: | 50 public: |
| 45 using PathAndDefaultLocale = std::pair<base::FilePath, std::string>; | |
| 46 using HostsInfo = std::map<HostID, PathAndDefaultLocale>; | |
| 47 | |
| 48 using SubstitutionMap = std::map<std::string, std::string>; | |
| 49 using LoadUserScriptsContentFunction = | |
| 50 base::Callback<bool(const HostID&, | |
| 51 UserScript::File*, | |
| 52 const SubstitutionMap*, | |
| 53 const scoped_refptr<ContentVerifier>&)>; | |
| 54 | |
| 55 // Parses the includes out of |script| and returns them in |includes|. | 51 // Parses the includes out of |script| and returns them in |includes|. |
| 56 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 52 static bool ParseMetadataHeader(const base::StringPiece& script_text, |
| 57 UserScript* script); | 53 UserScript* script); |
| 58 | 54 |
| 59 UserScriptLoader(Profile* profile, | |
| 60 const HostID& host_id, | |
| 61 const scoped_refptr<ContentVerifier>& content_verifier); | |
| 62 ~UserScriptLoader() override; | |
| 63 | |
| 64 // A wrapper around the method to load user scripts, which is normally run on | 55 // A wrapper around the method to load user scripts, which is normally run on |
| 65 // the file thread. Exposed only for tests. | 56 // the file thread. Exposed only for tests. |
| 66 void LoadScriptsForTest(UserScriptList* user_scripts); | 57 static void LoadScriptsForTest(UserScriptList* user_scripts); |
| 58 |
| 59 UserScriptLoader(Profile* profile, |
| 60 const ExtensionId& owner_extension_id, |
| 61 bool listen_for_extension_system_loaded); |
| 62 ~UserScriptLoader() override; |
| 67 | 63 |
| 68 // Add |scripts| to the set of scripts managed by this loader. | 64 // Add |scripts| to the set of scripts managed by this loader. |
| 69 void AddScripts(const std::set<UserScript>& scripts); | 65 void AddScripts(const std::set<UserScript>& scripts); |
| 70 | 66 |
| 71 // Remove |scripts| from the set of scripts managed by this loader. | 67 // Remove |scripts| from the set of scripts managed by this loader. |
| 72 void RemoveScripts(const std::set<UserScript>& scripts); | 68 void RemoveScripts(const std::set<UserScript>& scripts); |
| 73 | 69 |
| 74 // Clears the set of scripts managed by this loader. | 70 // Clears the set of scripts managed by this loader. |
| 75 void ClearScripts(); | 71 void ClearScripts(); |
| 76 | 72 |
| 77 // Initiates procedure to start loading scripts on the file thread. | 73 // Initiates procedure to start loading scripts on the file thread. |
| 78 void StartLoad(); | 74 void StartLoad(); |
| 79 | 75 |
| 80 // Returns true if we have any scripts ready. | 76 // Return true if we have any scripts ready. |
| 81 bool scripts_ready() const { return shared_memory_.get() != NULL; } | 77 bool scripts_ready() const { return shared_memory_.get() != NULL; } |
| 82 | 78 |
| 83 protected: | |
| 84 // Updates |hosts_info_| to contain info for each element of | |
| 85 // |changed_hosts_|. | |
| 86 virtual void UpdateHostsInfo(const std::set<HostID>& changed_hosts) = 0; | |
| 87 | |
| 88 // Returns a function pointer of a static funcion to load user scripts. | |
| 89 // Derived classes can specify their ways to load scripts in the static | |
| 90 // function they return. | |
| 91 // Note: It has to be safe to call multiple times. | |
| 92 virtual LoadUserScriptsContentFunction GetLoadUserScriptsFunction() = 0; | |
| 93 | |
| 94 // Adds the |host_id, location| to the |hosts_info_| map. | |
| 95 // Only inserts the entry to the map when the given host_id doesn't | |
| 96 // exists. | |
| 97 void AddHostInfo(const HostID& host_id, const PathAndDefaultLocale& location); | |
| 98 | |
| 99 // Removes the entries with the given host_id from the |hosts_info_| map. | |
| 100 void RemoveHostInfo(const HostID& host_id); | |
| 101 | |
| 102 // Sets the flag if the initial set of hosts has finished loading; if it's | |
| 103 // set to be true, calls AttempLoad() to bootstrap. | |
| 104 void SetReady(bool ready); | |
| 105 | |
| 106 Profile* profile() const { return profile_; } | |
| 107 const HostID& host_id() const { return host_id_; } | |
| 108 | |
| 109 private: | 79 private: |
| 110 // content::NotificationObserver implementation. | 80 // content::NotificationObserver implementation. |
| 111 void Observe(int type, | 81 void Observe(int type, |
| 112 const content::NotificationSource& source, | 82 const content::NotificationSource& source, |
| 113 const content::NotificationDetails& details) override; | 83 const content::NotificationDetails& details) override; |
| 114 | 84 |
| 85 // ExtensionRegistryObserver implementation. |
| 86 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 87 const Extension* extension, |
| 88 UnloadedExtensionInfo::Reason reason) override; |
| 89 |
| 90 // Initiates script load when we have been waiting for the extension system |
| 91 // to be ready. |
| 92 void OnExtensionSystemReady(); |
| 93 |
| 115 // Returns whether or not it is possible that calls to AddScripts(), | 94 // Returns whether or not it is possible that calls to AddScripts(), |
| 116 // RemoveScripts(), and/or ClearScripts() have caused any real change in the | 95 // RemoveScripts(), and/or ClearScripts() have caused any real change in the |
| 117 // set of scripts to be loaded. | 96 // set of scripts to be loaded. |
| 118 bool ScriptsMayHaveChanged() const; | 97 bool ScriptsMayHaveChanged() const; |
| 119 | 98 |
| 120 // Attempts to initiate a load. | 99 // Attempt to initiate a load. |
| 121 void AttemptLoad(); | 100 void AttemptLoad(); |
| 122 | 101 |
| 123 // Called once we have finished loading the scripts on the file thread. | 102 // Called once we have finished loading the scripts on the file thread. |
| 124 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, | 103 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, |
| 125 scoped_ptr<base::SharedMemory> shared_memory); | 104 scoped_ptr<base::SharedMemory> shared_memory); |
| 126 | 105 |
| 127 // Sends the renderer process a new set of user scripts. If | 106 // Sends the renderer process a new set of user scripts. If |
| 128 // |changed_hosts| is not empty, this signals that only the scripts from | 107 // |changed_extensions| is not empty, this signals that only the scripts from |
| 129 // those hosts should be updated. Otherwise, all hosts will be | 108 // those extensions should be updated. Otherwise, all extensions will be |
| 130 // updated. | 109 // updated. |
| 131 void SendUpdate(content::RenderProcessHost* process, | 110 void SendUpdate(content::RenderProcessHost* process, |
| 132 base::SharedMemory* shared_memory, | 111 base::SharedMemory* shared_memory, |
| 133 const std::set<HostID>& changed_hosts); | 112 const std::set<ExtensionId>& changed_extensions); |
| 113 |
| 114 // Add to |changed_extensions_| those extensions referred to by |scripts|. |
| 115 void ExpandChangedExtensions(const std::set<UserScript>& scripts); |
| 116 |
| 117 // Update |extensions_info_| to contain info for each element of |
| 118 // |changed_extensions_|. |
| 119 void UpdateExtensionsInfo(); |
| 134 | 120 |
| 135 bool is_loading() const { | 121 bool is_loading() const { |
| 136 // Ownership of |user_scripts_| is passed to the file thread when loading. | 122 // Ownership of |user_scripts_| is passed to the file thread when loading. |
| 137 return user_scripts_.get() == NULL; | 123 return user_scripts_.get() == NULL; |
| 138 } | 124 } |
| 139 | 125 |
| 140 // Manages our notification registrations. | 126 // Manages our notification registrations. |
| 141 content::NotificationRegistrar registrar_; | 127 content::NotificationRegistrar registrar_; |
| 142 | 128 |
| 143 // Contains the scripts that were found the last time scripts were updated. | 129 // Contains the scripts that were found the last time scripts were updated. |
| 144 scoped_ptr<base::SharedMemory> shared_memory_; | 130 scoped_ptr<base::SharedMemory> shared_memory_; |
| 145 | 131 |
| 146 // List of scripts from currently-installed extensions we should load. | 132 // List of scripts from currently-installed extensions we should load. |
| 147 scoped_ptr<UserScriptList> user_scripts_; | 133 scoped_ptr<UserScriptList> user_scripts_; |
| 148 | 134 |
| 149 // Maps host info needed for localization to a host ID. | 135 // Maps extension info needed for localization to an extension ID. |
| 150 HostsInfo hosts_info_; | 136 ExtensionsInfo extensions_info_; |
| 151 | 137 |
| 152 // The mutually-exclusive sets of scripts that were added or removed since the | 138 // The mutually-exclusive sets of scripts that were added or removed since the |
| 153 // last script load. | 139 // last script load. |
| 154 std::set<UserScript> added_scripts_; | 140 std::set<UserScript> added_scripts_; |
| 155 std::set<UserScript> removed_scripts_; | 141 std::set<UserScript> removed_scripts_; |
| 156 | 142 |
| 157 // Indicates whether the the collection of scripts should be cleared before | 143 // Indicates whether the the collection of scripts should be cleared before |
| 158 // additions and removals on the next script load. | 144 // additions and removals on the next script load. |
| 159 bool clear_scripts_; | 145 bool clear_scripts_; |
| 160 | 146 |
| 161 // The IDs of the extensions which changed in the last update sent to the | 147 // The IDs of the extensions which changed in the last update sent to the |
| 162 // renderer. | 148 // renderer. |
| 163 std::set<HostID> changed_hosts_; | 149 ExtensionIdSet changed_extensions_; |
| 164 | 150 |
| 165 // If the initial set of hosts has finished loading. | 151 // If the extensions service has finished loading its initial set of |
| 166 bool ready_; | 152 // extensions. |
| 153 bool extension_system_ready_; |
| 167 | 154 |
| 168 // If list of user scripts is modified while we're loading it, we note | 155 // If list of user scripts is modified while we're loading it, we note |
| 169 // that we're currently mid-load and then start over again once the load | 156 // that we're currently mid-load and then start over again once the load |
| 170 // finishes. This boolean tracks whether another load is pending. | 157 // finishes. This boolean tracks whether another load is pending. |
| 171 bool pending_load_; | 158 bool pending_load_; |
| 172 | 159 |
| 173 // Whether or not we are currently loading. | 160 // Whether or not we are currently loading. |
| 174 bool is_loading_; | 161 bool is_loading_; |
| 175 | 162 |
| 176 // The profile for which the scripts managed here are installed. | 163 // The profile for which the scripts managed here are installed. |
| 177 Profile* profile_; | 164 Profile* profile_; |
| 178 | 165 |
| 179 // ID of the host that owns these scripts, if any. This is only set to a | 166 // ID of the extension that owns these scripts, if any. This is only set to a |
| 180 // non-empty value for declarative user script shared memory regions. | 167 // non-empty value for declarative user script shared memory regions. |
| 181 HostID host_id_; | 168 ExtensionId owner_extension_id_; |
| 182 | 169 |
| 183 // Manages content verification of the loaded user scripts. | 170 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 184 scoped_refptr<ContentVerifier> content_verifier_; | 171 extension_registry_observer_; |
| 185 | 172 |
| 186 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | 173 base::WeakPtrFactory<UserScriptLoader> weak_factory_; |
| 187 | 174 |
| 188 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | 175 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); |
| 189 }; | 176 }; |
| 190 | 177 |
| 191 } // namespace extensions | 178 } // namespace extensions |
| 192 | 179 |
| 193 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | 180 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ |
| OLD | NEW |