Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "base/atomic_sequence_num.h" | |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 15 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
| 16 #include "extensions/common/error_utils.h" | 17 #include "extensions/common/error_utils.h" |
| 17 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
| 18 #include "extensions/common/extension_resource.h" | 19 #include "extensions/common/extension_resource.h" |
| 19 #include "extensions/common/manifest_constants.h" | 20 #include "extensions/common/manifest_constants.h" |
| 20 #include "extensions/common/manifest_handlers/permissions_parser.h" | 21 #include "extensions/common/manifest_handlers/permissions_parser.h" |
| 21 #include "extensions/common/permissions/permissions_data.h" | 22 #include "extensions/common/permissions/permissions_data.h" |
| 22 #include "extensions/common/url_pattern.h" | 23 #include "extensions/common/url_pattern.h" |
| 23 #include "extensions/common/url_pattern_set.h" | 24 #include "extensions/common/url_pattern_set.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 26 | 27 |
| 27 namespace extensions { | 28 namespace extensions { |
| 28 | 29 |
| 29 namespace keys = extensions::manifest_keys; | 30 namespace keys = extensions::manifest_keys; |
| 30 namespace values = manifest_values; | 31 namespace values = manifest_values; |
| 31 namespace errors = manifest_errors; | 32 namespace errors = manifest_errors; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 // The globally-unique id for a user script. | 36 // The globally-unique id for a user script. |
| 36 int64 g_next_user_script_id = 0; | 37 base::StaticAtomicSequenceNumber g_user_script_id_generator; |
|
Devlin
2014/08/21 15:27:47
This seems... overarchitected. Any advantage over
Mark Dittmer
2014/08/21 19:45:41
In early testing of https://codereview.chromium.or
| |
| 37 | 38 |
| 38 // Helper method that loads either the include_globs or exclude_globs list | 39 // Helper method that loads either the include_globs or exclude_globs list |
| 39 // from an entry in the content_script lists of the manifest. | 40 // from an entry in the content_script lists of the manifest. |
| 40 bool LoadGlobsHelper(const base::DictionaryValue* content_script, | 41 bool LoadGlobsHelper(const base::DictionaryValue* content_script, |
| 41 int content_script_index, | 42 int content_script_index, |
| 42 const char* globs_property_name, | 43 const char* globs_property_name, |
| 43 base::string16* error, | 44 base::string16* error, |
| 44 void(UserScript::*add_method)(const std::string& glob), | 45 void(UserScript::*add_method)(const std::string& glob), |
| 45 UserScript* instance) { | 46 UserScript* instance) { |
| 46 if (!content_script->HasKey(globs_property_name)) | 47 if (!content_script->HasKey(globs_property_name)) |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 ContentScriptsHandler::~ContentScriptsHandler() { | 390 ContentScriptsHandler::~ContentScriptsHandler() { |
| 390 } | 391 } |
| 391 | 392 |
| 392 const std::vector<std::string> ContentScriptsHandler::Keys() const { | 393 const std::vector<std::string> ContentScriptsHandler::Keys() const { |
| 393 static const char* keys[] = { | 394 static const char* keys[] = { |
| 394 keys::kContentScripts | 395 keys::kContentScripts |
| 395 }; | 396 }; |
| 396 return std::vector<std::string>(keys, keys + arraysize(keys)); | 397 return std::vector<std::string>(keys, keys + arraysize(keys)); |
| 397 } | 398 } |
| 398 | 399 |
| 400 // static | |
| 401 int64 ContentScriptsHandler::GetNextUserScriptID() { | |
|
Devlin
2014/08/21 15:27:47
GetNext() returns an int, not an int64. Probably
Mark Dittmer
2014/08/21 19:45:41
This is done because UserScript IDs are int64. Sho
Devlin
2014/08/21 20:10:20
Yes. We should be consistent.
Mark Dittmer
2014/08/21 21:12:29
Done.
Devlin
2014/08/21 21:24:23
You're missing some. What about all the renderer
| |
| 402 return g_user_script_id_generator.GetNext(); | |
| 403 } | |
| 404 | |
| 399 bool ContentScriptsHandler::Parse(Extension* extension, base::string16* error) { | 405 bool ContentScriptsHandler::Parse(Extension* extension, base::string16* error) { |
| 400 scoped_ptr<ContentScriptsInfo> content_scripts_info(new ContentScriptsInfo); | 406 scoped_ptr<ContentScriptsInfo> content_scripts_info(new ContentScriptsInfo); |
| 401 const base::ListValue* scripts_list = NULL; | 407 const base::ListValue* scripts_list = NULL; |
| 402 if (!extension->manifest()->GetList(keys::kContentScripts, &scripts_list)) { | 408 if (!extension->manifest()->GetList(keys::kContentScripts, &scripts_list)) { |
| 403 *error = base::ASCIIToUTF16(errors::kInvalidContentScriptsList); | 409 *error = base::ASCIIToUTF16(errors::kInvalidContentScriptsList); |
| 404 return false; | 410 return false; |
| 405 } | 411 } |
| 406 | 412 |
| 407 for (size_t i = 0; i < scripts_list->GetSize(); ++i) { | 413 for (size_t i = 0; i < scripts_list->GetSize(); ++i) { |
| 408 const base::DictionaryValue* script_dict = NULL; | 414 const base::DictionaryValue* script_dict = NULL; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 421 &user_script)) { | 427 &user_script)) { |
| 422 return false; // Failed to parse script context definition. | 428 return false; // Failed to parse script context definition. |
| 423 } | 429 } |
| 424 | 430 |
| 425 user_script.set_extension_id(extension->id()); | 431 user_script.set_extension_id(extension->id()); |
| 426 if (extension->converted_from_user_script()) { | 432 if (extension->converted_from_user_script()) { |
| 427 user_script.set_emulate_greasemonkey(true); | 433 user_script.set_emulate_greasemonkey(true); |
| 428 // Greasemonkey matches all frames. | 434 // Greasemonkey matches all frames. |
| 429 user_script.set_match_all_frames(true); | 435 user_script.set_match_all_frames(true); |
| 430 } | 436 } |
| 431 user_script.set_id(g_next_user_script_id++); | 437 user_script.set_id(GetNextUserScriptID()); |
| 432 content_scripts_info->content_scripts.push_back(user_script); | 438 content_scripts_info->content_scripts.push_back(user_script); |
| 433 } | 439 } |
| 434 extension->SetManifestData(keys::kContentScripts, | 440 extension->SetManifestData(keys::kContentScripts, |
| 435 content_scripts_info.release()); | 441 content_scripts_info.release()); |
| 436 PermissionsParser::SetScriptableHosts( | 442 PermissionsParser::SetScriptableHosts( |
| 437 extension, ContentScriptsInfo::GetScriptableHosts(extension)); | 443 extension, ContentScriptsInfo::GetScriptableHosts(extension)); |
| 438 return true; | 444 return true; |
| 439 } | 445 } |
| 440 | 446 |
| 441 bool ContentScriptsHandler::Validate( | 447 bool ContentScriptsHandler::Validate( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 475 if (!IsScriptValid(path, css_script.relative_path(), | 481 if (!IsScriptValid(path, css_script.relative_path(), |
| 476 IDS_EXTENSION_LOAD_CSS_FAILED, error)) | 482 IDS_EXTENSION_LOAD_CSS_FAILED, error)) |
| 477 return false; | 483 return false; |
| 478 } | 484 } |
| 479 } | 485 } |
| 480 | 486 |
| 481 return true; | 487 return true; |
| 482 } | 488 } |
| 483 | 489 |
| 484 } // namespace extensions | 490 } // namespace extensions |
| OLD | NEW |