| 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 <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 17 #include "base/scoped_observer.h" | 17 #include "base/scoped_observer.h" |
| 18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 20 #include "extensions/browser/browser_user_script.h" |
| 20 #include "extensions/common/host_id.h" | 21 #include "extensions/common/host_id.h" |
| 21 #include "extensions/common/user_script.h" | 22 #include "extensions/common/user_script.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 class SharedMemory; | 25 class SharedMemory; |
| 25 } | 26 } |
| 26 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 class BrowserContext; | 29 class BrowserContext; |
| 29 class RenderProcessHost; | 30 class RenderProcessHost; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace extensions { | 33 namespace extensions { |
| 33 | 34 |
| 35 class BrowserUserScript; |
| 36 |
| 34 // Manages one "logical unit" of user scripts in shared memory by constructing a | 37 // 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 | 38 // 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 | 39 // renderers of new shared memory region when new renderers appear, or when |
| 37 // script reloading completes. Script loading lives on the UI thread. Instances | 40 // script reloading completes. Script loading lives on the UI thread. Instances |
| 38 // of this class are embedded within classes with names ending in | 41 // of this class are embedded within classes with names ending in |
| 39 // UserScriptMaster. These "master" classes implement the strategy for which | 42 // UserScriptMaster. These "master" classes implement the strategy for which |
| 40 // scripts to load/unload on this logical unit of scripts. | 43 // scripts to load/unload on this logical unit of scripts. |
| 41 class UserScriptLoader : public content::NotificationObserver { | 44 class UserScriptLoader : public content::NotificationObserver { |
| 42 public: | 45 public: |
| 43 using LoadScriptsCallback = | 46 using LoadScriptsCallback = base::Callback<void( |
| 44 base::Callback<void(std::unique_ptr<UserScriptList>, | 47 std::unique_ptr<std::vector<std::unique_ptr<BrowserUserScript>>>, |
| 45 std::unique_ptr<base::SharedMemory>)>; | 48 std::unique_ptr<base::SharedMemory>)>; |
| 46 class Observer { | 49 class Observer { |
| 47 public: | 50 public: |
| 48 virtual void OnScriptsLoaded(UserScriptLoader* loader) = 0; | 51 virtual void OnScriptsLoaded(UserScriptLoader* loader) = 0; |
| 49 virtual void OnUserScriptLoaderDestroyed(UserScriptLoader* loader) = 0; | 52 virtual void OnUserScriptLoaderDestroyed(UserScriptLoader* loader) = 0; |
| 50 }; | 53 }; |
| 51 | 54 |
| 52 // Parses the includes out of |script| and returns them in |includes|. | 55 // Parses the includes out of |script| and returns them in |includes|. |
| 53 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 56 static bool ParseMetadataHeader(const base::StringPiece& script_text, |
| 54 UserScript* script); | 57 BrowserUserScript* script); |
| 55 | 58 |
| 56 UserScriptLoader(content::BrowserContext* browser_context, | 59 UserScriptLoader(content::BrowserContext* browser_context, |
| 57 const HostID& host_id); | 60 const HostID& host_id); |
| 58 ~UserScriptLoader() override; | 61 ~UserScriptLoader() override; |
| 59 | 62 |
| 60 // Add |scripts| to the set of scripts managed by this loader. | 63 // Add |scripts| to the set of scripts managed by this loader. |
| 61 void AddScripts(const UserScriptList& scripts); | 64 void AddScripts(BrowserUserScriptList& scripts); |
| 62 | 65 |
| 63 // Add |scripts| to the set of scripts managed by this loader. | 66 // Add |scripts| to the set of scripts managed by this loader. |
| 64 // The fetch of the content of the script starts URL request | 67 // The fetch of the content of the script starts URL request |
| 65 // to the associated render specified by | 68 // to the associated render specified by |
| 66 // |render_process_id, render_view_id|. | 69 // |render_process_id, render_view_id|. |
| 67 // TODO(hanxi): The renderer information doesn't really belong in this base | 70 // TODO(hanxi): The renderer information doesn't really belong in this base |
| 68 // class, but it's not an easy fix. | 71 // class, but it's not an easy fix. |
| 69 virtual void AddScripts(const UserScriptList& scripts, | 72 virtual void AddScripts(BrowserUserScriptList& scripts, |
| 70 int render_process_id, | 73 int render_process_id, |
| 71 int render_view_id); | 74 int render_view_id); |
| 72 | 75 |
| 73 // Removes scripts with ids specified in |scripts| from the set of scripts | 76 // Removes scripts with ids specified in |scripts| from the set of scripts |
| 74 // managed by this loader. | 77 // managed by this loader. |
| 75 // TODO(lazyboy): Likely we can make |scripts| a std::vector, but | 78 // TODO(lazyboy): Likely we can make |scripts| a std::vector, but |
| 76 // WebViewContentScriptManager makes this non-trivial. | 79 // WebViewContentScriptManager makes this non-trivial. |
| 77 void RemoveScripts(const std::set<UserScriptIDPair>& scripts); | 80 void RemoveScripts(const std::set<UserScriptIDPair>& scripts); |
| 78 | 81 |
| 79 // Clears the set of scripts managed by this loader. | 82 // Clears the set of scripts managed by this loader. |
| 80 void ClearScripts(); | 83 void ClearScripts(); |
| 81 | 84 |
| 82 // Initiates procedure to start loading scripts on the file thread. | 85 // Initiates procedure to start loading scripts on the file thread. |
| 83 void StartLoad(); | 86 void StartLoad(); |
| 84 | 87 |
| 85 // Returns true if we have any scripts ready. | 88 // Returns true if we have any scripts ready. |
| 86 bool scripts_ready() const { return shared_memory_.get() != NULL; } | 89 bool scripts_ready() const { return shared_memory_.get() != NULL; } |
| 87 | 90 |
| 88 // Pickle user scripts and return pointer to the shared memory. | 91 // Pickle user scripts and return pointer to the shared memory. |
| 89 static std::unique_ptr<base::SharedMemory> Serialize( | 92 static std::unique_ptr<base::SharedMemory> Serialize( |
| 90 const extensions::UserScriptList& scripts); | 93 const extensions::BrowserUserScriptList& scripts); |
| 91 | 94 |
| 92 // Adds or removes observers. | 95 // Adds or removes observers. |
| 93 void AddObserver(Observer* observer); | 96 void AddObserver(Observer* observer); |
| 94 void RemoveObserver(Observer* observer); | 97 void RemoveObserver(Observer* observer); |
| 95 | 98 |
| 96 protected: | 99 protected: |
| 97 // Allows the derived classes have different ways to load user scripts. | 100 // Allows the derived classes have different ways to load user scripts. |
| 98 virtual void LoadScripts(std::unique_ptr<UserScriptList> user_scripts, | 101 virtual void LoadScripts(std::unique_ptr<BrowserUserScriptList> user_scripts, |
| 99 const std::set<HostID>& changed_hosts, | 102 const std::set<HostID>& changed_hosts, |
| 100 const std::set<int>& added_script_ids, | 103 const std::set<int>& added_script_ids, |
| 101 LoadScriptsCallback callback) = 0; | 104 LoadScriptsCallback callback) = 0; |
| 102 | 105 |
| 103 // Sets the flag if the initial set of hosts has finished loading; if it's | 106 // Sets the flag if the initial set of hosts has finished loading; if it's |
| 104 // set to be true, calls AttempLoad() to bootstrap. | 107 // set to be true, calls AttempLoad() to bootstrap. |
| 105 void SetReady(bool ready); | 108 void SetReady(bool ready); |
| 106 | 109 |
| 107 content::BrowserContext* browser_context() const { return browser_context_; } | 110 content::BrowserContext* browser_context() const { return browser_context_; } |
| 108 const HostID& host_id() const { return host_id_; } | 111 const HostID& host_id() const { return host_id_; } |
| 109 | 112 |
| 110 private: | 113 private: |
| 111 // content::NotificationObserver implementation. | 114 // content::NotificationObserver implementation. |
| 112 void Observe(int type, | 115 void Observe(int type, |
| 113 const content::NotificationSource& source, | 116 const content::NotificationSource& source, |
| 114 const content::NotificationDetails& details) override; | 117 const content::NotificationDetails& details) override; |
| 115 | 118 |
| 116 // Returns whether or not it is possible that calls to AddScripts(), | 119 // Returns whether or not it is possible that calls to AddScripts(), |
| 117 // RemoveScripts(), and/or ClearScripts() have caused any real change in the | 120 // RemoveScripts(), and/or ClearScripts() have caused any real change in the |
| 118 // set of scripts to be loaded. | 121 // set of scripts to be loaded. |
| 119 bool ScriptsMayHaveChanged() const; | 122 bool ScriptsMayHaveChanged() const; |
| 120 | 123 |
| 121 // Attempts to initiate a load. | 124 // Attempts to initiate a load. |
| 122 void AttemptLoad(); | 125 void AttemptLoad(); |
| 123 | 126 |
| 124 // Called once we have finished loading the scripts on the file thread. | 127 // Called once we have finished loading the scripts on the file thread. |
| 125 void OnScriptsLoaded(std::unique_ptr<UserScriptList> user_scripts, | 128 void OnScriptsLoaded(std::unique_ptr<BrowserUserScriptList> user_scripts, |
| 126 std::unique_ptr<base::SharedMemory> shared_memory); | 129 std::unique_ptr<base::SharedMemory> shared_memory); |
| 127 | 130 |
| 128 // Sends the renderer process a new set of user scripts. If | 131 // Sends the renderer process a new set of user scripts. If |
| 129 // |changed_hosts| is not empty, this signals that only the scripts from | 132 // |changed_hosts| is not empty, this signals that only the scripts from |
| 130 // those hosts should be updated. Otherwise, all hosts will be | 133 // those hosts should be updated. Otherwise, all hosts will be |
| 131 // updated. | 134 // updated. |
| 132 void SendUpdate(content::RenderProcessHost* process, | 135 void SendUpdate(content::RenderProcessHost* process, |
| 133 base::SharedMemory* shared_memory, | 136 base::SharedMemory* shared_memory, |
| 134 const std::set<HostID>& changed_hosts); | 137 const std::set<HostID>& changed_hosts); |
| 135 | 138 |
| 136 bool is_loading() const { | 139 bool is_loading() const { |
| 137 // Ownership of |user_scripts_| is passed to the file thread when loading. | 140 // Ownership of |user_scripts_| is passed to the file thread when loading. |
| 138 return user_scripts_.get() == nullptr; | 141 return user_scripts_.get() == nullptr; |
| 139 } | 142 } |
| 140 | 143 |
| 141 // Manages our notification registrations. | 144 // Manages our notification registrations. |
| 142 content::NotificationRegistrar registrar_; | 145 content::NotificationRegistrar registrar_; |
| 143 | 146 |
| 144 // Contains the scripts that were found the last time scripts were updated. | 147 // Contains the scripts that were found the last time scripts were updated. |
| 145 std::unique_ptr<base::SharedMemory> shared_memory_; | 148 std::unique_ptr<base::SharedMemory> shared_memory_; |
| 146 | 149 |
| 147 // List of scripts from currently-installed extensions we should load. | 150 // List of scripts from currently-installed extensions we should load. |
| 148 std::unique_ptr<UserScriptList> user_scripts_; | 151 std::unique_ptr<BrowserUserScriptList> user_scripts_; |
| 149 | 152 |
| 150 // The mutually-exclusive information about sets of scripts that were added or | 153 // The mutually-exclusive information about sets of scripts that were added or |
| 151 // removed since the last script load. These maps are keyed by script's id. | 154 // removed since the last script load. These maps are keyed by script's id. |
| 152 // Note that we only need HostID information for removal. | 155 // Note that we only need HostID information for removal. |
| 153 std::map<int, UserScript> added_scripts_map_; | 156 std::map<int, std::unique_ptr<BrowserUserScript>> added_scripts_map_; |
| 154 std::set<UserScriptIDPair> removed_script_hosts_; | 157 std::set<UserScriptIDPair> removed_script_hosts_; |
| 155 | 158 |
| 156 // Indicates whether the the collection of scripts should be cleared before | 159 // Indicates whether the the collection of scripts should be cleared before |
| 157 // additions and removals on the next script load. | 160 // additions and removals on the next script load. |
| 158 bool clear_scripts_; | 161 bool clear_scripts_; |
| 159 | 162 |
| 160 // The IDs of the extensions which changed in the last update sent to the | 163 // The IDs of the extensions which changed in the last update sent to the |
| 161 // renderer. | 164 // renderer. |
| 162 std::set<HostID> changed_hosts_; | 165 std::set<HostID> changed_hosts_; |
| 163 | 166 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 180 base::ObserverList<Observer> observers_; | 183 base::ObserverList<Observer> observers_; |
| 181 | 184 |
| 182 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | 185 base::WeakPtrFactory<UserScriptLoader> weak_factory_; |
| 183 | 186 |
| 184 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | 187 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); |
| 185 }; | 188 }; |
| 186 | 189 |
| 187 } // namespace extensions | 190 } // namespace extensions |
| 188 | 191 |
| 189 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ | 192 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
| OLD | NEW |