| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/component_updater/pnacl_component_installer.h" | 5 #include "chrome/browser/component_updater/pnacl_component_installer.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 return base_path.AppendASCII("_platform_specific").AppendASCII(arch); | 88 return base_path.AppendASCII("_platform_specific").AppendASCII(arch); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Tell the rest of the world where to find the platform-specific PNaCl files. | 91 // Tell the rest of the world where to find the platform-specific PNaCl files. |
| 92 void OverrideDirPnaclComponent(const base::FilePath& base_path) { | 92 void OverrideDirPnaclComponent(const base::FilePath& base_path) { |
| 93 PathService::Override(chrome::DIR_PNACL_COMPONENT, GetPlatformDir(base_path)); | 93 PathService::Override(chrome::DIR_PNACL_COMPONENT, GetPlatformDir(base_path)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 bool GetLatestPnaclDirectory(const scoped_refptr<PnaclComponentInstaller>& pci, | 96 bool GetLatestPnaclDirectory(const scoped_refptr<PnaclComponentInstaller>& pci, |
| 97 base::FilePath* latest_dir, | 97 base::FilePath* latest_dir, |
| 98 Version* latest_version, | 98 base::Version* latest_version, |
| 99 std::vector<base::FilePath>* older_dirs) { | 99 std::vector<base::FilePath>* older_dirs) { |
| 100 // Enumerate all versions starting from the base directory. | 100 // Enumerate all versions starting from the base directory. |
| 101 base::FilePath base_dir = pci->GetPnaclBaseDirectory(); | 101 base::FilePath base_dir = pci->GetPnaclBaseDirectory(); |
| 102 bool found = false; | 102 bool found = false; |
| 103 base::FileEnumerator file_enumerator( | 103 base::FileEnumerator file_enumerator( |
| 104 base_dir, false, base::FileEnumerator::DIRECTORIES); | 104 base_dir, false, base::FileEnumerator::DIRECTORIES); |
| 105 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); | 105 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); |
| 106 path = file_enumerator.Next()) { | 106 path = file_enumerator.Next()) { |
| 107 Version version(path.BaseName().MaybeAsASCII()); | 107 base::Version version(path.BaseName().MaybeAsASCII()); |
| 108 if (!version.IsValid()) | 108 if (!version.IsValid()) |
| 109 continue; | 109 continue; |
| 110 if (found) { | 110 if (found) { |
| 111 if (version.CompareTo(*latest_version) > 0) { | 111 if (version.CompareTo(*latest_version) > 0) { |
| 112 older_dirs->push_back(*latest_dir); | 112 older_dirs->push_back(*latest_dir); |
| 113 *latest_dir = path; | 113 *latest_dir = path; |
| 114 *latest_version = version; | 114 *latest_version = version; |
| 115 } else { | 115 } else { |
| 116 older_dirs->push_back(path); | 116 older_dirs->push_back(path); |
| 117 } | 117 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); | 152 unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); |
| 153 if (!base::PathExists(manifest_path)) | 153 if (!base::PathExists(manifest_path)) |
| 154 return NULL; | 154 return NULL; |
| 155 return ReadJSONManifest(manifest_path); | 155 return ReadJSONManifest(manifest_path); |
| 156 } | 156 } |
| 157 | 157 |
| 158 // Check that the component's manifest is for PNaCl, and check the | 158 // Check that the component's manifest is for PNaCl, and check the |
| 159 // PNaCl manifest indicates this is the correct arch-specific package. | 159 // PNaCl manifest indicates this is the correct arch-specific package. |
| 160 bool CheckPnaclComponentManifest(const base::DictionaryValue& manifest, | 160 bool CheckPnaclComponentManifest(const base::DictionaryValue& manifest, |
| 161 const base::DictionaryValue& pnacl_manifest, | 161 const base::DictionaryValue& pnacl_manifest, |
| 162 Version* version_out) { | 162 base::Version* version_out) { |
| 163 // Make sure we have the right |manifest| file. | 163 // Make sure we have the right |manifest| file. |
| 164 std::string name; | 164 std::string name; |
| 165 if (!manifest.GetStringASCII("name", &name)) { | 165 if (!manifest.GetStringASCII("name", &name)) { |
| 166 LOG(WARNING) << "'name' field is missing from manifest!"; | 166 LOG(WARNING) << "'name' field is missing from manifest!"; |
| 167 return false; | 167 return false; |
| 168 } | 168 } |
| 169 // For the webstore, we've given different names to each of the | 169 // For the webstore, we've given different names to each of the |
| 170 // architecture specific packages (and test/QA vs not test/QA) | 170 // architecture specific packages (and test/QA vs not test/QA) |
| 171 // so only part of it is the same. | 171 // so only part of it is the same. |
| 172 if (name.find(kPnaclManifestName) == std::string::npos) { | 172 if (name.find(kPnaclManifestName) == std::string::npos) { |
| 173 LOG(WARNING) << "'name' field in manifest is invalid (" << name | 173 LOG(WARNING) << "'name' field in manifest is invalid (" << name |
| 174 << ") -- missing (" << kPnaclManifestName << ")"; | 174 << ") -- missing (" << kPnaclManifestName << ")"; |
| 175 return false; | 175 return false; |
| 176 } | 176 } |
| 177 | 177 |
| 178 std::string proposed_version; | 178 std::string proposed_version; |
| 179 if (!manifest.GetStringASCII("version", &proposed_version)) { | 179 if (!manifest.GetStringASCII("version", &proposed_version)) { |
| 180 LOG(WARNING) << "'version' field is missing from manifest!"; | 180 LOG(WARNING) << "'version' field is missing from manifest!"; |
| 181 return false; | 181 return false; |
| 182 } | 182 } |
| 183 Version version(proposed_version.c_str()); | 183 base::Version version(proposed_version.c_str()); |
| 184 if (!version.IsValid()) { | 184 if (!version.IsValid()) { |
| 185 LOG(WARNING) << "'version' field in manifest is invalid " | 185 LOG(WARNING) << "'version' field in manifest is invalid " |
| 186 << version.GetString(); | 186 << version.GetString(); |
| 187 return false; | 187 return false; |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Now check the |pnacl_manifest|. | 190 // Now check the |pnacl_manifest|. |
| 191 std::string arch; | 191 std::string arch; |
| 192 if (!pnacl_manifest.GetStringASCII("pnacl-arch", &arch)) { | 192 if (!pnacl_manifest.GetStringASCII("pnacl-arch", &arch)) { |
| 193 LOG(WARNING) << "'pnacl-arch' field is missing from pnacl-manifest!"; | 193 LOG(WARNING) << "'pnacl-arch' field is missing from pnacl-manifest!"; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 227 |
| 228 bool PnaclComponentInstaller::Install(const base::DictionaryValue& manifest, | 228 bool PnaclComponentInstaller::Install(const base::DictionaryValue& manifest, |
| 229 const base::FilePath& unpack_path) { | 229 const base::FilePath& unpack_path) { |
| 230 std::unique_ptr<base::DictionaryValue> pnacl_manifest( | 230 std::unique_ptr<base::DictionaryValue> pnacl_manifest( |
| 231 ReadPnaclManifest(unpack_path)); | 231 ReadPnaclManifest(unpack_path)); |
| 232 if (pnacl_manifest == NULL) { | 232 if (pnacl_manifest == NULL) { |
| 233 LOG(WARNING) << "Failed to read pnacl manifest."; | 233 LOG(WARNING) << "Failed to read pnacl manifest."; |
| 234 return false; | 234 return false; |
| 235 } | 235 } |
| 236 | 236 |
| 237 Version version; | 237 base::Version version; |
| 238 if (!CheckPnaclComponentManifest(manifest, *pnacl_manifest, &version)) { | 238 if (!CheckPnaclComponentManifest(manifest, *pnacl_manifest, &version)) { |
| 239 LOG(WARNING) << "CheckPnaclComponentManifest failed, not installing."; | 239 LOG(WARNING) << "CheckPnaclComponentManifest failed, not installing."; |
| 240 return false; | 240 return false; |
| 241 } | 241 } |
| 242 | 242 |
| 243 // Don't install if the current version is actually newer. | 243 // Don't install if the current version is actually newer. |
| 244 if (current_version().CompareTo(version) > 0) { | 244 if (current_version().CompareTo(version) > 0) { |
| 245 return false; | 245 return false; |
| 246 } | 246 } |
| 247 | 247 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 265 OverrideDirPnaclComponent(path); | 265 OverrideDirPnaclComponent(path); |
| 266 return true; | 266 return true; |
| 267 } | 267 } |
| 268 | 268 |
| 269 // Given |file|, which can be a path like "_platform_specific/arm/pnacl_foo", | 269 // Given |file|, which can be a path like "_platform_specific/arm/pnacl_foo", |
| 270 // returns the assumed install path. The path separator in |file| is '/' | 270 // returns the assumed install path. The path separator in |file| is '/' |
| 271 // for all platforms. Caller is responsible for checking that the | 271 // for all platforms. Caller is responsible for checking that the |
| 272 // |installed_file| actually exists. | 272 // |installed_file| actually exists. |
| 273 bool PnaclComponentInstaller::GetInstalledFile(const std::string& file, | 273 bool PnaclComponentInstaller::GetInstalledFile(const std::string& file, |
| 274 base::FilePath* installed_file) { | 274 base::FilePath* installed_file) { |
| 275 if (current_version() == Version(kNullVersion)) | 275 if (current_version() == base::Version(kNullVersion)) |
| 276 return false; | 276 return false; |
| 277 | 277 |
| 278 *installed_file = GetPnaclBaseDirectory() | 278 *installed_file = GetPnaclBaseDirectory() |
| 279 .AppendASCII(current_version().GetString()) | 279 .AppendASCII(current_version().GetString()) |
| 280 .AppendASCII(file); | 280 .AppendASCII(file); |
| 281 return true; | 281 return true; |
| 282 } | 282 } |
| 283 | 283 |
| 284 bool PnaclComponentInstaller::Uninstall() { | 284 bool PnaclComponentInstaller::Uninstall() { |
| 285 return false; | 285 return false; |
| 286 } | 286 } |
| 287 | 287 |
| 288 CrxComponent PnaclComponentInstaller::GetCrxComponent() { | 288 CrxComponent PnaclComponentInstaller::GetCrxComponent() { |
| 289 CrxComponent pnacl_component; | 289 CrxComponent pnacl_component; |
| 290 pnacl_component.version = current_version(); | 290 pnacl_component.version = current_version(); |
| 291 pnacl_component.name = "pnacl"; | 291 pnacl_component.name = "pnacl"; |
| 292 pnacl_component.installer = this; | 292 pnacl_component.installer = this; |
| 293 pnacl_component.fingerprint = current_fingerprint(); | 293 pnacl_component.fingerprint = current_fingerprint(); |
| 294 SetPnaclHash(&pnacl_component); | 294 SetPnaclHash(&pnacl_component); |
| 295 | 295 |
| 296 return pnacl_component; | 296 return pnacl_component; |
| 297 } | 297 } |
| 298 | 298 |
| 299 namespace { | 299 namespace { |
| 300 | 300 |
| 301 void FinishPnaclUpdateRegistration( | 301 void FinishPnaclUpdateRegistration( |
| 302 const Version& current_version, | 302 const base::Version& current_version, |
| 303 const std::string& current_fingerprint, | 303 const std::string& current_fingerprint, |
| 304 const scoped_refptr<PnaclComponentInstaller>& pci) { | 304 const scoped_refptr<PnaclComponentInstaller>& pci) { |
| 305 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 305 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 306 pci->set_current_version(current_version); | 306 pci->set_current_version(current_version); |
| 307 CheckVersionCompatiblity(current_version); | 307 CheckVersionCompatiblity(current_version); |
| 308 pci->set_current_fingerprint(current_fingerprint); | 308 pci->set_current_fingerprint(current_fingerprint); |
| 309 CrxComponent pnacl_component = pci->GetCrxComponent(); | 309 CrxComponent pnacl_component = pci->GetCrxComponent(); |
| 310 | 310 |
| 311 if (!pci->cus()->RegisterComponent(pnacl_component)) | 311 if (!pci->cus()->RegisterComponent(pnacl_component)) |
| 312 NOTREACHED() << "Pnacl component registration failed."; | 312 NOTREACHED() << "Pnacl component registration failed."; |
| 313 } | 313 } |
| 314 | 314 |
| 315 // Check if there is an existing version on disk first to know when | 315 // Check if there is an existing version on disk first to know when |
| 316 // a hosted version is actually newer. | 316 // a hosted version is actually newer. |
| 317 void StartPnaclUpdateRegistration( | 317 void StartPnaclUpdateRegistration( |
| 318 const scoped_refptr<PnaclComponentInstaller>& pci) { | 318 const scoped_refptr<PnaclComponentInstaller>& pci) { |
| 319 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 319 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 320 base::FilePath path = pci->GetPnaclBaseDirectory(); | 320 base::FilePath path = pci->GetPnaclBaseDirectory(); |
| 321 if (!base::PathExists(path)) { | 321 if (!base::PathExists(path)) { |
| 322 if (!base::CreateDirectory(path)) { | 322 if (!base::CreateDirectory(path)) { |
| 323 NOTREACHED() << "Could not create base Pnacl directory."; | 323 NOTREACHED() << "Could not create base Pnacl directory."; |
| 324 return; | 324 return; |
| 325 } | 325 } |
| 326 } | 326 } |
| 327 | 327 |
| 328 Version current_version(kNullVersion); | 328 base::Version current_version(kNullVersion); |
| 329 std::string current_fingerprint; | 329 std::string current_fingerprint; |
| 330 std::vector<base::FilePath> older_dirs; | 330 std::vector<base::FilePath> older_dirs; |
| 331 if (GetLatestPnaclDirectory(pci, &path, ¤t_version, &older_dirs)) { | 331 if (GetLatestPnaclDirectory(pci, &path, ¤t_version, &older_dirs)) { |
| 332 std::unique_ptr<base::DictionaryValue> manifest( | 332 std::unique_ptr<base::DictionaryValue> manifest( |
| 333 ReadComponentManifest(path)); | 333 ReadComponentManifest(path)); |
| 334 std::unique_ptr<base::DictionaryValue> pnacl_manifest( | 334 std::unique_ptr<base::DictionaryValue> pnacl_manifest( |
| 335 ReadPnaclManifest(path)); | 335 ReadPnaclManifest(path)); |
| 336 Version manifest_version; | 336 base::Version manifest_version; |
| 337 // Check that the component manifest and PNaCl manifest files | 337 // Check that the component manifest and PNaCl manifest files |
| 338 // are legit, and that the indicated version matches the one | 338 // are legit, and that the indicated version matches the one |
| 339 // encoded within the path name. | 339 // encoded within the path name. |
| 340 if (manifest == NULL || pnacl_manifest == NULL || | 340 if (manifest == NULL || pnacl_manifest == NULL || |
| 341 !CheckPnaclComponentManifest(*manifest, | 341 !CheckPnaclComponentManifest(*manifest, |
| 342 *pnacl_manifest, | 342 *pnacl_manifest, |
| 343 &manifest_version) || | 343 &manifest_version) || |
| 344 current_version != manifest_version) { | 344 current_version != manifest_version) { |
| 345 current_version = Version(kNullVersion); | 345 current_version = base::Version(kNullVersion); |
| 346 } else { | 346 } else { |
| 347 OverrideDirPnaclComponent(path); | 347 OverrideDirPnaclComponent(path); |
| 348 base::ReadFileToString(path.AppendASCII("manifest.fingerprint"), | 348 base::ReadFileToString(path.AppendASCII("manifest.fingerprint"), |
| 349 ¤t_fingerprint); | 349 ¤t_fingerprint); |
| 350 } | 350 } |
| 351 } | 351 } |
| 352 | 352 |
| 353 BrowserThread::PostTask(BrowserThread::UI, | 353 BrowserThread::PostTask(BrowserThread::UI, |
| 354 FROM_HERE, | 354 FROM_HERE, |
| 355 base::Bind(&FinishPnaclUpdateRegistration, | 355 base::Bind(&FinishPnaclUpdateRegistration, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 378 } // namespace component_updater | 378 } // namespace component_updater |
| 379 | 379 |
| 380 namespace pnacl { | 380 namespace pnacl { |
| 381 | 381 |
| 382 bool NeedsOnDemandUpdate() { | 382 bool NeedsOnDemandUpdate() { |
| 383 return base::subtle::NoBarrier_Load( | 383 return base::subtle::NoBarrier_Load( |
| 384 &component_updater::needs_on_demand_update) != 0; | 384 &component_updater::needs_on_demand_update) != 0; |
| 385 } | 385 } |
| 386 | 386 |
| 387 } // namespace pnacl | 387 } // namespace pnacl |
| OLD | NEW |