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 PathService::Get(base::DIR_EXE, &base_path); |
| 63 // TODO(beng): this won't handle user-specific components. |
| 64 return base_path.AppendASCII(kMojoApplicationsDirName).AppendASCII(path). |
| 65 AppendASCII("resources"); |
| 66 } |
| 67 |
| 68 } // namespace |
16 | 69 |
17 Catalog::Catalog(base::SequencedWorkerPool* worker_pool, | 70 Catalog::Catalog(base::SequencedWorkerPool* worker_pool, |
18 std::unique_ptr<Store> store, | 71 std::unique_ptr<Store> store, |
19 ManifestProvider* manifest_provider) | 72 ManifestProvider* manifest_provider) |
20 : Catalog(std::move(store)) { | 73 : Catalog(std::move(store)) { |
21 system_reader_.reset(new Reader(worker_pool, manifest_provider)); | 74 system_reader_.reset(new Reader(worker_pool, manifest_provider)); |
22 ScanSystemPackageDir(); | 75 ScanSystemPackageDir(); |
23 } | 76 } |
24 | 77 |
25 Catalog::Catalog(base::SingleThreadTaskRunner* task_runner, | 78 Catalog::Catalog(base::SingleThreadTaskRunner* task_runner, |
(...skipping 20 matching lines...) Expand all Loading... |
46 base::FilePath system_package_dir; | 99 base::FilePath system_package_dir; |
47 PathService::Get(base::DIR_MODULE, &system_package_dir); | 100 PathService::Get(base::DIR_MODULE, &system_package_dir); |
48 system_package_dir = system_package_dir.AppendASCII(kMojoApplicationsDirName); | 101 system_package_dir = system_package_dir.AppendASCII(kMojoApplicationsDirName); |
49 system_reader_->Read(system_package_dir, &system_cache_, | 102 system_reader_->Read(system_package_dir, &system_cache_, |
50 base::Bind(&Catalog::SystemPackageDirScanned, | 103 base::Bind(&Catalog::SystemPackageDirScanned, |
51 weak_factory_.GetWeakPtr())); | 104 weak_factory_.GetWeakPtr())); |
52 } | 105 } |
53 | 106 |
54 bool Catalog::AcceptConnection(shell::Connection* connection) { | 107 bool Catalog::AcceptConnection(shell::Connection* connection) { |
55 connection->AddInterface<mojom::Catalog>(this); | 108 connection->AddInterface<mojom::Catalog>(this); |
| 109 connection->AddInterface<filesystem::Directory>(this); |
56 connection->AddInterface<shell::mojom::ShellResolver>(this); | 110 connection->AddInterface<shell::mojom::ShellResolver>(this); |
57 return true; | 111 return true; |
58 } | 112 } |
59 | 113 |
60 void Catalog::Create(shell::Connection* connection, | 114 void Catalog::Create(shell::Connection* connection, |
61 shell::mojom::ShellResolverRequest request) { | 115 shell::mojom::ShellResolverRequest request) { |
62 Instance* instance = | 116 Instance* instance = |
63 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); | 117 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); |
64 instance->BindShellResolver(std::move(request)); | 118 instance->BindShellResolver(std::move(request)); |
65 } | 119 } |
66 | 120 |
67 void Catalog::Create(shell::Connection* connection, | 121 void Catalog::Create(shell::Connection* connection, |
68 mojom::CatalogRequest request) { | 122 mojom::CatalogRequest request) { |
69 Instance* instance = | 123 Instance* instance = |
70 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); | 124 GetInstanceForUserId(connection->GetRemoteIdentity().user_id()); |
71 instance->BindCatalog(std::move(request)); | 125 instance->BindCatalog(std::move(request)); |
72 } | 126 } |
73 | 127 |
| 128 void Catalog::Create(shell::Connection* connection, |
| 129 filesystem::DirectoryRequest request) { |
| 130 if (!lock_table_) |
| 131 lock_table_ = new filesystem::LockTable; |
| 132 base::FilePath resources_path = |
| 133 GetPathForApplicationName(connection->GetRemoteIdentity().name()); |
| 134 new filesystem::DirectoryImpl(std::move(request), resources_path, |
| 135 scoped_refptr<filesystem::SharedTempDir>(), |
| 136 lock_table_); |
| 137 } |
| 138 |
74 Instance* Catalog::GetInstanceForUserId(const std::string& user_id) { | 139 Instance* Catalog::GetInstanceForUserId(const std::string& user_id) { |
75 auto it = instances_.find(user_id); | 140 auto it = instances_.find(user_id); |
76 if (it != instances_.end()) | 141 if (it != instances_.end()) |
77 return it->second.get(); | 142 return it->second.get(); |
78 | 143 |
79 // TODO(beng): There needs to be a way to load the store from different users. | 144 // 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()); | 145 Instance* instance = new Instance(std::move(store_), system_reader_.get()); |
81 instances_[user_id] = base::WrapUnique(instance); | 146 instances_[user_id] = base::WrapUnique(instance); |
82 if (loaded_) | 147 if (loaded_) |
83 instance->CacheReady(&system_cache_); | 148 instance->CacheReady(&system_cache_); |
84 | 149 |
85 return instance; | 150 return instance; |
86 } | 151 } |
87 | 152 |
88 void Catalog::SystemPackageDirScanned() { | 153 void Catalog::SystemPackageDirScanned() { |
89 loaded_ = true; | 154 loaded_ = true; |
90 for (auto& instance : instances_) | 155 for (auto& instance : instances_) |
91 instance.second->CacheReady(&system_cache_); | 156 instance.second->CacheReady(&system_cache_); |
92 } | 157 } |
93 | 158 |
94 } // namespace catalog | 159 } // namespace catalog |
OLD | NEW |