| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/shared_memory.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/scoped_observer.h" | |
| 16 #include "content/public/browser/notification_observer.h" | |
| 17 #include "content/public/browser/notification_registrar.h" | |
| 18 #include "extensions/common/host_id.h" | |
| 19 #include "extensions/common/user_script.h" | |
| 20 | |
| 21 namespace base { | |
| 22 class SharedMemory; | |
| 23 } | |
| 24 | |
| 25 namespace content { | |
| 26 class BrowserContext; | |
| 27 class RenderProcessHost; | |
| 28 } | |
| 29 | |
| 30 class Profile; | |
| 31 | |
| 32 namespace extensions { | |
| 33 | |
| 34 class ContentVerifier; | |
| 35 | |
| 36 // 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 | |
| 38 // renderers of new shared memory region when new renderers appear, or when | |
| 39 // script reloading completes. Script loading lives on the UI thread. Instances | |
| 40 // of this class are embedded within classes with names ending in | |
| 41 // UserScriptMaster. These "master" classes implement the strategy for which | |
| 42 // scripts to load/unload on this logical unit of scripts. | |
| 43 class UserScriptLoader : public content::NotificationObserver { | |
| 44 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|. | |
| 56 static bool ParseMetadataHeader(const base::StringPiece& script_text, | |
| 57 UserScript* script); | |
| 58 | |
| 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 | |
| 65 // the file thread. Exposed only for tests. | |
| 66 void LoadScriptsForTest(UserScriptList* user_scripts); | |
| 67 | |
| 68 // Add |scripts| to the set of scripts managed by this loader. | |
| 69 void AddScripts(const std::set<UserScript>& scripts); | |
| 70 | |
| 71 // Remove |scripts| from the set of scripts managed by this loader. | |
| 72 void RemoveScripts(const std::set<UserScript>& scripts); | |
| 73 | |
| 74 // Clears the set of scripts managed by this loader. | |
| 75 void ClearScripts(); | |
| 76 | |
| 77 // Initiates procedure to start loading scripts on the file thread. | |
| 78 void StartLoad(); | |
| 79 | |
| 80 // Returns true if we have any scripts ready. | |
| 81 bool scripts_ready() const { return shared_memory_.get() != NULL; } | |
| 82 | |
| 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: | |
| 110 // content::NotificationObserver implementation. | |
| 111 void Observe(int type, | |
| 112 const content::NotificationSource& source, | |
| 113 const content::NotificationDetails& details) override; | |
| 114 | |
| 115 // Returns whether or not it is possible that calls to AddScripts(), | |
| 116 // RemoveScripts(), and/or ClearScripts() have caused any real change in the | |
| 117 // set of scripts to be loaded. | |
| 118 bool ScriptsMayHaveChanged() const; | |
| 119 | |
| 120 // Attempts to initiate a load. | |
| 121 void AttemptLoad(); | |
| 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 | |
| 127 // Sends the renderer process a new set of user scripts. If | |
| 128 // |changed_hosts| is not empty, this signals that only the scripts from | |
| 129 // those hosts should be updated. Otherwise, all hosts will be | |
| 130 // updated. | |
| 131 void SendUpdate(content::RenderProcessHost* process, | |
| 132 base::SharedMemory* shared_memory, | |
| 133 const std::set<HostID>& changed_hosts); | |
| 134 | |
| 135 bool is_loading() const { | |
| 136 // Ownership of |user_scripts_| is passed to the file thread when loading. | |
| 137 return user_scripts_.get() == NULL; | |
| 138 } | |
| 139 | |
| 140 // Manages our notification registrations. | |
| 141 content::NotificationRegistrar registrar_; | |
| 142 | |
| 143 // Contains the scripts that were found the last time scripts were updated. | |
| 144 scoped_ptr<base::SharedMemory> shared_memory_; | |
| 145 | |
| 146 // List of scripts from currently-installed extensions we should load. | |
| 147 scoped_ptr<UserScriptList> user_scripts_; | |
| 148 | |
| 149 // Maps host info needed for localization to a host ID. | |
| 150 HostsInfo hosts_info_; | |
| 151 | |
| 152 // The mutually-exclusive sets of scripts that were added or removed since the | |
| 153 // last script load. | |
| 154 std::set<UserScript> added_scripts_; | |
| 155 std::set<UserScript> removed_scripts_; | |
| 156 | |
| 157 // Indicates whether the the collection of scripts should be cleared before | |
| 158 // additions and removals on the next script load. | |
| 159 bool clear_scripts_; | |
| 160 | |
| 161 // The IDs of the extensions which changed in the last update sent to the | |
| 162 // renderer. | |
| 163 std::set<HostID> changed_hosts_; | |
| 164 | |
| 165 // If the initial set of hosts has finished loading. | |
| 166 bool ready_; | |
| 167 | |
| 168 // 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 | |
| 170 // finishes. This boolean tracks whether another load is pending. | |
| 171 bool pending_load_; | |
| 172 | |
| 173 // Whether or not we are currently loading. | |
| 174 bool is_loading_; | |
| 175 | |
| 176 // The profile for which the scripts managed here are installed. | |
| 177 Profile* profile_; | |
| 178 | |
| 179 // ID of the host that owns these scripts, if any. This is only set to a | |
| 180 // non-empty value for declarative user script shared memory regions. | |
| 181 HostID host_id_; | |
| 182 | |
| 183 // Manages content verification of the loaded user scripts. | |
| 184 scoped_refptr<ContentVerifier> content_verifier_; | |
| 185 | |
| 186 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | |
| 187 | |
| 188 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | |
| 189 }; | |
| 190 | |
| 191 } // namespace extensions | |
| 192 | |
| 193 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | |
| OLD | NEW |