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/api/permissions/permissions_api_helpers.h" | 5 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/common/extensions/api/permissions.h" |
8 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
9 #include "chrome/common/extensions/extension_error_utils.h" | 10 #include "chrome/common/extensions/extension_error_utils.h" |
10 #include "chrome/common/extensions/extension_permission_set.h" | 11 #include "chrome/common/extensions/extension_permission_set.h" |
11 #include "chrome/common/extensions/url_pattern_set.h" | 12 #include "chrome/common/extensions/url_pattern_set.h" |
12 | 13 |
13 namespace extensions { | 14 namespace extensions { |
14 namespace permissions_api { | 15 |
| 16 using api::permissions::Permissions; |
| 17 |
| 18 namespace permissions_api_helpers { |
15 | 19 |
16 namespace { | 20 namespace { |
17 | 21 |
18 const char kInvalidOrigin[] = | 22 const char kInvalidOrigin[] = |
19 "Invalid value for origin pattern *: *"; | 23 "Invalid value for origin pattern *: *"; |
20 const char kUnknownPermissionError[] = | 24 const char kUnknownPermissionError[] = |
21 "'*' is not a recognized permission."; | 25 "'*' is not a recognized permission."; |
22 | 26 |
23 const char kApisKey[] = "permissions"; | |
24 const char kOriginsKey[] = "origins"; | |
25 | |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 DictionaryValue* PackPermissionsToValue(const ExtensionPermissionSet* set) { | 29 scoped_ptr<Permissions> PackPermissionSet(const ExtensionPermissionSet* set) { |
29 DictionaryValue* value = new DictionaryValue(); | 30 Permissions* permissions(new Permissions()); |
30 | 31 |
31 // Generate the list of API permissions. | 32 permissions->permissions.reset(new std::vector<std::string>()); |
32 ListValue* apis = new ListValue(); | |
33 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); | 33 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); |
34 for (ExtensionAPIPermissionSet::const_iterator i = set->apis().begin(); | 34 for (ExtensionAPIPermissionSet::const_iterator i = set->apis().begin(); |
35 i != set->apis().end(); ++i) | 35 i != set->apis().end(); ++i) { |
36 apis->Append(Value::CreateStringValue(info->GetByID(*i)->name())); | 36 permissions->permissions->push_back(info->GetByID(*i)->name()); |
| 37 } |
37 | 38 |
38 // Generate the list of origin permissions. | 39 permissions->origins.reset(new std::vector<std::string>()); |
39 URLPatternSet hosts = set->explicit_hosts(); | 40 URLPatternSet hosts = set->explicit_hosts(); |
40 ListValue* origins = new ListValue(); | |
41 for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i) | 41 for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i) |
42 origins->Append(Value::CreateStringValue(i->GetAsString())); | 42 permissions->origins->push_back(i->GetAsString()); |
43 | 43 |
44 value->Set(kApisKey, apis); | 44 return scoped_ptr<Permissions>(permissions); |
45 value->Set(kOriginsKey, origins); | |
46 return value; | |
47 } | 45 } |
48 | 46 |
49 // Creates a new ExtensionPermissionSet from its |value| and passes ownership to | 47 scoped_refptr<ExtensionPermissionSet> UnpackPermissionSet( |
50 // the caller through |ptr|. Sets |bad_message| to true if the message is badly | 48 const Permissions& permissions, std::string* error) { |
51 // formed. Returns false if the method fails to unpack a permission set. | |
52 bool UnpackPermissionsFromValue(DictionaryValue* value, | |
53 scoped_refptr<ExtensionPermissionSet>* ptr, | |
54 bool* bad_message, | |
55 std::string* error) { | |
56 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); | |
57 ExtensionAPIPermissionSet apis; | 49 ExtensionAPIPermissionSet apis; |
58 if (value->HasKey(kApisKey)) { | 50 std::vector<std::string>* permissions_list = permissions.permissions.get(); |
59 ListValue* api_list = NULL; | 51 if (permissions_list) { |
60 if (!value->GetList(kApisKey, &api_list)) { | 52 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); |
61 *bad_message = true; | 53 for (std::vector<std::string>::iterator it = permissions_list->begin(); |
62 return false; | 54 it != permissions_list->end(); ++it) { |
63 } | 55 ExtensionAPIPermission* permission = info->GetByName(*it); |
64 for (size_t i = 0; i < api_list->GetSize(); ++i) { | |
65 std::string api_name; | |
66 if (!api_list->GetString(i, &api_name)) { | |
67 *bad_message = true; | |
68 return false; | |
69 } | |
70 | |
71 ExtensionAPIPermission* permission = info->GetByName(api_name); | |
72 if (!permission) { | 56 if (!permission) { |
73 *error = ExtensionErrorUtils::FormatErrorMessage( | 57 *error = ExtensionErrorUtils::FormatErrorMessage( |
74 kUnknownPermissionError, api_name); | 58 kUnknownPermissionError, *it); |
75 return false; | 59 return NULL; |
76 } | 60 } |
77 apis.insert(permission->id()); | 61 apis.insert(permission->id()); |
78 } | 62 } |
79 } | 63 } |
80 | 64 |
81 URLPatternSet origins; | 65 URLPatternSet origins; |
82 if (value->HasKey(kOriginsKey)) { | 66 if (permissions.origins.get()) { |
83 ListValue* origin_list = NULL; | 67 for (std::vector<std::string>::iterator it = permissions.origins->begin(); |
84 if (!value->GetList(kOriginsKey, &origin_list)) { | 68 it != permissions.origins->end(); ++it) { |
85 *bad_message = true; | |
86 return false; | |
87 } | |
88 for (size_t i = 0; i < origin_list->GetSize(); ++i) { | |
89 std::string pattern; | |
90 if (!origin_list->GetString(i, &pattern)) { | |
91 *bad_message = true; | |
92 return false; | |
93 } | |
94 | |
95 URLPattern origin(Extension::kValidHostPermissionSchemes); | 69 URLPattern origin(Extension::kValidHostPermissionSchemes); |
96 URLPattern::ParseResult parse_result = origin.Parse(pattern); | 70 URLPattern::ParseResult parse_result = origin.Parse(*it); |
97 if (URLPattern::PARSE_SUCCESS != parse_result) { | 71 if (URLPattern::PARSE_SUCCESS != parse_result) { |
98 *error = ExtensionErrorUtils::FormatErrorMessage( | 72 *error = ExtensionErrorUtils::FormatErrorMessage( |
99 kInvalidOrigin, | 73 kInvalidOrigin, |
100 pattern, | 74 *it, |
101 URLPattern::GetParseResultString(parse_result)); | 75 URLPattern::GetParseResultString(parse_result)); |
102 return false; | 76 return NULL; |
103 } | 77 } |
104 origins.AddPattern(origin); | 78 origins.AddPattern(origin); |
105 } | 79 } |
106 } | 80 } |
107 | 81 |
108 *ptr = new ExtensionPermissionSet(apis, origins, URLPatternSet()); | 82 return scoped_refptr<ExtensionPermissionSet>( |
109 return true; | 83 new ExtensionPermissionSet(apis, origins, URLPatternSet())); |
110 } | 84 } |
111 | 85 |
112 } // namespace permissions_api | 86 } // namespace permissions_api |
113 } // namespace extensions | 87 } // namespace extensions |
OLD | NEW |