Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(290)

Side by Side Diff: extensions/common/user_script.h

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: uplaod with base Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/pickle.h"
12 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
13 #include "extensions/common/host_id.h" 14 #include "extensions/common/host_id.h"
14 #include "extensions/common/url_pattern.h" 15 #include "extensions/common/url_pattern.h"
15 #include "extensions/common/url_pattern_set.h" 16 #include "extensions/common/url_pattern_set.h"
16 #include "url/gurl.h" 17 #include "url/gurl.h"
17 18
18 namespace base { 19 namespace base {
19 class Pickle; 20 class Pickle;
20 class PickleIterator; 21 class PickleIterator;
21 } 22 }
22 23
23 namespace extensions { 24 namespace extensions {
24 25
25 // Represents a user script, either a standalone one, or one that is part of an
26 // extension.
27 class UserScript { 26 class UserScript {
28 public: 27 public:
29 // The file extension for standalone user scripts. 28 // The file extension for standalone user scripts.
30 static const char kFileExtension[]; 29 static const char kFileExtension[];
31 30
32 static int GenerateUserScriptID(); 31 static int GenerateUserScriptID();
33 32
34 // Check if a URL should be treated as a user script and converted to an 33 // Check if a URL should be treated as a user script and converted to an
35 // extension. 34 // extension.
36 static bool IsURLUserScript(const GURL& url, const std::string& mime_type); 35 static bool IsURLUserScript(const GURL& url, const std::string& mime_type);
(...skipping 29 matching lines...) Expand all
66 // is "idle". Currently this uses the simple heuristic of: 65 // is "idle". Currently this uses the simple heuristic of:
67 // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no 66 // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no
68 // particular injection point is guaranteed. 67 // particular injection point is guaranteed.
69 RUN_DEFERRED, // The user script's injection was deferred for permissions 68 RUN_DEFERRED, // The user script's injection was deferred for permissions
70 // reasons, and was executed at a later time. 69 // reasons, and was executed at a later time.
71 BROWSER_DRIVEN, // The user script will be injected when triggered by an 70 BROWSER_DRIVEN, // The user script will be injected when triggered by an
72 // IPC in the browser process. 71 // IPC in the browser process.
73 RUN_LOCATION_LAST // Leave this as the last item. 72 RUN_LOCATION_LAST // Leave this as the last item.
74 }; 73 };
75 74
76 // Holds actual script file info.
77 class File {
78 public:
79 File(const base::FilePath& extension_root,
80 const base::FilePath& relative_path,
81 const GURL& url);
82 File();
83 File(const File& other);
84 ~File();
85
86 const base::FilePath& extension_root() const { return extension_root_; }
87 const base::FilePath& relative_path() const { return relative_path_; }
88
89 const GURL& url() const { return url_; }
90 void set_url(const GURL& url) { url_ = url; }
91
92 // If external_content_ is set returns it as content otherwise it returns
93 // content_
94 const base::StringPiece GetContent() const {
95 if (external_content_.data())
96 return external_content_;
97 else
98 return content_;
99 }
100 void set_external_content(const base::StringPiece& content) {
101 external_content_ = content;
102 }
103 void set_content(const base::StringPiece& content) {
104 content_.assign(content.begin(), content.end());
105 }
106
107 // Serialization support. The content and FilePath members will not be
108 // serialized!
109 void Pickle(base::Pickle* pickle) const;
110 void Unpickle(const base::Pickle& pickle, base::PickleIterator* iter);
111
112 private:
113 // Where the script file lives on the disk. We keep the path split so that
114 // it can be localized at will.
115 base::FilePath extension_root_;
116 base::FilePath relative_path_;
117
118 // The url to this scipt file.
119 GURL url_;
120
121 // The script content. It can be set to either loaded_content_ or
122 // externally allocated string.
123 base::StringPiece external_content_;
124
125 // Set when the content is loaded by LoadContent
126 std::string content_;
127 };
128
129 typedef std::vector<File> FileList;
130 75
131 // Type of a API consumer instance that user scripts will be injected on. 76 // Type of a API consumer instance that user scripts will be injected on.
132 enum ConsumerInstanceType { TAB, WEBVIEW }; 77 enum ConsumerInstanceType { TAB, WEBVIEW };
78 };
133 79
80 // Contains information related to user scripts, either a standalone one, or one
81 // that is part of an extension.
82 // This class does not contain any script file (JS/CSS) related information.
83 class UserScriptInfo {
84 public:
134 // Constructor. Default the run location to document end, which is like 85 // Constructor. Default the run location to document end, which is like
135 // Greasemonkey and probably more useful for typical scripts. 86 // Greasemonkey and probably more useful for typical scripts.
136 UserScript(); 87 UserScriptInfo();
137 UserScript(const UserScript& other); 88 UserScriptInfo(const UserScriptInfo& other);
138 ~UserScript(); 89 virtual ~UserScriptInfo();
139 90
140 const std::string& name_space() const { return name_space_; } 91 const std::string& name_space() const { return name_space_; }
141 void set_name_space(const std::string& name_space) { 92 void set_name_space(const std::string& name_space) {
142 name_space_ = name_space; 93 name_space_ = name_space;
143 } 94 }
144 95
145 const std::string& name() const { return name_; } 96 const std::string& name() const { return name_; }
146 void set_name(const std::string& name) { name_ = name; } 97 void set_name(const std::string& name) { name_ = name; }
147 98
148 const std::string& version() const { return version_; } 99 const std::string& version() const { return version_; }
149 void set_version(const std::string& version) { 100 void set_version(const std::string& version) {
150 version_ = version; 101 version_ = version;
151 } 102 }
152 103
153 const std::string& description() const { return description_; } 104 const std::string& description() const { return description_; }
154 void set_description(const std::string& description) { 105 void set_description(const std::string& description) {
155 description_ = description; 106 description_ = description;
156 } 107 }
157 108
158 // The place in the document to run the script. 109 // The place in the document to run the script.
159 RunLocation run_location() const { return run_location_; } 110 UserScript::RunLocation run_location() const { return run_location_; }
160 void set_run_location(RunLocation location) { run_location_ = location; } 111 void set_run_location(UserScript::RunLocation location) {
112 run_location_ = location;
113 }
161 114
162 // Whether to emulate greasemonkey when running this script. 115 // Whether to emulate greasemonkey when running this script.
163 bool emulate_greasemonkey() const { return emulate_greasemonkey_; } 116 bool emulate_greasemonkey() const { return emulate_greasemonkey_; }
164 void set_emulate_greasemonkey(bool val) { emulate_greasemonkey_ = val; } 117 void set_emulate_greasemonkey(bool val) { emulate_greasemonkey_ = val; }
165 118
166 // Whether to match all frames, or only the top one. 119 // Whether to match all frames, or only the top one.
167 bool match_all_frames() const { return match_all_frames_; } 120 bool match_all_frames() const { return match_all_frames_; }
168 void set_match_all_frames(bool val) { match_all_frames_ = val; } 121 void set_match_all_frames(bool val) { match_all_frames_ = val; }
169 122
170 // Whether to match about:blank and about:srcdoc. 123 // Whether to match about:blank and about:srcdoc.
(...skipping 15 matching lines...) Expand all
186 139
187 // The URLPatterns, if any, that determine which pages this script runs 140 // The URLPatterns, if any, that determine which pages this script runs
188 // against. 141 // against.
189 const URLPatternSet& url_patterns() const { return url_set_; } 142 const URLPatternSet& url_patterns() const { return url_set_; }
190 void add_url_pattern(const URLPattern& pattern); 143 void add_url_pattern(const URLPattern& pattern);
191 const URLPatternSet& exclude_url_patterns() const { 144 const URLPatternSet& exclude_url_patterns() const {
192 return exclude_url_set_; 145 return exclude_url_set_;
193 } 146 }
194 void add_exclude_url_pattern(const URLPattern& pattern); 147 void add_exclude_url_pattern(const URLPattern& pattern);
195 148
196 // List of js scripts for this user script
197 FileList& js_scripts() { return js_scripts_; }
198 const FileList& js_scripts() const { return js_scripts_; }
199
200 // List of css scripts for this user script
201 FileList& css_scripts() { return css_scripts_; }
202 const FileList& css_scripts() const { return css_scripts_; }
203
204 const std::string& extension_id() const { return host_id_.id(); } 149 const std::string& extension_id() const { return host_id_.id(); }
205 150
206 const HostID& host_id() const { return host_id_; } 151 const HostID& host_id() const { return host_id_; }
207 void set_host_id(const HostID& host_id) { host_id_ = host_id; } 152 void set_host_id(const HostID& host_id) { host_id_ = host_id; }
208 153
209 const ConsumerInstanceType& consumer_instance_type() const { 154 const UserScript::ConsumerInstanceType& consumer_instance_type() const {
210 return consumer_instance_type_; 155 return consumer_instance_type_;
211 } 156 }
212 void set_consumer_instance_type( 157 void set_consumer_instance_type(
213 const ConsumerInstanceType& consumer_instance_type) { 158 const UserScript::ConsumerInstanceType& consumer_instance_type) {
214 consumer_instance_type_ = consumer_instance_type; 159 consumer_instance_type_ = consumer_instance_type;
215 } 160 }
216 161
217 int id() const { return user_script_id_; } 162 int id() const { return user_script_id_; }
218 void set_id(int id) { user_script_id_ = id; } 163 void set_id(int id) { user_script_id_ = id; }
219 164
220 bool is_incognito_enabled() const { return incognito_enabled_; } 165 bool is_incognito_enabled() const { return incognito_enabled_; }
221 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; } 166 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; }
222 167
223 // Returns true if the script should be applied to the specified URL, false 168 // Returns true if the script should be applied to the specified URL, false
224 // otherwise. 169 // otherwise.
225 bool MatchesURL(const GURL& url) const; 170 bool MatchesURL(const GURL& url) const;
226 171
227 // Serialize the UserScript into a pickle. The content of the scripts and 172 protected:
173 // Serializes the UserScriptInfo into a pickle. The content of the scripts and
228 // paths to UserScript::Files will not be serialized! 174 // paths to UserScript::Files will not be serialized!
229 void Pickle(base::Pickle* pickle) const; 175 void Pickle(base::Pickle* pickle) const;
230 176
231 // Deserialize the script from a pickle. Note that this always succeeds 177 // Deserializes the script from a pickle. Note that this always succeeds
232 // because presumably we were the one that pickled it, and we did it 178 // because presumably we were the one that pickled it, and we did it
233 // correctly. 179 // correctly.
234 void Unpickle(const base::Pickle& pickle, base::PickleIterator* iter); 180 void Unpickle(const base::Pickle& pickle, base::PickleIterator* iter);
235 181
236 private:
237 // base::Pickle helper functions used to pickle the individual types of 182 // base::Pickle helper functions used to pickle the individual types of
238 // components. 183 // components.
239 void PickleGlobs(base::Pickle* pickle, 184 void PickleGlobs(base::Pickle* pickle,
240 const std::vector<std::string>& globs) const; 185 const std::vector<std::string>& globs) const;
241 void PickleHostID(base::Pickle* pickle, const HostID& host_id) const; 186 void PickleHostID(base::Pickle* pickle, const HostID& host_id) const;
242 void PickleURLPatternSet(base::Pickle* pickle, 187 void PickleURLPatternSet(base::Pickle* pickle,
243 const URLPatternSet& pattern_list) const; 188 const URLPatternSet& pattern_list) const;
244 void PickleScripts(base::Pickle* pickle, const FileList& scripts) const;
245 189
246 // Unpickle helper functions used to unpickle individual types of components. 190 // Unpickle helper functions used to unpickle individual types of components.
247 void UnpickleGlobs(const base::Pickle& pickle, 191 void UnpickleGlobs(const base::Pickle& pickle,
248 base::PickleIterator* iter, 192 base::PickleIterator* iter,
249 std::vector<std::string>* globs); 193 std::vector<std::string>* globs);
250 void UnpickleHostID(const base::Pickle& pickle, 194 void UnpickleHostID(const base::Pickle& pickle,
251 base::PickleIterator* iter, 195 base::PickleIterator* iter,
252 HostID* host_id); 196 HostID* host_id);
253 void UnpickleURLPatternSet(const base::Pickle& pickle, 197 void UnpickleURLPatternSet(const base::Pickle& pickle,
254 base::PickleIterator* iter, 198 base::PickleIterator* iter,
255 URLPatternSet* pattern_list); 199 URLPatternSet* pattern_list);
256 void UnpickleScripts(const base::Pickle& pickle,
257 base::PickleIterator* iter,
258 FileList* scripts);
259 200
260 // The location to run the script inside the document. 201 // The location to run the script inside the document.
261 RunLocation run_location_; 202 UserScript::RunLocation run_location_;
262 203
263 // The namespace of the script. This is used by Greasemonkey in the same way 204 // The namespace of the script. This is used by Greasemonkey in the same way
264 // as XML namespaces. Only used when parsing Greasemonkey-style scripts. 205 // as XML namespaces. Only used when parsing Greasemonkey-style scripts.
265 std::string name_space_; 206 std::string name_space_;
266 207
267 // The script's name. Only used when parsing Greasemonkey-style scripts. 208 // The script's name. Only used when parsing Greasemonkey-style scripts.
268 std::string name_; 209 std::string name_;
269 210
270 // A longer description. Only used when parsing Greasemonkey-style scripts. 211 // A longer description. Only used when parsing Greasemonkey-style scripts.
271 std::string description_; 212 std::string description_;
272 213
273 // A version number of the script. Only used when parsing Greasemonkey-style 214 // A version number of the script. Only used when parsing Greasemonkey-style
274 // scripts. 215 // scripts.
275 std::string version_; 216 std::string version_;
276 217
277 // Greasemonkey-style globs that determine pages to inject the script into. 218 // Greasemonkey-style globs that determine pages to inject the script into.
278 // These are only used with standalone scripts. 219 // These are only used with standalone scripts.
279 std::vector<std::string> globs_; 220 std::vector<std::string> globs_;
280 std::vector<std::string> exclude_globs_; 221 std::vector<std::string> exclude_globs_;
281 222
282 // URLPatterns that determine pages to inject the script into. These are 223 // URLPatterns that determine pages to inject the script into. These are
283 // only used with scripts that are part of extensions. 224 // only used with scripts that are part of extensions.
284 URLPatternSet url_set_; 225 URLPatternSet url_set_;
285 URLPatternSet exclude_url_set_; 226 URLPatternSet exclude_url_set_;
286 227
287 // List of js scripts defined in content_scripts
288 FileList js_scripts_;
289
290 // List of css scripts defined in content_scripts
291 FileList css_scripts_;
292
293 // The ID of the host this script is a part of. The |ID| of the 228 // The ID of the host this script is a part of. The |ID| of the
294 // |host_id| can be empty if the script is a "standlone" user script. 229 // |host_id| can be empty if the script is a "standlone" user script.
295 HostID host_id_; 230 HostID host_id_;
296 231
297 // The type of the consumer instance that the script will be injected. 232 // The type of the consumer instance that the script will be injected.
298 ConsumerInstanceType consumer_instance_type_; 233 UserScript::ConsumerInstanceType consumer_instance_type_;
299 234
300 // The globally-unique id associated with this user script. Defaults to 235 // The globally-unique id associated with this user script. Defaults to
301 // -1 for invalid. 236 // -1 for invalid.
302 int user_script_id_; 237 int user_script_id_;
303 238
304 // Whether we should try to emulate Greasemonkey's APIs when running this 239 // Whether we should try to emulate Greasemonkey's APIs when running this
305 // script. 240 // script.
306 bool emulate_greasemonkey_; 241 bool emulate_greasemonkey_;
307 242
308 // Whether the user script should run in all frames, or only just the top one. 243 // Whether the user script should run in all frames, or only just the top one.
309 // Defaults to false. 244 // Defaults to false.
310 bool match_all_frames_; 245 bool match_all_frames_;
311 246
312 // Whether the user script should run in about:blank and about:srcdoc as well. 247 // Whether the user script should run in about:blank and about:srcdoc as well.
313 // Defaults to false. 248 // Defaults to false.
314 bool match_about_blank_; 249 bool match_about_blank_;
315 250
316 // True if the script should be injected into an incognito tab. 251 // True if the script should be injected into an incognito tab.
317 bool incognito_enabled_; 252 bool incognito_enabled_;
253
254 private:
255 FRIEND_TEST_ALL_PREFIXES(ExtensionUserScriptTest, Pickle);
318 }; 256 };
319 257
320 // Information we need while removing scripts from a UserScriptLoader. 258 // Represents a set of JS/CSS files for a user script.
259 template <typename FileType>
260 class UserScriptFiles {
261 public:
262 UserScriptFiles() {}
263 virtual ~UserScriptFiles() {}
264
265 using FileListType = std::vector<std::unique_ptr<FileType>>;
266
267 FileListType& js_scripts() { return js_scripts_; }
268 const FileListType& js_scripts() const { return js_scripts_; }
269 FileListType& css_scripts() { return css_scripts_; }
270 const FileListType& css_scripts() const { return css_scripts_; }
271
272 void PickleFiles(base::Pickle* pickle) const {
273 PickleFiles(js_scripts_, pickle);
274 PickleFiles(css_scripts_, pickle);
275 }
276
277 void UnpickleFiles(const base::Pickle& pickle, base::PickleIterator* iter) {
278 UnpickleFiles(pickle, iter, &js_scripts_);
279 UnpickleFiles(pickle, iter, &css_scripts_);
280 }
281
282 protected:
283 std::vector<std::unique_ptr<FileType>> js_scripts_;
284 std::vector<std::unique_ptr<FileType>> css_scripts_;
285
286 private:
287 void PickleFiles(const std::vector<std::unique_ptr<FileType>>& scripts,
288 base::Pickle* pickle) const {
289 pickle->WriteUInt32(scripts.size());
290 for (const std::unique_ptr<FileType>& file : scripts)
291 file->Pickle(pickle);
292 }
293
294 void UnpickleFiles(const base::Pickle& pickle,
295 base::PickleIterator* iter,
296 std::vector<std::unique_ptr<FileType>>* scripts) {
297 uint32_t num_files = 0;
298 CHECK(iter->ReadUInt32(&num_files));
299 scripts->clear();
300 scripts->reserve(num_files);
301 for (uint32_t i = 0; i < num_files; ++i) {
302 std::unique_ptr<FileType> file(new FileType());
303 file->Unpickle(pickle, iter);
304 scripts->push_back(std::move(file));
305 }
306 }
307 };
308
309 // Holds information about a single script file: JS or CSS.
310 // Does not contain any file content.
311 class UserScriptFileInfo {
Devlin 2016/08/11 17:05:54 (possibly see comment in extensions/browser/browse
312 public:
313 UserScriptFileInfo(const base::FilePath& extension_root,
314 const base::FilePath& relative_path,
315 const GURL& url);
316 UserScriptFileInfo(const UserScriptFileInfo& other);
317 UserScriptFileInfo();
318 virtual ~UserScriptFileInfo();
319
320 const base::FilePath& extension_root() const { return extension_root_; }
321 const base::FilePath& relative_path() const { return relative_path_; }
322
323 const GURL& url() const { return url_; }
324 void set_url(const GURL& url) { url_ = url; }
325
326 protected:
327 void Pickle(base::Pickle* pickle) const;
328 void Unpickle(const base::Pickle& pickle, base::PickleIterator* iter);
329
330 // Where the script file lives on the disk. We keep the path split so that
331 // it can be localized at will.
332 base::FilePath extension_root_;
333 base::FilePath relative_path_;
334
335 // The url to this scipt file.
336 GURL url_;
337
338 private:
339 template <typename FileType>
340 friend class UserScriptFiles;
341
342 FRIEND_TEST_ALL_PREFIXES(ExtensionUserScriptTest, Pickle);
343 };
344
345 // Contains all metadata about extension's user script(s).
346 // e.g. it will contain information about the script itself and a list of file
347 // urls/paths for that script.
348 class ScriptMetadata : public UserScriptInfo,
349 public UserScriptFiles<UserScriptFileInfo> {
Devlin 2016/08/11 17:05:54 I think style disallows multiple inheritance of im
350 public:
351 ScriptMetadata();
352 ScriptMetadata(const UserScriptInfo& meta);
353 ~ScriptMetadata() override;
354
355 private:
356 DISALLOW_COPY_AND_ASSIGN(ScriptMetadata);
357 };
358
321 struct UserScriptIDPair { 359 struct UserScriptIDPair {
322 UserScriptIDPair(int id, const HostID& host_id); 360 UserScriptIDPair(int id, const HostID& host_id);
323 UserScriptIDPair(int id); 361 UserScriptIDPair(int id);
324 362
325 int id; 363 int id;
326 HostID host_id; 364 HostID host_id;
327 }; 365 };
328 366
329 // For storing UserScripts with unique IDs in sets.
330 bool operator<(const UserScript& script1, const UserScript& script2);
331
332 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b); 367 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b);
333 368
334 typedef std::vector<UserScript> UserScriptList; 369 using ScriptMetadataList = std::vector<std::unique_ptr<ScriptMetadata>>;
335 370
336 } // namespace extensions 371 } // namespace extensions
337 372
338 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_ 373 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698