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 extensions::PermissionIDSet permissions
;
26 permissions
.insert(id());
30 bool HasMessages() const override
{
31 return info()->message_id() > PermissionMessage::kNone
;
34 PermissionMessages
GetMessages() const override
{
35 DCHECK(HasMessages());
36 PermissionMessages result
;
37 result
.push_back(GetMessage_());
41 bool Check(const APIPermission::CheckParam
* param
) const override
{
45 bool Contains(const APIPermission
* rhs
) const override
{
46 CHECK_EQ(info(), rhs
->info());
50 bool Equal(const APIPermission
* rhs
) const override
{
52 CHECK_EQ(info(), rhs
->info());
56 bool FromValue(const base::Value
* value
,
57 std::string
* /*error*/,
58 std::vector
<std::string
>* /*unhandled_permissions*/) override
{
59 return (value
== NULL
);
62 scoped_ptr
<base::Value
> ToValue() const override
{
63 return scoped_ptr
<base::Value
>();
66 APIPermission
* Clone() const override
{
67 return new SimpleAPIPermission(info());
70 APIPermission
* Diff(const APIPermission
* rhs
) const override
{
71 CHECK_EQ(info(), rhs
->info());
75 APIPermission
* Union(const APIPermission
* rhs
) const override
{
76 CHECK_EQ(info(), rhs
->info());
77 return new SimpleAPIPermission(info());
80 APIPermission
* Intersect(const APIPermission
* rhs
) const override
{
81 CHECK_EQ(info(), rhs
->info());
82 return new SimpleAPIPermission(info());
85 void Write(IPC::Message
* m
) const override
{}
87 bool Read(const IPC::Message
* m
, base::PickleIterator
* iter
) override
{
91 void Log(std::string
* log
) const override
{}
96 namespace extensions
{
98 APIPermission::APIPermission(const APIPermissionInfo
* info
)
103 APIPermission::~APIPermission() { }
105 APIPermission::ID
APIPermission::id() const {
109 const char* APIPermission::name() const {
110 return info()->name();
113 PermissionMessage
APIPermission::GetMessage_() const {
114 return info()->GetMessage_();
121 APIPermissionInfo::APIPermissionInfo(const APIPermissionInfo::InitInfo
& info
)
125 l10n_message_id_(info
.l10n_message_id
),
126 message_id_(info
.message_id
? info
.message_id
: PermissionMessage::kNone
),
127 api_permission_constructor_(info
.constructor
) {
130 APIPermissionInfo::~APIPermissionInfo() { }
132 APIPermission
* APIPermissionInfo::CreateAPIPermission() const {
133 return api_permission_constructor_
?
134 api_permission_constructor_(this) : new SimpleAPIPermission(this);
137 PermissionMessage
APIPermissionInfo::GetMessage_() const {
138 return PermissionMessage(
139 message_id_
, l10n_util::GetStringUTF16(l10n_message_id_
));
142 } // namespace extensions