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

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

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync @tott 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
« no previous file with comments | « extensions/common/user_script.h ('k') | extensions/common/user_script_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "extensions/common/user_script.h" 5 #include "extensions/common/user_script.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/atomic_sequence_num.h" 10 #include "base/atomic_sequence_num.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 UserScript::File::File(const base::FilePath& extension_root, 75 UserScript::File::File(const base::FilePath& extension_root,
76 const base::FilePath& relative_path, 76 const base::FilePath& relative_path,
77 const GURL& url) 77 const GURL& url)
78 : extension_root_(extension_root), 78 : extension_root_(extension_root),
79 relative_path_(relative_path), 79 relative_path_(relative_path),
80 url_(url) { 80 url_(url) {
81 } 81 }
82 82
83 UserScript::File::File() {} 83 UserScript::File::File() {}
84 84
85 UserScript::File::File(const File& other) = default; 85 // File content is not copied.
86 UserScript::File::File(const File& other)
87 : extension_root_(other.extension_root_),
88 relative_path_(other.relative_path_),
89 url_(other.url_) {}
86 90
87 UserScript::File::~File() {} 91 UserScript::File::~File() {}
88 92
89 UserScript::UserScript() 93 UserScript::UserScript()
90 : run_location_(DOCUMENT_IDLE), 94 : run_location_(DOCUMENT_IDLE),
91 consumer_instance_type_(TAB), 95 consumer_instance_type_(TAB),
92 user_script_id_(-1), 96 user_script_id_(-1),
93 emulate_greasemonkey_(false), 97 emulate_greasemonkey_(false),
94 match_all_frames_(false), 98 match_all_frames_(false),
95 match_about_blank_(false), 99 match_about_blank_(false),
96 incognito_enabled_(false) {} 100 incognito_enabled_(false) {}
97 101
98 UserScript::UserScript(const UserScript& other) = default; 102 UserScript::~UserScript() {
103 }
99 104
100 UserScript::~UserScript() { 105 // static.
106 std::unique_ptr<UserScript> UserScript::CopyMetadataFrom(
107 const UserScript& other) {
108 std::unique_ptr<UserScript> script(new UserScript());
109 script->run_location_ = other.run_location_;
110 script->name_space_ = other.name_space_;
111 script->name_ = other.name_;
112 script->description_ = other.description_;
113 script->version_ = other.version_;
114 script->globs_ = other.globs_;
115 script->exclude_globs_ = other.exclude_globs_;
116 script->url_set_ = other.url_set_;
117 script->exclude_url_set_ = other.exclude_url_set_;
118
119 // Note: File content is not copied.
120 for (const std::unique_ptr<File>& file : other.js_scripts()) {
121 std::unique_ptr<File> file_copy(new File(*file));
122 script->js_scripts_.push_back(std::move(file_copy));
123 }
124 for (const std::unique_ptr<File>& file : other.css_scripts()) {
125 std::unique_ptr<File> file_copy(new File(*file));
126 script->css_scripts_.push_back(std::move(file_copy));
127 }
128 script->host_id_ = other.host_id_;
129 script->consumer_instance_type_ = other.consumer_instance_type_;
130 script->user_script_id_ = other.user_script_id_;
131 script->emulate_greasemonkey_ = other.emulate_greasemonkey_;
132 script->match_all_frames_ = other.match_all_frames_;
133 script->match_about_blank_ = other.match_about_blank_;
134 script->incognito_enabled_ = other.incognito_enabled_;
135
136 return script;
101 } 137 }
102 138
103 void UserScript::add_url_pattern(const URLPattern& pattern) { 139 void UserScript::add_url_pattern(const URLPattern& pattern) {
104 url_set_.AddPattern(pattern); 140 url_set_.AddPattern(pattern);
105 } 141 }
106 142
107 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { 143 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) {
108 exclude_url_set_.AddPattern(pattern); 144 exclude_url_set_.AddPattern(pattern);
109 } 145 }
110 146
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); 222 for (URLPatternSet::const_iterator pattern = pattern_list.begin();
187 pattern != pattern_list.end(); ++pattern) { 223 pattern != pattern_list.end(); ++pattern) {
188 pickle->WriteInt(pattern->valid_schemes()); 224 pickle->WriteInt(pattern->valid_schemes());
189 pickle->WriteString(pattern->GetAsString()); 225 pickle->WriteString(pattern->GetAsString());
190 } 226 }
191 } 227 }
192 228
193 void UserScript::PickleScripts(base::Pickle* pickle, 229 void UserScript::PickleScripts(base::Pickle* pickle,
194 const FileList& scripts) const { 230 const FileList& scripts) const {
195 pickle->WriteUInt32(scripts.size()); 231 pickle->WriteUInt32(scripts.size());
196 for (FileList::const_iterator file = scripts.begin(); 232 for (const std::unique_ptr<File>& file : scripts)
197 file != scripts.end(); ++file) {
198 file->Pickle(pickle); 233 file->Pickle(pickle);
199 }
200 } 234 }
201 235
202 void UserScript::Unpickle(const base::Pickle& pickle, 236 void UserScript::Unpickle(const base::Pickle& pickle,
203 base::PickleIterator* iter) { 237 base::PickleIterator* iter) {
204 // Read the run location. 238 // Read the run location.
205 int run_location = 0; 239 int run_location = 0;
206 CHECK(iter->ReadInt(&run_location)); 240 CHECK(iter->ReadInt(&run_location));
207 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); 241 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST);
208 run_location_ = static_cast<RunLocation>(run_location); 242 run_location_ = static_cast<RunLocation>(run_location);
209 243
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 } 309 }
276 } 310 }
277 311
278 void UserScript::UnpickleScripts(const base::Pickle& pickle, 312 void UserScript::UnpickleScripts(const base::Pickle& pickle,
279 base::PickleIterator* iter, 313 base::PickleIterator* iter,
280 FileList* scripts) { 314 FileList* scripts) {
281 uint32_t num_files = 0; 315 uint32_t num_files = 0;
282 CHECK(iter->ReadUInt32(&num_files)); 316 CHECK(iter->ReadUInt32(&num_files));
283 scripts->clear(); 317 scripts->clear();
284 for (uint32_t i = 0; i < num_files; ++i) { 318 for (uint32_t i = 0; i < num_files; ++i) {
285 File file; 319 std::unique_ptr<File> file(new File());
286 file.Unpickle(pickle, iter); 320 file->Unpickle(pickle, iter);
287 scripts->push_back(file); 321 scripts->push_back(std::move(file));
288 } 322 }
289 } 323 }
290 324
291 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id) 325 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id)
292 : id(id), host_id(host_id) {} 326 : id(id), host_id(host_id) {}
293 327
294 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {} 328 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {}
295 329
296 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) { 330 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) {
297 return a.id < b.id; 331 return a.id < b.id;
298 } 332 }
299 333
300 bool operator<(const UserScript& script1, const UserScript& script2) {
301 // The only kind of script that should be compared is the kind that has its
302 // IDs initialized to a meaningful value.
303 DCHECK(script1.id() != -1 && script2.id() != -1);
304 return script1.id() < script2.id();
305 }
306
307 } // namespace extensions 334 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/common/user_script.h ('k') | extensions/common/user_script_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698