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 <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/prefs/pref_notifier.h" | 9 #include "base/prefs/pref_notifier.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // Preferences that hold which permissions the user has granted the extension. | 150 // Preferences that hold which permissions the user has granted the extension. |
151 // We explicitly keep track of these so that extensions can contain unknown | 151 // We explicitly keep track of these so that extensions can contain unknown |
152 // permissions, for backwards compatibility reasons, and we can still prompt | 152 // permissions, for backwards compatibility reasons, and we can still prompt |
153 // the user to accept them once recognized. We store the active permission | 153 // the user to accept them once recognized. We store the active permission |
154 // permissions because they may differ from those defined in the manifest. | 154 // permissions because they may differ from those defined in the manifest. |
155 const char kPrefActivePermissions[] = "active_permissions"; | 155 const char kPrefActivePermissions[] = "active_permissions"; |
156 const char kPrefGrantedPermissions[] = "granted_permissions"; | 156 const char kPrefGrantedPermissions[] = "granted_permissions"; |
157 | 157 |
158 // The preference names for PermissionSet values. | 158 // The preference names for PermissionSet values. |
159 const char kPrefAPIs[] = "api"; | 159 const char kPrefAPIs[] = "api"; |
| 160 const char kPrefManifestPermissions[] = "manifest_permissions"; |
160 const char kPrefExplicitHosts[] = "explicit_host"; | 161 const char kPrefExplicitHosts[] = "explicit_host"; |
161 const char kPrefScriptableHosts[] = "scriptable_host"; | 162 const char kPrefScriptableHosts[] = "scriptable_host"; |
162 | 163 |
163 // The preference names for the old granted permissions scheme. | 164 // The preference names for the old granted permissions scheme. |
164 const char kPrefOldGrantedFullAccess[] = "granted_permissions.full"; | 165 const char kPrefOldGrantedFullAccess[] = "granted_permissions.full"; |
165 const char kPrefOldGrantedHosts[] = "granted_permissions.host"; | 166 const char kPrefOldGrantedHosts[] = "granted_permissions.host"; |
166 const char kPrefOldGrantedAPIs[] = "granted_permissions.api"; | 167 const char kPrefOldGrantedAPIs[] = "granted_permissions.api"; |
167 | 168 |
168 // A preference that indicates when an extension was installed. | 169 // A preference that indicates when an extension was installed. |
169 const char kPrefInstallTime[] = "install_time"; | 170 const char kPrefInstallTime[] = "install_time"; |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 // for api_values format. | 536 // for api_values format. |
536 APIPermissionSet apis; | 537 APIPermissionSet apis; |
537 const ListValue* api_values = NULL; | 538 const ListValue* api_values = NULL; |
538 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); | 539 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); |
539 if (ReadPrefAsList(extension_id, api_pref, &api_values)) { | 540 if (ReadPrefAsList(extension_id, api_pref, &api_values)) { |
540 APIPermissionSet::ParseFromJSON(api_values, | 541 APIPermissionSet::ParseFromJSON(api_values, |
541 APIPermissionSet::kAllowInternalPermissions, | 542 APIPermissionSet::kAllowInternalPermissions, |
542 &apis, NULL, NULL); | 543 &apis, NULL, NULL); |
543 } | 544 } |
544 | 545 |
| 546 // Retrieve the Manifest Keys permissions. Please refer to |
| 547 // |SetExtensionPrefPermissionSet| for manifest_permissions_values format. |
| 548 ManifestPermissionSet manifest_permissions; |
| 549 const ListValue* manifest_permissions_values = NULL; |
| 550 std::string manifest_permission_pref = |
| 551 JoinPrefs(pref_key, kPrefManifestPermissions); |
| 552 if (ReadPrefAsList(extension_id, manifest_permission_pref, |
| 553 &manifest_permissions_values)) { |
| 554 ManifestPermissionSet::ParseFromJSON( |
| 555 manifest_permissions_values, &manifest_permissions, NULL, NULL); |
| 556 } |
| 557 |
545 // Retrieve the explicit host permissions. | 558 // Retrieve the explicit host permissions. |
546 URLPatternSet explicit_hosts; | 559 URLPatternSet explicit_hosts; |
547 ReadPrefAsURLPatternSet( | 560 ReadPrefAsURLPatternSet( |
548 extension_id, JoinPrefs(pref_key, kPrefExplicitHosts), | 561 extension_id, JoinPrefs(pref_key, kPrefExplicitHosts), |
549 &explicit_hosts, Extension::kValidHostPermissionSchemes); | 562 &explicit_hosts, Extension::kValidHostPermissionSchemes); |
550 | 563 |
551 // Retrieve the scriptable host permissions. | 564 // Retrieve the scriptable host permissions. |
552 URLPatternSet scriptable_hosts; | 565 URLPatternSet scriptable_hosts; |
553 ReadPrefAsURLPatternSet( | 566 ReadPrefAsURLPatternSet( |
554 extension_id, JoinPrefs(pref_key, kPrefScriptableHosts), | 567 extension_id, JoinPrefs(pref_key, kPrefScriptableHosts), |
555 &scriptable_hosts, UserScript::ValidUserScriptSchemes()); | 568 &scriptable_hosts, UserScript::ValidUserScriptSchemes()); |
556 | 569 |
557 return new PermissionSet(apis, explicit_hosts, scriptable_hosts); | 570 return new PermissionSet( |
| 571 apis, manifest_permissions, explicit_hosts, scriptable_hosts); |
| 572 } |
| 573 |
| 574 // Set the API or Manifest permissions. |
| 575 // The format of api_values is: |
| 576 // [ "permission_name1", // permissions do not support detail. |
| 577 // "permission_name2", |
| 578 // {"permission_name3": value }, |
| 579 // // permission supports detail, permission detail will be stored in value. |
| 580 // ... |
| 581 // ] |
| 582 template<typename T> |
| 583 static ListValue* CreatePermissionList(const T& permissions) { |
| 584 ListValue* values = new ListValue(); |
| 585 for (typename T::const_iterator i = permissions.begin(); |
| 586 i != permissions.end(); ++i) { |
| 587 scoped_ptr<Value> detail(i->ToValue()); |
| 588 if (detail) { |
| 589 DictionaryValue* tmp = new DictionaryValue(); |
| 590 tmp->Set(i->name(), detail.release()); |
| 591 values->Append(tmp); |
| 592 } else { |
| 593 values->Append(new base::StringValue(i->name())); |
| 594 } |
| 595 } |
| 596 return values; |
558 } | 597 } |
559 | 598 |
560 void ExtensionPrefs::SetExtensionPrefPermissionSet( | 599 void ExtensionPrefs::SetExtensionPrefPermissionSet( |
561 const std::string& extension_id, | 600 const std::string& extension_id, |
562 const std::string& pref_key, | 601 const std::string& pref_key, |
563 const PermissionSet* new_value) { | 602 const PermissionSet* new_value) { |
564 // Set the API permissions. | |
565 // The format of api_values is: | |
566 // [ "permission_name1", // permissions do not support detail. | |
567 // "permission_name2", | |
568 // {"permission_name3": value }, | |
569 // // permission supports detail, permission detail will be stored in value. | |
570 // ... | |
571 // ] | |
572 ListValue* api_values = new ListValue(); | |
573 APIPermissionSet apis = new_value->apis(); | |
574 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); | 603 std::string api_pref = JoinPrefs(pref_key, kPrefAPIs); |
575 for (APIPermissionSet::const_iterator i = apis.begin(); | 604 ListValue* api_values = CreatePermissionList(new_value->apis()); |
576 i != apis.end(); ++i) { | |
577 scoped_ptr<Value> detail(i->ToValue()); | |
578 if (detail) { | |
579 DictionaryValue* tmp = new DictionaryValue(); | |
580 tmp->Set(i->name(), detail.release()); | |
581 api_values->Append(tmp); | |
582 } else { | |
583 api_values->Append(new base::StringValue(i->name())); | |
584 } | |
585 } | |
586 UpdateExtensionPref(extension_id, api_pref, api_values); | 605 UpdateExtensionPref(extension_id, api_pref, api_values); |
587 | 606 |
| 607 std::string manifest_permissions_pref = |
| 608 JoinPrefs(pref_key, kPrefManifestPermissions); |
| 609 ListValue* manifest_permissions_values = CreatePermissionList( |
| 610 new_value->manifest_permissions()); |
| 611 UpdateExtensionPref(extension_id, |
| 612 manifest_permissions_pref, |
| 613 manifest_permissions_values); |
| 614 |
588 // Set the explicit host permissions. | 615 // Set the explicit host permissions. |
589 if (!new_value->explicit_hosts().is_empty()) { | 616 if (!new_value->explicit_hosts().is_empty()) { |
590 SetExtensionPrefURLPatternSet(extension_id, | 617 SetExtensionPrefURLPatternSet(extension_id, |
591 JoinPrefs(pref_key, kPrefExplicitHosts), | 618 JoinPrefs(pref_key, kPrefExplicitHosts), |
592 new_value->explicit_hosts()); | 619 new_value->explicit_hosts()); |
593 } | 620 } |
594 | 621 |
595 // Set the scriptable host permissions. | 622 // Set the scriptable host permissions. |
596 if (!new_value->scriptable_hosts().is_empty()) { | 623 if (!new_value->scriptable_hosts().is_empty()) { |
597 SetExtensionPrefURLPatternSet(extension_id, | 624 SetExtensionPrefURLPatternSet(extension_id, |
(...skipping 1320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1918 is_enabled = initial_state == Extension::ENABLED; | 1945 is_enabled = initial_state == Extension::ENABLED; |
1919 } | 1946 } |
1920 | 1947 |
1921 extension_pref_value_map_->RegisterExtension(extension_id, install_time, | 1948 extension_pref_value_map_->RegisterExtension(extension_id, install_time, |
1922 is_enabled); | 1949 is_enabled); |
1923 content_settings_store_->RegisterExtension(extension_id, install_time, | 1950 content_settings_store_->RegisterExtension(extension_id, install_time, |
1924 is_enabled); | 1951 is_enabled); |
1925 } | 1952 } |
1926 | 1953 |
1927 } // namespace extensions | 1954 } // namespace extensions |
OLD | NEW |