Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "native_client/src/trusted/plugin/json_manifest.h" | 9 #include "native_client/src/trusted/plugin/json_manifest.h" |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 // ISA Dictionary keys | 32 // ISA Dictionary keys |
| 33 const char* const kX8632Key = "x86-32"; | 33 const char* const kX8632Key = "x86-32"; |
| 34 const char* const kX8664Key = "x86-64"; | 34 const char* const kX8664Key = "x86-64"; |
| 35 const char* const kArmKey = "arm"; | 35 const char* const kArmKey = "arm"; |
| 36 const char* const kPortableKey = "portable"; | 36 const char* const kPortableKey = "portable"; |
| 37 | 37 |
| 38 // Url Resolution keys | 38 // Url Resolution keys |
| 39 const char* const kPnaclTranslateKey = "pnacl-translate"; | 39 const char* const kPnaclTranslateKey = "pnacl-translate"; |
| 40 const char* const kUrlKey = "url"; | 40 const char* const kUrlKey = "url"; |
| 41 | 41 |
| 42 // Cache support keys | |
| 43 const char* const kCacheIdentityKey = "sha256"; | |
| 44 | |
| 42 // Sample manifest file: | 45 // Sample manifest file: |
| 43 // { | 46 // { |
| 44 // "program": { | 47 // "program": { |
| 45 // "x86-32": {"url": "myprogram_x86-32.nexe"}, | 48 // "x86-32": {"url": "myprogram_x86-32.nexe"}, |
| 46 // "x86-64": {"url": "myprogram_x86-64.nexe"}, | 49 // "x86-64": {"url": "myprogram_x86-64.nexe"}, |
| 47 // "arm": {"url": "myprogram_arm.nexe"}, | 50 // "arm": {"url": "myprogram_arm.nexe"}, |
| 48 // "portable": {"pnacl-translate": {"url": "myprogram.pexe"} } | 51 // "portable": { |
| 52 // "pnacl-translate": { | |
| 53 // "url": "myprogram.pexe", | |
| 54 // "sha256": "..." | |
| 55 // } | |
| 56 // } | |
| 49 // }, | 57 // }, |
| 50 // "interpreter": { | 58 // "interpreter": { |
| 51 // "x86-32": {"url": "interpreter_x86-32.nexe"}, | 59 // "x86-32": {"url": "interpreter_x86-32.nexe"}, |
| 52 // "x86-64": {"url": "interpreter_x86-64.nexe"}, | 60 // "x86-64": {"url": "interpreter_x86-64.nexe"}, |
| 53 // "arm": {"url": "interpreter_arm.nexe"} | 61 // "arm": {"url": "interpreter_arm.nexe"} |
| 54 // }, | 62 // }, |
| 55 // "files": { | 63 // "files": { |
| 56 // "foo.txt": { | 64 // "foo.txt": { |
| 57 // "portable": {"url": "foo.txt"} | 65 // "portable": {"url": "foo.txt"} |
| 58 // }, | 66 // }, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 return true; | 138 return true; |
| 131 } | 139 } |
| 132 | 140 |
| 133 // Validate a "url" dictionary assuming it was resolved from container_key. | 141 // Validate a "url" dictionary assuming it was resolved from container_key. |
| 134 // E.g., "container_key" : { "url": "foo.txt" } | 142 // E.g., "container_key" : { "url": "foo.txt" } |
| 135 bool IsValidUrlSpec(const Json::Value& url_spec, | 143 bool IsValidUrlSpec(const Json::Value& url_spec, |
| 136 const nacl::string& container_key, | 144 const nacl::string& container_key, |
| 137 const nacl::string& parent_key, | 145 const nacl::string& parent_key, |
| 138 nacl::string* error_string) { | 146 nacl::string* error_string) { |
| 139 static const char* kManifestUrlSpecProperties[] = { | 147 static const char* kManifestUrlSpecProperties[] = { |
| 140 kUrlKey | 148 kUrlKey, |
| 149 kCacheIdentityKey, | |
| 141 }; | 150 }; |
| 142 if (!IsValidDictionary(url_spec, container_key, parent_key, | 151 if (!IsValidDictionary(url_spec, container_key, parent_key, |
| 143 kManifestUrlSpecProperties, | 152 kManifestUrlSpecProperties, |
| 144 NACL_ARRAY_SIZE(kManifestUrlSpecProperties), | 153 NACL_ARRAY_SIZE(kManifestUrlSpecProperties), |
| 145 kManifestUrlSpecProperties, | 154 kManifestUrlSpecProperties, |
| 146 NACL_ARRAY_SIZE(kManifestUrlSpecProperties), | 155 NACL_ARRAY_SIZE(kManifestUrlSpecProperties), |
| 147 error_string)) { | 156 error_string)) { |
| 148 return false; | 157 return false; |
| 149 } | 158 } |
| 150 Json::Value url = url_spec[kUrlKey]; | 159 Json::Value url = url_spec[kUrlKey]; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 | 248 |
| 240 if (!has_isa && !has_portable) { | 249 if (!has_isa && !has_portable) { |
| 241 *error_string = parent_key + | 250 *error_string = parent_key + |
| 242 " no version given for current arch and no portable version found."; | 251 " no version given for current arch and no portable version found."; |
| 243 return false; | 252 return false; |
| 244 } | 253 } |
| 245 | 254 |
| 246 return true; | 255 return true; |
| 247 } | 256 } |
| 248 | 257 |
| 258 bool GrabUrlAndCacheIdentity(const Json::Value& url_spec, | |
| 259 nacl::string* url, | |
| 260 nacl::string* cache_identity) { | |
| 261 *url = url_spec[kUrlKey].asString(); | |
| 262 if (url_spec.isMember(kCacheIdentityKey)) { | |
| 263 *cache_identity = url_spec[kCacheIdentityKey].asString(); | |
| 264 } | |
| 265 } | |
| 266 | |
| 249 bool GetURLFromISADictionary(const Json::Value& dictionary, | 267 bool GetURLFromISADictionary(const Json::Value& dictionary, |
| 250 const nacl::string& parent_key, | 268 const nacl::string& parent_key, |
| 251 const nacl::string& sandbox_isa, | 269 const nacl::string& sandbox_isa, |
| 252 bool prefer_portable, | 270 bool prefer_portable, |
| 253 nacl::string* url, | 271 nacl::string* url, |
| 272 nacl::string* cache_identity, | |
| 254 nacl::string* error_string, | 273 nacl::string* error_string, |
| 255 bool* pnacl_translate) { | 274 bool* pnacl_translate) { |
| 256 if (url == NULL || error_string == NULL || pnacl_translate == NULL) | 275 if (url == NULL || cache_identity == NULL || |
| 276 error_string == NULL || pnacl_translate == NULL) | |
| 257 return false; | 277 return false; |
| 258 | 278 |
| 259 if (!IsValidISADictionary(dictionary, parent_key, sandbox_isa, error_string)) | 279 if (!IsValidISADictionary(dictionary, parent_key, sandbox_isa, error_string)) |
| 260 return false; | 280 return false; |
| 261 | 281 |
| 282 *url = ""; | |
| 283 *cache_identity = ""; | |
| 284 *pnacl_translate = false; | |
| 285 | |
| 262 // The call to IsValidISADictionary() above guarantees that either | 286 // The call to IsValidISADictionary() above guarantees that either |
| 263 // sandbox_isa or kPortableKey is present in the dictionary. | 287 // sandbox_isa or kPortableKey is present in the dictionary. |
| 264 bool has_portable = dictionary.isMember(kPortableKey); | 288 bool has_portable = dictionary.isMember(kPortableKey); |
| 265 bool has_isa = dictionary.isMember(sandbox_isa); | 289 bool has_isa = dictionary.isMember(sandbox_isa); |
| 266 nacl::string chosen_isa; | 290 nacl::string chosen_isa; |
| 267 if ((has_portable && prefer_portable) || !has_isa) { | 291 if ((has_portable && prefer_portable) || !has_isa) { |
| 268 chosen_isa = kPortableKey; | 292 chosen_isa = kPortableKey; |
| 269 } else { | 293 } else { |
| 270 chosen_isa = sandbox_isa; | 294 chosen_isa = sandbox_isa; |
| 271 } | 295 } |
| 272 const Json::Value& isa_spec = dictionary[chosen_isa]; | 296 const Json::Value& isa_spec = dictionary[chosen_isa]; |
| 273 // Check if this requires a pnacl-translate, otherwise just grab the URL. | 297 // Check if this requires a pnacl-translate, otherwise just grab the URL. |
| 274 // We may have pnacl-translate for isa-specific bitcode for CPU tuning. | 298 // We may have pnacl-translate for isa-specific bitcode for CPU tuning. |
| 275 if (isa_spec.isMember(kPnaclTranslateKey)) { | 299 if (isa_spec.isMember(kPnaclTranslateKey)) { |
| 276 *url = isa_spec[kPnaclTranslateKey][kUrlKey].asString(); | 300 GrabUrlAndCacheIdentity(isa_spec[kPnaclTranslateKey], url, cache_identity); |
| 277 *pnacl_translate = true; | 301 *pnacl_translate = true; |
| 278 } else { | 302 } else { |
| 279 *url = isa_spec[kUrlKey].asString(); | 303 GrabUrlAndCacheIdentity(isa_spec[kPnaclTranslateKey], url, cache_identity); |
|
sehr (please use chromium)
2012/02/22 19:30:37
The if guard makes sure that the dictionary index
jvoung - send to chromium...
2012/02/22 19:45:37
Good catch thanks! Copy-paste bug =(
| |
| 280 *pnacl_translate = false; | 304 *pnacl_translate = false; |
| 281 } | 305 } |
| 306 | |
| 282 return true; | 307 return true; |
| 283 } | 308 } |
| 284 | 309 |
| 285 bool GetKeyUrl(const Json::Value& dictionary, | 310 bool GetKeyUrl(const Json::Value& dictionary, |
| 286 const nacl::string& key, | 311 const nacl::string& key, |
| 287 const nacl::string& sandbox_isa, | 312 const nacl::string& sandbox_isa, |
| 288 const Manifest* manifest, | 313 const Manifest* manifest, |
| 289 bool prefer_portable, | 314 bool prefer_portable, |
| 290 nacl::string* full_url, | 315 nacl::string* full_url, |
| 316 nacl::string* cache_identity, | |
| 291 ErrorInfo* error_info, | 317 ErrorInfo* error_info, |
| 292 bool* pnacl_translate) { | 318 bool* pnacl_translate) { |
| 293 CHECK(full_url != NULL && error_info != NULL); | 319 CHECK(full_url != NULL && error_info != NULL); |
| 294 *full_url = ""; | |
| 295 *pnacl_translate = false; | |
| 296 if (!dictionary.isMember(key)) { | 320 if (!dictionary.isMember(key)) { |
| 297 error_info->SetReport(ERROR_MANIFEST_RESOLVE_URL, | 321 error_info->SetReport(ERROR_MANIFEST_RESOLVE_URL, |
| 298 "file key not found in manifest"); | 322 "file key not found in manifest"); |
| 299 return false; | 323 return false; |
| 300 } | 324 } |
| 301 const Json::Value& isa_dict = dictionary[key]; | 325 const Json::Value& isa_dict = dictionary[key]; |
| 302 nacl::string error_string; | 326 nacl::string error_string; |
| 303 nacl::string relative_url; | 327 nacl::string relative_url; |
| 304 if (!GetURLFromISADictionary(isa_dict, key, sandbox_isa, prefer_portable, | 328 if (!GetURLFromISADictionary(isa_dict, key, sandbox_isa, prefer_portable, |
| 305 &relative_url, &error_string, pnacl_translate)) { | 329 &relative_url, cache_identity, |
| 330 &error_string, pnacl_translate)) { | |
| 306 error_info->SetReport(ERROR_MANIFEST_RESOLVE_URL, | 331 error_info->SetReport(ERROR_MANIFEST_RESOLVE_URL, |
| 307 key + nacl::string(" manifest resolution error: ") + | 332 key + nacl::string(" manifest resolution error: ") + |
| 308 error_string); | 333 error_string); |
| 309 return false; | 334 return false; |
| 310 } | 335 } |
| 311 return manifest->ResolveURL(relative_url, full_url, error_info); | 336 return manifest->ResolveURL(relative_url, full_url, error_info); |
| 312 } | 337 } |
| 313 | 338 |
| 314 } // namespace | 339 } // namespace |
| 315 | 340 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 430 "could not resolve url '" + relative_url + | 455 "could not resolve url '" + relative_url + |
| 431 "' relative to manifest base url '" + manifest_base_url_.c_str() + | 456 "' relative to manifest base url '" + manifest_base_url_.c_str() + |
| 432 "'."); | 457 "'."); |
| 433 return false; | 458 return false; |
| 434 } | 459 } |
| 435 *full_url = resolved_url.AsString(); | 460 *full_url = resolved_url.AsString(); |
| 436 return true; | 461 return true; |
| 437 } | 462 } |
| 438 | 463 |
| 439 bool JsonManifest::GetProgramURL(nacl::string* full_url, | 464 bool JsonManifest::GetProgramURL(nacl::string* full_url, |
| 465 nacl::string* cache_identity, | |
| 440 ErrorInfo* error_info, | 466 ErrorInfo* error_info, |
| 441 bool* pnacl_translate) const { | 467 bool* pnacl_translate) const { |
| 442 if (full_url == NULL || error_info == NULL || pnacl_translate == NULL) | 468 if (full_url == NULL || cache_identity == NULL || |
| 469 error_info == NULL || pnacl_translate == NULL) | |
| 443 return false; | 470 return false; |
| 444 | 471 |
| 445 Json::Value program = dictionary_[kProgramKey]; | 472 Json::Value program = dictionary_[kProgramKey]; |
| 446 | 473 |
| 447 nacl::string nexe_url; | 474 nacl::string nexe_url; |
| 448 nacl::string error_string; | 475 nacl::string error_string; |
| 449 | 476 |
| 450 if (!GetURLFromISADictionary(program, | 477 if (!GetURLFromISADictionary(program, |
| 451 kProgramKey, | 478 kProgramKey, |
| 452 sandbox_isa_, | 479 sandbox_isa_, |
| 453 prefer_portable_, | 480 prefer_portable_, |
| 454 &nexe_url, | 481 &nexe_url, |
| 482 cache_identity, | |
| 455 &error_string, | 483 &error_string, |
| 456 pnacl_translate)) { | 484 pnacl_translate)) { |
| 457 error_info->SetReport(ERROR_MANIFEST_GET_NEXE_URL, | 485 error_info->SetReport(ERROR_MANIFEST_GET_NEXE_URL, |
| 458 nacl::string("program:") + sandbox_isa_ + | 486 nacl::string("program:") + sandbox_isa_ + |
| 459 error_string); | 487 error_string); |
| 460 return false; | 488 return false; |
| 461 } | 489 } |
| 462 | 490 |
| 463 return ResolveURL(nexe_url, full_url, error_info); | 491 return ResolveURL(nexe_url, full_url, error_info); |
| 464 } | 492 } |
| 465 | 493 |
| 466 bool JsonManifest::GetFileKeys(std::set<nacl::string>* keys) const { | 494 bool JsonManifest::GetFileKeys(std::set<nacl::string>* keys) const { |
| 467 if (!dictionary_.isMember(kFilesKey)) { | 495 if (!dictionary_.isMember(kFilesKey)) { |
| 468 // trivial success: no keys when there is no "files" section. | 496 // trivial success: no keys when there is no "files" section. |
| 469 return true; | 497 return true; |
| 470 } | 498 } |
| 471 const Json::Value& files = dictionary_[kFilesKey]; | 499 const Json::Value& files = dictionary_[kFilesKey]; |
| 472 CHECK(files.isObject()); | 500 CHECK(files.isObject()); |
| 473 Json::Value::Members members = files.getMemberNames(); | 501 Json::Value::Members members = files.getMemberNames(); |
| 474 for (size_t i = 0; i < members.size(); ++i) { | 502 for (size_t i = 0; i < members.size(); ++i) { |
| 475 keys->insert(members[i]); | 503 keys->insert(members[i]); |
| 476 } | 504 } |
| 477 return true; | 505 return true; |
| 478 } | 506 } |
| 479 | 507 |
| 480 bool JsonManifest::ResolveKey(const nacl::string& key, | 508 bool JsonManifest::ResolveKey(const nacl::string& key, |
| 481 nacl::string* full_url, | 509 nacl::string* full_url, |
| 510 nacl::string* cache_identity, | |
| 482 ErrorInfo* error_info, | 511 ErrorInfo* error_info, |
| 483 bool* pnacl_translate) const { | 512 bool* pnacl_translate) const { |
| 484 NaClLog(3, "JsonManifest::ResolveKey(%s)\n", key.c_str()); | 513 NaClLog(3, "JsonManifest::ResolveKey(%s)\n", key.c_str()); |
| 485 // key must be one of kProgramKey or kFileKey '/' file-section-key | 514 // key must be one of kProgramKey or kFileKey '/' file-section-key |
| 486 | 515 |
| 487 if (full_url == NULL || error_info == NULL || pnacl_translate == NULL) | 516 if (full_url == NULL || cache_identity == NULL || |
| 517 error_info == NULL || pnacl_translate == NULL) | |
| 488 return false; | 518 return false; |
| 489 | 519 |
| 490 *full_url = ""; | |
| 491 *pnacl_translate = false; | |
| 492 if (key == kProgramKey) { | 520 if (key == kProgramKey) { |
| 493 return GetKeyUrl(dictionary_, key, sandbox_isa_, this, prefer_portable_, | 521 return GetKeyUrl(dictionary_, key, sandbox_isa_, this, prefer_portable_, |
| 494 full_url, error_info, pnacl_translate); | 522 full_url, cache_identity, error_info, pnacl_translate); |
| 495 } | 523 } |
| 496 nacl::string::const_iterator p = find(key.begin(), key.end(), '/'); | 524 nacl::string::const_iterator p = find(key.begin(), key.end(), '/'); |
| 497 if (p == key.end()) { | 525 if (p == key.end()) { |
| 498 error_info->SetReport(ERROR_MANIFEST_RESOLVE_URL, | 526 error_info->SetReport(ERROR_MANIFEST_RESOLVE_URL, |
| 499 nacl::string("ResolveKey: invalid key, no slash: ") | 527 nacl::string("ResolveKey: invalid key, no slash: ") |
| 500 + key); | 528 + key); |
| 501 return false; | 529 return false; |
| 502 } | 530 } |
| 503 | 531 |
| 504 // generalize to permit other sections? | 532 // generalize to permit other sections? |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 519 nacl::string("ResolveKey: no \"files\" dictionary")); | 547 nacl::string("ResolveKey: no \"files\" dictionary")); |
| 520 return false; | 548 return false; |
| 521 } | 549 } |
| 522 if (!files.isMember(rest)) { | 550 if (!files.isMember(rest)) { |
| 523 error_info->SetReport( | 551 error_info->SetReport( |
| 524 ERROR_MANIFEST_RESOLVE_URL, | 552 ERROR_MANIFEST_RESOLVE_URL, |
| 525 nacl::string("ResolveKey: no such \"files\" entry: ") + key); | 553 nacl::string("ResolveKey: no such \"files\" entry: ") + key); |
| 526 return false; | 554 return false; |
| 527 } | 555 } |
| 528 return GetKeyUrl(files, rest, sandbox_isa_, this, prefer_portable_, | 556 return GetKeyUrl(files, rest, sandbox_isa_, this, prefer_portable_, |
| 529 full_url, error_info, pnacl_translate); | 557 full_url, cache_identity, error_info, pnacl_translate); |
| 530 } | 558 } |
| 531 | 559 |
| 532 } // namespace plugin | 560 } // namespace plugin |
| OLD | NEW |