| 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 "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 UserScript::File::File() {} | 78 UserScript::File::File() {} |
| 79 | 79 |
| 80 UserScript::File::~File() {} | 80 UserScript::File::~File() {} |
| 81 | 81 |
| 82 UserScript::UserScript() | 82 UserScript::UserScript() |
| 83 : run_location_(DOCUMENT_IDLE), | 83 : run_location_(DOCUMENT_IDLE), |
| 84 user_script_id_(-1), | 84 user_script_id_(-1), |
| 85 emulate_greasemonkey_(false), | 85 emulate_greasemonkey_(false), |
| 86 match_all_frames_(false), | 86 match_all_frames_(false), |
| 87 match_about_blank_(false), | 87 match_about_blank_(false), |
| 88 incognito_enabled_(false) {} | 88 incognito_enabled_(false) { |
| 89 } |
| 89 | 90 |
| 90 UserScript::~UserScript() { | 91 UserScript::~UserScript() { |
| 91 } | 92 } |
| 92 | 93 |
| 94 const std::string& UserScript::GetExtensionID() const { |
| 95 DCHECK(consumer_id_.get()); |
| 96 return consumer_id_->host_id(); |
| 97 } |
| 98 |
| 93 void UserScript::add_url_pattern(const URLPattern& pattern) { | 99 void UserScript::add_url_pattern(const URLPattern& pattern) { |
| 94 url_set_.AddPattern(pattern); | 100 url_set_.AddPattern(pattern); |
| 95 } | 101 } |
| 96 | 102 |
| 97 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { | 103 void UserScript::add_exclude_url_pattern(const URLPattern& pattern) { |
| 98 exclude_url_set_.AddPattern(pattern); | 104 exclude_url_set_.AddPattern(pattern); |
| 99 } | 105 } |
| 100 | 106 |
| 101 bool UserScript::MatchesURL(const GURL& url) const { | 107 bool UserScript::MatchesURL(const GURL& url) const { |
| 102 if (!url_set_.is_empty()) { | 108 if (!url_set_.is_empty()) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 131 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 137 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
| 132 // Read the url from the pickle. | 138 // Read the url from the pickle. |
| 133 std::string url; | 139 std::string url; |
| 134 CHECK(iter->ReadString(&url)); | 140 CHECK(iter->ReadString(&url)); |
| 135 set_url(GURL(url)); | 141 set_url(GURL(url)); |
| 136 } | 142 } |
| 137 | 143 |
| 138 void UserScript::Pickle(::Pickle* pickle) const { | 144 void UserScript::Pickle(::Pickle* pickle) const { |
| 139 // Write the simple types to the pickle. | 145 // Write the simple types to the pickle. |
| 140 pickle->WriteInt(run_location()); | 146 pickle->WriteInt(run_location()); |
| 141 pickle->WriteString(extension_id()); | |
| 142 pickle->WriteInt(user_script_id_); | 147 pickle->WriteInt(user_script_id_); |
| 143 pickle->WriteBool(emulate_greasemonkey()); | 148 pickle->WriteBool(emulate_greasemonkey()); |
| 144 pickle->WriteBool(match_all_frames()); | 149 pickle->WriteBool(match_all_frames()); |
| 145 pickle->WriteBool(match_about_blank()); | 150 pickle->WriteBool(match_about_blank()); |
| 146 pickle->WriteBool(is_incognito_enabled()); | 151 pickle->WriteBool(is_incognito_enabled()); |
| 147 | 152 |
| 153 PickleConsumerID(pickle, consumer_id_); |
| 148 PickleGlobs(pickle, globs_); | 154 PickleGlobs(pickle, globs_); |
| 149 PickleGlobs(pickle, exclude_globs_); | 155 PickleGlobs(pickle, exclude_globs_); |
| 150 PickleURLPatternSet(pickle, url_set_); | 156 PickleURLPatternSet(pickle, url_set_); |
| 151 PickleURLPatternSet(pickle, exclude_url_set_); | 157 PickleURLPatternSet(pickle, exclude_url_set_); |
| 152 PickleScripts(pickle, js_scripts_); | 158 PickleScripts(pickle, js_scripts_); |
| 153 PickleScripts(pickle, css_scripts_); | 159 PickleScripts(pickle, css_scripts_); |
| 154 } | 160 } |
| 155 | 161 |
| 156 void UserScript::PickleGlobs(::Pickle* pickle, | 162 void UserScript::PickleGlobs(::Pickle* pickle, |
| 157 const std::vector<std::string>& globs) const { | 163 const std::vector<std::string>& globs) const { |
| 158 pickle->WriteSizeT(globs.size()); | 164 pickle->WriteSizeT(globs.size()); |
| 159 for (std::vector<std::string>::const_iterator glob = globs.begin(); | 165 for (std::vector<std::string>::const_iterator glob = globs.begin(); |
| 160 glob != globs.end(); ++glob) { | 166 glob != globs.end(); ++glob) { |
| 161 pickle->WriteString(*glob); | 167 pickle->WriteString(*glob); |
| 162 } | 168 } |
| 163 } | 169 } |
| 164 | 170 |
| 171 void UserScript::PickleConsumerID(::Pickle* pickle, |
| 172 linked_ptr<ConsumerID> consumer_id) const { |
| 173 pickle->WriteInt(consumer_id->host_type()); |
| 174 pickle->WriteString(consumer_id->host_id()); |
| 175 pickle->WriteInt(consumer_id->instance_type()); |
| 176 pickle->WriteInt(consumer_id->instance_id()); |
| 177 } |
| 178 |
| 165 void UserScript::PickleURLPatternSet(::Pickle* pickle, | 179 void UserScript::PickleURLPatternSet(::Pickle* pickle, |
| 166 const URLPatternSet& pattern_list) const { | 180 const URLPatternSet& pattern_list) const { |
| 167 pickle->WriteSizeT(pattern_list.patterns().size()); | 181 pickle->WriteSizeT(pattern_list.patterns().size()); |
| 168 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); | 182 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); |
| 169 pattern != pattern_list.end(); ++pattern) { | 183 pattern != pattern_list.end(); ++pattern) { |
| 170 pickle->WriteInt(pattern->valid_schemes()); | 184 pickle->WriteInt(pattern->valid_schemes()); |
| 171 pickle->WriteString(pattern->GetAsString()); | 185 pickle->WriteString(pattern->GetAsString()); |
| 172 } | 186 } |
| 173 } | 187 } |
| 174 | 188 |
| 175 void UserScript::PickleScripts(::Pickle* pickle, | 189 void UserScript::PickleScripts(::Pickle* pickle, |
| 176 const FileList& scripts) const { | 190 const FileList& scripts) const { |
| 177 pickle->WriteSizeT(scripts.size()); | 191 pickle->WriteSizeT(scripts.size()); |
| 178 for (FileList::const_iterator file = scripts.begin(); | 192 for (FileList::const_iterator file = scripts.begin(); |
| 179 file != scripts.end(); ++file) { | 193 file != scripts.end(); ++file) { |
| 180 file->Pickle(pickle); | 194 file->Pickle(pickle); |
| 181 } | 195 } |
| 182 } | 196 } |
| 183 | 197 |
| 184 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 198 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
| 185 // Read the run location. | 199 // Read the run location. |
| 186 int run_location = 0; | 200 int run_location = 0; |
| 187 CHECK(iter->ReadInt(&run_location)); | 201 CHECK(iter->ReadInt(&run_location)); |
| 188 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 202 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
| 189 run_location_ = static_cast<RunLocation>(run_location); | 203 run_location_ = static_cast<RunLocation>(run_location); |
| 190 | 204 |
| 191 CHECK(iter->ReadString(&extension_id_)); | |
| 192 CHECK(iter->ReadInt(&user_script_id_)); | 205 CHECK(iter->ReadInt(&user_script_id_)); |
| 193 CHECK(iter->ReadBool(&emulate_greasemonkey_)); | 206 CHECK(iter->ReadBool(&emulate_greasemonkey_)); |
| 194 CHECK(iter->ReadBool(&match_all_frames_)); | 207 CHECK(iter->ReadBool(&match_all_frames_)); |
| 195 CHECK(iter->ReadBool(&match_about_blank_)); | 208 CHECK(iter->ReadBool(&match_about_blank_)); |
| 196 CHECK(iter->ReadBool(&incognito_enabled_)); | 209 CHECK(iter->ReadBool(&incognito_enabled_)); |
| 197 | 210 |
| 211 consumer_id_.reset(UnpickleConsumerID(pickle, iter).release()); |
| 198 UnpickleGlobs(pickle, iter, &globs_); | 212 UnpickleGlobs(pickle, iter, &globs_); |
| 199 UnpickleGlobs(pickle, iter, &exclude_globs_); | 213 UnpickleGlobs(pickle, iter, &exclude_globs_); |
| 200 UnpickleURLPatternSet(pickle, iter, &url_set_); | 214 UnpickleURLPatternSet(pickle, iter, &url_set_); |
| 201 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); | 215 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); |
| 202 UnpickleScripts(pickle, iter, &js_scripts_); | 216 UnpickleScripts(pickle, iter, &js_scripts_); |
| 203 UnpickleScripts(pickle, iter, &css_scripts_); | 217 UnpickleScripts(pickle, iter, &css_scripts_); |
| 204 } | 218 } |
| 205 | 219 |
| 206 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, | 220 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, |
| 207 std::vector<std::string>* globs) { | 221 std::vector<std::string>* globs) { |
| 208 size_t num_globs = 0; | 222 size_t num_globs = 0; |
| 209 CHECK(iter->ReadSizeT(&num_globs)); | 223 CHECK(iter->ReadSizeT(&num_globs)); |
| 210 globs->clear(); | 224 globs->clear(); |
| 211 for (size_t i = 0; i < num_globs; ++i) { | 225 for (size_t i = 0; i < num_globs; ++i) { |
| 212 std::string glob; | 226 std::string glob; |
| 213 CHECK(iter->ReadString(&glob)); | 227 CHECK(iter->ReadString(&glob)); |
| 214 globs->push_back(glob); | 228 globs->push_back(glob); |
| 215 } | 229 } |
| 216 } | 230 } |
| 217 | 231 |
| 232 linked_ptr<ConsumerID> UserScript::UnpickleConsumerID(const ::Pickle& pickle, |
| 233 PickleIterator* iter) { |
| 234 int host_type; |
| 235 std::string host_id; |
| 236 int instance_type; |
| 237 int instance_id; |
| 238 CHECK(iter->ReadInt(&host_type)); |
| 239 CHECK(iter->ReadString(&host_id)); |
| 240 CHECK(iter->ReadInt(&instance_type)); |
| 241 CHECK(iter->ReadInt(&instance_id)); |
| 242 return make_linked_ptr( |
| 243 new ConsumerID(static_cast<ConsumerID::HostType>(host_type), host_id, |
| 244 static_cast<ConsumerID::InstanceType>(instance_type), |
| 245 instance_id)); |
| 246 } |
| 247 |
| 218 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, | 248 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, |
| 219 PickleIterator* iter, | 249 PickleIterator* iter, |
| 220 URLPatternSet* pattern_list) { | 250 URLPatternSet* pattern_list) { |
| 221 size_t num_patterns = 0; | 251 size_t num_patterns = 0; |
| 222 CHECK(iter->ReadSizeT(&num_patterns)); | 252 CHECK(iter->ReadSizeT(&num_patterns)); |
| 223 | 253 |
| 224 pattern_list->ClearPatterns(); | 254 pattern_list->ClearPatterns(); |
| 225 for (size_t i = 0; i < num_patterns; ++i) { | 255 for (size_t i = 0; i < num_patterns; ++i) { |
| 226 int valid_schemes; | 256 int valid_schemes; |
| 227 CHECK(iter->ReadInt(&valid_schemes)); | 257 CHECK(iter->ReadInt(&valid_schemes)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 252 } | 282 } |
| 253 | 283 |
| 254 bool operator<(const UserScript& script1, const UserScript& script2) { | 284 bool operator<(const UserScript& script1, const UserScript& script2) { |
| 255 // The only kind of script that should be compared is the kind that has its | 285 // The only kind of script that should be compared is the kind that has its |
| 256 // IDs initialized to a meaningful value. | 286 // IDs initialized to a meaningful value. |
| 257 DCHECK(script1.id() != -1 && script2.id() != -1); | 287 DCHECK(script1.id() != -1 && script2.id() != -1); |
| 258 return script1.id() < script2.id(); | 288 return script1.id() < script2.id(); |
| 259 } | 289 } |
| 260 | 290 |
| 261 } // namespace extensions | 291 } // namespace extensions |
| OLD | NEW |