| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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::CopyFrom(const UserScript& other) { |
| 107 std::unique_ptr<UserScript> script(new UserScript()); |
| 108 script->run_location_ = other.run_location_; |
| 109 script->name_space_ = other.name_space_; |
| 110 script->name_ = other.name_; |
| 111 script->description_ = other.description_; |
| 112 script->version_ = other.version_; |
| 113 script->globs_ = other.globs_; |
| 114 script->exclude_globs_ = other.exclude_globs_; |
| 115 script->url_set_ = other.url_set_; |
| 116 script->exclude_url_set_ = other.exclude_url_set_; |
| 117 |
| 118 // Note: File content is not copied. |
| 119 for (const std::unique_ptr<File>& file : other.js_scripts()) { |
| 120 std::unique_ptr<File> file_copy(new File(*file)); |
| 121 script->js_scripts_.push_back(std::move(file_copy)); |
| 122 } |
| 123 for (const std::unique_ptr<File>& file : other.css_scripts()) { |
| 124 std::unique_ptr<File> file_copy(new File(*file)); |
| 125 script->css_scripts_.push_back(std::move(file_copy)); |
| 126 } |
| 127 script->host_id_ = other.host_id_; |
| 128 script->consumer_instance_type_ = other.consumer_instance_type_; |
| 129 script->user_script_id_ = other.user_script_id_; |
| 130 script->emulate_greasemonkey_ = other.emulate_greasemonkey_; |
| 131 script->match_all_frames_ = other.match_all_frames_; |
| 132 script->match_about_blank_ = other.match_about_blank_; |
| 133 script->incognito_enabled_ = other.incognito_enabled_; |
| 134 |
| 135 return script; |
| 101 } | 136 } |
| 102 | 137 |
| 103 void UserScript::add_url_pattern(const URLPattern& pattern) { | 138 void UserScript::add_url_pattern(const URLPattern& pattern) { |
| 104 url_set_.AddPattern(pattern); | 139 url_set_.AddPattern(pattern); |
| 105 } | 140 } |
| 106 | 141 |
| 107 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { | 142 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { |
| 108 exclude_url_set_.AddPattern(pattern); | 143 exclude_url_set_.AddPattern(pattern); |
| 109 } | 144 } |
| 110 | 145 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); | 221 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); |
| 187 pattern != pattern_list.end(); ++pattern) { | 222 pattern != pattern_list.end(); ++pattern) { |
| 188 pickle->WriteInt(pattern->valid_schemes()); | 223 pickle->WriteInt(pattern->valid_schemes()); |
| 189 pickle->WriteString(pattern->GetAsString()); | 224 pickle->WriteString(pattern->GetAsString()); |
| 190 } | 225 } |
| 191 } | 226 } |
| 192 | 227 |
| 193 void UserScript::PickleScripts(base::Pickle* pickle, | 228 void UserScript::PickleScripts(base::Pickle* pickle, |
| 194 const FileList& scripts) const { | 229 const FileList& scripts) const { |
| 195 pickle->WriteUInt32(scripts.size()); | 230 pickle->WriteUInt32(scripts.size()); |
| 196 for (FileList::const_iterator file = scripts.begin(); | 231 for (const std::unique_ptr<File>& file : scripts) |
| 197 file != scripts.end(); ++file) { | |
| 198 file->Pickle(pickle); | 232 file->Pickle(pickle); |
| 199 } | |
| 200 } | 233 } |
| 201 | 234 |
| 202 void UserScript::Unpickle(const base::Pickle& pickle, | 235 void UserScript::Unpickle(const base::Pickle& pickle, |
| 203 base::PickleIterator* iter) { | 236 base::PickleIterator* iter) { |
| 204 // Read the run location. | 237 // Read the run location. |
| 205 int run_location = 0; | 238 int run_location = 0; |
| 206 CHECK(iter->ReadInt(&run_location)); | 239 CHECK(iter->ReadInt(&run_location)); |
| 207 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 240 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
| 208 run_location_ = static_cast<RunLocation>(run_location); | 241 run_location_ = static_cast<RunLocation>(run_location); |
| 209 | 242 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 } | 308 } |
| 276 } | 309 } |
| 277 | 310 |
| 278 void UserScript::UnpickleScripts(const base::Pickle& pickle, | 311 void UserScript::UnpickleScripts(const base::Pickle& pickle, |
| 279 base::PickleIterator* iter, | 312 base::PickleIterator* iter, |
| 280 FileList* scripts) { | 313 FileList* scripts) { |
| 281 uint32_t num_files = 0; | 314 uint32_t num_files = 0; |
| 282 CHECK(iter->ReadUInt32(&num_files)); | 315 CHECK(iter->ReadUInt32(&num_files)); |
| 283 scripts->clear(); | 316 scripts->clear(); |
| 284 for (uint32_t i = 0; i < num_files; ++i) { | 317 for (uint32_t i = 0; i < num_files; ++i) { |
| 285 File file; | 318 std::unique_ptr<File> file(new File()); |
| 286 file.Unpickle(pickle, iter); | 319 file->Unpickle(pickle, iter); |
| 287 scripts->push_back(file); | 320 scripts->push_back(std::move(file)); |
| 288 } | 321 } |
| 289 } | 322 } |
| 290 | 323 |
| 291 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id) | 324 UserScriptIDPair::UserScriptIDPair(int id, const HostID& host_id) |
| 292 : id(id), host_id(host_id) {} | 325 : id(id), host_id(host_id) {} |
| 293 | 326 |
| 294 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {} | 327 UserScriptIDPair::UserScriptIDPair(int id) : id(id), host_id(HostID()) {} |
| 295 | 328 |
| 296 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) { | 329 bool operator<(const UserScriptIDPair& a, const UserScriptIDPair& b) { |
| 297 return a.id < b.id; | 330 return a.id < b.id; |
| 298 } | 331 } |
| 299 | 332 |
| 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 | 333 } // namespace extensions |
| OLD | NEW |