Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / common / permissions / manifest_permission_set.cc
blobfecb611dbe16ea7a2ef729b86aceddeebde86f8d
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/common/permissions/manifest_permission_set.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/values.h"
10 #include "extensions/common/error_utils.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "extensions/common/manifest_handler.h"
13 #include "extensions/common/permissions/manifest_permission.h"
15 namespace {
17 using extensions::ErrorUtils;
18 using extensions::ManifestPermission;
19 using extensions::ManifestPermissionSet;
20 using extensions::ManifestHandler;
21 namespace errors = extensions::manifest_errors;
23 bool CreateManifestPermission(
24 const std::string& permission_name,
25 const base::Value* permission_value,
26 ManifestPermissionSet* manifest_permissions,
27 base::string16* error,
28 std::vector<std::string>* unhandled_permissions) {
30 scoped_ptr<ManifestPermission> permission(
31 ManifestHandler::CreatePermission(permission_name));
33 if (!permission) {
34 if (unhandled_permissions)
35 unhandled_permissions->push_back(permission_name);
36 else
37 LOG(WARNING) << "Unknown permission[" << permission_name << "].";
38 return true;
41 if (!permission->FromValue(permission_value)) {
42 if (error) {
43 *error = ErrorUtils::FormatErrorMessageUTF16(
44 errors::kInvalidPermission, permission_name);
45 return false;
47 LOG(WARNING) << "Parse permission failed.";
48 return true;
49 } else {
50 manifest_permissions->insert(permission.release());
51 return true;
55 } // namespace
57 namespace extensions {
59 // static
60 bool ManifestPermissionSet::ParseFromJSON(
61 const base::ListValue* permissions,
62 ManifestPermissionSet* manifest_permissions,
63 base::string16* error,
64 std::vector<std::string>* unhandled_permissions) {
65 for (size_t i = 0; i < permissions->GetSize(); ++i) {
66 std::string permission_name;
67 const base::Value* permission_value = NULL;
68 if (!permissions->GetString(i, &permission_name)) {
69 const base::DictionaryValue* dict = NULL;
70 // permission should be a string or a single key dict.
71 if (!permissions->GetDictionary(i, &dict) || dict->size() != 1) {
72 if (error) {
73 *error = ErrorUtils::FormatErrorMessageUTF16(
74 errors::kInvalidPermission, base::IntToString(i));
75 return false;
77 LOG(WARNING) << "Permission is not a string or single key dict.";
78 continue;
80 base::DictionaryValue::Iterator it(*dict);
81 permission_name = it.key();
82 permission_value = &it.value();
85 if (!CreateManifestPermission(permission_name, permission_value,
86 manifest_permissions, error,
87 unhandled_permissions))
88 return false;
90 return true;
93 } // namespace extensions