| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/stl_util-inl.h" | 13 #include "base/stl_util-inl.h" |
| 14 #include "base/third_party/nss/blapi.h" | 14 #include "base/third_party/nss/blapi.h" |
| 15 #include "base/third_party/nss/sha256.h" | 15 #include "base/third_party/nss/sha256.h" |
| 16 #include "chrome/browser/extensions/extension_l10n_util.h" |
| 16 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
| 17 #include "chrome/common/extensions/extension_constants.h" | 18 #include "chrome/common/extensions/extension_constants.h" |
| 18 #include "chrome/common/extensions/extension_error_reporter.h" | 19 #include "chrome/common/extensions/extension_error_reporter.h" |
| 19 #include "chrome/common/extensions/extension_error_utils.h" | 20 #include "chrome/common/extensions/extension_error_utils.h" |
| 20 #include "chrome/common/extensions/user_script.h" | 21 #include "chrome/common/extensions/user_script.h" |
| 21 #include "chrome/common/notification_service.h" | 22 #include "chrome/common/notification_service.h" |
| 22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 23 #include "net/base/base64.h" | 24 #include "net/base/base64.h" |
| 24 #include "net/base/net_util.h" | |
| 25 | 25 |
| 26 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 27 #include "base/registry.h" | 27 #include "base/registry.h" |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 namespace keys = extension_manifest_keys; | 30 namespace keys = extension_manifest_keys; |
| 31 namespace values = extension_manifest_values; | 31 namespace values = extension_manifest_values; |
| 32 namespace errors = extension_manifest_errors; | 32 namespace errors = extension_manifest_errors; |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 ++script_index) { | 275 ++script_index) { |
| 276 Value* value; | 276 Value* value; |
| 277 std::wstring relative; | 277 std::wstring relative; |
| 278 if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) { | 278 if (!js->Get(script_index, &value) || !value->GetAsString(&relative)) { |
| 279 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJs, | 279 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJs, |
| 280 IntToString(definition_index), IntToString(script_index)); | 280 IntToString(definition_index), IntToString(script_index)); |
| 281 return false; | 281 return false; |
| 282 } | 282 } |
| 283 // TODO(georged): Make GetResourceURL accept wstring too | 283 // TODO(georged): Make GetResourceURL accept wstring too |
| 284 GURL url = GetResourceURL(WideToUTF8(relative)); | 284 GURL url = GetResourceURL(WideToUTF8(relative)); |
| 285 FilePath path = GetResourcePath(WideToUTF8(relative)); | 285 ExtensionResource resource = GetResource(WideToUTF8(relative)); |
| 286 result->js_scripts().push_back(UserScript::File(path, url)); | 286 result->js_scripts().push_back(UserScript::File(resource, url)); |
| 287 } | 287 } |
| 288 } | 288 } |
| 289 | 289 |
| 290 if (css) { | 290 if (css) { |
| 291 for (size_t script_index = 0; script_index < css->GetSize(); | 291 for (size_t script_index = 0; script_index < css->GetSize(); |
| 292 ++script_index) { | 292 ++script_index) { |
| 293 Value* value; | 293 Value* value; |
| 294 std::wstring relative; | 294 std::wstring relative; |
| 295 if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) { | 295 if (!css->Get(script_index, &value) || !value->GetAsString(&relative)) { |
| 296 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidCss, | 296 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidCss, |
| 297 IntToString(definition_index), IntToString(script_index)); | 297 IntToString(definition_index), IntToString(script_index)); |
| 298 return false; | 298 return false; |
| 299 } | 299 } |
| 300 // TODO(georged): Make GetResourceURL accept wstring too | 300 // TODO(georged): Make GetResourceURL accept wstring too |
| 301 GURL url = GetResourceURL(WideToUTF8(relative)); | 301 GURL url = GetResourceURL(WideToUTF8(relative)); |
| 302 FilePath path = GetResourcePath(WideToUTF8(relative)); | 302 ExtensionResource resource = GetResource(WideToUTF8(relative)); |
| 303 result->css_scripts().push_back(UserScript::File(path, url)); | 303 result->css_scripts().push_back(UserScript::File(resource, url)); |
| 304 } | 304 } |
| 305 } | 305 } |
| 306 | 306 |
| 307 return true; | 307 return true; |
| 308 } | 308 } |
| 309 | 309 |
| 310 // Helper method that loads a PageAction or BrowserAction object from a | 310 // Helper method that loads a PageAction or BrowserAction object from a |
| 311 // dictionary in the page_actions list or browser_action key of the manifest. | 311 // dictionary in the page_actions list or browser_action key of the manifest. |
| 312 ExtensionAction* Extension::LoadExtensionActionHelper( | 312 ExtensionAction* Extension::LoadExtensionActionHelper( |
| 313 const DictionaryValue* page_action, int definition_index, | 313 const DictionaryValue* page_action, int definition_index, |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 while (iter != source.end_keys()) { | 420 while (iter != source.end_keys()) { |
| 421 std::wstring key = (*iter); | 421 std::wstring key = (*iter); |
| 422 if (theme_keys.find(key) == theme_keys.end()) | 422 if (theme_keys.find(key) == theme_keys.end()) |
| 423 return true; | 423 return true; |
| 424 ++iter; | 424 ++iter; |
| 425 } | 425 } |
| 426 return false; | 426 return false; |
| 427 } | 427 } |
| 428 | 428 |
| 429 // static | 429 // static |
| 430 FilePath Extension::GetResourcePath(const FilePath& extension_path, | 430 ExtensionResource Extension::GetResource(const FilePath& extension_path, |
| 431 const std::string& relative_path) { | 431 const std::string& relative_path) { |
| 432 // Build up a file:// URL and convert that back to a FilePath. This avoids | 432 FilePath relative_resource_path; |
| 433 // URL encoding and path separator issues. | 433 return ExtensionResource(extension_path, |
| 434 | 434 relative_resource_path.AppendASCII(relative_path)); |
| 435 // Convert the extension's root to a file:// URL. | |
| 436 GURL extension_url = net::FilePathToFileURL(extension_path); | |
| 437 if (!extension_url.is_valid()) | |
| 438 return FilePath(); | |
| 439 | |
| 440 // Append the requested path. | |
| 441 GURL::Replacements replacements; | |
| 442 std::string new_path(extension_url.path()); | |
| 443 new_path += "/"; | |
| 444 new_path += relative_path; | |
| 445 replacements.SetPathStr(new_path); | |
| 446 GURL file_url = extension_url.ReplaceComponents(replacements); | |
| 447 if (!file_url.is_valid()) | |
| 448 return FilePath(); | |
| 449 | |
| 450 // Convert the result back to a FilePath. | |
| 451 FilePath ret_val; | |
| 452 if (!net::FileURLToFilePath(file_url, &ret_val)) | |
| 453 return FilePath(); | |
| 454 | |
| 455 // Double-check that the path we ended up with is actually inside the | |
| 456 // extension root. | |
| 457 if (!extension_path.IsParent(ret_val)) | |
| 458 return FilePath(); | |
| 459 | |
| 460 return ret_val; | |
| 461 } | 435 } |
| 462 | 436 |
| 463 Extension::Extension(const FilePath& path) | 437 Extension::Extension(const FilePath& path) |
| 464 : is_theme_(false), background_page_ready_(false) { | 438 : is_theme_(false), background_page_ready_(false) { |
| 465 DCHECK(path.IsAbsolute()); | 439 DCHECK(path.IsAbsolute()); |
| 466 location_ = INVALID; | 440 location_ = INVALID; |
| 467 | 441 |
| 468 #if defined(OS_WIN) | 442 #if defined(OS_WIN) |
| 469 // Normalize any drive letter to upper-case. We do this for consistency with | 443 // Normalize any drive letter to upper-case. We do this for consistency with |
| 470 // net_utils::FilePathToFileURL(), which does the same thing, to make string | 444 // net_utils::FilePathToFileURL(), which does the same thing, to make string |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1112 | 1086 |
| 1113 void Extension::SetBackgroundPageReady() { | 1087 void Extension::SetBackgroundPageReady() { |
| 1114 DCHECK(!background_url().is_empty()); | 1088 DCHECK(!background_url().is_empty()); |
| 1115 background_page_ready_ = true; | 1089 background_page_ready_ = true; |
| 1116 NotificationService::current()->Notify( | 1090 NotificationService::current()->Notify( |
| 1117 NotificationType::EXTENSION_BACKGROUND_PAGE_READY, | 1091 NotificationType::EXTENSION_BACKGROUND_PAGE_READY, |
| 1118 Source<Extension>(this), | 1092 Source<Extension>(this), |
| 1119 NotificationService::NoDetails()); | 1093 NotificationService::NoDetails()); |
| 1120 } | 1094 } |
| 1121 | 1095 |
| 1122 FilePath Extension::GetIconPath(Icons icon) { | 1096 ExtensionResource Extension::GetIconPath(Icons icon) { |
| 1123 std::map<int, std::string>::const_iterator iter = | 1097 std::map<int, std::string>::const_iterator iter = |
| 1124 icons_.find(Extension::EXTENSION_ICON_LARGE); | 1098 icons_.find(Extension::EXTENSION_ICON_LARGE); |
| 1125 if (iter == icons_.end()) | 1099 if (iter == icons_.end()) |
| 1126 return FilePath(); | 1100 return ExtensionResource(); |
| 1127 return GetResourcePath(iter->second); | 1101 return GetResource(iter->second); |
| 1128 } | 1102 } |
| 1129 | 1103 |
| 1130 bool Extension::CanAccessHost(const GURL& url) const { | 1104 bool Extension::CanAccessHost(const GURL& url) const { |
| 1131 for (HostPermissions::const_iterator host = host_permissions_.begin(); | 1105 for (HostPermissions::const_iterator host = host_permissions_.begin(); |
| 1132 host != host_permissions_.end(); ++host) { | 1106 host != host_permissions_.end(); ++host) { |
| 1133 if (host->MatchesUrl(url)) | 1107 if (host->MatchesUrl(url)) |
| 1134 return true; | 1108 return true; |
| 1135 } | 1109 } |
| 1136 | 1110 |
| 1137 return false; | 1111 return false; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1167 UserScript::PatternList::const_iterator pattern = | 1141 UserScript::PatternList::const_iterator pattern = |
| 1168 content_script->url_patterns().begin(); | 1142 content_script->url_patterns().begin(); |
| 1169 for (; pattern != content_script->url_patterns().end(); ++pattern) { | 1143 for (; pattern != content_script->url_patterns().end(); ++pattern) { |
| 1170 if (pattern->match_subdomains() && pattern->host().empty()) | 1144 if (pattern->match_subdomains() && pattern->host().empty()) |
| 1171 return true; | 1145 return true; |
| 1172 } | 1146 } |
| 1173 } | 1147 } |
| 1174 | 1148 |
| 1175 return false; | 1149 return false; |
| 1176 } | 1150 } |
| OLD | NEW |