OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/extensions/user_script.h" | 5 #include "chrome/common/extensions/user_script.h" |
6 | 6 |
7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 relative_path_(relative_path), | 54 relative_path_(relative_path), |
55 url_(url) { | 55 url_(url) { |
56 } | 56 } |
57 | 57 |
58 UserScript::File::File() {} | 58 UserScript::File::File() {} |
59 | 59 |
60 UserScript::File::~File() {} | 60 UserScript::File::~File() {} |
61 | 61 |
62 UserScript::UserScript() | 62 UserScript::UserScript() |
63 : run_location_(DOCUMENT_IDLE), emulate_greasemonkey_(false), | 63 : run_location_(DOCUMENT_IDLE), emulate_greasemonkey_(false), |
64 match_all_frames_(false), incognito_enabled_(false), | 64 match_all_frames_(false), incognito_enabled_(false) { |
65 allow_file_access_(false) { | |
66 } | 65 } |
67 | 66 |
68 UserScript::~UserScript() { | 67 UserScript::~UserScript() { |
69 } | 68 } |
70 | 69 |
71 void UserScript::add_url_pattern(const URLPattern& pattern) { | 70 void UserScript::add_url_pattern(const URLPattern& pattern) { |
72 url_patterns_.push_back(pattern); | 71 url_patterns_.push_back(pattern); |
73 } | 72 } |
74 | 73 |
75 void UserScript::clear_url_patterns() { url_patterns_.clear(); } | |
76 | |
77 bool UserScript::MatchesUrl(const GURL& url) const { | 74 bool UserScript::MatchesUrl(const GURL& url) const { |
78 if (!url_patterns_.empty()) { | 75 if (!url_patterns_.empty()) { |
79 if (!UrlMatchesPatterns(&url_patterns_, url)) | 76 if (!UrlMatchesPatterns(&url_patterns_, url)) |
80 return false; | 77 return false; |
81 } | 78 } |
82 | 79 |
83 if (!globs_.empty()) { | 80 if (!globs_.empty()) { |
84 if (!UrlMatchesGlobs(&globs_, url)) | 81 if (!UrlMatchesGlobs(&globs_, url)) |
85 return false; | 82 return false; |
86 } | 83 } |
(...skipping 19 matching lines...) Expand all Loading... |
106 set_url(GURL(url)); | 103 set_url(GURL(url)); |
107 } | 104 } |
108 | 105 |
109 void UserScript::Pickle(::Pickle* pickle) const { | 106 void UserScript::Pickle(::Pickle* pickle) const { |
110 // Write simple types. | 107 // Write simple types. |
111 pickle->WriteInt(run_location()); | 108 pickle->WriteInt(run_location()); |
112 pickle->WriteString(extension_id()); | 109 pickle->WriteString(extension_id()); |
113 pickle->WriteBool(emulate_greasemonkey()); | 110 pickle->WriteBool(emulate_greasemonkey()); |
114 pickle->WriteBool(match_all_frames()); | 111 pickle->WriteBool(match_all_frames()); |
115 pickle->WriteBool(is_incognito_enabled()); | 112 pickle->WriteBool(is_incognito_enabled()); |
116 pickle->WriteBool(allow_file_access()); | |
117 | 113 |
118 // Write globs. | 114 // Write globs. |
119 std::vector<std::string>::const_iterator glob; | 115 std::vector<std::string>::const_iterator glob; |
120 pickle->WriteSize(globs_.size()); | 116 pickle->WriteSize(globs_.size()); |
121 for (glob = globs_.begin(); glob != globs_.end(); ++glob) { | 117 for (glob = globs_.begin(); glob != globs_.end(); ++glob) { |
122 pickle->WriteString(*glob); | 118 pickle->WriteString(*glob); |
123 } | 119 } |
124 pickle->WriteSize(exclude_globs_.size()); | 120 pickle->WriteSize(exclude_globs_.size()); |
125 for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) { | 121 for (glob = exclude_globs_.begin(); glob != exclude_globs_.end(); ++glob) { |
126 pickle->WriteString(*glob); | 122 pickle->WriteString(*glob); |
(...skipping 26 matching lines...) Expand all Loading... |
153 // Read the run location. | 149 // Read the run location. |
154 int run_location = 0; | 150 int run_location = 0; |
155 CHECK(pickle.ReadInt(iter, &run_location)); | 151 CHECK(pickle.ReadInt(iter, &run_location)); |
156 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); | 152 CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); |
157 run_location_ = static_cast<RunLocation>(run_location); | 153 run_location_ = static_cast<RunLocation>(run_location); |
158 | 154 |
159 CHECK(pickle.ReadString(iter, &extension_id_)); | 155 CHECK(pickle.ReadString(iter, &extension_id_)); |
160 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); | 156 CHECK(pickle.ReadBool(iter, &emulate_greasemonkey_)); |
161 CHECK(pickle.ReadBool(iter, &match_all_frames_)); | 157 CHECK(pickle.ReadBool(iter, &match_all_frames_)); |
162 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); | 158 CHECK(pickle.ReadBool(iter, &incognito_enabled_)); |
163 CHECK(pickle.ReadBool(iter, &allow_file_access_)); | |
164 | 159 |
165 // Read globs. | 160 // Read globs. |
166 size_t num_globs = 0; | 161 size_t num_globs = 0; |
167 CHECK(pickle.ReadSize(iter, &num_globs)); | 162 CHECK(pickle.ReadSize(iter, &num_globs)); |
168 globs_.clear(); | 163 globs_.clear(); |
169 for (size_t i = 0; i < num_globs; ++i) { | 164 for (size_t i = 0; i < num_globs; ++i) { |
170 std::string glob; | 165 std::string glob; |
171 CHECK(pickle.ReadString(iter, &glob)); | 166 CHECK(pickle.ReadString(iter, &glob)); |
172 globs_.push_back(glob); | 167 globs_.push_back(glob); |
173 } | 168 } |
(...skipping 10 matching lines...) Expand all Loading... |
184 size_t num_patterns = 0; | 179 size_t num_patterns = 0; |
185 CHECK(pickle.ReadSize(iter, &num_patterns)); | 180 CHECK(pickle.ReadSize(iter, &num_patterns)); |
186 | 181 |
187 url_patterns_.clear(); | 182 url_patterns_.clear(); |
188 for (size_t i = 0; i < num_patterns; ++i) { | 183 for (size_t i = 0; i < num_patterns; ++i) { |
189 int valid_schemes; | 184 int valid_schemes; |
190 CHECK(pickle.ReadInt(iter, &valid_schemes)); | 185 CHECK(pickle.ReadInt(iter, &valid_schemes)); |
191 std::string pattern_str; | 186 std::string pattern_str; |
192 URLPattern pattern(valid_schemes); | 187 URLPattern pattern(valid_schemes); |
193 CHECK(pickle.ReadString(iter, &pattern_str)); | 188 CHECK(pickle.ReadString(iter, &pattern_str)); |
| 189 |
| 190 // We remove the file scheme if it's not actually allowed (see Extension:: |
| 191 // LoadUserScriptHelper), but we need it temporarily while loading the |
| 192 // pattern so that it's valid. |
| 193 bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0; |
| 194 if (!had_file_scheme) |
| 195 pattern.set_valid_schemes(valid_schemes | URLPattern::SCHEME_FILE); |
194 CHECK(URLPattern::PARSE_SUCCESS == | 196 CHECK(URLPattern::PARSE_SUCCESS == |
195 pattern.Parse(pattern_str, URLPattern::PARSE_LENIENT)); | 197 pattern.Parse(pattern_str, URLPattern::PARSE_LENIENT)); |
| 198 if (!had_file_scheme) |
| 199 pattern.set_valid_schemes(valid_schemes); |
| 200 |
196 url_patterns_.push_back(pattern); | 201 url_patterns_.push_back(pattern); |
197 } | 202 } |
198 | 203 |
199 // Read js scripts. | 204 // Read js scripts. |
200 size_t num_js_files = 0; | 205 size_t num_js_files = 0; |
201 CHECK(pickle.ReadSize(iter, &num_js_files)); | 206 CHECK(pickle.ReadSize(iter, &num_js_files)); |
202 js_scripts_.clear(); | 207 js_scripts_.clear(); |
203 for (size_t i = 0; i < num_js_files; ++i) { | 208 for (size_t i = 0; i < num_js_files; ++i) { |
204 File file; | 209 File file; |
205 file.Unpickle(pickle, iter); | 210 file.Unpickle(pickle, iter); |
206 js_scripts_.push_back(file); | 211 js_scripts_.push_back(file); |
207 } | 212 } |
208 | 213 |
209 // Read css scripts. | 214 // Read css scripts. |
210 size_t num_css_files = 0; | 215 size_t num_css_files = 0; |
211 CHECK(pickle.ReadSize(iter, &num_css_files)); | 216 CHECK(pickle.ReadSize(iter, &num_css_files)); |
212 css_scripts_.clear(); | 217 css_scripts_.clear(); |
213 for (size_t i = 0; i < num_css_files; ++i) { | 218 for (size_t i = 0; i < num_css_files; ++i) { |
214 File file; | 219 File file; |
215 file.Unpickle(pickle, iter); | 220 file.Unpickle(pickle, iter); |
216 css_scripts_.push_back(file); | 221 css_scripts_.push_back(file); |
217 } | 222 } |
218 } | 223 } |
OLD | NEW |