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

Unified Diff: components/resource_provider/file_utils.cc

Issue 1108403008: Adds resource_provider::ResourceProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 5 years, 8 months 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 | « components/resource_provider/file_utils.h ('k') | components/resource_provider/file_utils_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/resource_provider/file_utils.cc
diff --git a/components/resource_provider/file_utils.cc b/components/resource_provider/file_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..468d27ef87a7a3c7e08a41c3b54c1dd81999103d
--- /dev/null
+++ b/components/resource_provider/file_utils.cc
@@ -0,0 +1,66 @@
+// Copyright 2015 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 "components/resource_provider/file_utils.h"
+
+#include "base/files/file_path.h"
+#include "base/path_service.h"
+#include "base/strings/string_util.h"
+#include "url/gurl.h"
+
+namespace resource_provider {
+namespace {
+
+bool IsPathNameValid(const std::string& name) {
+ if (name.empty() || name == "." || name == "..")
+ return false;
+
+ for (auto c : name) {
+ if (!IsAsciiAlpha(c) && !IsAsciiDigit(c) && c != '_' && c != '.')
+ return false;
+ }
+ return true;
+}
+
+} // namespace
+
+base::FilePath GetPathForApplicationUrl(const GURL& application_url) {
+ if (application_url.scheme() != "mojo")
+ return base::FilePath();
+
+ std::string path = application_url.path();
+ base::TrimString(path, "/", &path);
+
+ if (!IsPathNameValid(path))
+ return base::FilePath();
+
+ base::FilePath base_path;
+ PathService::Get(base::DIR_EXE, &base_path);
+ return base_path.AppendASCII(path).AppendASCII("resources");
+}
+
+base::FilePath GetPathForResourceNamed(const base::FilePath& app_path,
+ const std::string& resource_path) {
+ CHECK(!app_path.empty());
+
+ if (resource_path.empty() || resource_path[0] == '/' ||
+ resource_path.back() == '/' ||
+ resource_path.find("//") != std::string::npos)
+ return base::FilePath();
+
+ std::vector<std::string> path_components;
+ Tokenize(resource_path, "/", &path_components);
+ if (path_components.empty())
+ return base::FilePath();
+
+ base::FilePath result(app_path);
+ for (const auto& path_component : path_components) {
+ if (!IsPathNameValid(path_component))
+ return base::FilePath();
+ result = result.AppendASCII(path_component);
+ }
+ return result;
+}
+
+} // namespace resource_provider
« no previous file with comments | « components/resource_provider/file_utils.h ('k') | components/resource_provider/file_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698