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

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: remove browser/renderer specific file impl as it is not helping much 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/strings/string_piece.h" 12 #include "base/strings/string_piece.h"
13 #include "extensions/common/host_id.h" 13 #include "extensions/common/host_id.h"
14 #include "extensions/common/url_pattern.h" 14 #include "extensions/common/url_pattern.h"
15 #include "extensions/common/url_pattern_set.h" 15 #include "extensions/common/url_pattern_set.h"
16 #include "url/gurl.h" 16 #include "url/gurl.h"
17 17
18 namespace base { 18 namespace base {
19 class Pickle; 19 class Pickle;
20 class PickleIterator; 20 class PickleIterator;
21 } 21 }
22 22
23 namespace extensions { 23 namespace extensions {
24 24
25 // 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
26 // extension. 26 // extension.
27 // Contains information related to user scripts, either a standalone one, or one
28 // that is part of an extension.
29 // This class also contains script file (JS/CSS) related information, but not
30 // their file contents.
Devlin 2016/08/17 16:39:31 outdated, right?
lazyboy 2016/08/17 18:55:52 Yes, fixed.
27 class UserScript { 31 class UserScript {
28 public: 32 public:
29 // The file extension for standalone user scripts. 33 // The file extension for standalone user scripts.
30 static const char kFileExtension[]; 34 static const char kFileExtension[];
31 35
32 static int GenerateUserScriptID(); 36 static int GenerateUserScriptID();
33 37
34 // Check if a URL should be treated as a user script and converted to an 38 // Check if a URL should be treated as a user script and converted to an
35 // extension. 39 // extension.
36 static bool IsURLUserScript(const GURL& url, const std::string& mime_type); 40 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: 70 // is "idle". Currently this uses the simple heuristic of:
67 // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no 71 // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no
68 // particular injection point is guaranteed. 72 // particular injection point is guaranteed.
69 RUN_DEFERRED, // The user script's injection was deferred for permissions 73 RUN_DEFERRED, // The user script's injection was deferred for permissions
70 // reasons, and was executed at a later time. 74 // reasons, and was executed at a later time.
71 BROWSER_DRIVEN, // The user script will be injected when triggered by an 75 BROWSER_DRIVEN, // The user script will be injected when triggered by an
72 // IPC in the browser process. 76 // IPC in the browser process.
73 RUN_LOCATION_LAST // Leave this as the last item. 77 RUN_LOCATION_LAST // Leave this as the last item.
74 }; 78 };
75 79
76 // Holds actual script file info. 80 // Holds script file info.
77 class File { 81 class File {
78 public: 82 public:
79 File(const base::FilePath& extension_root, 83 File(const base::FilePath& extension_root,
80 const base::FilePath& relative_path, 84 const base::FilePath& relative_path,
81 const GURL& url); 85 const GURL& url);
82 File(); 86 File();
83 File(const File& other); 87 File(const File& other);
84 ~File(); 88 ~File();
85 89
86 const base::FilePath& extension_root() const { return extension_root_; } 90 const base::FilePath& extension_root() const { return extension_root_; }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 GURL url_; 123 GURL url_;
120 124
121 // The script content. It can be set to either loaded_content_ or 125 // The script content. It can be set to either loaded_content_ or
122 // externally allocated string. 126 // externally allocated string.
123 base::StringPiece external_content_; 127 base::StringPiece external_content_;
124 128
125 // Set when the content is loaded by LoadContent 129 // Set when the content is loaded by LoadContent
126 std::string content_; 130 std::string content_;
127 }; 131 };
128 132
129 typedef std::vector<File> FileList; 133 using FileList = std::vector<std::unique_ptr<File>>;
130 134
131 // Type of a API consumer instance that user scripts will be injected on. 135 // Type of a API consumer instance that user scripts will be injected on.
132 enum ConsumerInstanceType { TAB, WEBVIEW }; 136 enum ConsumerInstanceType { TAB, WEBVIEW };
133 137
134 // Constructor. Default the run location to document end, which is like 138 // Constructor. Default the run location to document end, which is like
135 // Greasemonkey and probably more useful for typical scripts. 139 // Greasemonkey and probably more useful for typical scripts.
136 UserScript(); 140 UserScript();
137 UserScript(const UserScript& other);
138 ~UserScript(); 141 ~UserScript();
139 142
143 // Peforms a copy of all fields.
144 static std::unique_ptr<UserScript> CopyFrom(const UserScript& other);
145
140 const std::string& name_space() const { return name_space_; } 146 const std::string& name_space() const { return name_space_; }
141 void set_name_space(const std::string& name_space) { 147 void set_name_space(const std::string& name_space) {
142 name_space_ = name_space; 148 name_space_ = name_space;
143 } 149 }
144 150
145 const std::string& name() const { return name_; } 151 const std::string& name() const { return name_; }
146 void set_name(const std::string& name) { name_ = name; } 152 void set_name(const std::string& name) { name_ = name; }
147 153
148 const std::string& version() const { return version_; } 154 const std::string& version() const { return version_; }
149 void set_version(const std::string& version) { 155 void set_version(const std::string& version) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 int id() const { return user_script_id_; } 223 int id() const { return user_script_id_; }
218 void set_id(int id) { user_script_id_ = id; } 224 void set_id(int id) { user_script_id_ = id; }
219 225
220 bool is_incognito_enabled() const { return incognito_enabled_; } 226 bool is_incognito_enabled() const { return incognito_enabled_; }
221 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; } 227 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; }
222 228
223 // Returns true if the script should be applied to the specified URL, false 229 // Returns true if the script should be applied to the specified URL, false
224 // otherwise. 230 // otherwise.
225 bool MatchesURL(const GURL& url) const; 231 bool MatchesURL(const GURL& url) const;
226 232
227 // Serialize the UserScript into a pickle. The content of the scripts and 233 // Serializes the UserScript into a pickle. The content of the scripts and
228 // paths to UserScript::Files will not be serialized! 234 // paths to UserScript::Files will not be serialized!
229 void Pickle(base::Pickle* pickle) const; 235 void Pickle(base::Pickle* pickle) const;
230 236
231 // Deserialize the script from a pickle. Note that this always succeeds 237 // 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 238 // because presumably we were the one that pickled it, and we did it
233 // correctly. 239 // correctly.
234 void Unpickle(const base::Pickle& pickle, base::PickleIterator* iter); 240 void Unpickle(const base::Pickle& pickle, base::PickleIterator* iter);
235 241
236 private: 242 private:
237 // base::Pickle helper functions used to pickle the individual types of 243 // base::Pickle helper functions used to pickle the individual types of
238 // components. 244 // components.
239 void PickleGlobs(base::Pickle* pickle, 245 void PickleGlobs(base::Pickle* pickle,
240 const std::vector<std::string>& globs) const; 246 const std::vector<std::string>& globs) const;
241 void PickleHostID(base::Pickle* pickle, const HostID& host_id) const; 247 void PickleHostID(base::Pickle* pickle, const HostID& host_id) const;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 // Whether the user script should run in all frames, or only just the top one. 314 // Whether the user script should run in all frames, or only just the top one.
309 // Defaults to false. 315 // Defaults to false.
310 bool match_all_frames_; 316 bool match_all_frames_;
311 317
312 // Whether the user script should run in about:blank and about:srcdoc as well. 318 // Whether the user script should run in about:blank and about:srcdoc as well.
313 // Defaults to false. 319 // Defaults to false.
314 bool match_about_blank_; 320 bool match_about_blank_;
315 321
316 // True if the script should be injected into an incognito tab. 322 // True if the script should be injected into an incognito tab.
317 bool incognito_enabled_; 323 bool incognito_enabled_;
324
325 private:
Devlin 2016/08/17 16:39:31 this already is private:, right?
lazyboy 2016/08/17 18:55:52 Done.
326 DISALLOW_COPY_AND_ASSIGN(UserScript);
318 }; 327 };
319 328
320 // Information we need while removing scripts from a UserScriptLoader. 329 // Information we need while removing scripts from a UserScriptLoader.
321 struct UserScriptIDPair { 330 struct UserScriptIDPair {
322 UserScriptIDPair(int id, const HostID& host_id); 331 UserScriptIDPair(int id, const HostID& host_id);
323 UserScriptIDPair(int id); 332 UserScriptIDPair(int id);
324 333
325 int id; 334 int id;
326 HostID host_id; 335 HostID host_id;
327 }; 336 };
328 337
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); 338 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b);
333 339
334 typedef std::vector<UserScript> UserScriptList; 340 using UserScriptList = std::vector<std::unique_ptr<UserScript>>;
335 341
336 } // namespace extensions 342 } // namespace extensions
337 343
338 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_ 344 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698