diff options
author | erikkay@chromium.org <erikkay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-09 20:04:13 +0000 |
---|---|---|
committer | erikkay@chromium.org <erikkay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-09 20:04:13 +0000 |
commit | cf59e2dae675b777ce193268ec4040de63e4b4f1 (patch) | |
tree | 536d359c5465710330aa30cb2c325defa1af3913 | |
parent | 9b43769a306eb33fee4b934e0f98957892950bf9 (diff) | |
download | chromium_src-cf59e2dae675b777ce193268ec4040de63e4b4f1.zip chromium_src-cf59e2dae675b777ce193268ec4040de63e4b4f1.tar.gz chromium_src-cf59e2dae675b777ce193268ec4040de63e4b4f1.tar.bz2 |
relative URL handling improvements
BUG=54715
TEST=ExtensionFileUtil.ExtensionURLToRelativeFilePath
Review URL: http://codereview.chromium.org/3332012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58984 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/common/extensions/extension_file_util.cc | 23 | ||||
-rw-r--r-- | chrome/common/extensions/extension_file_util_unittest.cc | 15 | ||||
-rw-r--r-- | chrome/renderer/resources/renderer_extension_bindings.js | 5 |
3 files changed, 37 insertions, 6 deletions
diff --git a/chrome/common/extensions/extension_file_util.cc b/chrome/common/extensions/extension_file_util.cc index b143f7f..4967916e 100644 --- a/chrome/common/extensions/extension_file_util.cc +++ b/chrome/common/extensions/extension_file_util.cc @@ -492,15 +492,30 @@ FilePath ExtensionURLToRelativeFilePath(const GURL& url) { if (url_path.empty() || url_path[0] != '/') return FilePath(); - // Drop the leading slash and convert %-encoded UTF8 to regular UTF8. - std::string file_path = UnescapeURLComponent(url_path.substr(1), + // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8. + std::string file_path = UnescapeURLComponent(url_path, UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); + size_t skip = file_path.find_first_not_of("/\\"); + if (skip != file_path.npos) + file_path = file_path.substr(skip); + FilePath path = #if defined(OS_POSIX) - return FilePath(file_path); + FilePath(file_path); #elif defined(OS_WIN) - return FilePath(UTF8ToWide(file_path)); + FilePath(UTF8ToWide(file_path)); +#else + FilePath(); + NOTIMPLEMENTED(); #endif + + // It's still possible for someone to construct an annoying URL whose path + // would still wind up not being considered relative at this point. + // For example: chrome-extension://id/c:////foo.html + if (path.IsAbsolute()) + return FilePath(); + + return path; } } // namespace extension_file_util diff --git a/chrome/common/extensions/extension_file_util_unittest.cc b/chrome/common/extensions/extension_file_util_unittest.cc index 9fc9d84..d5f08b6 100644 --- a/chrome/common/extensions/extension_file_util_unittest.cc +++ b/chrome/common/extensions/extension_file_util_unittest.cc @@ -207,6 +207,18 @@ TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) { "escape spaces.html" }, { URL_PREFIX "%C3%9Cber.html", "\xC3\x9C" "ber.html" }, +#if defined(OS_WIN) + { URL_PREFIX "C%3A/simple.html", + "" }, +#endif + { URL_PREFIX "////simple.html", + "simple.html" }, + { URL_PREFIX "/simple.html", + "simple.html" }, + { URL_PREFIX "\\simple.html", + "simple.html" }, + { URL_PREFIX "\\\\foo\\simple.html", + "foo/simple.html" }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { @@ -221,7 +233,8 @@ TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) { extension_file_util::ExtensionURLToRelativeFilePath(url); EXPECT_FALSE(actual_path.IsAbsolute()) << " For the path " << actual_path.value(); - EXPECT_EQ(expected_path.value(), actual_path.value()); + EXPECT_EQ(expected_path.value(), actual_path.value()) << + " For the path " << url; } } diff --git a/chrome/renderer/resources/renderer_extension_bindings.js b/chrome/renderer/resources/renderer_extension_bindings.js index f7955d1..90a68bc 100644 --- a/chrome/renderer/resources/renderer_extension_bindings.js +++ b/chrome/renderer/resources/renderer_extension_bindings.js @@ -204,7 +204,10 @@ var chrome = chrome || {}; // Returns a resource URL that can be used to fetch a resource from this // extension. chrome.extension.getURL = function(path) { - return "chrome-extension://" + extensionId + "/" + path; + path = String(path); + if (!path.length || path[0] != "/") + path = "/" + path; + return "chrome-extension://" + extensionId + path; }; chrome.i18n = chrome.i18n || {}; |