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/api_permission.h"
7 #include "extensions/common/permissions/api_permission_set.h"
8 #include "ui/base/l10n/l10n_util.h"
12 using extensions::APIPermission
;
13 using extensions::APIPermissionInfo
;
14 using extensions::PermissionMessage
;
15 using extensions::PermissionMessages
;
17 class SimpleAPIPermission
: public APIPermission
{
19 explicit SimpleAPIPermission(const APIPermissionInfo
* permission
)
20 : APIPermission(permission
) { }
22 ~SimpleAPIPermission() override
{}
24 extensions::PermissionIDSet
GetPermissions() const override
{
25 return extensions::PermissionIDSet(id());
28 bool HasMessages() const override
{
29 return info()->message_id() > PermissionMessage::kNone
;
32 PermissionMessages
GetMessages() const override
{
33 DCHECK(HasMessages());
34 PermissionMessages result
;
35 result
.push_back(GetMessage_());
39 bool Check(const APIPermission::CheckParam
* param
) const override
{
43 bool Contains(const APIPermission
* rhs
) const override
{
44 CHECK_EQ(info(), rhs
->info());
48 bool Equal(const APIPermission
* rhs
) const override
{
50 CHECK_EQ(info(), rhs
->info());
54 bool FromValue(const base::Value
* value
,
55 std::string
* /*error*/,
56 std::vector
<std::string
>* /*unhandled_permissions*/) override
{
57 return (value
== NULL
);
60 scoped_ptr
<base::Value
> ToValue() const override
{
61 return scoped_ptr
<base::Value
>();
64 APIPermission
* Clone() const override
{
65 return new SimpleAPIPermission(info());
68 APIPermission
* Diff(const APIPermission
* rhs
) const override
{
69 CHECK_EQ(info(), rhs
->info());
73 APIPermission
* Union(const APIPermission
* rhs
) const override
{
74 CHECK_EQ(info(), rhs
->info());
75 return new SimpleAPIPermission(info());
78 APIPermission
* Intersect(const APIPermission
* rhs
) const override
{
79 CHECK_EQ(info(), rhs
->info());
80 return new SimpleAPIPermission(info());
83 void Write(IPC::Message
* m
) const override
{}
85 bool Read(const IPC::Message
* m
, PickleIterator
* iter
) override
{
89 void Log(std::string
* log
) const override
{}
94 namespace extensions
{
96 APIPermission::APIPermission(const APIPermissionInfo
* info
)
101 APIPermission::~APIPermission() { }
103 APIPermission::ID
APIPermission::id() const {
107 const char* APIPermission::name() const {
108 return info()->name();
111 PermissionMessage
APIPermission::GetMessage_() const {
112 return info()->GetMessage_();
119 APIPermissionInfo::APIPermissionInfo(const APIPermissionInfo::InitInfo
& info
)
123 l10n_message_id_(info
.l10n_message_id
),
124 message_id_(info
.message_id
? info
.message_id
: PermissionMessage::kNone
),
125 api_permission_constructor_(info
.constructor
) {
128 APIPermissionInfo::~APIPermissionInfo() { }
130 APIPermission
* APIPermissionInfo::CreateAPIPermission() const {
131 return api_permission_constructor_
?
132 api_permission_constructor_(this) : new SimpleAPIPermission(this);
135 PermissionMessage
APIPermissionInfo::GetMessage_() const {
136 return PermissionMessage(
137 message_id_
, l10n_util::GetStringUTF16(l10n_message_id_
));
140 } // namespace extensions