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