| 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/common/extensions/permissions/api_permission_set.h" | 5 #include "chrome/common/extensions/permissions/api_permission_set.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/common/extensions/extension_error_utils.h" | |
| 11 #include "chrome/common/extensions/extension_manifest_constants.h" | 10 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 12 #include "chrome/common/extensions/permissions/permissions_info.h" | 11 #include "chrome/common/extensions/permissions/permissions_info.h" |
| 12 #include "extensions/common/error_utils.h" |
| 13 | 13 |
| 14 namespace errors = extension_manifest_errors; | 14 namespace errors = extension_manifest_errors; |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 using extensions::PermissionsInfo; | 18 using extensions::PermissionsInfo; |
| 19 using extensions::APIPermission; | 19 using extensions::APIPermission; |
| 20 using extensions::APIPermissionInfo; | 20 using extensions::APIPermissionInfo; |
| 21 using extensions::APIPermissionSet; | 21 using extensions::APIPermissionSet; |
| 22 using extensions::ErrorUtils; |
| 22 | 23 |
| 23 bool CreateAPIPermission( | 24 bool CreateAPIPermission( |
| 24 const std::string& permission_str, | 25 const std::string& permission_str, |
| 25 const base::Value* permission_value, | 26 const base::Value* permission_value, |
| 26 APIPermissionSet* api_permissions, | 27 APIPermissionSet* api_permissions, |
| 27 string16* error, | 28 string16* error, |
| 28 std::vector<std::string>* unhandled_permissions) { | 29 std::vector<std::string>* unhandled_permissions) { |
| 29 PermissionsInfo* info = PermissionsInfo::GetInstance(); | 30 PermissionsInfo* info = PermissionsInfo::GetInstance(); |
| 30 | 31 |
| 31 const APIPermissionInfo* permission_info = info->GetByName(permission_str); | 32 const APIPermissionInfo* permission_info = info->GetByName(permission_str); |
| 32 if (permission_info) { | 33 if (permission_info) { |
| 33 scoped_ptr<APIPermission> permission( | 34 scoped_ptr<APIPermission> permission( |
| 34 permission_info->CreateAPIPermission()); | 35 permission_info->CreateAPIPermission()); |
| 35 if (!permission->FromValue(permission_value)) { | 36 if (!permission->FromValue(permission_value)) { |
| 36 if (error) { | 37 if (error) { |
| 37 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 38 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 38 errors::kInvalidPermission, permission_info->name()); | 39 errors::kInvalidPermission, permission_info->name()); |
| 39 return false; | 40 return false; |
| 40 } | 41 } |
| 41 LOG(WARNING) << "Parse permission failed."; | 42 LOG(WARNING) << "Parse permission failed."; |
| 42 } else { | 43 } else { |
| 43 api_permissions->insert(permission.release()); | 44 api_permissions->insert(permission.release()); |
| 44 } | 45 } |
| 45 return true; | 46 return true; |
| 46 } | 47 } |
| 47 | 48 |
| 48 if (unhandled_permissions) | 49 if (unhandled_permissions) |
| 49 unhandled_permissions->push_back(permission_str); | 50 unhandled_permissions->push_back(permission_str); |
| 50 else | 51 else |
| 51 LOG(WARNING) << "Unknown permission[" << permission_str << "]."; | 52 LOG(WARNING) << "Unknown permission[" << permission_str << "]."; |
| 52 | 53 |
| 53 return true; | 54 return true; |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool ParseChildPermissions(const std::string& base_name, | 57 bool ParseChildPermissions(const std::string& base_name, |
| 57 const Value* permission_value, | 58 const Value* permission_value, |
| 58 APIPermissionSet* api_permissions, | 59 APIPermissionSet* api_permissions, |
| 59 string16* error, | 60 string16* error, |
| 60 std::vector<std::string>* unhandled_permissions) { | 61 std::vector<std::string>* unhandled_permissions) { |
| 61 if (permission_value) { | 62 if (permission_value) { |
| 62 const ListValue* permissions; | 63 const ListValue* permissions; |
| 63 if (!permission_value->GetAsList(&permissions)) { | 64 if (!permission_value->GetAsList(&permissions)) { |
| 64 if (error) { | 65 if (error) { |
| 65 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 66 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 66 errors::kInvalidPermission, base_name); | 67 errors::kInvalidPermission, base_name); |
| 67 return false; | 68 return false; |
| 68 } | 69 } |
| 69 LOG(WARNING) << "Permission value is not a list."; | 70 LOG(WARNING) << "Permission value is not a list."; |
| 70 // Failed to parse, but since error is NULL, failures are not fatal so | 71 // Failed to parse, but since error is NULL, failures are not fatal so |
| 71 // return true here anyway. | 72 // return true here anyway. |
| 72 return true; | 73 return true; |
| 73 } | 74 } |
| 74 | 75 |
| 75 for (size_t i = 0; i < permissions->GetSize(); ++i) { | 76 for (size_t i = 0; i < permissions->GetSize(); ++i) { |
| 76 std::string permission_str; | 77 std::string permission_str; |
| 77 if (!permissions->GetString(i, &permission_str)) { | 78 if (!permissions->GetString(i, &permission_str)) { |
| 78 // permission should be a string | 79 // permission should be a string |
| 79 if (error) { | 80 if (error) { |
| 80 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 81 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 81 errors::kInvalidPermission, | 82 errors::kInvalidPermission, |
| 82 base_name + '.' + base::IntToString(i)); | 83 base_name + '.' + base::IntToString(i)); |
| 83 return false; | 84 return false; |
| 84 } | 85 } |
| 85 LOG(WARNING) << "Permission is not a string."; | 86 LOG(WARNING) << "Permission is not a string."; |
| 86 continue; | 87 continue; |
| 87 } | 88 } |
| 88 | 89 |
| 89 if (!CreateAPIPermission(base_name + '.' + permission_str, NULL, | 90 if (!CreateAPIPermission(base_name + '.' + permission_str, NULL, |
| 90 api_permissions, error, unhandled_permissions)) | 91 api_permissions, error, unhandled_permissions)) |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 std::vector<std::string>* unhandled_permissions) { | 287 std::vector<std::string>* unhandled_permissions) { |
| 287 PermissionsInfo* info = PermissionsInfo::GetInstance(); | 288 PermissionsInfo* info = PermissionsInfo::GetInstance(); |
| 288 for (size_t i = 0; i < permissions->GetSize(); ++i) { | 289 for (size_t i = 0; i < permissions->GetSize(); ++i) { |
| 289 std::string permission_str; | 290 std::string permission_str; |
| 290 const base::Value* permission_value = NULL; | 291 const base::Value* permission_value = NULL; |
| 291 if (!permissions->GetString(i, &permission_str)) { | 292 if (!permissions->GetString(i, &permission_str)) { |
| 292 const base::DictionaryValue* dict = NULL; | 293 const base::DictionaryValue* dict = NULL; |
| 293 // permission should be a string or a single key dict. | 294 // permission should be a string or a single key dict. |
| 294 if (!permissions->GetDictionary(i, &dict) || dict->size() != 1) { | 295 if (!permissions->GetDictionary(i, &dict) || dict->size() != 1) { |
| 295 if (error) { | 296 if (error) { |
| 296 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | 297 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 297 errors::kInvalidPermission, base::IntToString(i)); | 298 errors::kInvalidPermission, base::IntToString(i)); |
| 298 return false; | 299 return false; |
| 299 } | 300 } |
| 300 LOG(WARNING) << "Permission is not a string or single key dict."; | 301 LOG(WARNING) << "Permission is not a string or single key dict."; |
| 301 continue; | 302 continue; |
| 302 } | 303 } |
| 303 base::DictionaryValue::Iterator it(*dict); | 304 base::DictionaryValue::Iterator it(*dict); |
| 304 permission_str = it.key(); | 305 permission_str = it.key(); |
| 305 permission_value = &it.value(); | 306 permission_value = &it.value(); |
| 306 } | 307 } |
| 307 | 308 |
| 308 // Check if this permission is a special case where its value should | 309 // Check if this permission is a special case where its value should |
| 309 // be treated as a list of child permissions. | 310 // be treated as a list of child permissions. |
| 310 if (info->HasChildPermissions(permission_str)) { | 311 if (info->HasChildPermissions(permission_str)) { |
| 311 if (!ParseChildPermissions(permission_str, permission_value, | 312 if (!ParseChildPermissions(permission_str, permission_value, |
| 312 api_permissions, error, unhandled_permissions)) | 313 api_permissions, error, unhandled_permissions)) |
| 313 return false; | 314 return false; |
| 314 continue; | 315 continue; |
| 315 } | 316 } |
| 316 | 317 |
| 317 if (!CreateAPIPermission(permission_str, permission_value, | 318 if (!CreateAPIPermission(permission_str, permission_value, |
| 318 api_permissions, error, unhandled_permissions)) | 319 api_permissions, error, unhandled_permissions)) |
| 319 return false; | 320 return false; |
| 320 } | 321 } |
| 321 return true; | 322 return true; |
| 322 } | 323 } |
| 323 | 324 |
| 324 } // namespace extensions | 325 } // namespace extensions |
| OLD | NEW |