1 // Copyright 2014 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 #ifndef EXTENSIONS_COMMON_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_
6 #define EXTENSIONS_COMMON_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_
11 #include "base/json/json_writer.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/permissions/api_permission.h"
16 #include "ipc/ipc_message.h"
17 #include "ipc/ipc_message_utils.h"
19 namespace extensions
{
21 // An abstract base class for permissions that are represented by the
22 // disjunction of a set of conditions. Each condition is represented by a
23 // |PermissionDataType| (e.g. SocketPermissionData). If an
24 // APIPermission::CheckParam matches any of the conditions in the set, the
25 // permission is granted.
27 // For an example of how to use this class, see SocketPermission.
28 template <class PermissionDataType
, class DerivedType
>
29 class SetDisjunctionPermission
: public APIPermission
{
31 explicit SetDisjunctionPermission(const APIPermissionInfo
* info
)
32 : APIPermission(info
) {}
34 ~SetDisjunctionPermission() override
{}
36 // APIPermission overrides
37 bool Check(const APIPermission::CheckParam
* param
) const override
{
38 for (typename
std::set
<PermissionDataType
>::const_iterator i
=
48 bool Contains(const APIPermission
* rhs
) const override
{
49 CHECK(rhs
->info() == info());
50 const SetDisjunctionPermission
* perm
=
51 static_cast<const SetDisjunctionPermission
*>(rhs
);
52 return base::STLIncludes
<std::set
<PermissionDataType
> >(
53 data_set_
, perm
->data_set_
);
56 bool Equal(const APIPermission
* rhs
) const override
{
57 CHECK(rhs
->info() == info());
58 const SetDisjunctionPermission
* perm
=
59 static_cast<const SetDisjunctionPermission
*>(rhs
);
60 return data_set_
== perm
->data_set_
;
63 APIPermission
* Clone() const override
{
64 SetDisjunctionPermission
* result
= new DerivedType(info());
65 result
->data_set_
= data_set_
;
69 APIPermission
* Diff(const APIPermission
* rhs
) const override
{
70 CHECK(rhs
->info() == info());
71 const SetDisjunctionPermission
* perm
=
72 static_cast<const SetDisjunctionPermission
*>(rhs
);
73 scoped_ptr
<SetDisjunctionPermission
> result(new DerivedType(info()));
74 result
->data_set_
= base::STLSetDifference
<std::set
<PermissionDataType
> >(
75 data_set_
, perm
->data_set_
);
76 return result
->data_set_
.empty() ? NULL
: result
.release();
79 APIPermission
* Union(const APIPermission
* rhs
) const override
{
80 CHECK(rhs
->info() == info());
81 const SetDisjunctionPermission
* perm
=
82 static_cast<const SetDisjunctionPermission
*>(rhs
);
83 scoped_ptr
<SetDisjunctionPermission
> result(new DerivedType(info()));
84 result
->data_set_
= base::STLSetUnion
<std::set
<PermissionDataType
> >(
85 data_set_
, perm
->data_set_
);
86 return result
.release();
89 APIPermission
* Intersect(const APIPermission
* rhs
) const override
{
90 CHECK(rhs
->info() == info());
91 const SetDisjunctionPermission
* perm
=
92 static_cast<const SetDisjunctionPermission
*>(rhs
);
93 scoped_ptr
<SetDisjunctionPermission
> result(new DerivedType(info()));
94 result
->data_set_
= base::STLSetIntersection
<std::set
<PermissionDataType
> >(
95 data_set_
, perm
->data_set_
);
96 return result
->data_set_
.empty() ? NULL
: result
.release();
100 const base::Value
* value
,
102 std::vector
<std::string
>* unhandled_permissions
) override
{
104 const base::ListValue
* list
= NULL
;
106 if (!value
|| !value
->GetAsList(&list
) || list
->GetSize() == 0) {
108 *error
= "NULL or empty permission list";
112 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
113 const base::Value
* item_value
= NULL
;
114 bool got_item
= list
->Get(i
, &item_value
);
118 PermissionDataType data
;
119 if (data
.FromValue(item_value
)) {
120 data_set_
.insert(data
);
122 std::string unknown_permission
;
123 base::JSONWriter::Write(*item_value
, &unknown_permission
);
124 if (unhandled_permissions
) {
125 unhandled_permissions
->push_back(unknown_permission
);
128 *error
= "Cannot parse an item from the permission list: " +
138 scoped_ptr
<base::Value
> ToValue() const override
{
139 base::ListValue
* list
= new base::ListValue();
140 typename
std::set
<PermissionDataType
>::const_iterator i
;
141 for (i
= data_set_
.begin(); i
!= data_set_
.end(); ++i
) {
142 scoped_ptr
<base::Value
> item_value(i
->ToValue());
143 list
->Append(item_value
.release());
145 return scoped_ptr
<base::Value
>(list
);
148 void Write(IPC::Message
* m
) const override
{
149 IPC::WriteParam(m
, data_set_
);
152 bool Read(const IPC::Message
* m
, base::PickleIterator
* iter
) override
{
153 return IPC::ReadParam(m
, iter
, &data_set_
);
156 void Log(std::string
* log
) const override
{
157 IPC::LogParam(data_set_
, log
);
161 std::set
<PermissionDataType
> data_set_
;
164 } // namespace extensions
166 #endif // EXTENSIONS_COMMON_PERMISSIONS_SET_DISJUNCTION_PERMISSION_H_