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/api/permissions.h" |
9 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
| 10 #include "chrome/common/extensions/permissions/bluetooth_device_permission.h" |
10 #include "chrome/common/extensions/permissions/permission_set.h" | 11 #include "chrome/common/extensions/permissions/permission_set.h" |
11 #include "chrome/common/extensions/permissions/permissions_info.h" | 12 #include "chrome/common/extensions/permissions/permissions_info.h" |
12 #include "extensions/common/error_utils.h" | 13 #include "extensions/common/error_utils.h" |
13 #include "extensions/common/url_pattern_set.h" | 14 #include "extensions/common/url_pattern_set.h" |
14 | 15 |
15 using extensions::APIPermission; | 16 using extensions::APIPermission; |
16 using extensions::PermissionSet; | 17 using extensions::PermissionSet; |
17 using extensions::PermissionsInfo; | 18 using extensions::PermissionsInfo; |
18 | 19 |
19 namespace extensions { | 20 namespace extensions { |
20 | 21 |
21 using api::permissions::Permissions; | 22 using api::permissions::Permissions; |
22 | 23 |
23 namespace permissions_api_helpers { | 24 namespace permissions_api_helpers { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 const char kInvalidOrigin[] = | 28 const char kInvalidOrigin[] = |
28 "Invalid value for origin pattern *: *"; | 29 "Invalid value for origin pattern *: *"; |
29 const char kUnknownPermissionError[] = | 30 const char kUnknownPermissionError[] = |
30 "'*' is not a recognized permission."; | 31 "'*' is not a recognized permission."; |
| 32 const char kNonBluetoothPermissionWithArgument[] = |
| 33 "Only the bluetoothDevice permission supports arguments."; |
31 | 34 |
32 } // namespace | 35 } // namespace |
33 | 36 |
34 scoped_ptr<Permissions> PackPermissionSet(const PermissionSet* set) { | 37 scoped_ptr<Permissions> PackPermissionSet(const PermissionSet* set) { |
35 Permissions* permissions(new Permissions()); | 38 Permissions* permissions(new Permissions()); |
36 | 39 |
37 permissions->permissions.reset(new std::vector<std::string>()); | 40 permissions->permissions.reset(new std::vector<std::string>()); |
38 for (APIPermissionSet::const_iterator i = set->apis().begin(); | 41 for (APIPermissionSet::const_iterator i = set->apis().begin(); |
39 i != set->apis().end(); ++i) { | 42 i != set->apis().end(); ++i) { |
40 permissions->permissions->push_back(i->name()); | 43 permissions->permissions->push_back(i->ToString()); |
41 } | 44 } |
42 | 45 |
43 permissions->origins.reset(new std::vector<std::string>()); | 46 permissions->origins.reset(new std::vector<std::string>()); |
44 URLPatternSet hosts = set->explicit_hosts(); | 47 URLPatternSet hosts = set->explicit_hosts(); |
45 for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i) | 48 for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i) |
46 permissions->origins->push_back(i->GetAsString()); | 49 permissions->origins->push_back(i->GetAsString()); |
47 | 50 |
48 return scoped_ptr<Permissions>(permissions); | 51 return scoped_ptr<Permissions>(permissions); |
49 } | 52 } |
50 | 53 |
51 scoped_refptr<PermissionSet> UnpackPermissionSet( | 54 scoped_refptr<PermissionSet> UnpackPermissionSet( |
52 const Permissions& permissions, std::string* error) { | 55 const Permissions& permissions, std::string* error) { |
53 APIPermissionSet apis; | 56 APIPermissionSet apis; |
54 std::vector<std::string>* permissions_list = permissions.permissions.get(); | 57 std::vector<std::string>* permissions_list = permissions.permissions.get(); |
55 if (permissions_list) { | 58 if (permissions_list) { |
56 PermissionsInfo* info = PermissionsInfo::GetInstance(); | 59 PermissionsInfo* info = PermissionsInfo::GetInstance(); |
57 for (std::vector<std::string>::iterator it = permissions_list->begin(); | 60 for (std::vector<std::string>::iterator it = permissions_list->begin(); |
58 it != permissions_list->end(); ++it) { | 61 it != permissions_list->end(); ++it) { |
59 const APIPermissionInfo* permission_info = info->GetByName(*it); | 62 // This is a compromise: we currently can't switch to a blend of |
60 if (!permission_info) { | 63 // objects/strings all the way through the API. Until then, put this |
61 *error = ErrorUtils::FormatErrorMessage( | 64 // processing here. |
62 kUnknownPermissionError, *it); | 65 // http://code.google.com/p/chromium/issues/detail?id=162042 |
63 return NULL; | 66 if (it->find("|") != std::string::npos) { |
| 67 size_t delimiter = it->find("|"); |
| 68 std::string permission_name = it->substr(0, delimiter); |
| 69 std::string permission_arg = it->substr(delimiter + 1); |
| 70 |
| 71 // Restrict this to the bluetoothDevice permission for now, to |
| 72 // discourage the use of this style of permission spreading until it is |
| 73 // better supported. |
| 74 const APIPermissionInfo* permission_info = info->GetByID( |
| 75 APIPermission::kBluetoothDevice); |
| 76 if (permission_name != permission_info->name()) { |
| 77 *error = kNonBluetoothPermissionWithArgument; |
| 78 return NULL; |
| 79 } |
| 80 |
| 81 BluetoothDevicePermission *permission = |
| 82 new BluetoothDevicePermission(permission_info); |
| 83 permission->AddDevicesFromString(permission_arg); |
| 84 |
| 85 apis.insert(permission); |
| 86 } else { |
| 87 const APIPermissionInfo* permission_info = info->GetByName(*it); |
| 88 if (!permission_info) { |
| 89 *error = ErrorUtils::FormatErrorMessage( |
| 90 kUnknownPermissionError, *it); |
| 91 return NULL; |
| 92 } |
| 93 apis.insert(permission_info->id()); |
64 } | 94 } |
65 apis.insert(permission_info->id()); | |
66 } | 95 } |
67 } | 96 } |
68 | 97 |
69 URLPatternSet origins; | 98 URLPatternSet origins; |
70 if (permissions.origins.get()) { | 99 if (permissions.origins.get()) { |
71 for (std::vector<std::string>::iterator it = permissions.origins->begin(); | 100 for (std::vector<std::string>::iterator it = permissions.origins->begin(); |
72 it != permissions.origins->end(); ++it) { | 101 it != permissions.origins->end(); ++it) { |
73 URLPattern origin(Extension::kValidHostPermissionSchemes); | 102 URLPattern origin(Extension::kValidHostPermissionSchemes); |
74 URLPattern::ParseResult parse_result = origin.Parse(*it); | 103 URLPattern::ParseResult parse_result = origin.Parse(*it); |
75 if (URLPattern::PARSE_SUCCESS != parse_result) { | 104 if (URLPattern::PARSE_SUCCESS != parse_result) { |
76 *error = ErrorUtils::FormatErrorMessage( | 105 *error = ErrorUtils::FormatErrorMessage( |
77 kInvalidOrigin, | 106 kInvalidOrigin, |
78 *it, | 107 *it, |
79 URLPattern::GetParseResultString(parse_result)); | 108 URLPattern::GetParseResultString(parse_result)); |
80 return NULL; | 109 return NULL; |
81 } | 110 } |
82 origins.AddPattern(origin); | 111 origins.AddPattern(origin); |
83 } | 112 } |
84 } | 113 } |
85 | 114 |
86 return scoped_refptr<PermissionSet>( | 115 return scoped_refptr<PermissionSet>( |
87 new PermissionSet(apis, origins, URLPatternSet())); | 116 new PermissionSet(apis, origins, URLPatternSet())); |
88 } | 117 } |
89 | 118 |
90 } // namespace permissions_api | 119 } // namespace permissions_api |
91 } // namespace extensions | 120 } // namespace extensions |
OLD | NEW |