| OLD | NEW |
| 1 // Copyright (c) 2011 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/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 const size_t Extension::kNumHostedAppPermissions = | 257 const size_t Extension::kNumHostedAppPermissions = |
| 258 arraysize(Extension::kHostedAppPermissionNames); | 258 arraysize(Extension::kHostedAppPermissionNames); |
| 259 | 259 |
| 260 // We purposefully don't put this into kPermissionNames. | 260 // We purposefully don't put this into kPermissionNames. |
| 261 const char Extension::kOldUnlimitedStoragePermission[] = "unlimited_storage"; | 261 const char Extension::kOldUnlimitedStoragePermission[] = "unlimited_storage"; |
| 262 | 262 |
| 263 const int Extension::kValidWebExtentSchemes = | 263 const int Extension::kValidWebExtentSchemes = |
| 264 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS; | 264 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS; |
| 265 | 265 |
| 266 const int Extension::kValidHostPermissionSchemes = | 266 const int Extension::kValidHostPermissionSchemes = |
| 267 (UserScript::kValidUserScriptSchemes | | 267 UserScript::kValidUserScriptSchemes | URLPattern::SCHEME_CHROMEUI; |
| 268 URLPattern::SCHEME_CHROMEUI) & ~URLPattern::SCHEME_FILE; | |
| 269 | 268 |
| 270 // | 269 // |
| 271 // Extension | 270 // Extension |
| 272 // | 271 // |
| 273 | 272 |
| 274 // static | 273 // static |
| 275 scoped_refptr<Extension> Extension::Create(const FilePath& path, | 274 scoped_refptr<Extension> Extension::Create(const FilePath& path, |
| 276 Location location, | 275 Location location, |
| 277 const DictionaryValue& value, | 276 const DictionaryValue& value, |
| 278 int flags, | 277 int flags, |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 *output = StringToLowerASCII(base::HexEncode(hash, sizeof(hash))); | 548 *output = StringToLowerASCII(base::HexEncode(hash, sizeof(hash))); |
| 550 ConvertHexadecimalToIDAlphabet(output); | 549 ConvertHexadecimalToIDAlphabet(output); |
| 551 | 550 |
| 552 return true; | 551 return true; |
| 553 } | 552 } |
| 554 | 553 |
| 555 // Helper method that loads a UserScript object from a dictionary in the | 554 // Helper method that loads a UserScript object from a dictionary in the |
| 556 // content_script list of the manifest. | 555 // content_script list of the manifest. |
| 557 bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, | 556 bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, |
| 558 int definition_index, | 557 int definition_index, |
| 559 URLPattern::ParseOption parse_strictness, | 558 int flags, |
| 560 std::string* error, | 559 std::string* error, |
| 561 UserScript* result) { | 560 UserScript* result) { |
| 561 // When strict error checks are enabled, make URL pattern parsing strict. |
| 562 URLPattern::ParseOption parse_strictness = |
| 563 (flags & STRICT_ERROR_CHECKS ? URLPattern::PARSE_STRICT |
| 564 : URLPattern::PARSE_LENIENT); |
| 565 |
| 562 // run_at | 566 // run_at |
| 563 if (content_script->HasKey(keys::kRunAt)) { | 567 if (content_script->HasKey(keys::kRunAt)) { |
| 564 std::string run_location; | 568 std::string run_location; |
| 565 if (!content_script->GetString(keys::kRunAt, &run_location)) { | 569 if (!content_script->GetString(keys::kRunAt, &run_location)) { |
| 566 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidRunAt, | 570 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidRunAt, |
| 567 base::IntToString(definition_index)); | 571 base::IntToString(definition_index)); |
| 568 return false; | 572 return false; |
| 569 } | 573 } |
| 570 | 574 |
| 571 if (run_location == values::kRunAtDocumentStart) { | 575 if (run_location == values::kRunAtDocumentStart) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 parse_strictness); | 628 parse_strictness); |
| 625 if (parse_result != URLPattern::PARSE_SUCCESS) { | 629 if (parse_result != URLPattern::PARSE_SUCCESS) { |
| 626 *error = ExtensionErrorUtils::FormatErrorMessage( | 630 *error = ExtensionErrorUtils::FormatErrorMessage( |
| 627 errors::kInvalidMatch, | 631 errors::kInvalidMatch, |
| 628 base::IntToString(definition_index), | 632 base::IntToString(definition_index), |
| 629 base::IntToString(j), | 633 base::IntToString(j), |
| 630 URLPattern::GetParseResultString(parse_result)); | 634 URLPattern::GetParseResultString(parse_result)); |
| 631 return false; | 635 return false; |
| 632 } | 636 } |
| 633 | 637 |
| 638 if (pattern.MatchesScheme(chrome::kFileScheme) && |
| 639 !CanExecuteScriptEverywhere()) { |
| 640 wants_file_access_ = true; |
| 641 if (!(flags & ALLOW_FILE_ACCESS)) |
| 642 pattern.set_valid_schemes( |
| 643 pattern.valid_schemes() & ~URLPattern::SCHEME_FILE); |
| 644 } |
| 645 |
| 634 result->add_url_pattern(pattern); | 646 result->add_url_pattern(pattern); |
| 635 } | 647 } |
| 636 | 648 |
| 637 // include/exclude globs (mostly for Greasemonkey compatibility) | 649 // include/exclude globs (mostly for Greasemonkey compatibility) |
| 638 if (!LoadGlobsHelper(content_script, definition_index, keys::kIncludeGlobs, | 650 if (!LoadGlobsHelper(content_script, definition_index, keys::kIncludeGlobs, |
| 639 error, &UserScript::add_glob, result)) { | 651 error, &UserScript::add_glob, result)) { |
| 640 return false; | 652 return false; |
| 641 } | 653 } |
| 642 | 654 |
| 643 if (!LoadGlobsHelper(content_script, definition_index, keys::kExcludeGlobs, | 655 if (!LoadGlobsHelper(content_script, definition_index, keys::kExcludeGlobs, |
| (...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1205 | 1217 |
| 1206 Extension::Extension(const FilePath& path, Location location) | 1218 Extension::Extension(const FilePath& path, Location location) |
| 1207 : incognito_split_mode_(false), | 1219 : incognito_split_mode_(false), |
| 1208 location_(location), | 1220 location_(location), |
| 1209 converted_from_user_script_(false), | 1221 converted_from_user_script_(false), |
| 1210 is_theme_(false), | 1222 is_theme_(false), |
| 1211 is_app_(false), | 1223 is_app_(false), |
| 1212 is_storage_isolated_(false), | 1224 is_storage_isolated_(false), |
| 1213 launch_container_(extension_misc::LAUNCH_TAB), | 1225 launch_container_(extension_misc::LAUNCH_TAB), |
| 1214 launch_width_(0), | 1226 launch_width_(0), |
| 1215 launch_height_(0) { | 1227 launch_height_(0), |
| 1228 wants_file_access_(false) { |
| 1216 DCHECK(path.IsAbsolute()); | 1229 DCHECK(path.IsAbsolute()); |
| 1217 path_ = MaybeNormalizePath(path); | 1230 path_ = MaybeNormalizePath(path); |
| 1218 } | 1231 } |
| 1219 Extension::~Extension() { | 1232 Extension::~Extension() { |
| 1220 } | 1233 } |
| 1221 ExtensionResource Extension::GetResource( | 1234 ExtensionResource Extension::GetResource( |
| 1222 const std::string& relative_path) const { | 1235 const std::string& relative_path) const { |
| 1223 #if defined(OS_POSIX) | 1236 #if defined(OS_POSIX) |
| 1224 FilePath relative_file_path(relative_path); | 1237 FilePath relative_file_path(relative_path); |
| 1225 #elif defined(OS_WIN) | 1238 #elif defined(OS_WIN) |
| (...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1767 | 1780 |
| 1768 for (size_t i = 0; i < list_value->GetSize(); ++i) { | 1781 for (size_t i = 0; i < list_value->GetSize(); ++i) { |
| 1769 DictionaryValue* content_script; | 1782 DictionaryValue* content_script; |
| 1770 if (!list_value->GetDictionary(i, &content_script)) { | 1783 if (!list_value->GetDictionary(i, &content_script)) { |
| 1771 *error = ExtensionErrorUtils::FormatErrorMessage( | 1784 *error = ExtensionErrorUtils::FormatErrorMessage( |
| 1772 errors::kInvalidContentScript, base::IntToString(i)); | 1785 errors::kInvalidContentScript, base::IntToString(i)); |
| 1773 return false; | 1786 return false; |
| 1774 } | 1787 } |
| 1775 | 1788 |
| 1776 UserScript script; | 1789 UserScript script; |
| 1777 if (!LoadUserScriptHelper(content_script, i, parse_strictness, error, | 1790 if (!LoadUserScriptHelper(content_script, i, flags, error, &script)) |
| 1778 &script)) | |
| 1779 return false; // Failed to parse script context definition. | 1791 return false; // Failed to parse script context definition. |
| 1780 script.set_extension_id(id()); | 1792 script.set_extension_id(id()); |
| 1781 if (converted_from_user_script_) { | 1793 if (converted_from_user_script_) { |
| 1782 script.set_emulate_greasemonkey(true); | 1794 script.set_emulate_greasemonkey(true); |
| 1783 script.set_match_all_frames(true); // Greasemonkey matches all frames. | 1795 script.set_match_all_frames(true); // Greasemonkey matches all frames. |
| 1784 } | 1796 } |
| 1785 content_scripts_.push_back(script); | 1797 content_scripts_.push_back(script); |
| 1786 } | 1798 } |
| 1787 } | 1799 } |
| 1788 | 1800 |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1954 if (!CanSpecifyHostPermission(pattern)) { | 1966 if (!CanSpecifyHostPermission(pattern)) { |
| 1955 *error = ExtensionErrorUtils::FormatErrorMessage( | 1967 *error = ExtensionErrorUtils::FormatErrorMessage( |
| 1956 errors::kInvalidPermissionScheme, base::IntToString(i)); | 1968 errors::kInvalidPermissionScheme, base::IntToString(i)); |
| 1957 return false; | 1969 return false; |
| 1958 } | 1970 } |
| 1959 | 1971 |
| 1960 // The path component is not used for host permissions, so we force it | 1972 // The path component is not used for host permissions, so we force it |
| 1961 // to match all paths. | 1973 // to match all paths. |
| 1962 pattern.SetPath("/*"); | 1974 pattern.SetPath("/*"); |
| 1963 | 1975 |
| 1976 if (pattern.MatchesScheme(chrome::kFileScheme) && |
| 1977 !CanExecuteScriptEverywhere()) { |
| 1978 wants_file_access_ = true; |
| 1979 if (!(flags & ALLOW_FILE_ACCESS)) |
| 1980 pattern.set_valid_schemes( |
| 1981 pattern.valid_schemes() & ~URLPattern::SCHEME_FILE); |
| 1982 } |
| 1983 |
| 1964 host_permissions_.push_back(pattern); | 1984 host_permissions_.push_back(pattern); |
| 1965 } | 1985 } |
| 1966 | 1986 |
| 1967 // If it's not a host permission, then it's probably an unknown API | 1987 // If it's not a host permission, then it's probably an unknown API |
| 1968 // permission. Do not throw an error so extensions can retain | 1988 // permission. Do not throw an error so extensions can retain |
| 1969 // backwards compatability (http://crbug.com/42742). | 1989 // backwards compatability (http://crbug.com/42742). |
| 1970 // TODO(jstritar): We can improve error messages by adding better | 1990 // TODO(jstritar): We can improve error messages by adding better |
| 1971 // validation of API permissions here. | 1991 // validation of API permissions here. |
| 1972 // TODO(skerner): Consider showing the reason |permission_str| is not | 1992 // TODO(skerner): Consider showing the reason |permission_str| is not |
| 1973 // a valid URL pattern if it is almost valid. For example, if it has | 1993 // a valid URL pattern if it is almost valid. For example, if it has |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2405 if (browser_action()) | 2425 if (browser_action()) |
| 2406 ++num_surfaces; | 2426 ++num_surfaces; |
| 2407 | 2427 |
| 2408 if (is_app()) | 2428 if (is_app()) |
| 2409 ++num_surfaces; | 2429 ++num_surfaces; |
| 2410 | 2430 |
| 2411 return num_surfaces > 1; | 2431 return num_surfaces > 1; |
| 2412 } | 2432 } |
| 2413 | 2433 |
| 2414 bool Extension::CanExecuteScriptOnPage(const GURL& page_url, | 2434 bool Extension::CanExecuteScriptOnPage(const GURL& page_url, |
| 2415 UserScript* script, | 2435 const UserScript* script, |
| 2416 std::string* error) const { | 2436 std::string* error) const { |
| 2417 // The gallery is special-cased as a restricted URL for scripting to prevent | 2437 // The gallery is special-cased as a restricted URL for scripting to prevent |
| 2418 // access to special JS bindings we expose to the gallery (and avoid things | 2438 // access to special JS bindings we expose to the gallery (and avoid things |
| 2419 // like extensions removing the "report abuse" link). | 2439 // like extensions removing the "report abuse" link). |
| 2420 // TODO(erikkay): This seems like the wrong test. Shouldn't we we testing | 2440 // TODO(erikkay): This seems like the wrong test. Shouldn't we we testing |
| 2421 // against the store app extent? | 2441 // against the store app extent? |
| 2422 if ((page_url.host() == GURL(Extension::ChromeStoreLaunchURL()).host()) && | 2442 if ((page_url.host() == GURL(Extension::ChromeStoreLaunchURL()).host()) && |
| 2423 !CanExecuteScriptEverywhere() && | 2443 !CanExecuteScriptEverywhere() && |
| 2424 !CommandLine::ForCurrentProcess()->HasSwitch( | 2444 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 2425 switches::kAllowScriptingGallery)) { | 2445 switches::kAllowScriptingGallery)) { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2564 | 2584 |
| 2565 UninstalledExtensionInfo::~UninstalledExtensionInfo() {} | 2585 UninstalledExtensionInfo::~UninstalledExtensionInfo() {} |
| 2566 | 2586 |
| 2567 | 2587 |
| 2568 UnloadedExtensionInfo::UnloadedExtensionInfo( | 2588 UnloadedExtensionInfo::UnloadedExtensionInfo( |
| 2569 const Extension* extension, | 2589 const Extension* extension, |
| 2570 Reason reason) | 2590 Reason reason) |
| 2571 : reason(reason), | 2591 : reason(reason), |
| 2572 already_disabled(false), | 2592 already_disabled(false), |
| 2573 extension(extension) {} | 2593 extension(extension) {} |
| OLD | NEW |