OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_COMMON_USER_SCRIPT_H_ | 5 #ifndef EXTENSIONS_COMMON_USER_SCRIPT_H_ |
6 #define EXTENSIONS_COMMON_USER_SCRIPT_H_ | 6 #define EXTENSIONS_COMMON_USER_SCRIPT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/linked_ptr.h" | |
13 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
15 #include "extensions/common/consumer.h" | |
14 #include "extensions/common/url_pattern.h" | 16 #include "extensions/common/url_pattern.h" |
15 #include "extensions/common/url_pattern_set.h" | 17 #include "extensions/common/url_pattern_set.h" |
16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
17 | 19 |
18 class Pickle; | 20 class Pickle; |
19 class PickleIterator; | 21 class PickleIterator; |
20 | 22 |
21 namespace extensions { | 23 namespace extensions { |
22 | 24 |
23 // Represents a user script, either a standalone one, or one that is part of an | 25 // Represents a user script, either a standalone one, or one that is part of an |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 void add_exclude_url_pattern(const URLPattern& pattern); | 189 void add_exclude_url_pattern(const URLPattern& pattern); |
188 | 190 |
189 // List of js scripts for this user script | 191 // List of js scripts for this user script |
190 FileList& js_scripts() { return js_scripts_; } | 192 FileList& js_scripts() { return js_scripts_; } |
191 const FileList& js_scripts() const { return js_scripts_; } | 193 const FileList& js_scripts() const { return js_scripts_; } |
192 | 194 |
193 // List of css scripts for this user script | 195 // List of css scripts for this user script |
194 FileList& css_scripts() { return css_scripts_; } | 196 FileList& css_scripts() { return css_scripts_; } |
195 const FileList& css_scripts() const { return css_scripts_; } | 197 const FileList& css_scripts() const { return css_scripts_; } |
196 | 198 |
197 const std::string& extension_id() const { return extension_id_; } | 199 const ConsumerID* consumer_id() const { return consumer_id_.get(); } |
198 void set_extension_id(const std::string& id) { extension_id_ = id; } | 200 void set_consumer_id(const ConsumerID& consumer_id) { |
201 consumer_id_.reset(new ConsumerID(consumer_id)); | |
202 } | |
199 | 203 |
200 int id() const { return user_script_id_; } | 204 int id() const { return user_script_id_; } |
201 void set_id(int id) { user_script_id_ = id; } | 205 void set_id(int id) { user_script_id_ = id; } |
202 | 206 |
203 bool is_incognito_enabled() const { return incognito_enabled_; } | 207 bool is_incognito_enabled() const { return incognito_enabled_; } |
204 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; } | 208 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; } |
205 | 209 |
206 bool is_standalone() const { return extension_id_.empty(); } | 210 bool is_standalone() const { return GetExtensionID().empty(); } |
211 | |
212 const std::string& GetExtensionID() const; | |
207 | 213 |
208 // Returns true if the script should be applied to the specified URL, false | 214 // Returns true if the script should be applied to the specified URL, false |
209 // otherwise. | 215 // otherwise. |
210 bool MatchesURL(const GURL& url) const; | 216 bool MatchesURL(const GURL& url) const; |
211 | 217 |
212 // Serialize the UserScript into a pickle. The content of the scripts and | 218 // Serialize the UserScript into a pickle. The content of the scripts and |
213 // paths to UserScript::Files will not be serialized! | 219 // paths to UserScript::Files will not be serialized! |
214 void Pickle(::Pickle* pickle) const; | 220 void Pickle(::Pickle* pickle) const; |
215 | 221 |
216 // Deserialize the script from a pickle. Note that this always succeeds | 222 // Deserialize the script from a pickle. Note that this always succeeds |
217 // because presumably we were the one that pickled it, and we did it | 223 // because presumably we were the one that pickled it, and we did it |
218 // correctly. | 224 // correctly. |
219 void Unpickle(const ::Pickle& pickle, PickleIterator* iter); | 225 void Unpickle(const ::Pickle& pickle, PickleIterator* iter); |
220 | 226 |
221 private: | 227 private: |
222 // Pickle helper functions used to pickle the individual types of components. | 228 // Pickle helper functions used to pickle the individual types of components. |
223 void PickleGlobs(::Pickle* pickle, | 229 void PickleGlobs(::Pickle* pickle, |
224 const std::vector<std::string>& globs) const; | 230 const std::vector<std::string>& globs) const; |
231 void PickleConsumerID(::Pickle* pickle, | |
232 linked_ptr<ConsumerID> consumer_id) const; | |
225 void PickleURLPatternSet(::Pickle* pickle, | 233 void PickleURLPatternSet(::Pickle* pickle, |
226 const URLPatternSet& pattern_list) const; | 234 const URLPatternSet& pattern_list) const; |
227 void PickleScripts(::Pickle* pickle, const FileList& scripts) const; | 235 void PickleScripts(::Pickle* pickle, const FileList& scripts) const; |
228 | 236 |
229 // Unpickle helper functions used to unpickle individual types of components. | 237 // Unpickle helper functions used to unpickle individual types of components. |
230 void UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, | 238 void UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, |
231 std::vector<std::string>* globs); | 239 std::vector<std::string>* globs); |
240 linked_ptr<ConsumerID> UnpickleConsumerID(const ::Pickle& pickle, | |
241 PickleIterator* iter); | |
232 void UnpickleURLPatternSet(const ::Pickle& pickle, PickleIterator* iter, | 242 void UnpickleURLPatternSet(const ::Pickle& pickle, PickleIterator* iter, |
233 URLPatternSet* pattern_list); | 243 URLPatternSet* pattern_list); |
234 void UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, | 244 void UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, |
235 FileList* scripts); | 245 FileList* scripts); |
236 | 246 |
237 // The location to run the script inside the document. | 247 // The location to run the script inside the document. |
238 RunLocation run_location_; | 248 RunLocation run_location_; |
239 | 249 |
240 // The namespace of the script. This is used by Greasemonkey in the same way | 250 // The namespace of the script. This is used by Greasemonkey in the same way |
241 // as XML namespaces. Only used when parsing Greasemonkey-style scripts. | 251 // as XML namespaces. Only used when parsing Greasemonkey-style scripts. |
(...skipping 18 matching lines...) Expand all Loading... | |
260 // only used with scripts that are part of extensions. | 270 // only used with scripts that are part of extensions. |
261 URLPatternSet url_set_; | 271 URLPatternSet url_set_; |
262 URLPatternSet exclude_url_set_; | 272 URLPatternSet exclude_url_set_; |
263 | 273 |
264 // List of js scripts defined in content_scripts | 274 // List of js scripts defined in content_scripts |
265 FileList js_scripts_; | 275 FileList js_scripts_; |
266 | 276 |
267 // List of css scripts defined in content_scripts | 277 // List of css scripts defined in content_scripts |
268 FileList css_scripts_; | 278 FileList css_scripts_; |
269 | 279 |
270 // The ID of the extension this script is a part of, if any. Can be empty if | 280 // The ID of the consumer this script is a part of. The host_id of the |
271 // the script is a "standlone" user script. | 281 // consumer_id can be empty if the script is a "standlone" user script. |
272 std::string extension_id_; | 282 linked_ptr<ConsumerID> consumer_id_; |
Devlin
2015/01/14 16:45:10
Let's not have these be linked_ptrs.
Xi Han
2015/01/14 23:46:04
Well, it has to be a pointer here since the consum
Devlin
2015/01/16 17:05:04
This should be fixable once you can just assign Co
Xi Han
2015/01/19 22:40:05
Done.
| |
273 | 283 |
274 // The globally-unique id associated with this user script. Defaults to | 284 // The globally-unique id associated with this user script. Defaults to |
275 // -1 for invalid. | 285 // -1 for invalid. |
276 int user_script_id_; | 286 int user_script_id_; |
277 | 287 |
278 // Whether we should try to emulate Greasemonkey's APIs when running this | 288 // Whether we should try to emulate Greasemonkey's APIs when running this |
279 // script. | 289 // script. |
280 bool emulate_greasemonkey_; | 290 bool emulate_greasemonkey_; |
281 | 291 |
282 // Whether the user script should run in all frames, or only just the top one. | 292 // Whether the user script should run in all frames, or only just the top one. |
283 // Defaults to false. | 293 // Defaults to false. |
284 bool match_all_frames_; | 294 bool match_all_frames_; |
285 | 295 |
286 // Whether the user script should run in about:blank and about:srcdoc as well. | 296 // Whether the user script should run in about:blank and about:srcdoc as well. |
287 // Defaults to false. | 297 // Defaults to false. |
288 bool match_about_blank_; | 298 bool match_about_blank_; |
289 | 299 |
290 // True if the script should be injected into an incognito tab. | 300 // True if the script should be injected into an incognito tab. |
291 bool incognito_enabled_; | 301 bool incognito_enabled_; |
292 }; | 302 }; |
293 | 303 |
294 // For storing UserScripts with unique IDs in sets. | 304 // For storing UserScripts with unique IDs in sets. |
295 bool operator<(const UserScript& script1, const UserScript& script2); | 305 bool operator<(const UserScript& script1, const UserScript& script2); |
296 | 306 |
297 typedef std::vector<UserScript> UserScriptList; | 307 typedef std::vector<UserScript> UserScriptList; |
298 | 308 |
299 } // namespace extensions | 309 } // namespace extensions |
300 | 310 |
301 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_ | 311 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_ |
OLD | NEW |