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 <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/base_paths.h" |
8 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
9 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/json/json_reader.h" |
| 14 #include "base/lazy_instance.h" |
10 #include "base/macros.h" | 15 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
12 #include "base/path_service.h" | 17 #include "base/path_service.h" |
13 #include "base/strings/string_split.h" | |
14 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
15 #include "components/filesystem/directory_impl.h" | 19 #include "components/filesystem/directory_impl.h" |
16 #include "components/filesystem/lock_table.h" | 20 #include "components/filesystem/lock_table.h" |
17 #include "components/filesystem/public/interfaces/types.mojom.h" | 21 #include "components/filesystem/public/interfaces/types.mojom.h" |
18 #include "mojo/public/cpp/bindings/strong_binding.h" | 22 #include "mojo/public/cpp/bindings/strong_binding.h" |
19 #include "services/catalog/constants.h" | 23 #include "services/catalog/constants.h" |
20 #include "services/catalog/entry_cache.h" | 24 #include "services/catalog/entry_cache.h" |
21 #include "services/catalog/instance.h" | 25 #include "services/catalog/instance.h" |
22 #include "services/catalog/reader.h" | |
23 #include "services/service_manager/public/cpp/connection.h" | 26 #include "services/service_manager/public/cpp/connection.h" |
24 #include "services/service_manager/public/cpp/interface_registry.h" | 27 #include "services/service_manager/public/cpp/interface_registry.h" |
25 #include "services/service_manager/public/cpp/service_context.h" | 28 #include "services/service_manager/public/cpp/service_context.h" |
26 | 29 |
27 namespace catalog { | 30 namespace catalog { |
| 31 |
28 namespace { | 32 namespace { |
29 | 33 |
30 bool IsPathNameValid(const std::string& name) { | 34 const char kCatalogServicesKey[] = "services"; |
31 if (name.empty() || name == "." || name == "..") | 35 const char kCatalogServiceEmbeddedKey[] = "embedded"; |
32 return false; | 36 const char kCatalogServiceExecutableKey[] = "executable"; |
| 37 const char kCatalogServiceManifestKey[] = "manifest"; |
33 | 38 |
34 for (auto c : name) { | 39 base::LazyInstance<std::unique_ptr<base::Value>> g_default_static_manifest = |
35 if (!base::IsAsciiAlpha(c) && !base::IsAsciiDigit(c) && | 40 LAZY_INSTANCE_INITIALIZER; |
36 c != '_' && c != '.') | 41 |
37 return false; | 42 void LoadCatalogManifestIntoCache(const base::Value* root, EntryCache* cache) { |
| 43 DCHECK(root); |
| 44 const base::DictionaryValue* catalog = nullptr; |
| 45 if (!root->GetAsDictionary(&catalog)) { |
| 46 LOG(ERROR) << "Catalog manifest is not a dictionary value."; |
| 47 return; |
38 } | 48 } |
39 return true; | 49 DCHECK(catalog); |
40 } | |
41 | 50 |
42 base::FilePath GetPathForApplicationName(const std::string& application_name) { | 51 const base::DictionaryValue* services = nullptr; |
43 static const char kServicePrefix[] = ""; | 52 if (!catalog->GetDictionary(kCatalogServicesKey, &services)) { |
44 std::string path = application_name; | 53 LOG(ERROR) << "Catalog manifest \"services\" is not a dictionary value."; |
45 const bool is_service = base::StartsWith( | 54 return; |
46 path, kServicePrefix, base::CompareCase::INSENSITIVE_ASCII); | 55 } |
47 if (!is_service) | |
48 return base::FilePath(); | |
49 if (path.find('.') != std::string::npos) | |
50 return base::FilePath(); | |
51 path.erase(path.begin(), path.begin() + strlen(kServicePrefix)); | |
52 base::TrimString(path, "/", &path); | |
53 size_t end_of_name = path.find('/'); | |
54 if (end_of_name != std::string::npos) | |
55 path.erase(path.begin() + end_of_name, path.end()); | |
56 | 56 |
57 if (!IsPathNameValid(path)) | 57 for (base::DictionaryValue::Iterator it(*services); !it.IsAtEnd(); |
58 return base::FilePath(); | 58 it.Advance()) { |
| 59 const base::DictionaryValue* service_entry = nullptr; |
| 60 if (!it.value().GetAsDictionary(&service_entry)) { |
| 61 LOG(ERROR) << "Catalog service entry for \"" << it.key() |
| 62 << "\" is not a dictionary value."; |
| 63 continue; |
| 64 } |
59 | 65 |
60 base::FilePath base_path; | 66 bool is_embedded = false; |
61 PathService::Get(base::DIR_EXE, &base_path); | 67 service_entry->GetBoolean(kCatalogServiceEmbeddedKey, &is_embedded); |
62 // TODO(beng): this won't handle user-specific components. | 68 |
63 return base_path.AppendASCII(kPackagesDirName).AppendASCII(path). | 69 base::FilePath executable_path; |
64 AppendASCII("resources"); | 70 std::string executable_path_string; |
| 71 if (service_entry->GetString(kCatalogServiceExecutableKey, |
| 72 &executable_path_string)) { |
| 73 base::FilePath exe_dir; |
| 74 CHECK(base::PathService::Get(base::DIR_EXE, &exe_dir)); |
| 75 #if defined(OS_WIN) |
| 76 executable_path_string += ".exe"; |
| 77 base::ReplaceFirstSubstringAfterOffset( |
| 78 &executable_path_string, 0, "@EXE_DIR", |
| 79 base::UTF16ToUTF8(exe_dir.value())); |
| 80 executable_path = |
| 81 base::FilePath(base::UTF8ToUTF16(executable_path_string)); |
| 82 #else |
| 83 base::ReplaceFirstSubstringAfterOffset( |
| 84 &executable_path_string, 0, "@EXE_DIR", exe_dir.value()); |
| 85 executable_path = base::FilePath(executable_path_string); |
| 86 #endif |
| 87 } |
| 88 |
| 89 const base::DictionaryValue* manifest = nullptr; |
| 90 if (!service_entry->GetDictionary(kCatalogServiceManifestKey, &manifest)) { |
| 91 LOG(ERROR) << "Catalog entry for \"" << it.key() << "\" has an invalid " |
| 92 << "\"manifest\" value."; |
| 93 continue; |
| 94 } |
| 95 |
| 96 DCHECK(!(is_embedded && !executable_path.empty())); |
| 97 |
| 98 auto entry = Entry::Deserialize(*manifest); |
| 99 if (entry) { |
| 100 if (!executable_path.empty()) |
| 101 entry->set_path(executable_path); |
| 102 bool added = cache->AddRootEntry(std::move(entry)); |
| 103 DCHECK(added); |
| 104 } else { |
| 105 LOG(ERROR) << "Failed to read manifest entry for \"" << it.key() << "\"."; |
| 106 } |
| 107 } |
65 } | 108 } |
66 | 109 |
67 } // namespace | 110 } // namespace |
68 | 111 |
69 class Catalog::ServiceImpl : public service_manager::Service { | 112 class Catalog::ServiceImpl : public service_manager::Service { |
70 public: | 113 public: |
71 explicit ServiceImpl(Catalog* catalog) : catalog_(catalog) {} | 114 explicit ServiceImpl(Catalog* catalog) : catalog_(catalog) {} |
72 ~ServiceImpl() override {} | 115 ~ServiceImpl() override {} |
73 | 116 |
74 // service_manager::Service: | 117 // service_manager::Service: |
75 bool OnConnect(const service_manager::ServiceInfo& remote_info, | 118 bool OnConnect(const service_manager::ServiceInfo& remote_info, |
76 service_manager::InterfaceRegistry* registry) override { | 119 service_manager::InterfaceRegistry* registry) override { |
77 registry->AddInterface<mojom::Catalog>(catalog_); | 120 registry->AddInterface<mojom::Catalog>(catalog_); |
78 registry->AddInterface<mojom::CatalogControl>(catalog_); | |
79 registry->AddInterface<filesystem::mojom::Directory>(catalog_); | 121 registry->AddInterface<filesystem::mojom::Directory>(catalog_); |
80 registry->AddInterface<service_manager::mojom::Resolver>(catalog_); | 122 registry->AddInterface<service_manager::mojom::Resolver>(catalog_); |
81 return true; | 123 return true; |
82 } | 124 } |
83 | 125 |
84 private: | 126 private: |
85 Catalog* const catalog_; | 127 Catalog* const catalog_; |
86 | 128 |
87 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); | 129 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); |
88 }; | 130 }; |
89 | 131 |
90 Catalog::Catalog(std::unique_ptr<base::Value> static_manifest) : Catalog() { | 132 Catalog::Catalog(std::unique_ptr<base::Value> static_manifest, |
91 system_reader_.reset(new Reader(std::move(static_manifest), | 133 ManifestProvider* service_manifest_provider) |
92 system_cache_.get())); | 134 : service_context_(new service_manager::ServiceContext( |
93 loaded_ = true; | 135 base::MakeUnique<ServiceImpl>(this), |
94 } | 136 service_manager::mojom::ServiceRequest(&service_))), |
95 | 137 service_manifest_provider_(service_manifest_provider), |
96 Catalog::Catalog(base::SequencedWorkerPool* worker_pool, | 138 weak_factory_(this) { |
97 ManifestProvider* manifest_provider) : Catalog() { | 139 if (static_manifest) { |
98 system_reader_.reset(new Reader(worker_pool, manifest_provider)); | 140 LoadCatalogManifestIntoCache(static_manifest.get(), &system_cache_); |
99 ScanSystemPackageDir(); | 141 } else if (g_default_static_manifest.Get()) { |
100 } | 142 LoadCatalogManifestIntoCache( |
101 | 143 g_default_static_manifest.Get().get(), &system_cache_); |
102 Catalog::Catalog(base::SingleThreadTaskRunner* task_runner, | 144 } |
103 ManifestProvider* manifest_provider) : Catalog() { | |
104 system_reader_.reset(new Reader(task_runner, manifest_provider)); | |
105 ScanSystemPackageDir(); | |
106 } | 145 } |
107 | 146 |
108 Catalog::~Catalog() {} | 147 Catalog::~Catalog() {} |
109 | 148 |
110 void Catalog::OverridePackageName(const std::string& service_name, | |
111 const std::string& package_name) { | |
112 system_reader_->OverridePackageName(service_name, package_name); | |
113 } | |
114 | |
115 service_manager::mojom::ServicePtr Catalog::TakeService() { | 149 service_manager::mojom::ServicePtr Catalog::TakeService() { |
116 return std::move(service_); | 150 return std::move(service_); |
117 } | 151 } |
118 | 152 |
119 Catalog::Catalog() : system_cache_(new EntryCache), weak_factory_(this) { | 153 // static |
120 service_context_.reset(new service_manager::ServiceContext( | 154 void Catalog::SetDefaultCatalogManifest( |
121 base::MakeUnique<ServiceImpl>(this), | 155 std::unique_ptr<base::Value> static_manifest) { |
122 service_manager::mojom::ServiceRequest(&service_))); | 156 g_default_static_manifest.Get() = std::move(static_manifest); |
123 } | 157 } |
124 | 158 |
125 void Catalog::ScanSystemPackageDir() { | 159 // static |
126 base::FilePath system_package_dir; | 160 void Catalog::LoadDefaultCatalogManifest(const base::FilePath& path) { |
127 PathService::Get(base::DIR_MODULE, &system_package_dir); | 161 std::string catalog_contents; |
128 system_package_dir = system_package_dir.AppendASCII(kPackagesDirName); | 162 base::FilePath exe_path; |
129 system_reader_->Read(system_package_dir, system_cache_.get(), | 163 base::PathService::Get(base::DIR_EXE, &exe_path); |
130 base::Bind(&Catalog::SystemPackageDirScanned, | 164 base::FilePath catalog_path = exe_path.Append(path); |
131 weak_factory_.GetWeakPtr())); | 165 DCHECK(base::ReadFileToString(catalog_path, &catalog_contents)); |
| 166 std::unique_ptr<base::Value> manifest_value = |
| 167 base::JSONReader::Read(catalog_contents); |
| 168 DCHECK(manifest_value); |
| 169 catalog::Catalog::SetDefaultCatalogManifest(std::move(manifest_value)); |
132 } | 170 } |
133 | 171 |
134 void Catalog::Create(const service_manager::Identity& remote_identity, | 172 void Catalog::Create(const service_manager::Identity& remote_identity, |
135 service_manager::mojom::ResolverRequest request) { | 173 service_manager::mojom::ResolverRequest request) { |
136 Instance* instance = GetInstanceForUserId(remote_identity.user_id()); | 174 Instance* instance = GetInstanceForUserId(remote_identity.user_id()); |
137 instance->BindResolver(std::move(request)); | 175 instance->BindResolver(std::move(request)); |
138 } | 176 } |
139 | 177 |
140 void Catalog::Create(const service_manager::Identity& remote_identity, | 178 void Catalog::Create(const service_manager::Identity& remote_identity, |
141 mojom::CatalogRequest request) { | 179 mojom::CatalogRequest request) { |
142 Instance* instance = GetInstanceForUserId(remote_identity.user_id()); | 180 Instance* instance = GetInstanceForUserId(remote_identity.user_id()); |
143 instance->BindCatalog(std::move(request)); | 181 instance->BindCatalog(std::move(request)); |
144 } | 182 } |
145 | 183 |
146 void Catalog::Create(const service_manager::Identity& remote_identity, | 184 void Catalog::Create(const service_manager::Identity& remote_identity, |
147 filesystem::mojom::DirectoryRequest request) { | 185 filesystem::mojom::DirectoryRequest request) { |
148 if (!lock_table_) | 186 if (!lock_table_) |
149 lock_table_ = new filesystem::LockTable; | 187 lock_table_ = new filesystem::LockTable; |
150 base::FilePath resources_path = | 188 |
151 GetPathForApplicationName(remote_identity.name()); | 189 base::FilePath resources_path; |
| 190 base::PathService::Get(base::DIR_MODULE, &resources_path); |
152 mojo::MakeStrongBinding( | 191 mojo::MakeStrongBinding( |
153 base::MakeUnique<filesystem::DirectoryImpl>( | 192 base::MakeUnique<filesystem::DirectoryImpl>( |
154 resources_path, scoped_refptr<filesystem::SharedTempDir>(), | 193 resources_path, scoped_refptr<filesystem::SharedTempDir>(), |
155 lock_table_), | 194 lock_table_), |
156 std::move(request)); | 195 std::move(request)); |
157 } | 196 } |
158 | 197 |
159 void Catalog::Create(const service_manager::Identity& remote_identity, | |
160 mojom::CatalogControlRequest request) { | |
161 control_bindings_.AddBinding(this, std::move(request)); | |
162 } | |
163 | |
164 void Catalog::OverrideManifestPath( | |
165 const std::string& service_name, | |
166 const base::FilePath& path, | |
167 const OverrideManifestPathCallback& callback) { | |
168 system_reader_->OverrideManifestPath(service_name, path); | |
169 callback.Run(); | |
170 } | |
171 | |
172 Instance* Catalog::GetInstanceForUserId(const std::string& user_id) { | 198 Instance* Catalog::GetInstanceForUserId(const std::string& user_id) { |
173 auto it = instances_.find(user_id); | 199 auto it = instances_.find(user_id); |
174 if (it != instances_.end()) | 200 if (it != instances_.end()) |
175 return it->second.get(); | 201 return it->second.get(); |
176 | 202 |
177 Instance* instance = new Instance(system_reader_.get()); | 203 auto result = instances_.insert(std::make_pair( |
178 instances_[user_id] = base::WrapUnique(instance); | 204 user_id, |
179 if (loaded_) | 205 base::MakeUnique<Instance>(&system_cache_, service_manifest_provider_))); |
180 instance->CacheReady(system_cache_.get()); | 206 return result.first->second.get(); |
181 | |
182 return instance; | |
183 } | |
184 | |
185 void Catalog::SystemPackageDirScanned() { | |
186 loaded_ = true; | |
187 for (auto& instance : instances_) | |
188 instance.second->CacheReady(system_cache_.get()); | |
189 } | 207 } |
190 | 208 |
191 } // namespace catalog | 209 } // namespace catalog |
OLD | NEW |