OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/catalog/catalog.h" | 5 #include "services/catalog/catalog.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
8 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/path_service.h" | |
12 #include "base/strings/string_split.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "components/filesystem/directory_impl.h" | |
15 #include "components/filesystem/lock_table.h" | |
16 #include "components/filesystem/public/interfaces/types.mojom.h" | |
9 #include "services/catalog/constants.h" | 17 #include "services/catalog/constants.h" |
10 #include "services/catalog/instance.h" | 18 #include "services/catalog/instance.h" |
11 #include "services/catalog/reader.h" | 19 #include "services/catalog/reader.h" |
12 #include "services/shell/public/cpp/connection.h" | 20 #include "services/shell/public/cpp/connection.h" |
13 #include "services/shell/public/cpp/shell_connection.h" | 21 #include "services/shell/public/cpp/shell_connection.h" |
14 | 22 |
15 namespace catalog { | 23 namespace catalog { |
24 namespace { | |
25 | |
26 bool IsPathNameValid(const std::string& name) { | |
27 if (name.empty() || name == "." || name == "..") | |
28 return false; | |
29 | |
30 for (auto c : name) { | |
31 if (!base::IsAsciiAlpha(c) && !base::IsAsciiDigit(c) && | |
32 c != '_' && c != '.') | |
33 return false; | |
34 } | |
35 return true; | |
36 } | |
37 | |
38 base::FilePath GetPathForApplicationName(const std::string& application_name) { | |
39 std::string path = application_name; | |
40 const bool is_mojo = | |
41 base::StartsWith(path, "mojo:", base::CompareCase::INSENSITIVE_ASCII); | |
42 const bool is_exe = | |
43 !is_mojo && | |
44 base::StartsWith(path, "exe:", base::CompareCase::INSENSITIVE_ASCII); | |
45 if (!is_mojo && !is_exe) | |
46 return base::FilePath(); | |
47 if (path.find('.') != std::string::npos) | |
48 return base::FilePath(); | |
49 if (is_mojo) | |
50 path.erase(path.begin(), path.begin() + 5); | |
51 else | |
52 path.erase(path.begin(), path.begin() + 4); | |
53 base::TrimString(path, "/", &path); | |
54 size_t end_of_name = path.find('/'); | |
55 if (end_of_name != std::string::npos) | |
56 path.erase(path.begin() + end_of_name, path.end()); | |
57 | |
58 if (!IsPathNameValid(path)) | |
59 return base::FilePath(); | |
60 | |
61 base::FilePath base_path; | |
62 #if defined(OS_ANDROID) | |
63 PathService::Get(base::DIR_ANDROID_APP_DATA, &base_path); | |
64 // |base_path| on android has an additional path, need to go up a level to get | |
65 // at other apps resources. | |
66 base_path = base_path.DirName(); | |
67 base_path = base_path.AppendASCII("app_cached_apps"); | |
68 #else | |
69 PathService::Get(base::DIR_EXE, &base_path); | |
70 #endif | |
71 // TODO(beng): this won't handle user-specific components. | |
72 return base_path.AppendASCII("Mojo Applications").AppendASCII(path). | |
73 AppendASCII("resources"); | |
74 } | |
75 | |
76 } // namespace | |
16 | 77 |
17 Catalog::Catalog(base::SequencedWorkerPool* worker_pool, | 78 Catalog::Catalog(base::SequencedWorkerPool* worker_pool, |
18 std::unique_ptr<Store> store, | 79 std::unique_ptr<Store> store, |
19 ManifestProvider* manifest_provider) | 80 ManifestProvider* manifest_provider) |
20 : Catalog(std::move(store)) { | 81 : Catalog(std::move(store)) { |
21 system_reader_.reset(new Reader(worker_pool, manifest_provider)); | 82 system_reader_.reset(new Reader(worker_pool, manifest_provider)); |
22 ScanSystemPackageDir(); | 83 ScanSystemPackageDir(); |
23 } | 84 } |
24 | 85 |
25 Catalog::Catalog(base::SingleThreadTaskRunner* task_runner, | 86 Catalog::Catalog(base::SingleThreadTaskRunner* task_runner, |
(...skipping 20 matching lines...) Expand all Loading... | |
46 base::FilePath system_package_dir; | 107 base::FilePath system_package_dir; |
47 PathService::Get(base::DIR_MODULE, &system_package_dir); | 108 PathService::Get(base::DIR_MODULE, &system_package_dir); |
48 system_package_dir = system_package_dir.AppendASCII(kMojoApplicationsDirName); | 109 system_package_dir = system_package_dir.AppendASCII(kMojoApplicationsDirName); |
49 system_reader_->Read(system_package_dir, &system_cache_, | 110 system_reader_->Read(system_package_dir, &system_cache_, |
50 base::Bind(&Catalog::SystemPackageDirScanned, | 111 base::Bind(&Catalog::SystemPackageDirScanned, |
51 weak_factory_.GetWeakPtr())); | 112 weak_factory_.GetWeakPtr())); |
52 } | 113 } |
53 | 114 |
54 bool Catalog::AcceptConnection(shell::Connection* connection) { | 115 bool Catalog::AcceptConnection(shell::Connection* connection) { |
55 connection->AddInterface<mojom::Catalog>(this); | 116 connection->AddInterface<mojom::Catalog>(this); |
117 connection->AddInterface<filesystem::Directory>(this); | |
56 connection->AddInterface<shell::mojom::ShellResolver>(this); | 118 connection->AddInterface<shell::mojom::ShellResolver>(this); |
57 return true; | 119 return true; |
58 } | 120 } |
59 | 121 |
60 void Catalog::Create(shell::Connection* connection, | 122 void Catalog::Create(shell::Connection* connection, |
61 shell::mojom::ShellResolverRequest request) { | 123 shell::mojom::ShellResolverRequest request) { |
62 Instance* instance = | 124 Instance* instance = |
63 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); | 125 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); |
64 instance->BindShellResolver(std::move(request)); | 126 instance->BindShellResolver(std::move(request)); |
65 } | 127 } |
66 | 128 |
67 void Catalog::Create(shell::Connection* connection, | 129 void Catalog::Create(shell::Connection* connection, |
68 mojom::CatalogRequest request) { | 130 mojom::CatalogRequest request) { |
69 Instance* instance = | 131 Instance* instance = |
70 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); | 132 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); |
71 instance->BindCatalog(std::move(request)); | 133 instance->BindCatalog(std::move(request)); |
72 } | 134 } |
73 | 135 |
136 void Catalog::Create(shell::Connection* connection, | |
137 filesystem::DirectoryRequest request) { | |
138 if (!lock_table_) | |
139 lock_table_ = new filesystem::LockTable; | |
140 base::FilePath resources_path = | |
141 GetPathForApplicationName(connection->GetRemoteIdentity().name()); | |
142 new filesystem::DirectoryImpl(std::move(request), resources_path, | |
143 std::unique_ptr<base::ScopedTempDir>(), | |
yzshen1
2016/05/02 21:22:56
After the fix, this line still failed because argu
| |
144 lock_table_); | |
145 } | |
146 | |
74 Instance* Catalog::GetInstanceForUserId(const std::string& user_id) { | 147 Instance* Catalog::GetInstanceForUserId(const std::string& user_id) { |
75 auto it = instances_.find(user_id); | 148 auto it = instances_.find(user_id); |
76 if (it != instances_.end()) | 149 if (it != instances_.end()) |
77 return it->second.get(); | 150 return it->second.get(); |
78 | 151 |
79 // TODO(beng): There needs to be a way to load the store from different users. | 152 // TODO(beng): There needs to be a way to load the store from different users. |
80 Instance* instance = new Instance(std::move(store_), system_reader_.get()); | 153 Instance* instance = new Instance(std::move(store_), system_reader_.get()); |
81 instances_[user_id] = base::WrapUnique(instance); | 154 instances_[user_id] = base::WrapUnique(instance); |
82 if (loaded_) | 155 if (loaded_) |
83 instance->CacheReady(&system_cache_); | 156 instance->CacheReady(&system_cache_); |
84 | 157 |
85 return instance; | 158 return instance; |
86 } | 159 } |
87 | 160 |
88 void Catalog::SystemPackageDirScanned() { | 161 void Catalog::SystemPackageDirScanned() { |
89 loaded_ = true; | 162 loaded_ = true; |
90 for (auto& instance : instances_) | 163 for (auto& instance : instances_) |
91 instance.second->CacheReady(&system_cache_); | 164 instance.second->CacheReady(&system_cache_); |
92 } | 165 } |
93 | 166 |
94 } // namespace catalog | 167 } // namespace catalog |
OLD | NEW |