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