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"
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
));
34 if (unhandled_permissions
)
35 unhandled_permissions
->push_back(permission_name
);
37 LOG(WARNING
) << "Unknown permission[" << permission_name
<< "].";
41 if (!permission
->FromValue(permission_value
)) {
43 *error
= ErrorUtils::FormatErrorMessageUTF16(
44 errors::kInvalidPermission
, permission_name
);
47 LOG(WARNING
) << "Parse permission failed.";
50 manifest_permissions
->insert(permission
.release());
57 namespace extensions
{
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) {
73 *error
= ErrorUtils::FormatErrorMessageUTF16(
74 errors::kInvalidPermission
, base::IntToString(i
));
77 LOG(WARNING
) << "Permission is not a string or single key dict.";
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
))
93 } // namespace extensions