OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/resource_provider/file_utils.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/path_service.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "url/gurl.h" | |
11 | |
12 namespace resource_provider { | |
13 namespace { | |
14 | |
15 bool IsPathNameValid(const std::string& name) { | |
16 if (name.empty() || name == "." || name == "..") | |
17 return false; | |
18 | |
19 for (auto c : name) { | |
20 if (!IsAsciiAlpha(c) && !IsAsciiDigit(c) && c != '_' && c != '.') | |
21 return false; | |
22 } | |
23 return true; | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 base::FilePath GetPathForApplicationUrl(const GURL& application_url) { | |
29 if (application_url.scheme() != "mojo") | |
30 return base::FilePath(); | |
31 | |
32 std::string path = application_url.path(); | |
33 base::TrimString(path, "/", &path); | |
34 | |
35 if (!IsPathNameValid(path)) | |
36 return base::FilePath(); | |
37 | |
38 base::FilePath base_path; | |
39 PathService::Get(base::DIR_EXE, &base_path); | |
40 return base_path.AppendASCII(path).AppendASCII("resources"); | |
41 } | |
42 | |
43 base::FilePath GetPathForResourceNamed(const base::FilePath& app_path, | |
44 const std::string& resource_path) { | |
45 CHECK(!app_path.empty()); | |
46 | |
47 if (resource_path.empty() || resource_path[0] == '/' || | |
48 resource_path.back() == '/' || | |
49 resource_path.find("//") != std::string::npos) | |
50 return base::FilePath(); | |
51 | |
52 std::vector<std::string> path_components; | |
53 Tokenize(resource_path, "/", &path_components); | |
54 if (path_components.empty()) | |
55 return base::FilePath(); | |
56 | |
57 base::FilePath result(app_path); | |
58 for (const auto& path_component : path_components) { | |
59 if (!IsPathNameValid(path_component)) | |
60 return base::FilePath(); | |
61 result = result.AppendASCII(path_component); | |
62 } | |
63 return result; | |
64 } | |
65 | |
66 } // namespace resource_provider | |
OLD | NEW |