Chromium Code Reviews| 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 #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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 | 88 |
| 89 UserScript::UserScript() | 89 UserScript::UserScript() |
| 90 : run_location_(DOCUMENT_IDLE), | 90 : run_location_(DOCUMENT_IDLE), |
| 91 consumer_instance_type_(TAB), | 91 consumer_instance_type_(TAB), |
| 92 user_script_id_(-1), | 92 user_script_id_(-1), |
| 93 emulate_greasemonkey_(false), | 93 emulate_greasemonkey_(false), |
| 94 match_all_frames_(false), | 94 match_all_frames_(false), |
| 95 match_about_blank_(false), | 95 match_about_blank_(false), |
| 96 incognito_enabled_(false) {} | 96 incognito_enabled_(false) {} |
| 97 | 97 |
| 98 UserScript::UserScript(const UserScript& other) = default; | 98 UserScript::~UserScript() { |
| 99 } | |
| 99 | 100 |
| 100 UserScript::~UserScript() { | 101 // static. |
| 102 std::unique_ptr<UserScript> UserScript::CopyFrom(const UserScript& other) { | |
| 103 std::unique_ptr<UserScript> script(new UserScript()); | |
| 104 script->run_location_ = other.run_location_; | |
| 105 script->name_space_ = other.name_space_; | |
| 106 script->name_ = other.name_; | |
| 107 script->description_ = other.description_; | |
| 108 script->version_ = other.version_; | |
| 109 script->globs_ = other.globs_; | |
| 110 script->exclude_globs_ = other.exclude_globs_; | |
| 111 script->url_set_ = other.url_set_; | |
| 112 script->exclude_url_set_ = other.exclude_url_set_; | |
| 113 | |
| 114 // Note: File content is not copied. | |
|
Devlin
2016/08/17 16:39:31
Won't this copy file content? The copy ctor is de
lazyboy
2016/08/17 18:55:52
Good catch, I've made the copy ctor non-default.
| |
| 115 for (const std::unique_ptr<File>& file : other.js_scripts()) { | |
| 116 std::unique_ptr<File> file_copy(new File(*file)); | |
| 117 script->js_scripts_.push_back(std::move(file_copy)); | |
| 118 } | |
| 119 for (const std::unique_ptr<File>& file : other.css_scripts()) { | |
| 120 std::unique_ptr<File> file_copy(new File(*file)); | |
| 121 script->css_scripts_.push_back(std::move(file_copy)); | |
| 122 } | |
| 123 script->host_id_ = other.host_id_; | |
| 124 script->consumer_instance_type_ = other.consumer_instance_type_; | |
| 125 script->user_script_id_ = other.user_script_id_; | |
| 126 script->emulate_greasemonkey_ = other.emulate_greasemonkey_; | |
| 127 script->match_all_frames_ = other.match_all_frames_; | |
| 128 script->match_about_blank_ = other.match_about_blank_; | |
| 129 script->incognito_enabled_ = other.incognito_enabled_; | |
| 130 | |
| 131 return script; | |
| 101 } | 132 } |
| 102 | 133 |
| 103 void UserScript::add_url_pattern(const URLPattern& pattern) { | 134 void UserScript::add_url_pattern(const URLPattern& pattern) { |
| 104 url_set_.AddPattern(pattern); | 135 url_set_.AddPattern(pattern); |
| 105 } | 136 } |
| 106 | 137 |
| 107 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { | 138 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { |
| 108 exclude_url_set_.AddPattern(pattern); | 139 exclude_url_set_.AddPattern(pattern); |
| 109 } | 140 } |
| 110 | 141 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); | 217 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); |
| 187 pattern != pattern_list.end(); ++pattern) { | 218 pattern != pattern_list.end(); ++pattern) { |
| 188 pickle->WriteInt(pattern->valid_schemes()); | 219 pickle->WriteInt(pattern->valid_schemes()); |
| 189 pickle->WriteString(pattern->GetAsString()); | 220 pickle->WriteString(pattern->GetAsString()); |
| 190 } | 221 } |
| 191 } | 222 } |
| 192 | 223 |
| 193 void UserScript::PickleScripts(base::Pickle* pickle, | 224 void UserScript::PickleScripts(base::Pickle* pickle, |
| 194 const FileList& scripts) const { | 225 const FileList& scripts) const { |
| 195 pickle->WriteUInt32(scripts.size()); | 226 pickle->WriteUInt32(scripts.size()); |
| 196 for (FileList::const_iterator file = scripts.begin(); | 227 for (const std::unique_ptr<File>& file : scripts) |
| 197 file != scripts.end(); ++file) { | |
| 198 file->Pickle(pickle); | 228 file->Pickle(pickle); |
| 199 } | |
| 200 } | 229 } |
| 201 | 230 |
| 202 void UserScript::Unpickle(const base::Pickle& pickle, | 231 void UserScript::Unpickle(const base::Pickle& pickle, |
| 203 base::PickleIterator* iter) { | 232 base::PickleIterator* iter) { |
| 204 // Read the run location. | 233 // Read the run location. |
| 205 int run_location = 0; | 234 int run_location = 0; |
| 206 CHECK(iter->ReadInt(&run_location)); | 235 CHECK(iter->ReadInt(&run_location)); |
| 207 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 236 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
| 208 run_location_ = static_cast<RunLocation>(run_location); | 237 run_location_ = static_cast<RunLocation>(run_location); |
| 209 | 238 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 } | 304 } |
| 276 } | 305 } |
| 277 | 306 |
| 278 void UserScript::UnpickleScripts(const base::Pickle& pickle, | 307 void UserScript::UnpickleScripts(const base::Pickle& pickle, |
| 279 base::PickleIterator* iter, | 308 base::PickleIterator* iter, |
| 280 FileList* scripts) { | 309 FileList* scripts) { |
| 281 uint32_t num_files = 0; | 310 uint32_t num_files = 0; |
| 282 CHECK(iter->ReadUInt32(&num_files)); | 311 CHECK(iter->ReadUInt32(&num_files)); |
| 283 scripts->clear(); | 312 scripts->clear(); |
| 284 for (uint32_t i = 0; i < num_files; ++i) { | 313 for (uint32_t i = 0; i < num_files; ++i) { |
| 285 File file; | 314 std::unique_ptr<File> file(new File()); |
| 286 file.Unpickle(pickle, iter); | 315 file->Unpickle(pickle, iter); |
| 287 scripts->push_back(file); | 316 scripts->push_back(std::move(file)); |
| 288 } | 317 } |
| 289 } | 318 } |
| 290 | 319 |
| 291 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id) | 320 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id) |
| 292 : id(id), host_id(host_id) {} | 321 : id(id), host_id(host_id) {} |
| 293 | 322 |
| 294 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {} | 323 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {} |
| 295 | 324 |
| 296 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) { | 325 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) { |
| 297 return a.id < b.id; | 326 return a.id < b.id; |
| 298 } | 327 } |
| 299 | 328 |
| 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 | 329 } // namespace extensions |
| OLD | NEW |