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 <set> | 10 #include <set> |
10 | 11 |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.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/common/host_id.h" | 20 #include "extensions/common/host_id.h" |
21 #include "extensions/common/user_script.h" | 21 #include "extensions/common/user_script.h" |
22 | 22 |
23 namespace base { | 23 namespace base { |
(...skipping 10 matching lines...) Expand all Loading... |
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 LoadScriptsCallback = | 43 using LoadScriptsCallback = |
44 base::Callback<void(scoped_ptr<UserScriptList>, | 44 base::Callback<void(std::unique_ptr<UserScriptList>, |
45 scoped_ptr<base::SharedMemory>)>; | 45 std::unique_ptr<base::SharedMemory>)>; |
46 class Observer { | 46 class Observer { |
47 public: | 47 public: |
48 virtual void OnScriptsLoaded(UserScriptLoader* loader) = 0; | 48 virtual void OnScriptsLoaded(UserScriptLoader* loader) = 0; |
49 virtual void OnUserScriptLoaderDestroyed(UserScriptLoader* loader) = 0; | 49 virtual void OnUserScriptLoaderDestroyed(UserScriptLoader* loader) = 0; |
50 }; | 50 }; |
51 | 51 |
52 // Parses the includes out of |script| and returns them in |includes|. | 52 // Parses the includes out of |script| and returns them in |includes|. |
53 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 53 static bool ParseMetadataHeader(const base::StringPiece& script_text, |
54 UserScript* script); | 54 UserScript* script); |
55 | 55 |
(...skipping 20 matching lines...) Expand all Loading... |
76 // Clears the set of scripts managed by this loader. | 76 // Clears the set of scripts managed by this loader. |
77 void ClearScripts(); | 77 void ClearScripts(); |
78 | 78 |
79 // Initiates procedure to start loading scripts on the file thread. | 79 // Initiates procedure to start loading scripts on the file thread. |
80 void StartLoad(); | 80 void StartLoad(); |
81 | 81 |
82 // Returns true if we have any scripts ready. | 82 // Returns true if we have any scripts ready. |
83 bool scripts_ready() const { return shared_memory_.get() != NULL; } | 83 bool scripts_ready() const { return shared_memory_.get() != NULL; } |
84 | 84 |
85 // Pickle user scripts and return pointer to the shared memory. | 85 // Pickle user scripts and return pointer to the shared memory. |
86 static scoped_ptr<base::SharedMemory> Serialize( | 86 static std::unique_ptr<base::SharedMemory> Serialize( |
87 const extensions::UserScriptList& scripts); | 87 const extensions::UserScriptList& scripts); |
88 | 88 |
89 // Adds or removes observers. | 89 // Adds or removes observers. |
90 void AddObserver(Observer* observer); | 90 void AddObserver(Observer* observer); |
91 void RemoveObserver(Observer* observer); | 91 void RemoveObserver(Observer* observer); |
92 | 92 |
93 protected: | 93 protected: |
94 // Allows the derived classes have different ways to load user scripts. | 94 // Allows the derived classes have different ways to load user scripts. |
95 virtual void LoadScripts(scoped_ptr<UserScriptList> user_scripts, | 95 virtual void LoadScripts(std::unique_ptr<UserScriptList> user_scripts, |
96 const std::set<HostID>& changed_hosts, | 96 const std::set<HostID>& changed_hosts, |
97 const std::set<int>& added_script_ids, | 97 const std::set<int>& added_script_ids, |
98 LoadScriptsCallback callback) = 0; | 98 LoadScriptsCallback callback) = 0; |
99 | 99 |
100 // Sets the flag if the initial set of hosts has finished loading; if it's | 100 // Sets the flag if the initial set of hosts has finished loading; if it's |
101 // set to be true, calls AttempLoad() to bootstrap. | 101 // set to be true, calls AttempLoad() to bootstrap. |
102 void SetReady(bool ready); | 102 void SetReady(bool ready); |
103 | 103 |
104 content::BrowserContext* browser_context() const { return browser_context_; } | 104 content::BrowserContext* browser_context() const { return browser_context_; } |
105 const HostID& host_id() const { return host_id_; } | 105 const HostID& host_id() const { return host_id_; } |
106 | 106 |
107 private: | 107 private: |
108 // content::NotificationObserver implementation. | 108 // content::NotificationObserver implementation. |
109 void Observe(int type, | 109 void Observe(int type, |
110 const content::NotificationSource& source, | 110 const content::NotificationSource& source, |
111 const content::NotificationDetails& details) override; | 111 const content::NotificationDetails& details) override; |
112 | 112 |
113 // Returns whether or not it is possible that calls to AddScripts(), | 113 // Returns whether or not it is possible that calls to AddScripts(), |
114 // RemoveScripts(), and/or ClearScripts() have caused any real change in the | 114 // RemoveScripts(), and/or ClearScripts() have caused any real change in the |
115 // set of scripts to be loaded. | 115 // set of scripts to be loaded. |
116 bool ScriptsMayHaveChanged() const; | 116 bool ScriptsMayHaveChanged() const; |
117 | 117 |
118 // Attempts to initiate a load. | 118 // Attempts to initiate a load. |
119 void AttemptLoad(); | 119 void AttemptLoad(); |
120 | 120 |
121 // Called once we have finished loading the scripts on the file thread. | 121 // Called once we have finished loading the scripts on the file thread. |
122 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, | 122 void OnScriptsLoaded(std::unique_ptr<UserScriptList> user_scripts, |
123 scoped_ptr<base::SharedMemory> shared_memory); | 123 std::unique_ptr<base::SharedMemory> shared_memory); |
124 | 124 |
125 // Sends the renderer process a new set of user scripts. If | 125 // Sends the renderer process a new set of user scripts. If |
126 // |changed_hosts| is not empty, this signals that only the scripts from | 126 // |changed_hosts| is not empty, this signals that only the scripts from |
127 // those hosts should be updated. Otherwise, all hosts will be | 127 // those hosts should be updated. Otherwise, all hosts will be |
128 // updated. | 128 // updated. |
129 void SendUpdate(content::RenderProcessHost* process, | 129 void SendUpdate(content::RenderProcessHost* process, |
130 base::SharedMemory* shared_memory, | 130 base::SharedMemory* shared_memory, |
131 const std::set<HostID>& changed_hosts); | 131 const std::set<HostID>& changed_hosts); |
132 | 132 |
133 bool is_loading() const { | 133 bool is_loading() const { |
134 // Ownership of |user_scripts_| is passed to the file thread when loading. | 134 // Ownership of |user_scripts_| is passed to the file thread when loading. |
135 return user_scripts_.get() == NULL; | 135 return user_scripts_.get() == NULL; |
136 } | 136 } |
137 | 137 |
138 // Manages our notification registrations. | 138 // Manages our notification registrations. |
139 content::NotificationRegistrar registrar_; | 139 content::NotificationRegistrar registrar_; |
140 | 140 |
141 // Contains the scripts that were found the last time scripts were updated. | 141 // Contains the scripts that were found the last time scripts were updated. |
142 scoped_ptr<base::SharedMemory> shared_memory_; | 142 std::unique_ptr<base::SharedMemory> shared_memory_; |
143 | 143 |
144 // List of scripts from currently-installed extensions we should load. | 144 // List of scripts from currently-installed extensions we should load. |
145 scoped_ptr<UserScriptList> user_scripts_; | 145 std::unique_ptr<UserScriptList> user_scripts_; |
146 | 146 |
147 // The mutually-exclusive sets of scripts that were added or removed since the | 147 // The mutually-exclusive sets of scripts that were added or removed since the |
148 // last script load. | 148 // last script load. |
149 std::set<UserScript> added_scripts_; | 149 std::set<UserScript> added_scripts_; |
150 std::set<UserScript> removed_scripts_; | 150 std::set<UserScript> removed_scripts_; |
151 | 151 |
152 // Indicates whether the the collection of scripts should be cleared before | 152 // Indicates whether the the collection of scripts should be cleared before |
153 // additions and removals on the next script load. | 153 // additions and removals on the next script load. |
154 bool clear_scripts_; | 154 bool clear_scripts_; |
155 | 155 |
(...skipping 20 matching lines...) Expand all Loading... |
176 base::ObserverList<Observer> observers_; | 176 base::ObserverList<Observer> observers_; |
177 | 177 |
178 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | 178 base::WeakPtrFactory<UserScriptLoader> weak_factory_; |
179 | 179 |
180 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | 180 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); |
181 }; | 181 }; |
182 | 182 |
183 } // namespace extensions | 183 } // namespace extensions |
184 | 184 |
185 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ | 185 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
OLD | NEW |