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 <stddef.h> | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "base/path_service.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "build/build_config.h" | |
14 | |
15 namespace resource_provider { | |
16 namespace { | |
17 | |
18 bool IsPathNameValid(const std::string& name) { | |
19 if (name.empty() || name == "." || name == "..") | |
20 return false; | |
21 | |
22 for (auto c : name) { | |
23 if (!base::IsAsciiAlpha(c) && !base::IsAsciiDigit(c) && | |
24 c != '_' && c != '.') | |
25 return false; | |
26 } | |
27 return true; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 base::FilePath GetPathForApplicationName(const std::string& application_name) { | |
33 std::string path = application_name; | |
34 const bool is_mojo = | |
35 base::StartsWith(path, "mojo:", base::CompareCase::INSENSITIVE_ASCII); | |
36 const bool is_exe = | |
37 !is_mojo && | |
38 base::StartsWith(path, "exe:", base::CompareCase::INSENSITIVE_ASCII); | |
39 if (!is_mojo && !is_exe) | |
40 return base::FilePath(); | |
41 if (path.find('.') != std::string::npos) | |
42 return base::FilePath(); | |
43 if (is_mojo) | |
44 path.erase(path.begin(), path.begin() + 5); | |
45 else | |
46 path.erase(path.begin(), path.begin() + 4); | |
47 base::TrimString(path, "/", &path); | |
48 size_t end_of_name = path.find('/'); | |
49 if (end_of_name != std::string::npos) | |
50 path.erase(path.begin() + end_of_name, path.end()); | |
51 | |
52 if (!IsPathNameValid(path)) | |
53 return base::FilePath(); | |
54 | |
55 base::FilePath base_path; | |
56 #if defined(OS_ANDROID) | |
57 PathService::Get(base::DIR_ANDROID_APP_DATA, &base_path); | |
58 // |base_path| on android has an additional path, need to go up a level to get | |
59 // at other apps resources. | |
60 base_path = base_path.DirName(); | |
61 base_path = base_path.AppendASCII("app_cached_apps"); | |
62 #else | |
63 PathService::Get(base::DIR_EXE, &base_path); | |
64 #endif | |
65 // TODO(beng): this won't handle user-specific components. | |
66 return base_path.AppendASCII("Mojo Applications").AppendASCII(path). | |
67 AppendASCII("resources"); | |
68 } | |
69 | |
70 base::FilePath GetPathForResourceNamed(const base::FilePath& app_path, | |
71 const std::string& resource_path) { | |
72 CHECK(!app_path.empty()); | |
73 | |
74 if (resource_path.empty() || resource_path[0] == '/' || | |
75 resource_path[resource_path.size() - 1] == '/' || | |
76 resource_path.find("//") != std::string::npos) | |
77 return base::FilePath(); | |
78 | |
79 std::vector<std::string> path_components = base::SplitString( | |
80 resource_path, "/", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
81 if (path_components.empty()) | |
82 return base::FilePath(); | |
83 | |
84 base::FilePath result(app_path); | |
85 for (const auto& path_component : path_components) { | |
86 if (!IsPathNameValid(path_component)) | |
87 return base::FilePath(); | |
88 result = result.AppendASCII(path_component); | |
89 } | |
90 return result; | |
91 } | |
92 | |
93 } // namespace resource_provider | |
OLD | NEW |