summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/extension_resource.cc
diff options
context:
space:
mode:
authormad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-20 15:27:21 +0000
committermad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-20 15:27:21 +0000
commit9194b3fc91953bb1472d8671d2322ac5616ee0b4 (patch)
treed9d60fc09ec8dc5ad6fb65499ad5bcce1d06de0f /chrome/common/extensions/extension_resource.cc
parenta4dc33f2e485e586cc3fb03142c2800249fe9ced (diff)
downloadchromium_src-9194b3fc91953bb1472d8671d2322ac5616ee0b4.zip
chromium_src-9194b3fc91953bb1472d8671d2322ac5616ee0b4.tar.gz
chromium_src-9194b3fc91953bb1472d8671d2322ac5616ee0b4.tar.bz2
Minimize dependency of user scripts.
And made some minor lint fixes and code refactoring on the way, based on CR comments of previous attempt. BUG=none TEST=Make sure that the extension resources can still be properly localized and that they also load correctly when they are not localized. Review URL: http://codereview.chromium.org/267051 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29512 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions/extension_resource.cc')
-rw-r--r--chrome/common/extensions/extension_resource.cc72
1 files changed, 24 insertions, 48 deletions
diff --git a/chrome/common/extensions/extension_resource.cc b/chrome/common/extensions/extension_resource.cc
index 4ae2b0e..b3275cb 100644
--- a/chrome/common/extensions/extension_resource.cc
+++ b/chrome/common/extensions/extension_resource.cc
@@ -21,66 +21,39 @@ ExtensionResource::ExtensionResource(const FilePath& extension_root,
}
const FilePath& ExtensionResource::GetFilePath() const {
- if (extension_root_.empty() || relative_path_.empty())
+ if (extension_root_.empty() || relative_path_.empty()) {
+ DCHECK(full_resource_path_.empty());
return full_resource_path_;
+ }
// We've already checked, just return last value.
if (!full_resource_path_.empty())
return full_resource_path_;
+ full_resource_path_ = GetFilePath(extension_root_, relative_path_);
+ return full_resource_path_;
+}
+
+// Static version...
+FilePath ExtensionResource::GetFilePath(const FilePath& extension_root,
+ const FilePath& relative_path) {
// Stat l10n file, and return new path if it exists.
FilePath l10n_relative_path =
- extension_l10n_util::GetL10nRelativePath(relative_path_);
- full_resource_path_ = CombinePathsSafely(extension_root_, l10n_relative_path);
- if (file_util::PathExists(full_resource_path_)) {
- return full_resource_path_;
+ extension_l10n_util::GetL10nRelativePath(relative_path);
+ FilePath full_path;
+ if (extension_root.AppendAndResolveRelative(l10n_relative_path, &full_path) &&
+ extension_root.IsParent(full_path) &&
+ file_util::PathExists(full_path)) {
+ return full_path;
}
// Fall back to root resource.
- full_resource_path_ = CombinePathsSafely(extension_root_, relative_path_);
- return full_resource_path_;
-}
-
-FilePath ExtensionResource::CombinePathsSafely(
- const FilePath& extension_path,
- const FilePath& relative_resource_path) const {
- // Build up a file:// URL and convert that back to a FilePath. This avoids
- // URL encoding and path separator issues.
-
- // Convert the extension's root to a file:// URL.
- GURL extension_url = net::FilePathToFileURL(extension_path);
- if (!extension_url.is_valid())
- return FilePath();
-
- // Append the requested path.
- std::string relative_path =
- WideToUTF8(relative_resource_path.ToWStringHack());
- GURL::Replacements replacements;
- std::string new_path(extension_url.path());
- new_path += "/";
- new_path += relative_path;
- replacements.SetPathStr(new_path);
- GURL file_url = extension_url.ReplaceComponents(replacements);
- if (!file_url.is_valid())
- return FilePath();
-
- // Convert the result back to a FilePath.
- FilePath ret_val;
- if (!net::FileURLToFilePath(file_url, &ret_val))
- return FilePath();
-
- // Converting the extension_url back to a path removes all .. and . references
- // that may have been in extension_path that would cause isParent to break.
- FilePath sanitized_extension_path;
- if (!net::FileURLToFilePath(extension_url, &sanitized_extension_path))
- return FilePath();
-
- // Double-check that the path we ended up with is actually inside the
- // extension root.
- if (!sanitized_extension_path.IsParent(ret_val))
- return FilePath();
+ if (extension_root.AppendAndResolveRelative(relative_path, &full_path) &&
+ extension_root.IsParent(full_path)) {
+ return full_path;
+ }
- return ret_val;
+ return FilePath();
}
// Unittesting helpers.
@@ -97,6 +70,9 @@ FilePath::StringType ExtensionResource::NormalizeSeperators(
}
bool ExtensionResource::ComparePathWithDefault(const FilePath& path) const {
+ // Make sure we have a cached value to test against...
+ if (full_resource_path_.empty())
+ GetFilePath();
if (NormalizeSeperators(path.value()) ==
NormalizeSeperators(full_resource_path_.value())) {
return true;