| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/extensions/api/content_scripts/content_scripts_handler.h
" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/lazy_instance.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 16 #include "chrome/common/extensions/extension_resource.h" |
| 17 #include "content/public/common/url_constants.h" |
| 18 #include "extensions/common/error_utils.h" |
| 19 #include "extensions/common/url_pattern.h" |
| 20 #include "googleurl/src/gurl.h" |
| 21 #include "grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 namespace extensions { |
| 25 |
| 26 namespace keys = extension_manifest_keys; |
| 27 namespace values = extension_manifest_values; |
| 28 namespace errors = extension_manifest_errors; |
| 29 |
| 30 namespace { |
| 31 |
| 32 // Helper method that loads either the include_globs or exclude_globs list |
| 33 // from an entry in the content_script lists of the manifest. |
| 34 bool LoadGlobsHelper(const DictionaryValue* content_script, |
| 35 int content_script_index, |
| 36 const char* globs_property_name, |
| 37 string16* error, |
| 38 void(UserScript::*add_method)(const std::string& glob), |
| 39 UserScript* instance) { |
| 40 if (!content_script->HasKey(globs_property_name)) |
| 41 return true; // they are optional |
| 42 |
| 43 const ListValue* list = NULL; |
| 44 if (!content_script->GetList(globs_property_name, &list)) { |
| 45 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 46 errors::kInvalidGlobList, |
| 47 base::IntToString(content_script_index), |
| 48 globs_property_name); |
| 49 return false; |
| 50 } |
| 51 |
| 52 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 53 std::string glob; |
| 54 if (!list->GetString(i, &glob)) { |
| 55 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 56 errors::kInvalidGlob, |
| 57 base::IntToString(content_script_index), |
| 58 globs_property_name, |
| 59 base::IntToString(i)); |
| 60 return false; |
| 61 } |
| 62 |
| 63 (instance->*add_method)(glob); |
| 64 } |
| 65 |
| 66 return true; |
| 67 } |
| 68 |
| 69 // Helper method that loads a UserScript object from a dictionary in the |
| 70 // content_script list of the manifest. |
| 71 bool LoadUserScriptFromDictionary(const DictionaryValue* content_script, |
| 72 int definition_index, |
| 73 Extension* extension, |
| 74 string16* error, |
| 75 UserScript* result) { |
| 76 // run_at |
| 77 if (content_script->HasKey(keys::kRunAt)) { |
| 78 std::string run_location; |
| 79 if (!content_script->GetString(keys::kRunAt, &run_location)) { |
| 80 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 81 errors::kInvalidRunAt, |
| 82 base::IntToString(definition_index)); |
| 83 return false; |
| 84 } |
| 85 |
| 86 if (run_location == values::kRunAtDocumentStart) { |
| 87 result->set_run_location(UserScript::DOCUMENT_START); |
| 88 } else if (run_location == values::kRunAtDocumentEnd) { |
| 89 result->set_run_location(UserScript::DOCUMENT_END); |
| 90 } else if (run_location == values::kRunAtDocumentIdle) { |
| 91 result->set_run_location(UserScript::DOCUMENT_IDLE); |
| 92 } else { |
| 93 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 94 errors::kInvalidRunAt, |
| 95 base::IntToString(definition_index)); |
| 96 return false; |
| 97 } |
| 98 } |
| 99 |
| 100 // all frames |
| 101 if (content_script->HasKey(keys::kAllFrames)) { |
| 102 bool all_frames = false; |
| 103 if (!content_script->GetBoolean(keys::kAllFrames, &all_frames)) { |
| 104 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 105 errors::kInvalidAllFrames, base::IntToString(definition_index)); |
| 106 return false; |
| 107 } |
| 108 result->set_match_all_frames(all_frames); |
| 109 } |
| 110 |
| 111 // matches (required) |
| 112 const ListValue* matches = NULL; |
| 113 if (!content_script->GetList(keys::kMatches, &matches)) { |
| 114 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 115 errors::kInvalidMatches, |
| 116 base::IntToString(definition_index)); |
| 117 return false; |
| 118 } |
| 119 |
| 120 if (matches->GetSize() == 0) { |
| 121 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 122 errors::kInvalidMatchCount, |
| 123 base::IntToString(definition_index)); |
| 124 return false; |
| 125 } |
| 126 for (size_t j = 0; j < matches->GetSize(); ++j) { |
| 127 std::string match_str; |
| 128 if (!matches->GetString(j, &match_str)) { |
| 129 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 130 errors::kInvalidMatch, |
| 131 base::IntToString(definition_index), |
| 132 base::IntToString(j), |
| 133 errors::kExpectString); |
| 134 return false; |
| 135 } |
| 136 |
| 137 URLPattern pattern(UserScript::kValidUserScriptSchemes); |
| 138 if (extension->CanExecuteScriptEverywhere()) |
| 139 pattern.SetValidSchemes(URLPattern::SCHEME_ALL); |
| 140 |
| 141 URLPattern::ParseResult parse_result = pattern.Parse(match_str); |
| 142 if (parse_result != URLPattern::PARSE_SUCCESS) { |
| 143 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 144 errors::kInvalidMatch, |
| 145 base::IntToString(definition_index), |
| 146 base::IntToString(j), |
| 147 URLPattern::GetParseResultString(parse_result)); |
| 148 return false; |
| 149 } |
| 150 |
| 151 if (pattern.MatchesScheme(chrome::kFileScheme) && |
| 152 !extension->CanExecuteScriptEverywhere()) { |
| 153 extension->set_wants_file_access(true); |
| 154 if (!(extension->creation_flags() & Extension::ALLOW_FILE_ACCESS)) { |
| 155 pattern.SetValidSchemes( |
| 156 pattern.valid_schemes() & ~URLPattern::SCHEME_FILE); |
| 157 } |
| 158 } |
| 159 |
| 160 result->add_url_pattern(pattern); |
| 161 } |
| 162 |
| 163 // exclude_matches |
| 164 if (content_script->HasKey(keys::kExcludeMatches)) { // optional |
| 165 const ListValue* exclude_matches = NULL; |
| 166 if (!content_script->GetList(keys::kExcludeMatches, &exclude_matches)) { |
| 167 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 168 errors::kInvalidExcludeMatches, |
| 169 base::IntToString(definition_index)); |
| 170 return false; |
| 171 } |
| 172 |
| 173 for (size_t j = 0; j < exclude_matches->GetSize(); ++j) { |
| 174 std::string match_str; |
| 175 if (!exclude_matches->GetString(j, &match_str)) { |
| 176 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 177 errors::kInvalidExcludeMatch, |
| 178 base::IntToString(definition_index), |
| 179 base::IntToString(j), |
| 180 errors::kExpectString); |
| 181 return false; |
| 182 } |
| 183 |
| 184 URLPattern pattern(UserScript::kValidUserScriptSchemes); |
| 185 if (extension->CanExecuteScriptEverywhere()) |
| 186 pattern.SetValidSchemes(URLPattern::SCHEME_ALL); |
| 187 |
| 188 URLPattern::ParseResult parse_result = pattern.Parse(match_str); |
| 189 if (parse_result != URLPattern::PARSE_SUCCESS) { |
| 190 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 191 errors::kInvalidExcludeMatch, |
| 192 base::IntToString(definition_index), base::IntToString(j), |
| 193 URLPattern::GetParseResultString(parse_result)); |
| 194 return false; |
| 195 } |
| 196 |
| 197 result->add_exclude_url_pattern(pattern); |
| 198 } |
| 199 } |
| 200 |
| 201 // include/exclude globs (mostly for Greasemonkey compatibility) |
| 202 if (!LoadGlobsHelper(content_script, definition_index, keys::kIncludeGlobs, |
| 203 error, &UserScript::add_glob, result)) { |
| 204 return false; |
| 205 } |
| 206 |
| 207 if (!LoadGlobsHelper(content_script, definition_index, keys::kExcludeGlobs, |
| 208 error, &UserScript::add_exclude_glob, result)) { |
| 209 return false; |
| 210 } |
| 211 |
| 212 // js and css keys |
| 213 const ListValue* js = NULL; |
| 214 if (content_script->HasKey(keys::kJs) && |
| 215 !content_script->GetList(keys::kJs, &js)) { |
| 216 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 217 errors::kInvalidJsList, |
| 218 base::IntToString(definition_index)); |
| 219 return false; |
| 220 } |
| 221 |
| 222 const ListValue* css = NULL; |
| 223 if (content_script->HasKey(keys::kCss) && |
| 224 !content_script->GetList(keys::kCss, &css)) { |
| 225 *error = ErrorUtils:: |
| 226 FormatErrorMessageUTF16(errors::kInvalidCssList, |
| 227 base::IntToString(definition_index)); |
| 228 return false; |
| 229 } |
| 230 |
| 231 // The manifest needs to have at least one js or css user script definition. |
| 232 if (((js ? js->GetSize() : 0) + (css ? css->GetSize() : 0)) == 0) { |
| 233 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 234 errors::kMissingFile, |
| 235 base::IntToString(definition_index)); |
| 236 return false; |
| 237 } |
| 238 |
| 239 if (js) { |
| 240 for (size_t script_index = 0; script_index < js->GetSize(); |
| 241 ++script_index) { |
| 242 const Value* value; |
| 243 std::string relative; |
| 244 if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) { |
| 245 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 246 errors::kInvalidJs, |
| 247 base::IntToString(definition_index), |
| 248 base::IntToString(script_index)); |
| 249 return false; |
| 250 } |
| 251 GURL url = extension->GetResourceURL(relative); |
| 252 ExtensionResource resource = extension->GetResource(relative); |
| 253 result->js_scripts().push_back(UserScript::File( |
| 254 resource.extension_root(), resource.relative_path(), url)); |
| 255 } |
| 256 } |
| 257 |
| 258 if (css) { |
| 259 for (size_t script_index = 0; script_index < css->GetSize(); |
| 260 ++script_index) { |
| 261 const Value* value; |
| 262 std::string relative; |
| 263 if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) { |
| 264 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 265 errors::kInvalidCss, |
| 266 base::IntToString(definition_index), |
| 267 base::IntToString(script_index)); |
| 268 return false; |
| 269 } |
| 270 GURL url = extension->GetResourceURL(relative); |
| 271 ExtensionResource resource = extension->GetResource(relative); |
| 272 result->css_scripts().push_back(UserScript::File( |
| 273 resource.extension_root(), resource.relative_path(), url)); |
| 274 } |
| 275 } |
| 276 |
| 277 return true; |
| 278 } |
| 279 |
| 280 // Returns false and sets the error if script file can't be loaded, |
| 281 // or if it's not UTF-8 encoded. |
| 282 static bool IsScriptValid(const base::FilePath& path, |
| 283 const base::FilePath& relative_path, |
| 284 int message_id, |
| 285 std::string* error) { |
| 286 std::string content; |
| 287 if (!file_util::PathExists(path) || |
| 288 !file_util::ReadFileToString(path, &content)) { |
| 289 *error = l10n_util::GetStringFUTF8( |
| 290 message_id, |
| 291 relative_path.LossyDisplayName()); |
| 292 return false; |
| 293 } |
| 294 |
| 295 if (!IsStringUTF8(content)) { |
| 296 *error = l10n_util::GetStringFUTF8( |
| 297 IDS_EXTENSION_BAD_FILE_ENCODING, |
| 298 relative_path.LossyDisplayName()); |
| 299 return false; |
| 300 } |
| 301 |
| 302 return true; |
| 303 } |
| 304 |
| 305 struct EmptyUserScriptList { |
| 306 UserScriptList user_script_list; |
| 307 }; |
| 308 |
| 309 static base::LazyInstance<EmptyUserScriptList> g_empty_script_list = |
| 310 LAZY_INSTANCE_INITIALIZER; |
| 311 |
| 312 } // namespace |
| 313 |
| 314 ContentScriptsInfo::ContentScriptsInfo() { |
| 315 } |
| 316 |
| 317 ContentScriptsInfo::~ContentScriptsInfo() { |
| 318 } |
| 319 |
| 320 // static |
| 321 const UserScriptList& ContentScriptsInfo::GetContentScripts( |
| 322 const Extension* extension) { |
| 323 ContentScriptsInfo* info = static_cast<ContentScriptsInfo*>( |
| 324 extension->GetManifestData(keys::kContentScripts)); |
| 325 return info ? info->content_scripts |
| 326 : g_empty_script_list.Get().user_script_list; |
| 327 } |
| 328 |
| 329 ContentScriptsHandler::ContentScriptsHandler() { |
| 330 } |
| 331 |
| 332 ContentScriptsHandler::~ContentScriptsHandler() { |
| 333 } |
| 334 |
| 335 const std::vector<std::string> ContentScriptsHandler::Keys() const { |
| 336 static const char* keys[] = { |
| 337 keys::kContentScripts |
| 338 }; |
| 339 return std::vector<std::string>(keys, keys + arraysize(keys)); |
| 340 } |
| 341 |
| 342 bool ContentScriptsHandler::Parse(Extension* extension, string16* error) { |
| 343 scoped_ptr<ContentScriptsInfo> content_scripts_info(new ContentScriptsInfo); |
| 344 const ListValue* scripts_list = NULL; |
| 345 if (!extension->manifest()->GetList(keys::kContentScripts, &scripts_list)) { |
| 346 *error = ASCIIToUTF16(errors::kInvalidContentScriptsList); |
| 347 return false; |
| 348 } |
| 349 |
| 350 for (size_t i = 0; i < scripts_list->GetSize(); ++i) { |
| 351 const DictionaryValue* script_dict = NULL; |
| 352 if (!scripts_list->GetDictionary(i, &script_dict)) { |
| 353 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 354 extension_manifest_errors::kInvalidContentScript, |
| 355 base::IntToString(i)); |
| 356 return false; |
| 357 } |
| 358 |
| 359 UserScript user_script; |
| 360 if (!LoadUserScriptFromDictionary(script_dict, |
| 361 i, |
| 362 extension, |
| 363 error, |
| 364 &user_script)) { |
| 365 return false; // Failed to parse script context definition. |
| 366 } |
| 367 user_script.set_extension_id(extension->id()); |
| 368 if (extension->converted_from_user_script()) { |
| 369 user_script.set_emulate_greasemonkey(true); |
| 370 // Greasemonkey matches all frames. |
| 371 user_script.set_match_all_frames(true); |
| 372 } |
| 373 content_scripts_info->content_scripts.push_back(user_script); |
| 374 } |
| 375 extension->SetManifestData(keys::kContentScripts, |
| 376 content_scripts_info.release()); |
| 377 return true; |
| 378 } |
| 379 |
| 380 bool ContentScriptsHandler::Validate( |
| 381 const Extension* extension, |
| 382 std::string* error, |
| 383 std::vector<InstallWarning>* warnings) const { |
| 384 // Validate that claimed script resources actually exist, |
| 385 // and are UTF-8 encoded. |
| 386 ExtensionResource::SymlinkPolicy symlink_policy; |
| 387 if ((extension->creation_flags() & |
| 388 Extension::FOLLOW_SYMLINKS_ANYWHERE) != 0) { |
| 389 symlink_policy = ExtensionResource::FOLLOW_SYMLINKS_ANYWHERE; |
| 390 } else { |
| 391 symlink_policy = ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT; |
| 392 } |
| 393 |
| 394 const extensions::UserScriptList& content_scripts = |
| 395 extensions::ContentScriptsInfo::GetContentScripts(extension); |
| 396 for (size_t i = 0; i < content_scripts.size(); ++i) { |
| 397 const extensions::UserScript& script = content_scripts[i]; |
| 398 |
| 399 for (size_t j = 0; j < script.js_scripts().size(); j++) { |
| 400 const extensions::UserScript::File& js_script = script.js_scripts()[j]; |
| 401 const base::FilePath& path = ExtensionResource::GetFilePath( |
| 402 js_script.extension_root(), js_script.relative_path(), |
| 403 symlink_policy); |
| 404 if (!IsScriptValid(path, js_script.relative_path(), |
| 405 IDS_EXTENSION_LOAD_JAVASCRIPT_FAILED, error)) |
| 406 return false; |
| 407 } |
| 408 |
| 409 for (size_t j = 0; j < script.css_scripts().size(); j++) { |
| 410 const extensions::UserScript::File& css_script = script.css_scripts()[j]; |
| 411 const base::FilePath& path = ExtensionResource::GetFilePath( |
| 412 css_script.extension_root(), css_script.relative_path(), |
| 413 symlink_policy); |
| 414 if (!IsScriptValid(path, css_script.relative_path(), |
| 415 IDS_EXTENSION_LOAD_CSS_FAILED, error)) |
| 416 return false; |
| 417 } |
| 418 } |
| 419 |
| 420 return true; |
| 421 } |
| 422 |
| 423 } // namespace extensions |
| OLD | NEW |