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 22 matching lines...) Expand all Loading... | |
| 33 | 33 | 
| 34 // Manages one "logical unit" of user scripts in shared memory by constructing a | 34 // Manages one "logical unit" of user scripts in shared memory by constructing a | 
| 35 // new shared memory region when the set of scripts changes. Also notifies | 35 // new shared memory region when the set of scripts changes. Also notifies | 
| 36 // renderers of new shared memory region when new renderers appear, or when | 36 // renderers of new shared memory region when new renderers appear, or when | 
| 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>; | 
| 
 
Devlin
2015/04/23 16:34:38
Do we need these typedefs in this file?  I think a
 
Xi Han
2015/04/23 17:50:40
Good catch, moved.
 
 | |
| 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 = | 47 | 
| 48 base::Callback<bool(const HostID&, | 48 using LoadScriptsCallback = | 
| 49 UserScript::File*, | 49 base::Callback<void(scoped_ptr<UserScriptList>, | 
| 50 const SubstitutionMap*, | 50 scoped_ptr<base::SharedMemory>)>; | 
| 51 const scoped_refptr<ContentVerifier>&)>; | |
| 52 | 51 | 
| 53 // Parses the includes out of |script| and returns them in |includes|. | 52 // Parses the includes out of |script| and returns them in |includes|. | 
| 54 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 53 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 
| 55 UserScript* script); | 54 UserScript* script); | 
| 56 | 55 | 
| 57 UserScriptLoader(content::BrowserContext* browser_context, | 56 UserScriptLoader(content::BrowserContext* browser_context, | 
| 58 const HostID& host_id, | 57 const HostID& host_id); | 
| 59 const scoped_refptr<ContentVerifier>& content_verifier); | |
| 60 ~UserScriptLoader() override; | 58 ~UserScriptLoader() override; | 
| 61 | 59 | 
| 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. | 60 // Add |scripts| to the set of scripts managed by this loader. | 
| 67 void AddScripts(const std::set<UserScript>& scripts); | 61 void AddScripts(const std::set<UserScript>& scripts); | 
| 68 | 62 | 
| 63 // Add |scripts| to the set of scripts managed by this loader. | |
| 64 // The fetch of the content of the script starts URL request | |
| 65 // to the associated render specified by | |
| 66 // |render_process_id, render_view_id|. | |
| 67 // TODO(hanxi): The renderer information doesn't really belong in this base | |
| 68 // class, but it's not an easy fix. | |
| 69 virtual void AddScripts(const std::set<UserScript>& scripts, | |
| 70 int render_process_id, | |
| 71 int render_view_id); | |
| 72 | |
| 69 // Remove |scripts| from the set of scripts managed by this loader. | 73 // Remove |scripts| from the set of scripts managed by this loader. | 
| 70 void RemoveScripts(const std::set<UserScript>& scripts); | 74 void RemoveScripts(const std::set<UserScript>& scripts); | 
| 71 | 75 | 
| 72 // Clears the set of scripts managed by this loader. | 76 // Clears the set of scripts managed by this loader. | 
| 73 void ClearScripts(); | 77 void ClearScripts(); | 
| 74 | 78 | 
| 75 // Initiates procedure to start loading scripts on the file thread. | 79 // Initiates procedure to start loading scripts on the file thread. | 
| 76 void StartLoad(); | 80 void StartLoad(); | 
| 77 | 81 | 
| 78 // Returns true if we have any scripts ready. | 82 // Returns true if we have any scripts ready. | 
| 79 bool scripts_ready() const { return shared_memory_.get() != NULL; } | 83 bool scripts_ready() const { return shared_memory_.get() != NULL; } | 
| 80 | 84 | 
| 85 // Pickle user scripts and return pointer to the shared memory. | |
| 86 static scoped_ptr<base::SharedMemory> Serialize( | |
| 87 const extensions::UserScriptList& scripts); | |
| 88 | |
| 81 protected: | 89 protected: | 
| 82 // Updates |hosts_info_| to contain info for each element of | 90 // Allows the derived classes have different ways to load user scripts. | 
| 83 // |changed_hosts_|. | 91 virtual void LoadScripts(scoped_ptr<UserScriptList> user_scripts, | 
| 84 virtual void UpdateHostsInfo(const std::set<HostID>& changed_hosts) = 0; | 92 const std::set<HostID>& changed_hosts, | 
| 85 | 93 const std::set<int>& added_script_ids, | 
| 86 // Returns a function pointer of a static funcion to load user scripts. | 94 LoadScriptsCallback callback) = 0; | 
| 87 // Derived classes can specify their ways to load scripts in the static | |
| 88 // function they return. | |
| 89 // Note: It has to be safe to call multiple times. | |
| 90 virtual LoadUserScriptsContentFunction GetLoadUserScriptsFunction() = 0; | |
| 91 | |
| 92 // Adds the |host_id, location| to the |hosts_info_| map. | |
| 93 // Only inserts the entry to the map when the given host_id doesn't | |
| 94 // exists. | |
| 95 void AddHostInfo(const HostID& host_id, const PathAndDefaultLocale& location); | |
| 96 | |
| 97 // Removes the entries with the given host_id from the |hosts_info_| map. | |
| 98 void RemoveHostInfo(const HostID& host_id); | |
| 99 | 95 | 
| 100 // Sets the flag if the initial set of hosts has finished loading; if it's | 96 // Sets the flag if the initial set of hosts has finished loading; if it's | 
| 101 // set to be true, calls AttempLoad() to bootstrap. | 97 // set to be true, calls AttempLoad() to bootstrap. | 
| 102 void SetReady(bool ready); | 98 void SetReady(bool ready); | 
| 103 | 99 | 
| 104 content::BrowserContext* browser_context() const { return browser_context_; } | 100 content::BrowserContext* browser_context() const { return browser_context_; } | 
| 105 const HostID& host_id() const { return host_id_; } | 101 const HostID& host_id() const { return host_id_; } | 
| 106 | 102 | 
| 107 private: | 103 private: | 
| 108 // content::NotificationObserver implementation. | 104 // content::NotificationObserver implementation. | 
| (...skipping 28 matching lines...) Expand all Loading... | |
| 137 | 133 | 
| 138 // Manages our notification registrations. | 134 // Manages our notification registrations. | 
| 139 content::NotificationRegistrar registrar_; | 135 content::NotificationRegistrar registrar_; | 
| 140 | 136 | 
| 141 // Contains the scripts that were found the last time scripts were updated. | 137 // Contains the scripts that were found the last time scripts were updated. | 
| 142 scoped_ptr<base::SharedMemory> shared_memory_; | 138 scoped_ptr<base::SharedMemory> shared_memory_; | 
| 143 | 139 | 
| 144 // List of scripts from currently-installed extensions we should load. | 140 // List of scripts from currently-installed extensions we should load. | 
| 145 scoped_ptr<UserScriptList> user_scripts_; | 141 scoped_ptr<UserScriptList> user_scripts_; | 
| 146 | 142 | 
| 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 | 143 // The mutually-exclusive sets of scripts that were added or removed since the | 
| 151 // last script load. | 144 // last script load. | 
| 152 std::set<UserScript> added_scripts_; | 145 std::set<UserScript> added_scripts_; | 
| 153 std::set<UserScript> removed_scripts_; | 146 std::set<UserScript> removed_scripts_; | 
| 154 | 147 | 
| 155 // Indicates whether the the collection of scripts should be cleared before | 148 // Indicates whether the the collection of scripts should be cleared before | 
| 156 // additions and removals on the next script load. | 149 // additions and removals on the next script load. | 
| 157 bool clear_scripts_; | 150 bool clear_scripts_; | 
| 158 | 151 | 
| 159 // The IDs of the extensions which changed in the last update sent to the | 152 // 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. | 164 // Whether or not we are currently loading. | 
| 172 bool is_loading_; | 165 bool is_loading_; | 
| 173 | 166 | 
| 174 // The browser_context for which the scripts managed here are installed. | 167 // The browser_context for which the scripts managed here are installed. | 
| 175 content::BrowserContext* browser_context_; | 168 content::BrowserContext* browser_context_; | 
| 176 | 169 | 
| 177 // ID of the host that owns these scripts, if any. This is only set to a | 170 // 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. | 171 // non-empty value for declarative user script shared memory regions. | 
| 179 HostID host_id_; | 172 HostID host_id_; | 
| 180 | 173 | 
| 181 // Manages content verification of the loaded user scripts. | |
| 182 scoped_refptr<ContentVerifier> content_verifier_; | |
| 183 | |
| 184 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | 174 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | 
| 185 | 175 | 
| 186 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | 176 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | 
| 187 }; | 177 }; | 
| 188 | 178 | 
| 189 } // namespace extensions | 179 } // namespace extensions | 
| 190 | 180 | 
| 191 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ | 181 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ | 
| OLD | NEW |