Chromium Code Reviews| 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 EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ | 5 #ifndef EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
| 6 #define EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ | 6 #define EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 // script reloading completes. Script loading lives on the UI thread. Instances | 37 // script reloading completes. Script loading lives on the UI thread. Instances |
| 38 // of this class are embedded within classes with names ending in | 38 // of this class are embedded within classes with names ending in |
| 39 // UserScriptMaster. These "master" classes implement the strategy for which | 39 // UserScriptMaster. These "master" classes implement the strategy for which |
| 40 // scripts to load/unload on this logical unit of scripts. | 40 // scripts to load/unload on this logical unit of scripts. |
| 41 class UserScriptLoader : public content::NotificationObserver { | 41 class UserScriptLoader : public content::NotificationObserver { |
| 42 public: | 42 public: |
| 43 using PathAndDefaultLocale = std::pair<base::FilePath, std::string>; | 43 using PathAndDefaultLocale = std::pair<base::FilePath, std::string>; |
| 44 using HostsInfo = std::map<HostID, PathAndDefaultLocale>; | 44 using HostsInfo = std::map<HostID, PathAndDefaultLocale>; |
| 45 | 45 |
| 46 using SubstitutionMap = std::map<std::string, std::string>; | 46 using SubstitutionMap = std::map<std::string, std::string>; |
| 47 using LoadUserScriptsContentFunction = | |
| 48 base::Callback<bool(const HostID&, | |
| 49 UserScript::File*, | |
| 50 const SubstitutionMap*, | |
| 51 const scoped_refptr<ContentVerifier>&)>; | |
| 52 | 47 |
| 53 // Parses the includes out of |script| and returns them in |includes|. | 48 // Parses the includes out of |script| and returns them in |includes|. |
| 54 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 49 static bool ParseMetadataHeader(const base::StringPiece& script_text, |
| 55 UserScript* script); | 50 UserScript* script); |
| 56 | 51 |
| 57 UserScriptLoader(content::BrowserContext* browser_context, | 52 UserScriptLoader(content::BrowserContext* browser_context, |
| 58 const HostID& host_id, | 53 const HostID& host_id); |
| 59 const scoped_refptr<ContentVerifier>& content_verifier); | |
| 60 ~UserScriptLoader() override; | 54 ~UserScriptLoader() override; |
| 61 | 55 |
| 62 // A wrapper around the method to load user scripts, which is normally run on | |
| 63 // the file thread. Exposed only for tests. | |
| 64 void LoadScriptsForTest(UserScriptList* user_scripts); | |
| 65 | |
| 66 // Add |scripts| to the set of scripts managed by this loader. | 56 // Add |scripts| to the set of scripts managed by this loader. |
| 67 void AddScripts(const std::set<UserScript>& scripts); | 57 void AddScripts(const std::set<UserScript>& scripts); |
| 68 | 58 |
| 59 // Add |scripts| to the set of scripts managed by this loader. | |
| 60 // The fetch of the content of the script starts URL request | |
| 61 // to the associated render specified by | |
| 62 // |render_process_id, render_view_id|. | |
| 63 void AddScripts(const std::set<UserScript>& scripts, | |
| 64 int render_process_id, | |
| 65 int render_view_id); | |
| 66 | |
| 69 // Remove |scripts| from the set of scripts managed by this loader. | 67 // Remove |scripts| from the set of scripts managed by this loader. |
| 70 void RemoveScripts(const std::set<UserScript>& scripts); | 68 void RemoveScripts(const std::set<UserScript>& scripts); |
| 71 | 69 |
| 72 // Clears the set of scripts managed by this loader. | 70 // Clears the set of scripts managed by this loader. |
| 73 void ClearScripts(); | 71 // If |is_clear| is true, will attempt to load scripts. |
| 72 void ClearScripts(bool is_clear); | |
| 74 | 73 |
| 75 // Initiates procedure to start loading scripts on the file thread. | 74 // Initiates procedure to start loading scripts on the file thread. |
| 76 void StartLoad(); | 75 void StartLoad(); |
| 77 | 76 |
| 78 // Returns true if we have any scripts ready. | 77 // Returns true if we have any scripts ready. |
| 79 bool scripts_ready() const { return shared_memory_.get() != NULL; } | 78 bool scripts_ready() const { return shared_memory_.get() != NULL; } |
| 80 | 79 |
| 80 // Pickle user scripts and return pointer to the shared memory. | |
| 81 static scoped_ptr<base::SharedMemory> Serialize( | |
| 82 const extensions::UserScriptList& scripts); | |
| 83 | |
| 81 protected: | 84 protected: |
| 82 // Updates |hosts_info_| to contain info for each element of | 85 struct UserScriptRenderInfo { |
|
Devlin
2015/04/16 20:19:28
(Almost all of) this is a layering violation. The
Xi Han
2015/04/17 21:44:20
You are right. Refactoring is done to move all the
| |
| 83 // |changed_hosts_|. | 86 int render_process_id; |
| 84 virtual void UpdateHostsInfo(const std::set<HostID>& changed_hosts) = 0; | 87 int render_view_id; |
| 85 | 88 |
| 86 // Returns a function pointer of a static funcion to load user scripts. | 89 UserScriptRenderInfo() { |
| 87 // Derived classes can specify their ways to load scripts in the static | 90 render_process_id = -1; |
| 88 // function they return. | 91 render_view_id = -1; |
| 89 // Note: It has to be safe to call multiple times. | 92 } |
| 90 virtual LoadUserScriptsContentFunction GetLoadUserScriptsFunction() = 0; | |
| 91 | 93 |
| 92 // Adds the |host_id, location| to the |hosts_info_| map. | 94 UserScriptRenderInfo(int render_process_id, int render_view_id) { |
| 93 // Only inserts the entry to the map when the given host_id doesn't | 95 this->render_process_id = render_process_id; |
| 94 // exists. | 96 this->render_view_id = render_view_id; |
| 95 void AddHostInfo(const HostID& host_id, const PathAndDefaultLocale& location); | 97 } |
| 96 | 98 |
| 97 // Removes the entries with the given host_id from the |hosts_info_| map. | 99 bool operator<(const UserScriptRenderInfo& other) { |
| 98 void RemoveHostInfo(const HostID& host_id); | 100 if (render_process_id != other.render_process_id) |
| 101 return render_process_id < other.render_process_id; | |
| 102 | |
| 103 if (render_view_id != other.render_view_id) | |
| 104 return render_view_id < other.render_view_id; | |
| 105 | |
| 106 return false; | |
| 107 } | |
| 108 }; | |
| 109 | |
| 110 using UserScriptRenderInfoMap = std::map<int, UserScriptRenderInfo>; | |
| 111 | |
| 112 // Allows the derived classes have different ways to load user scripts. | |
| 113 virtual void LoadScripts(const std::set<HostID>& changed_hosts, | |
| 114 const std::set<int>& added_script_ids) = 0; | |
| 115 | |
| 116 bool GetScriptRenderInfo(int script_id, UserScriptRenderInfo* info); | |
| 117 void RemoveScriptRenderInfo(int script_id); | |
| 99 | 118 |
| 100 // Sets the flag if the initial set of hosts has finished loading; if it's | 119 // Sets the flag if the initial set of hosts has finished loading; if it's |
| 101 // set to be true, calls AttempLoad() to bootstrap. | 120 // set to be true, calls AttempLoad() to bootstrap. |
| 102 void SetReady(bool ready); | 121 void SetReady(bool ready); |
| 103 | 122 |
| 123 // Called once we have finished loading the scripts on the file thread. | |
| 124 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, | |
| 125 scoped_ptr<base::SharedMemory> shared_memory); | |
| 126 | |
| 104 content::BrowserContext* browser_context() const { return browser_context_; } | 127 content::BrowserContext* browser_context() const { return browser_context_; } |
| 105 const HostID& host_id() const { return host_id_; } | 128 const HostID& host_id() const { return host_id_; } |
| 106 | 129 |
| 130 // List of scripts from currently-installed extensions we should load. | |
| 131 scoped_ptr<UserScriptList> user_scripts_; | |
| 132 | |
| 107 private: | 133 private: |
| 108 // content::NotificationObserver implementation. | 134 // content::NotificationObserver implementation. |
| 109 void Observe(int type, | 135 void Observe(int type, |
| 110 const content::NotificationSource& source, | 136 const content::NotificationSource& source, |
| 111 const content::NotificationDetails& details) override; | 137 const content::NotificationDetails& details) override; |
| 112 | 138 |
| 113 // Returns whether or not it is possible that calls to AddScripts(), | 139 // Returns whether or not it is possible that calls to AddScripts(), |
| 114 // RemoveScripts(), and/or ClearScripts() have caused any real change in the | 140 // RemoveScripts(), and/or ClearScripts() have caused any real change in the |
| 115 // set of scripts to be loaded. | 141 // set of scripts to be loaded. |
| 116 bool ScriptsMayHaveChanged() const; | 142 bool ScriptsMayHaveChanged() const; |
| 117 | 143 |
| 118 // Attempts to initiate a load. | 144 // Attempts to initiate a load. |
| 119 void AttemptLoad(); | 145 void AttemptLoad(); |
| 120 | 146 |
| 121 // Called once we have finished loading the scripts on the file thread. | |
| 122 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, | |
| 123 scoped_ptr<base::SharedMemory> shared_memory); | |
| 124 | |
| 125 // Sends the renderer process a new set of user scripts. If | 147 // Sends the renderer process a new set of user scripts. If |
| 126 // |changed_hosts| is not empty, this signals that only the scripts from | 148 // |changed_hosts| is not empty, this signals that only the scripts from |
| 127 // those hosts should be updated. Otherwise, all hosts will be | 149 // those hosts should be updated. Otherwise, all hosts will be |
| 128 // updated. | 150 // updated. |
| 129 void SendUpdate(content::RenderProcessHost* process, | 151 void SendUpdate(content::RenderProcessHost* process, |
| 130 base::SharedMemory* shared_memory, | 152 base::SharedMemory* shared_memory, |
| 131 const std::set<HostID>& changed_hosts); | 153 const std::set<HostID>& changed_hosts); |
| 132 | 154 |
| 133 bool is_loading() const { | 155 bool is_loading() const { |
| 134 // Ownership of |user_scripts_| is passed to the file thread when loading. | 156 // Ownership of |user_scripts_| is passed to the file thread when loading. |
| 135 return user_scripts_.get() == NULL; | 157 return user_scripts_.get() == NULL; |
| 136 } | 158 } |
| 137 | 159 |
| 138 // Manages our notification registrations. | 160 // Manages our notification registrations. |
| 139 content::NotificationRegistrar registrar_; | 161 content::NotificationRegistrar registrar_; |
| 140 | 162 |
| 141 // Contains the scripts that were found the last time scripts were updated. | 163 // Contains the scripts that were found the last time scripts were updated. |
| 142 scoped_ptr<base::SharedMemory> shared_memory_; | 164 scoped_ptr<base::SharedMemory> shared_memory_; |
| 143 | 165 |
| 144 // List of scripts from currently-installed extensions we should load. | |
| 145 scoped_ptr<UserScriptList> user_scripts_; | |
| 146 | |
| 147 // Maps host info needed for localization to a host ID. | |
| 148 HostsInfo hosts_info_; | |
| 149 | |
| 150 // The mutually-exclusive sets of scripts that were added or removed since the | 166 // The mutually-exclusive sets of scripts that were added or removed since the |
| 151 // last script load. | 167 // last script load. |
| 152 std::set<UserScript> added_scripts_; | 168 std::set<UserScript> added_scripts_; |
| 153 std::set<UserScript> removed_scripts_; | 169 std::set<UserScript> removed_scripts_; |
| 154 | 170 |
| 155 // Indicates whether the the collection of scripts should be cleared before | 171 // Indicates whether the the collection of scripts should be cleared before |
| 156 // additions and removals on the next script load. | 172 // additions and removals on the next script load. |
| 157 bool clear_scripts_; | 173 bool clear_scripts_; |
| 158 | 174 |
| 159 // The IDs of the extensions which changed in the last update sent to the | 175 // The IDs of the extensions which changed in the last update sent to the |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 171 // Whether or not we are currently loading. | 187 // Whether or not we are currently loading. |
| 172 bool is_loading_; | 188 bool is_loading_; |
| 173 | 189 |
| 174 // The browser_context for which the scripts managed here are installed. | 190 // The browser_context for which the scripts managed here are installed. |
| 175 content::BrowserContext* browser_context_; | 191 content::BrowserContext* browser_context_; |
| 176 | 192 |
| 177 // ID of the host that owns these scripts, if any. This is only set to a | 193 // ID of the host that owns these scripts, if any. This is only set to a |
| 178 // non-empty value for declarative user script shared memory regions. | 194 // non-empty value for declarative user script shared memory regions. |
| 179 HostID host_id_; | 195 HostID host_id_; |
| 180 | 196 |
| 181 // Manages content verification of the loaded user scripts. | 197 // Caches the render info of script from WebUI when AddScripts is called. |
| 182 scoped_refptr<ContentVerifier> content_verifier_; | 198 // When starting to load the script, we look up this map to retrieve the |
| 199 // render info. It is used for the script from WebUI only, since the fetch | |
| 200 // of script content requires the info of associated render. | |
| 201 UserScriptRenderInfoMap script_render_info_map_; | |
| 183 | 202 |
| 184 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | 203 base::WeakPtrFactory<UserScriptLoader> weak_factory_; |
| 185 | 204 |
| 186 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | 205 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); |
| 187 }; | 206 }; |
| 188 | 207 |
| 189 } // namespace extensions | 208 } // namespace extensions |
| 190 | 209 |
| 191 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ | 210 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
| OLD | NEW |