| 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/reader.h" | 5 #include "services/catalog/reader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 13 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 14 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 15 #include "services/catalog/constants.h" | 16 #include "services/catalog/constants.h" |
| 16 #include "services/catalog/entry.h" | 17 #include "services/catalog/entry.h" |
| 17 #include "services/catalog/manifest_provider.h" | 18 #include "services/catalog/manifest_provider.h" |
| 18 #include "services/shell/public/cpp/names.h" | 19 #include "services/shell/public/cpp/names.h" |
| 19 | 20 |
| 20 namespace catalog { | 21 namespace catalog { |
| 21 namespace { | 22 namespace { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 47 #if defined OS_WIN | 48 #if defined OS_WIN |
| 48 std::string extension = ".exe"; | 49 std::string extension = ".exe"; |
| 49 #else | 50 #else |
| 50 std::string extension; | 51 std::string extension; |
| 51 #endif | 52 #endif |
| 52 return package_dir.AppendASCII(shell::GetNamePath(name) + extension); | 53 return package_dir.AppendASCII(shell::GetNamePath(name) + extension); |
| 53 } | 54 } |
| 54 return base::FilePath(); | 55 return base::FilePath(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 scoped_ptr<Entry> ProcessManifest(scoped_ptr<base::Value> manifest_root, | 58 std::unique_ptr<Entry> ProcessManifest( |
| 58 const base::FilePath& package_dir) { | 59 std::unique_ptr<base::Value> manifest_root, |
| 60 const base::FilePath& package_dir) { |
| 59 // Manifest was malformed or did not exist. | 61 // Manifest was malformed or did not exist. |
| 60 if (!manifest_root) | 62 if (!manifest_root) |
| 61 return nullptr; | 63 return nullptr; |
| 62 | 64 |
| 63 const base::DictionaryValue* dictionary = nullptr; | 65 const base::DictionaryValue* dictionary = nullptr; |
| 64 if (!manifest_root->GetAsDictionary(&dictionary)) | 66 if (!manifest_root->GetAsDictionary(&dictionary)) |
| 65 return nullptr; | 67 return nullptr; |
| 66 | 68 |
| 67 scoped_ptr<Entry> entry = Entry::Deserialize(*dictionary); | 69 std::unique_ptr<Entry> entry = Entry::Deserialize(*dictionary); |
| 68 if (!entry) | 70 if (!entry) |
| 69 return nullptr; | 71 return nullptr; |
| 70 entry->set_path(GetPackagePath(package_dir, entry->name())); | 72 entry->set_path(GetPackagePath(package_dir, entry->name())); |
| 71 return entry; | 73 return entry; |
| 72 } | 74 } |
| 73 | 75 |
| 74 scoped_ptr<Entry> CreateEntryForManifestAt( | 76 std::unique_ptr<Entry> CreateEntryForManifestAt( |
| 75 const base::FilePath& manifest_path, | 77 const base::FilePath& manifest_path, |
| 76 const base::FilePath& package_dir) { | 78 const base::FilePath& package_dir) { |
| 77 JSONFileValueDeserializer deserializer(manifest_path); | 79 JSONFileValueDeserializer deserializer(manifest_path); |
| 78 int error = 0; | 80 int error = 0; |
| 79 std::string message; | 81 std::string message; |
| 80 | 82 |
| 81 // TODO(beng): probably want to do more detailed error checking. This should | 83 // TODO(beng): probably want to do more detailed error checking. This should |
| 82 // be done when figuring out if to unblock connection completion. | 84 // be done when figuring out if to unblock connection completion. |
| 83 return ProcessManifest(deserializer.Deserialize(&error, &message), | 85 return ProcessManifest(deserializer.Deserialize(&error, &message), |
| 84 package_dir); | 86 package_dir); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void ScanDir( | 89 void ScanDir( |
| 88 const base::FilePath& package_dir, | 90 const base::FilePath& package_dir, |
| 89 const Reader::ReadManifestCallback& read_manifest_callback, | 91 const Reader::ReadManifestCallback& read_manifest_callback, |
| 90 scoped_refptr<base::SingleThreadTaskRunner> original_thread_task_runner, | 92 scoped_refptr<base::SingleThreadTaskRunner> original_thread_task_runner, |
| 91 const base::Closure& read_complete_closure) { | 93 const base::Closure& read_complete_closure) { |
| 92 base::FileEnumerator enumerator(package_dir, false, | 94 base::FileEnumerator enumerator(package_dir, false, |
| 93 base::FileEnumerator::DIRECTORIES); | 95 base::FileEnumerator::DIRECTORIES); |
| 94 while (1) { | 96 while (1) { |
| 95 base::FilePath path = enumerator.Next(); | 97 base::FilePath path = enumerator.Next(); |
| 96 if (path.empty()) | 98 if (path.empty()) |
| 97 break; | 99 break; |
| 98 base::FilePath manifest_path = path.AppendASCII("manifest.json"); | 100 base::FilePath manifest_path = path.AppendASCII("manifest.json"); |
| 99 scoped_ptr<Entry> entry = | 101 std::unique_ptr<Entry> entry = |
| 100 CreateEntryForManifestAt(manifest_path, package_dir); | 102 CreateEntryForManifestAt(manifest_path, package_dir); |
| 101 if (!entry) | 103 if (!entry) |
| 102 continue; | 104 continue; |
| 103 | 105 |
| 104 // Skip over subdirs that contain only manifests, they're artifacts of the | 106 // Skip over subdirs that contain only manifests, they're artifacts of the |
| 105 // build (e.g. for applications that are packaged into others) and are not | 107 // build (e.g. for applications that are packaged into others) and are not |
| 106 // valid standalone packages. | 108 // valid standalone packages. |
| 107 base::FilePath package_path = GetPackagePath(package_dir, entry->name()); | 109 base::FilePath package_path = GetPackagePath(package_dir, entry->name()); |
| 108 if (!base::PathExists(package_path)) | 110 if (!base::PathExists(package_path)) |
| 109 continue; | 111 continue; |
| 110 | 112 |
| 111 original_thread_task_runner->PostTask( | 113 original_thread_task_runner->PostTask( |
| 112 FROM_HERE, | 114 FROM_HERE, |
| 113 base::Bind(read_manifest_callback, base::Passed(&entry))); | 115 base::Bind(read_manifest_callback, base::Passed(&entry))); |
| 114 } | 116 } |
| 115 | 117 |
| 116 original_thread_task_runner->PostTask(FROM_HERE, read_complete_closure); | 118 original_thread_task_runner->PostTask(FROM_HERE, read_complete_closure); |
| 117 } | 119 } |
| 118 | 120 |
| 119 scoped_ptr<Entry> ReadManifest(const base::FilePath& package_dir, | 121 std::unique_ptr<Entry> ReadManifest(const base::FilePath& package_dir, |
| 120 const std::string& mojo_name) { | 122 const std::string& mojo_name) { |
| 121 scoped_ptr<Entry> entry = CreateEntryForManifestAt( | 123 std::unique_ptr<Entry> entry = CreateEntryForManifestAt( |
| 122 GetManifestPath(package_dir, mojo_name), package_dir); | 124 GetManifestPath(package_dir, mojo_name), package_dir); |
| 123 if (!entry) { | 125 if (!entry) { |
| 124 entry.reset(new Entry(mojo_name)); | 126 entry.reset(new Entry(mojo_name)); |
| 125 entry->set_path(GetPackagePath( | 127 entry->set_path(GetPackagePath( |
| 126 package_dir.AppendASCII(kMojoApplicationsDirName), mojo_name)); | 128 package_dir.AppendASCII(kMojoApplicationsDirName), mojo_name)); |
| 127 } | 129 } |
| 128 return entry; | 130 return entry; |
| 129 } | 131 } |
| 130 | 132 |
| 131 void AddEntryToCache(EntryCache* cache, scoped_ptr<Entry> entry) { | 133 void AddEntryToCache(EntryCache* cache, std::unique_ptr<Entry> entry) { |
| 132 for (auto child : entry->applications()) | 134 for (auto child : entry->applications()) |
| 133 AddEntryToCache(cache, make_scoped_ptr(child)); | 135 AddEntryToCache(cache, base::WrapUnique(child)); |
| 134 (*cache)[entry->name()] = std::move(entry); | 136 (*cache)[entry->name()] = std::move(entry); |
| 135 } | 137 } |
| 136 | 138 |
| 137 void DoNothing(shell::mojom::ResolveResultPtr) {} | 139 void DoNothing(shell::mojom::ResolveResultPtr) {} |
| 138 | 140 |
| 139 } // namespace | 141 } // namespace |
| 140 | 142 |
| 141 Reader::Reader(base::TaskRunner* file_task_runner, | 143 Reader::Reader(base::TaskRunner* file_task_runner, |
| 142 ManifestProvider* manifest_provider) | 144 ManifestProvider* manifest_provider) |
| 143 : file_task_runner_(file_task_runner), | 145 : file_task_runner_(file_task_runner), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 160 } | 162 } |
| 161 | 163 |
| 162 void Reader::CreateEntryForName( | 164 void Reader::CreateEntryForName( |
| 163 const std::string& mojo_name, | 165 const std::string& mojo_name, |
| 164 EntryCache* cache, | 166 EntryCache* cache, |
| 165 const CreateEntryForNameCallback& entry_created_callback) { | 167 const CreateEntryForNameCallback& entry_created_callback) { |
| 166 std::string manifest_contents; | 168 std::string manifest_contents; |
| 167 if (manifest_provider_ && | 169 if (manifest_provider_ && |
| 168 manifest_provider_->GetApplicationManifest(mojo_name, | 170 manifest_provider_->GetApplicationManifest(mojo_name, |
| 169 &manifest_contents)) { | 171 &manifest_contents)) { |
| 170 scoped_ptr<base::Value> manifest_root = | 172 std::unique_ptr<base::Value> manifest_root = |
| 171 base::JSONReader::Read(manifest_contents); | 173 base::JSONReader::Read(manifest_contents); |
| 172 base::PostTaskAndReplyWithResult( | 174 base::PostTaskAndReplyWithResult( |
| 173 file_task_runner_, FROM_HERE, | 175 file_task_runner_, FROM_HERE, |
| 174 base::Bind(&ProcessManifest, base::Passed(&manifest_root), | 176 base::Bind(&ProcessManifest, base::Passed(&manifest_root), |
| 175 system_package_dir_), | 177 system_package_dir_), |
| 176 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), | 178 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 177 cache, entry_created_callback)); | 179 cache, entry_created_callback)); |
| 178 } else { | 180 } else { |
| 179 base::PostTaskAndReplyWithResult( | 181 base::PostTaskAndReplyWithResult( |
| 180 file_task_runner_, FROM_HERE, | 182 file_task_runner_, FROM_HERE, |
| 181 base::Bind(&ReadManifest, system_package_dir_, mojo_name), | 183 base::Bind(&ReadManifest, system_package_dir_, mojo_name), |
| 182 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), | 184 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 183 cache, entry_created_callback)); | 185 cache, entry_created_callback)); |
| 184 | 186 |
| 185 } | 187 } |
| 186 } | 188 } |
| 187 | 189 |
| 188 void Reader::OnReadManifest( | 190 void Reader::OnReadManifest( |
| 189 EntryCache* cache, | 191 EntryCache* cache, |
| 190 const CreateEntryForNameCallback& entry_created_callback, | 192 const CreateEntryForNameCallback& entry_created_callback, |
| 191 scoped_ptr<Entry> entry) { | 193 std::unique_ptr<Entry> entry) { |
| 192 shell::mojom::ResolveResultPtr result = | 194 shell::mojom::ResolveResultPtr result = |
| 193 shell::mojom::ResolveResult::From(*entry); | 195 shell::mojom::ResolveResult::From(*entry); |
| 194 AddEntryToCache(cache, std::move(entry)); | 196 AddEntryToCache(cache, std::move(entry)); |
| 195 entry_created_callback.Run(std::move(result)); | 197 entry_created_callback.Run(std::move(result)); |
| 196 } | 198 } |
| 197 | 199 |
| 198 } // namespace catalog | 200 } // namespace catalog |
| OLD | NEW |