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/extensions/extension_prefs.h" | 5 #include "chrome/browser/extensions/extension_prefs.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/admin_policy.h" | 10 #include "chrome/browser/extensions/admin_policy.h" |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 const URLPatternSet& new_value) { | 502 const URLPatternSet& new_value) { |
503 UpdateExtensionPref(extension_id, pref_key, new_value.ToValue().release()); | 503 UpdateExtensionPref(extension_id, pref_key, new_value.ToValue().release()); |
504 } | 504 } |
505 | 505 |
506 PermissionSet* ExtensionPrefs::ReadExtensionPrefPermissionSet( | 506 PermissionSet* ExtensionPrefs::ReadExtensionPrefPermissionSet( |
507 const std::string& extension_id, | 507 const std::string& extension_id, |
508 const std::string& pref_key) { | 508 const std::string& pref_key) { |
509 if (!GetExtensionPref(extension_id)) | 509 if (!GetExtensionPref(extension_id)) |
510 return NULL; | 510 return NULL; |
511 | 511 |
512 // Retrieve the API permissions. | 512 // Retrieve the API permissions. Please refer SetExtensionPrefPermissionSet() |
| 513 // for api_values format. |
513 APIPermissionSet apis; | 514 APIPermissionSet apis; |
514 const ListValue* api_values = NULL; | 515 const ListValue* api_values = NULL; |
515 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); | 516 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); |
516 if (ReadExtensionPrefList(extension_id, api_pref, &api_values)) { | 517 if (ReadExtensionPrefList(extension_id, api_pref, &api_values)) { |
517 PermissionsInfo* info = PermissionsInfo::GetInstance(); | 518 PermissionsInfo* info = PermissionsInfo::GetInstance(); |
518 for (size_t i = 0; i < api_values->GetSize(); ++i) { | 519 for (size_t i = 0; i < api_values->GetSize(); ++i) { |
| 520 const DictionaryValue* permission_dict = NULL; |
519 std::string permission_name; | 521 std::string permission_name; |
520 if (api_values->GetString(i, &permission_name)) { | 522 if (!api_values->GetString(i, &permission_name) && |
521 APIPermission *permission = info->GetByName(permission_name); | 523 !api_values->GetDictionary(i, &permission_dict)) { |
522 if (permission) | 524 NOTREACHED() << "Permission is not a string or dict. "; |
523 apis.insert(permission->id()); | 525 continue; |
524 } | 526 } |
| 527 |
| 528 const base::Value *permission_detail = NULL; |
| 529 if (permission_dict) { |
| 530 if (permission_dict->size() != 1u) { |
| 531 NOTREACHED() << "Permission is not a single key dict."; |
| 532 continue; |
| 533 } |
| 534 base::DictionaryValue::Iterator it(*permission_dict); |
| 535 permission_name = it.key(); |
| 536 permission_detail = &it.value(); |
| 537 } |
| 538 |
| 539 APIPermission *permission = info->GetByName(permission_name); |
| 540 if (!permission) { |
| 541 NOTREACHED() << "Unknown permission[" << permission_name << "]."; |
| 542 continue; |
| 543 } |
| 544 |
| 545 scoped_refptr<APIPermissionDetail> detail = permission->CreateDetail(); |
| 546 if (!detail->FromValue(permission_detail)) { |
| 547 NOTREACHED() << "Parse permission detail failed."; |
| 548 continue; |
| 549 } |
| 550 apis.insert(detail); |
525 } | 551 } |
526 } | 552 } |
527 | 553 |
528 // Retrieve the explicit host permissions. | 554 // Retrieve the explicit host permissions. |
529 URLPatternSet explicit_hosts; | 555 URLPatternSet explicit_hosts; |
530 ReadExtensionPrefURLPatternSet( | 556 ReadExtensionPrefURLPatternSet( |
531 extension_id, JoinPrefs(pref_key, kPrefExplicitHosts), | 557 extension_id, JoinPrefs(pref_key, kPrefExplicitHosts), |
532 &explicit_hosts, Extension::kValidHostPermissionSchemes); | 558 &explicit_hosts, Extension::kValidHostPermissionSchemes); |
533 | 559 |
534 // Retrieve the scriptable host permissions. | 560 // Retrieve the scriptable host permissions. |
535 URLPatternSet scriptable_hosts; | 561 URLPatternSet scriptable_hosts; |
536 ReadExtensionPrefURLPatternSet( | 562 ReadExtensionPrefURLPatternSet( |
537 extension_id, JoinPrefs(pref_key, kPrefScriptableHosts), | 563 extension_id, JoinPrefs(pref_key, kPrefScriptableHosts), |
538 &scriptable_hosts, UserScript::kValidUserScriptSchemes); | 564 &scriptable_hosts, UserScript::kValidUserScriptSchemes); |
539 | 565 |
540 return new PermissionSet(apis, explicit_hosts, scriptable_hosts); | 566 return new PermissionSet(apis, explicit_hosts, scriptable_hosts); |
541 } | 567 } |
542 | 568 |
543 void ExtensionPrefs::SetExtensionPrefPermissionSet( | 569 void ExtensionPrefs::SetExtensionPrefPermissionSet( |
544 const std::string& extension_id, | 570 const std::string& extension_id, |
545 const std::string& pref_key, | 571 const std::string& pref_key, |
546 const PermissionSet* new_value) { | 572 const PermissionSet* new_value) { |
547 // Set the API permissions. | 573 // Set the API permissions. |
| 574 // The format of api_values is: |
| 575 // [ "permission_name1", // permissions do not support detail. |
| 576 // "permission_name2", |
| 577 // {"permission_name3": value }, |
| 578 // // permission supports detail, permission detail will be stored in value. |
| 579 // ... |
| 580 // ] |
548 ListValue* api_values = new ListValue(); | 581 ListValue* api_values = new ListValue(); |
549 APIPermissionSet apis = new_value->apis(); | 582 APIPermissionSet apis = new_value->apis(); |
550 PermissionsInfo* info = PermissionsInfo::GetInstance(); | |
551 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); | 583 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); |
552 for (APIPermissionSet::const_iterator i = apis.begin(); | 584 for (APIPermissionSet::const_iterator i = apis.begin(); |
553 i != apis.end(); ++i) { | 585 i != apis.end(); ++i) { |
554 APIPermission* perm = info->GetByID(*i); | 586 Value* detail = NULL; |
555 if (perm) | 587 i->ToValue(&detail); |
556 api_values->Append(Value::CreateStringValue(perm->name())); | 588 if (detail) { |
| 589 DictionaryValue* tmp = new DictionaryValue(); |
| 590 tmp->Set(i->name(), detail); |
| 591 api_values->Append(tmp); |
| 592 } else { |
| 593 api_values->Append(Value::CreateStringValue(i->name())); |
| 594 } |
557 } | 595 } |
558 UpdateExtensionPref(extension_id, api_pref, api_values); | 596 UpdateExtensionPref(extension_id, api_pref, api_values); |
559 | 597 |
560 // Set the explicit host permissions. | 598 // Set the explicit host permissions. |
561 if (!new_value->explicit_hosts().is_empty()) { | 599 if (!new_value->explicit_hosts().is_empty()) { |
562 SetExtensionPrefURLPatternSet(extension_id, | 600 SetExtensionPrefURLPatternSet(extension_id, |
563 JoinPrefs(pref_key, kPrefExplicitHosts), | 601 JoinPrefs(pref_key, kPrefExplicitHosts), |
564 new_value->explicit_hosts()); | 602 new_value->explicit_hosts()); |
565 } | 603 } |
566 | 604 |
(...skipping 1581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2148 const ExtensionIdSet& strings) { | 2186 const ExtensionIdSet& strings) { |
2149 ListPrefUpdate update(prefs_, pref); | 2187 ListPrefUpdate update(prefs_, pref); |
2150 ListValue* list_of_values = update.Get(); | 2188 ListValue* list_of_values = update.Get(); |
2151 list_of_values->Clear(); | 2189 list_of_values->Clear(); |
2152 for (ExtensionIdSet::const_iterator iter = strings.begin(); | 2190 for (ExtensionIdSet::const_iterator iter = strings.begin(); |
2153 iter != strings.end(); ++iter) | 2191 iter != strings.end(); ++iter) |
2154 list_of_values->Append(new StringValue(*iter)); | 2192 list_of_values->Append(new StringValue(*iter)); |
2155 } | 2193 } |
2156 | 2194 |
2157 } // namespace extensions | 2195 } // namespace extensions |
OLD | NEW |