Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Unified Diff: extensions/common/file_util.cc

Issue 65123002: Move chrome/common/extensions/background_info.h to src/extensions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase2 background_info Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/common/file_util.h ('k') | extensions/common/file_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/common/file_util.cc
diff --git a/extensions/common/file_util.cc b/extensions/common/file_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ba8abe416415831d3d3a7794b6d0b665a48809c1
--- /dev/null
+++ b/extensions/common/file_util.cc
@@ -0,0 +1,61 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "extensions/common/file_util.h"
+
+#include <string>
+
+#include "base/file_util.h"
+#include "base/files/file_path.h"
+#include "net/base/escape.h"
+#include "url/gurl.h"
+
+namespace extensions {
+namespace file_util {
+
+base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) {
+ std::string url_path = url.path();
+ if (url_path.empty() || url_path[0] != '/')
+ return base::FilePath();
+
+ // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8.
+ std::string file_path = net::UnescapeURLComponent(url_path,
+ net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
+ size_t skip = file_path.find_first_not_of("/\\");
+ if (skip != file_path.npos)
+ file_path = file_path.substr(skip);
+
+ base::FilePath path = base::FilePath::FromUTF8Unsafe(file_path);
+
+ // 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 base::FilePath();
+
+ return path;
+}
+
+base::FilePath ExtensionResourceURLToFilePath(const GURL& url,
+ const base::FilePath& root) {
+ std::string host = net::UnescapeURLComponent(url.host(),
+ net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
+ if (host.empty())
+ return base::FilePath();
+
+ base::FilePath relative_path = ExtensionURLToRelativeFilePath(url);
+ if (relative_path.empty())
+ return base::FilePath();
+
+ base::FilePath path = root.AppendASCII(host).Append(relative_path);
+ if (!base::PathExists(path))
+ return base::FilePath();
+ path = base::MakeAbsoluteFilePath(path);
+ if (path.empty() || !root.IsParent(path))
+ return base::FilePath();
+ return path;
+}
+
+} // namespace file_util
+} // namespace extensions
« no previous file with comments | « extensions/common/file_util.h ('k') | extensions/common/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698