Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: chrome/common/extensions/manifest_handlers/content_scripts_handler.cc

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync @tott Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" 5 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
14 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
17 #include "base/values.h" 18 #include "base/values.h"
18 #include "chrome/grit/generated_resources.h" 19 #include "chrome/grit/generated_resources.h"
19 #include "content/public/common/url_constants.h" 20 #include "content/public/common/url_constants.h"
20 #include "extensions/common/error_utils.h" 21 #include "extensions/common/error_utils.h"
21 #include "extensions/common/extension.h" 22 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_resource.h" 23 #include "extensions/common/extension_resource.h"
23 #include "extensions/common/host_id.h" 24 #include "extensions/common/host_id.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 } 68 }
68 69
69 (instance->*add_method)(glob); 70 (instance->*add_method)(glob);
70 } 71 }
71 72
72 return true; 73 return true;
73 } 74 }
74 75
75 // Helper method that loads a UserScript object from a dictionary in the 76 // Helper method that loads a UserScript object from a dictionary in the
76 // content_script list of the manifest. 77 // content_script list of the manifest.
77 bool LoadUserScriptFromDictionary(const base::DictionaryValue* content_script, 78 std::unique_ptr<UserScript> LoadUserScriptFromDictionary(
78 int definition_index, 79 const base::DictionaryValue* content_script,
79 Extension* extension, 80 int definition_index,
80 base::string16* error, 81 Extension* extension,
81 UserScript* result) { 82 base::string16* error) {
83 std::unique_ptr<UserScript> result(new UserScript());
82 // run_at 84 // run_at
83 if (content_script->HasKey(keys::kRunAt)) { 85 if (content_script->HasKey(keys::kRunAt)) {
84 std::string run_location; 86 std::string run_location;
85 if (!content_script->GetString(keys::kRunAt, &run_location)) { 87 if (!content_script->GetString(keys::kRunAt, &run_location)) {
86 *error = ErrorUtils::FormatErrorMessageUTF16( 88 *error = ErrorUtils::FormatErrorMessageUTF16(
87 errors::kInvalidRunAt, 89 errors::kInvalidRunAt,
88 base::IntToString(definition_index)); 90 base::IntToString(definition_index));
89 return false; 91 return std::unique_ptr<UserScript>();
90 } 92 }
91 93
92 if (run_location == values::kRunAtDocumentStart) { 94 if (run_location == values::kRunAtDocumentStart) {
93 result->set_run_location(UserScript::DOCUMENT_START); 95 result->set_run_location(UserScript::DOCUMENT_START);
94 } else if (run_location == values::kRunAtDocumentEnd) { 96 } else if (run_location == values::kRunAtDocumentEnd) {
95 result->set_run_location(UserScript::DOCUMENT_END); 97 result->set_run_location(UserScript::DOCUMENT_END);
96 } else if (run_location == values::kRunAtDocumentIdle) { 98 } else if (run_location == values::kRunAtDocumentIdle) {
97 result->set_run_location(UserScript::DOCUMENT_IDLE); 99 result->set_run_location(UserScript::DOCUMENT_IDLE);
98 } else { 100 } else {
99 *error = ErrorUtils::FormatErrorMessageUTF16( 101 *error = ErrorUtils::FormatErrorMessageUTF16(
100 errors::kInvalidRunAt, 102 errors::kInvalidRunAt,
101 base::IntToString(definition_index)); 103 base::IntToString(definition_index));
102 return false; 104 return std::unique_ptr<UserScript>();
103 } 105 }
104 } 106 }
105 107
106 // all frames 108 // all frames
107 if (content_script->HasKey(keys::kAllFrames)) { 109 if (content_script->HasKey(keys::kAllFrames)) {
108 bool all_frames = false; 110 bool all_frames = false;
109 if (!content_script->GetBoolean(keys::kAllFrames, &all_frames)) { 111 if (!content_script->GetBoolean(keys::kAllFrames, &all_frames)) {
110 *error = ErrorUtils::FormatErrorMessageUTF16( 112 *error = ErrorUtils::FormatErrorMessageUTF16(
111 errors::kInvalidAllFrames, base::IntToString(definition_index)); 113 errors::kInvalidAllFrames, base::IntToString(definition_index));
112 return false; 114 return std::unique_ptr<UserScript>();
113 } 115 }
114 result->set_match_all_frames(all_frames); 116 result->set_match_all_frames(all_frames);
115 } 117 }
116 118
117 // match about blank 119 // match about blank
118 if (content_script->HasKey(keys::kMatchAboutBlank)) { 120 if (content_script->HasKey(keys::kMatchAboutBlank)) {
119 bool match_about_blank = false; 121 bool match_about_blank = false;
120 if (!content_script->GetBoolean(keys::kMatchAboutBlank, 122 if (!content_script->GetBoolean(keys::kMatchAboutBlank,
121 &match_about_blank)) { 123 &match_about_blank)) {
122 *error = ErrorUtils::FormatErrorMessageUTF16( 124 *error = ErrorUtils::FormatErrorMessageUTF16(
123 errors::kInvalidMatchAboutBlank, base::IntToString(definition_index)); 125 errors::kInvalidMatchAboutBlank, base::IntToString(definition_index));
124 return false; 126 return std::unique_ptr<UserScript>();
125 } 127 }
126 result->set_match_about_blank(match_about_blank); 128 result->set_match_about_blank(match_about_blank);
127 } 129 }
128 130
129 // matches (required) 131 // matches (required)
130 const base::ListValue* matches = NULL; 132 const base::ListValue* matches = NULL;
131 if (!content_script->GetList(keys::kMatches, &matches)) { 133 if (!content_script->GetList(keys::kMatches, &matches)) {
132 *error = ErrorUtils::FormatErrorMessageUTF16( 134 *error = ErrorUtils::FormatErrorMessageUTF16(
133 errors::kInvalidMatches, 135 errors::kInvalidMatches,
134 base::IntToString(definition_index)); 136 base::IntToString(definition_index));
135 return false; 137 return std::unique_ptr<UserScript>();
136 } 138 }
137 139
138 if (matches->GetSize() == 0) { 140 if (matches->GetSize() == 0) {
139 *error = ErrorUtils::FormatErrorMessageUTF16( 141 *error = ErrorUtils::FormatErrorMessageUTF16(
140 errors::kInvalidMatchCount, 142 errors::kInvalidMatchCount,
141 base::IntToString(definition_index)); 143 base::IntToString(definition_index));
142 return false; 144 return std::unique_ptr<UserScript>();
143 } 145 }
144 for (size_t j = 0; j < matches->GetSize(); ++j) { 146 for (size_t j = 0; j < matches->GetSize(); ++j) {
145 std::string match_str; 147 std::string match_str;
146 if (!matches->GetString(j, &match_str)) { 148 if (!matches->GetString(j, &match_str)) {
147 *error = ErrorUtils::FormatErrorMessageUTF16( 149 *error = ErrorUtils::FormatErrorMessageUTF16(
148 errors::kInvalidMatch, base::IntToString(definition_index), 150 errors::kInvalidMatch, base::IntToString(definition_index),
149 base::SizeTToString(j), errors::kExpectString); 151 base::SizeTToString(j), errors::kExpectString);
150 return false; 152 return std::unique_ptr<UserScript>();
151 } 153 }
152 154
153 URLPattern pattern(UserScript::ValidUserScriptSchemes( 155 URLPattern pattern(UserScript::ValidUserScriptSchemes(
154 PermissionsData::CanExecuteScriptEverywhere(extension))); 156 PermissionsData::CanExecuteScriptEverywhere(extension)));
155 157
156 URLPattern::ParseResult parse_result = pattern.Parse(match_str); 158 URLPattern::ParseResult parse_result = pattern.Parse(match_str);
157 if (parse_result != URLPattern::PARSE_SUCCESS) { 159 if (parse_result != URLPattern::PARSE_SUCCESS) {
158 *error = ErrorUtils::FormatErrorMessageUTF16( 160 *error = ErrorUtils::FormatErrorMessageUTF16(
159 errors::kInvalidMatch, base::IntToString(definition_index), 161 errors::kInvalidMatch, base::IntToString(definition_index),
160 base::SizeTToString(j), 162 base::SizeTToString(j),
161 URLPattern::GetParseResultString(parse_result)); 163 URLPattern::GetParseResultString(parse_result));
162 return false; 164 return std::unique_ptr<UserScript>();
163 } 165 }
164 166
165 // TODO(aboxhall): check for webstore 167 // TODO(aboxhall): check for webstore
166 if (!PermissionsData::CanExecuteScriptEverywhere(extension) && 168 if (!PermissionsData::CanExecuteScriptEverywhere(extension) &&
167 pattern.scheme() != content::kChromeUIScheme) { 169 pattern.scheme() != content::kChromeUIScheme) {
168 // Exclude SCHEME_CHROMEUI unless it's been explicitly requested. 170 // Exclude SCHEME_CHROMEUI unless it's been explicitly requested.
169 // If the --extensions-on-chrome-urls flag has not been passed, requesting 171 // If the --extensions-on-chrome-urls flag has not been passed, requesting
170 // a chrome:// url will cause a parse failure above, so there's no need to 172 // a chrome:// url will cause a parse failure above, so there's no need to
171 // check the flag here. 173 // check the flag here.
172 pattern.SetValidSchemes( 174 pattern.SetValidSchemes(
(...skipping 12 matching lines...) Expand all
185 result->add_url_pattern(pattern); 187 result->add_url_pattern(pattern);
186 } 188 }
187 189
188 // exclude_matches 190 // exclude_matches
189 if (content_script->HasKey(keys::kExcludeMatches)) { // optional 191 if (content_script->HasKey(keys::kExcludeMatches)) { // optional
190 const base::ListValue* exclude_matches = NULL; 192 const base::ListValue* exclude_matches = NULL;
191 if (!content_script->GetList(keys::kExcludeMatches, &exclude_matches)) { 193 if (!content_script->GetList(keys::kExcludeMatches, &exclude_matches)) {
192 *error = ErrorUtils::FormatErrorMessageUTF16( 194 *error = ErrorUtils::FormatErrorMessageUTF16(
193 errors::kInvalidExcludeMatches, 195 errors::kInvalidExcludeMatches,
194 base::IntToString(definition_index)); 196 base::IntToString(definition_index));
195 return false; 197 return std::unique_ptr<UserScript>();
196 } 198 }
197 199
198 for (size_t j = 0; j < exclude_matches->GetSize(); ++j) { 200 for (size_t j = 0; j < exclude_matches->GetSize(); ++j) {
199 std::string match_str; 201 std::string match_str;
200 if (!exclude_matches->GetString(j, &match_str)) { 202 if (!exclude_matches->GetString(j, &match_str)) {
201 *error = ErrorUtils::FormatErrorMessageUTF16( 203 *error = ErrorUtils::FormatErrorMessageUTF16(
202 errors::kInvalidExcludeMatch, base::IntToString(definition_index), 204 errors::kInvalidExcludeMatch, base::IntToString(definition_index),
203 base::SizeTToString(j), errors::kExpectString); 205 base::SizeTToString(j), errors::kExpectString);
204 return false; 206 return std::unique_ptr<UserScript>();
205 } 207 }
206 208
207 int valid_schemes = UserScript::ValidUserScriptSchemes( 209 int valid_schemes = UserScript::ValidUserScriptSchemes(
208 PermissionsData::CanExecuteScriptEverywhere(extension)); 210 PermissionsData::CanExecuteScriptEverywhere(extension));
209 URLPattern pattern(valid_schemes); 211 URLPattern pattern(valid_schemes);
210 212
211 URLPattern::ParseResult parse_result = pattern.Parse(match_str); 213 URLPattern::ParseResult parse_result = pattern.Parse(match_str);
212 if (parse_result != URLPattern::PARSE_SUCCESS) { 214 if (parse_result != URLPattern::PARSE_SUCCESS) {
213 *error = ErrorUtils::FormatErrorMessageUTF16( 215 *error = ErrorUtils::FormatErrorMessageUTF16(
214 errors::kInvalidExcludeMatch, base::IntToString(definition_index), 216 errors::kInvalidExcludeMatch, base::IntToString(definition_index),
215 base::SizeTToString(j), 217 base::SizeTToString(j),
216 URLPattern::GetParseResultString(parse_result)); 218 URLPattern::GetParseResultString(parse_result));
217 return false; 219 return std::unique_ptr<UserScript>();
218 } 220 }
219 221
220 result->add_exclude_url_pattern(pattern); 222 result->add_exclude_url_pattern(pattern);
221 } 223 }
222 } 224 }
223 225
224 // include/exclude globs (mostly for Greasemonkey compatibility) 226 // include/exclude globs (mostly for Greasemonkey compatibility)
225 if (!LoadGlobsHelper(content_script, definition_index, keys::kIncludeGlobs, 227 if (!LoadGlobsHelper(content_script, definition_index, keys::kIncludeGlobs,
226 error, &UserScript::add_glob, result)) { 228 error, &UserScript::add_glob, result.get())) {
227 return false; 229 return std::unique_ptr<UserScript>();
228 } 230 }
229 231
230 if (!LoadGlobsHelper(content_script, definition_index, keys::kExcludeGlobs, 232 if (!LoadGlobsHelper(content_script, definition_index, keys::kExcludeGlobs,
231 error, &UserScript::add_exclude_glob, result)) { 233 error, &UserScript::add_exclude_glob, result.get())) {
232 return false; 234 return std::unique_ptr<UserScript>();
233 } 235 }
234 236
235 // js and css keys 237 // js and css keys
236 const base::ListValue* js = NULL; 238 const base::ListValue* js = NULL;
237 if (content_script->HasKey(keys::kJs) && 239 if (content_script->HasKey(keys::kJs) &&
238 !content_script->GetList(keys::kJs, &js)) { 240 !content_script->GetList(keys::kJs, &js)) {
239 *error = ErrorUtils::FormatErrorMessageUTF16( 241 *error = ErrorUtils::FormatErrorMessageUTF16(
240 errors::kInvalidJsList, 242 errors::kInvalidJsList,
241 base::IntToString(definition_index)); 243 base::IntToString(definition_index));
242 return false; 244 return std::unique_ptr<UserScript>();
243 } 245 }
244 246
245 const base::ListValue* css = NULL; 247 const base::ListValue* css = NULL;
246 if (content_script->HasKey(keys::kCss) && 248 if (content_script->HasKey(keys::kCss) &&
247 !content_script->GetList(keys::kCss, &css)) { 249 !content_script->GetList(keys::kCss, &css)) {
248 *error = ErrorUtils:: 250 *error = ErrorUtils::
249 FormatErrorMessageUTF16(errors::kInvalidCssList, 251 FormatErrorMessageUTF16(errors::kInvalidCssList,
250 base::IntToString(definition_index)); 252 base::IntToString(definition_index));
251 return false; 253 return std::unique_ptr<UserScript>();
252 } 254 }
253 255
254 // The manifest needs to have at least one js or css user script definition. 256 // The manifest needs to have at least one js or css user script definition.
255 if (((js ? js->GetSize() : 0) + (css ? css->GetSize() : 0)) == 0) { 257 if (((js ? js->GetSize() : 0) + (css ? css->GetSize() : 0)) == 0) {
256 *error = ErrorUtils::FormatErrorMessageUTF16( 258 *error = ErrorUtils::FormatErrorMessageUTF16(
257 errors::kMissingFile, 259 errors::kMissingFile,
258 base::IntToString(definition_index)); 260 base::IntToString(definition_index));
259 return false; 261 return std::unique_ptr<UserScript>();
260 } 262 }
261 263
262 if (js) { 264 if (js) {
265 result->js_scripts().reserve(js->GetSize());
263 for (size_t script_index = 0; script_index < js->GetSize(); 266 for (size_t script_index = 0; script_index < js->GetSize();
264 ++script_index) { 267 ++script_index) {
265 const base::Value* value; 268 const base::Value* value;
266 std::string relative; 269 std::string relative;
267 if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) { 270 if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) {
268 *error = ErrorUtils::FormatErrorMessageUTF16( 271 *error = ErrorUtils::FormatErrorMessageUTF16(
269 errors::kInvalidJs, base::IntToString(definition_index), 272 errors::kInvalidJs, base::IntToString(definition_index),
270 base::SizeTToString(script_index)); 273 base::SizeTToString(script_index));
271 return false; 274 return std::unique_ptr<UserScript>();
272 } 275 }
273 GURL url = extension->GetResourceURL(relative); 276 GURL url = extension->GetResourceURL(relative);
274 ExtensionResource resource = extension->GetResource(relative); 277 ExtensionResource resource = extension->GetResource(relative);
275 result->js_scripts().push_back(UserScript::File( 278 result->js_scripts().push_back(base::MakeUnique<UserScript::File>(
276 resource.extension_root(), resource.relative_path(), url)); 279 resource.extension_root(), resource.relative_path(), url));
277 } 280 }
278 } 281 }
279 282
280 if (css) { 283 if (css) {
284 result->css_scripts().reserve(css->GetSize());
281 for (size_t script_index = 0; script_index < css->GetSize(); 285 for (size_t script_index = 0; script_index < css->GetSize();
282 ++script_index) { 286 ++script_index) {
283 const base::Value* value; 287 const base::Value* value;
284 std::string relative; 288 std::string relative;
285 if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) { 289 if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) {
286 *error = ErrorUtils::FormatErrorMessageUTF16( 290 *error = ErrorUtils::FormatErrorMessageUTF16(
287 errors::kInvalidCss, base::IntToString(definition_index), 291 errors::kInvalidCss, base::IntToString(definition_index),
288 base::SizeTToString(script_index)); 292 base::SizeTToString(script_index));
289 return false; 293 return std::unique_ptr<UserScript>();
290 } 294 }
291 GURL url = extension->GetResourceURL(relative); 295 GURL url = extension->GetResourceURL(relative);
292 ExtensionResource resource = extension->GetResource(relative); 296 ExtensionResource resource = extension->GetResource(relative);
293 result->css_scripts().push_back(UserScript::File( 297 result->css_scripts().push_back(base::MakeUnique<UserScript::File>(
294 resource.extension_root(), resource.relative_path(), url)); 298 resource.extension_root(), resource.relative_path(), url));
295 } 299 }
296 } 300 }
297 301 return result;
298 return true;
299 } 302 }
300 303
301 // Returns false and sets the error if script file can't be loaded, 304 // Returns false and sets the error if script file can't be loaded,
302 // or if it's not UTF-8 encoded. 305 // or if it's not UTF-8 encoded.
303 static bool IsScriptValid(const base::FilePath& path, 306 static bool IsScriptValid(const base::FilePath& path,
304 const base::FilePath& relative_path, 307 const base::FilePath& relative_path,
305 int message_id, 308 int message_id,
306 std::string* error) { 309 std::string* error) {
307 std::string content; 310 std::string content;
308 if (!base::PathExists(path) || 311 if (!base::PathExists(path) ||
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 const Extension* extension) { 346 const Extension* extension) {
344 ContentScriptsInfo* info = static_cast<ContentScriptsInfo*>( 347 ContentScriptsInfo* info = static_cast<ContentScriptsInfo*>(
345 extension->GetManifestData(keys::kContentScripts)); 348 extension->GetManifestData(keys::kContentScripts));
346 return info ? info->content_scripts 349 return info ? info->content_scripts
347 : g_empty_script_list.Get().user_script_list; 350 : g_empty_script_list.Get().user_script_list;
348 } 351 }
349 352
350 // static 353 // static
351 bool ContentScriptsInfo::ExtensionHasScriptAtURL(const Extension* extension, 354 bool ContentScriptsInfo::ExtensionHasScriptAtURL(const Extension* extension,
352 const GURL& url) { 355 const GURL& url) {
353 const UserScriptList& content_scripts = GetContentScripts(extension); 356 for (const std::unique_ptr<UserScript>& script :
354 for (UserScriptList::const_iterator iter = content_scripts.begin(); 357 GetContentScripts(extension)) {
355 iter != content_scripts.end(); ++iter) { 358 if (script->MatchesURL(url))
356 if (iter->MatchesURL(url))
357 return true; 359 return true;
358 } 360 }
359 return false; 361 return false;
360 } 362 }
361 363
362 // static 364 // static
363 URLPatternSet ContentScriptsInfo::GetScriptableHosts( 365 URLPatternSet ContentScriptsInfo::GetScriptableHosts(
364 const Extension* extension) { 366 const Extension* extension) {
365 const UserScriptList& content_scripts = GetContentScripts(extension);
366 URLPatternSet scriptable_hosts; 367 URLPatternSet scriptable_hosts;
367 for (UserScriptList::const_iterator content_script = 368 for (const std::unique_ptr<UserScript>& script :
368 content_scripts.begin(); 369 GetContentScripts(extension)) {
369 content_script != content_scripts.end(); 370 for (const URLPattern& pattern : script->url_patterns())
370 ++content_script) { 371 scriptable_hosts.AddPattern(pattern);
371 URLPatternSet::const_iterator pattern =
372 content_script->url_patterns().begin();
373 for (; pattern != content_script->url_patterns().end(); ++pattern)
374 scriptable_hosts.AddPattern(*pattern);
375 } 372 }
376 return scriptable_hosts; 373 return scriptable_hosts;
377 } 374 }
378 375
379 ContentScriptsHandler::ContentScriptsHandler() { 376 ContentScriptsHandler::ContentScriptsHandler() {
380 } 377 }
381 378
382 ContentScriptsHandler::~ContentScriptsHandler() { 379 ContentScriptsHandler::~ContentScriptsHandler() {
383 } 380 }
384 381
(...skipping 14 matching lines...) Expand all
399 } 396 }
400 397
401 for (size_t i = 0; i < scripts_list->GetSize(); ++i) { 398 for (size_t i = 0; i < scripts_list->GetSize(); ++i) {
402 const base::DictionaryValue* script_dict = NULL; 399 const base::DictionaryValue* script_dict = NULL;
403 if (!scripts_list->GetDictionary(i, &script_dict)) { 400 if (!scripts_list->GetDictionary(i, &script_dict)) {
404 *error = ErrorUtils::FormatErrorMessageUTF16( 401 *error = ErrorUtils::FormatErrorMessageUTF16(
405 errors::kInvalidContentScript, base::SizeTToString(i)); 402 errors::kInvalidContentScript, base::SizeTToString(i));
406 return false; 403 return false;
407 } 404 }
408 405
409 UserScript user_script; 406 std::unique_ptr<UserScript> user_script =
410 if (!LoadUserScriptFromDictionary(script_dict, 407 LoadUserScriptFromDictionary(script_dict, i, extension, error);
411 i, 408 if (!user_script)
412 extension,
413 error,
414 &user_script)) {
415 return false; // Failed to parse script context definition. 409 return false; // Failed to parse script context definition.
410
411 user_script->set_host_id(HostID(HostID::EXTENSIONS, extension->id()));
412 if (extension->converted_from_user_script()) {
413 user_script->set_emulate_greasemonkey(true);
414 // Greasemonkey matches all frames.
415 user_script->set_match_all_frames(true);
416 } 416 }
417 417 user_script->set_id(UserScript::GenerateUserScriptID());
418 user_script.set_host_id(HostID(HostID::EXTENSIONS, extension->id())); 418 content_scripts_info->content_scripts.push_back(std::move(user_script));
419 if (extension->converted_from_user_script()) {
420 user_script.set_emulate_greasemonkey(true);
421 // Greasemonkey matches all frames.
422 user_script.set_match_all_frames(true);
423 }
424 user_script.set_id(UserScript::GenerateUserScriptID());
425 content_scripts_info->content_scripts.push_back(user_script);
426 } 419 }
427 extension->SetManifestData(keys::kContentScripts, 420 extension->SetManifestData(keys::kContentScripts,
428 content_scripts_info.release()); 421 content_scripts_info.release());
429 PermissionsParser::SetScriptableHosts( 422 PermissionsParser::SetScriptableHosts(
430 extension, ContentScriptsInfo::GetScriptableHosts(extension)); 423 extension, ContentScriptsInfo::GetScriptableHosts(extension));
431 return true; 424 return true;
432 } 425 }
433 426
434 bool ContentScriptsHandler::Validate( 427 bool ContentScriptsHandler::Validate(
435 const Extension* extension, 428 const Extension* extension,
436 std::string* error, 429 std::string* error,
437 std::vector<InstallWarning>* warnings) const { 430 std::vector<InstallWarning>* warnings) const {
438 // Validate that claimed script resources actually exist, 431 // Validate that claimed script resources actually exist,
439 // and are UTF-8 encoded. 432 // and are UTF-8 encoded.
440 ExtensionResource::SymlinkPolicy symlink_policy; 433 ExtensionResource::SymlinkPolicy symlink_policy;
441 if ((extension->creation_flags() & 434 if ((extension->creation_flags() &
442 Extension::FOLLOW_SYMLINKS_ANYWHERE) != 0) { 435 Extension::FOLLOW_SYMLINKS_ANYWHERE) != 0) {
443 symlink_policy = ExtensionResource::FOLLOW_SYMLINKS_ANYWHERE; 436 symlink_policy = ExtensionResource::FOLLOW_SYMLINKS_ANYWHERE;
444 } else { 437 } else {
445 symlink_policy = ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT; 438 symlink_policy = ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT;
446 } 439 }
447 440
448 const UserScriptList& content_scripts = 441 const UserScriptList& content_scripts =
449 ContentScriptsInfo::GetContentScripts(extension); 442 ContentScriptsInfo::GetContentScripts(extension);
450 for (size_t i = 0; i < content_scripts.size(); ++i) { 443 for (const std::unique_ptr<UserScript>& script : content_scripts) {
451 const UserScript& script = content_scripts[i]; 444 for (const std::unique_ptr<UserScript::File>& js_script :
452 445 script->js_scripts()) {
453 for (size_t j = 0; j < script.js_scripts().size(); j++) {
454 const UserScript::File& js_script = script.js_scripts()[j];
455 const base::FilePath& path = ExtensionResource::GetFilePath( 446 const base::FilePath& path = ExtensionResource::GetFilePath(
456 js_script.extension_root(), js_script.relative_path(), 447 js_script->extension_root(), js_script->relative_path(),
457 symlink_policy); 448 symlink_policy);
458 if (!IsScriptValid(path, js_script.relative_path(), 449 if (!IsScriptValid(path, js_script->relative_path(),
459 IDS_EXTENSION_LOAD_JAVASCRIPT_FAILED, error)) 450 IDS_EXTENSION_LOAD_JAVASCRIPT_FAILED, error))
460 return false; 451 return false;
461 } 452 }
462 453
463 for (size_t j = 0; j < script.css_scripts().size(); j++) { 454 for (const std::unique_ptr<UserScript::File>& css_script :
464 const UserScript::File& css_script = script.css_scripts()[j]; 455 script->css_scripts()) {
465 const base::FilePath& path = ExtensionResource::GetFilePath( 456 const base::FilePath& path = ExtensionResource::GetFilePath(
466 css_script.extension_root(), css_script.relative_path(), 457 css_script->extension_root(), css_script->relative_path(),
467 symlink_policy); 458 symlink_policy);
468 if (!IsScriptValid(path, css_script.relative_path(), 459 if (!IsScriptValid(path, css_script->relative_path(),
469 IDS_EXTENSION_LOAD_CSS_FAILED, error)) 460 IDS_EXTENSION_LOAD_CSS_FAILED, error))
470 return false; 461 return false;
471 } 462 }
472 } 463 }
473 464
474 return true; 465 return true;
475 } 466 }
476 467
477 } // namespace extensions 468 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698