| 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 131 void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
| 132 // Read the url from the pickle. | 132 // Read the url from the pickle. |
| 133 std::string url; | 133 std::string url; |
| 134 CHECK(iter->ReadString(&url)); | 134 CHECK(iter->ReadString(&url)); |
| 135 set_url(GURL(url)); | 135 set_url(GURL(url)); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void UserScript::Pickle(::Pickle* pickle) const { | 138 void UserScript::Pickle(::Pickle* pickle) const { |
| 139 // Write the simple types to the pickle. | 139 // Write the simple types to the pickle. |
| 140 pickle->WriteInt(run_location()); | 140 pickle->WriteInt(run_location()); |
| 141 pickle->WriteString(extension_id()); |
| 141 pickle->WriteInt(user_script_id_); | 142 pickle->WriteInt(user_script_id_); |
| 142 pickle->WriteBool(emulate_greasemonkey()); | 143 pickle->WriteBool(emulate_greasemonkey()); |
| 143 pickle->WriteBool(match_all_frames()); | 144 pickle->WriteBool(match_all_frames()); |
| 144 pickle->WriteBool(match_about_blank()); | 145 pickle->WriteBool(match_about_blank()); |
| 145 pickle->WriteBool(is_incognito_enabled()); | 146 pickle->WriteBool(is_incognito_enabled()); |
| 146 | 147 |
| 147 PickleHostID(pickle, host_id_); | |
| 148 PickleGlobs(pickle, globs_); | 148 PickleGlobs(pickle, globs_); |
| 149 PickleGlobs(pickle, exclude_globs_); | 149 PickleGlobs(pickle, exclude_globs_); |
| 150 PickleURLPatternSet(pickle, url_set_); | 150 PickleURLPatternSet(pickle, url_set_); |
| 151 PickleURLPatternSet(pickle, exclude_url_set_); | 151 PickleURLPatternSet(pickle, exclude_url_set_); |
| 152 PickleScripts(pickle, js_scripts_); | 152 PickleScripts(pickle, js_scripts_); |
| 153 PickleScripts(pickle, css_scripts_); | 153 PickleScripts(pickle, css_scripts_); |
| 154 } | 154 } |
| 155 | 155 |
| 156 void UserScript::PickleGlobs(::Pickle* pickle, | 156 void UserScript::PickleGlobs(::Pickle* pickle, |
| 157 const std::vector<std::string>& globs) const { | 157 const std::vector<std::string>& globs) const { |
| 158 pickle->WriteSizeT(globs.size()); | 158 pickle->WriteSizeT(globs.size()); |
| 159 for (std::vector<std::string>::const_iterator glob = globs.begin(); | 159 for (std::vector<std::string>::const_iterator glob = globs.begin(); |
| 160 glob != globs.end(); ++glob) { | 160 glob != globs.end(); ++glob) { |
| 161 pickle->WriteString(*glob); | 161 pickle->WriteString(*glob); |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 void UserScript::PickleHostID(::Pickle* pickle, const HostID& host_id) const { | |
| 166 pickle->WriteInt(host_id.type()); | |
| 167 pickle->WriteString(host_id.id()); | |
| 168 } | |
| 169 | |
| 170 void UserScript::PickleURLPatternSet(::Pickle* pickle, | 165 void UserScript::PickleURLPatternSet(::Pickle* pickle, |
| 171 const URLPatternSet& pattern_list) const { | 166 const URLPatternSet& pattern_list) const { |
| 172 pickle->WriteSizeT(pattern_list.patterns().size()); | 167 pickle->WriteSizeT(pattern_list.patterns().size()); |
| 173 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); | 168 for (URLPatternSet::const_iterator pattern = pattern_list.begin(); |
| 174 pattern != pattern_list.end(); ++pattern) { | 169 pattern != pattern_list.end(); ++pattern) { |
| 175 pickle->WriteInt(pattern->valid_schemes()); | 170 pickle->WriteInt(pattern->valid_schemes()); |
| 176 pickle->WriteString(pattern->GetAsString()); | 171 pickle->WriteString(pattern->GetAsString()); |
| 177 } | 172 } |
| 178 } | 173 } |
| 179 | 174 |
| 180 void UserScript::PickleScripts(::Pickle* pickle, | 175 void UserScript::PickleScripts(::Pickle* pickle, |
| 181 const FileList& scripts) const { | 176 const FileList& scripts) const { |
| 182 pickle->WriteSizeT(scripts.size()); | 177 pickle->WriteSizeT(scripts.size()); |
| 183 for (FileList::const_iterator file = scripts.begin(); | 178 for (FileList::const_iterator file = scripts.begin(); |
| 184 file != scripts.end(); ++file) { | 179 file != scripts.end(); ++file) { |
| 185 file->Pickle(pickle); | 180 file->Pickle(pickle); |
| 186 } | 181 } |
| 187 } | 182 } |
| 188 | 183 |
| 189 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { | 184 void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { |
| 190 // Read the run location. | 185 // Read the run location. |
| 191 int run_location = 0; | 186 int run_location = 0; |
| 192 CHECK(iter->ReadInt(&run_location)); | 187 CHECK(iter->ReadInt(&run_location)); |
| 193 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 188 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
| 194 run_location_ = static_cast<RunLocation>(run_location); | 189 run_location_ = static_cast<RunLocation>(run_location); |
| 195 | 190 |
| 191 CHECK(iter->ReadString(&extension_id_)); |
| 196 CHECK(iter->ReadInt(&user_script_id_)); | 192 CHECK(iter->ReadInt(&user_script_id_)); |
| 197 CHECK(iter->ReadBool(&emulate_greasemonkey_)); | 193 CHECK(iter->ReadBool(&emulate_greasemonkey_)); |
| 198 CHECK(iter->ReadBool(&match_all_frames_)); | 194 CHECK(iter->ReadBool(&match_all_frames_)); |
| 199 CHECK(iter->ReadBool(&match_about_blank_)); | 195 CHECK(iter->ReadBool(&match_about_blank_)); |
| 200 CHECK(iter->ReadBool(&incognito_enabled_)); | 196 CHECK(iter->ReadBool(&incognito_enabled_)); |
| 201 | 197 |
| 202 UnpickleHostID(pickle, iter, &host_id_); | |
| 203 UnpickleGlobs(pickle, iter, &globs_); | 198 UnpickleGlobs(pickle, iter, &globs_); |
| 204 UnpickleGlobs(pickle, iter, &exclude_globs_); | 199 UnpickleGlobs(pickle, iter, &exclude_globs_); |
| 205 UnpickleURLPatternSet(pickle, iter, &url_set_); | 200 UnpickleURLPatternSet(pickle, iter, &url_set_); |
| 206 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); | 201 UnpickleURLPatternSet(pickle, iter, &exclude_url_set_); |
| 207 UnpickleScripts(pickle, iter, &js_scripts_); | 202 UnpickleScripts(pickle, iter, &js_scripts_); |
| 208 UnpickleScripts(pickle, iter, &css_scripts_); | 203 UnpickleScripts(pickle, iter, &css_scripts_); |
| 209 } | 204 } |
| 210 | 205 |
| 211 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, | 206 void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, |
| 212 std::vector<std::string>* globs) { | 207 std::vector<std::string>* globs) { |
| 213 size_t num_globs = 0; | 208 size_t num_globs = 0; |
| 214 CHECK(iter->ReadSizeT(&num_globs)); | 209 CHECK(iter->ReadSizeT(&num_globs)); |
| 215 globs->clear(); | 210 globs->clear(); |
| 216 for (size_t i = 0; i < num_globs; ++i) { | 211 for (size_t i = 0; i < num_globs; ++i) { |
| 217 std::string glob; | 212 std::string glob; |
| 218 CHECK(iter->ReadString(&glob)); | 213 CHECK(iter->ReadString(&glob)); |
| 219 globs->push_back(glob); | 214 globs->push_back(glob); |
| 220 } | 215 } |
| 221 } | 216 } |
| 222 | 217 |
| 223 void UserScript::UnpickleHostID(const ::Pickle& pickle, | |
| 224 PickleIterator* iter, | |
| 225 HostID* host_id) { | |
| 226 int type = 0; | |
| 227 std::string id; | |
| 228 CHECK(iter->ReadInt(&type)); | |
| 229 CHECK(iter->ReadString(&id)); | |
| 230 *host_id = HostID(static_cast<HostID::HostType>(type), id); | |
| 231 } | |
| 232 | |
| 233 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, | 218 void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, |
| 234 PickleIterator* iter, | 219 PickleIterator* iter, |
| 235 URLPatternSet* pattern_list) { | 220 URLPatternSet* pattern_list) { |
| 236 size_t num_patterns = 0; | 221 size_t num_patterns = 0; |
| 237 CHECK(iter->ReadSizeT(&num_patterns)); | 222 CHECK(iter->ReadSizeT(&num_patterns)); |
| 238 | 223 |
| 239 pattern_list->ClearPatterns(); | 224 pattern_list->ClearPatterns(); |
| 240 for (size_t i = 0; i < num_patterns; ++i) { | 225 for (size_t i = 0; i < num_patterns; ++i) { |
| 241 int valid_schemes; | 226 int valid_schemes; |
| 242 CHECK(iter->ReadInt(&valid_schemes)); | 227 CHECK(iter->ReadInt(&valid_schemes)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 267 } | 252 } |
| 268 | 253 |
| 269 bool operator<(const UserScript& script1, const UserScript& script2) { | 254 bool operator<(const UserScript& script1, const UserScript& script2) { |
| 270 // The only kind of script that should be compared is the kind that has its | 255 // The only kind of script that should be compared is the kind that has its |
| 271 // IDs initialized to a meaningful value. | 256 // IDs initialized to a meaningful value. |
| 272 DCHECK(script1.id() != -1 && script2.id() != -1); | 257 DCHECK(script1.id() != -1 && script2.id() != -1); |
| 273 return script1.id() < script2.id(); | 258 return script1.id() < script2.id(); |
| 274 } | 259 } |
| 275 | 260 |
| 276 } // namespace extensions | 261 } // namespace extensions |
| OLD | NEW |