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.
4
5 #include "extensions/common/permissions/api_permission.h"
6
7 #include "ui/base/l10n/l10n_util.h"
8
9 namespace {
10
11 using extensions::APIPermission;
12 using extensions::APIPermissionInfo;
13 using extensions::PermissionMessage;
14 using extensions::PermissionMessages;
15
16 class SimpleAPIPermission : public APIPermission {
17 public:
SimpleAPIPermission(const APIPermissionInfo * permission)18 explicit SimpleAPIPermission(const APIPermissionInfo* permission)
19 : APIPermission(permission) { }
20
~SimpleAPIPermission()21 virtual ~SimpleAPIPermission() { }
22
HasMessages() const23 virtual bool HasMessages() const OVERRIDE {
24 return info()->message_id() > PermissionMessage::kNone;
25 }
26
GetMessages() const27 virtual PermissionMessages GetMessages() const OVERRIDE {
28 DCHECK(HasMessages());
29 PermissionMessages result;
30 result.push_back(GetMessage_());
31 return result;
32 }
33
Check(const APIPermission::CheckParam * param) const34 virtual bool Check(
35 const APIPermission::CheckParam* param) const OVERRIDE {
36 return !param;
37 }
38
Contains(const APIPermission * rhs) const39 virtual bool Contains(const APIPermission* rhs) const OVERRIDE {
40 CHECK(info() == rhs->info());
41 return true;
42 }
43
Equal(const APIPermission * rhs) const44 virtual bool Equal(const APIPermission* rhs) const OVERRIDE {
45 if (this == rhs)
46 return true;
47 CHECK(info() == rhs->info());
48 return true;
49 }
50
FromValue(const base::Value * value)51 virtual bool FromValue(const base::Value* value) OVERRIDE {
52 if (value)
53 return false;
54 return true;
55 }
56
ToValue() const57 virtual scoped_ptr<base::Value> ToValue() const OVERRIDE {
58 return scoped_ptr<base::Value>();
59 }
60
Clone() const61 virtual APIPermission* Clone() const OVERRIDE {
62 return new SimpleAPIPermission(info());
63 }
64
Diff(const APIPermission * rhs) const65 virtual APIPermission* Diff(const APIPermission* rhs) const OVERRIDE {
66 CHECK(info() == rhs->info());
67 return NULL;
68 }
69
Union(const APIPermission * rhs) const70 virtual APIPermission* Union(const APIPermission* rhs) const OVERRIDE {
71 CHECK(info() == rhs->info());
72 return new SimpleAPIPermission(info());
73 }
74
Intersect(const APIPermission * rhs) const75 virtual APIPermission* Intersect(const APIPermission* rhs) const OVERRIDE {
76 CHECK(info() == rhs->info());
77 return new SimpleAPIPermission(info());
78 }
79
Write(IPC::Message * m) const80 virtual void Write(IPC::Message* m) const OVERRIDE { }
81
Read(const IPC::Message * m,PickleIterator * iter)82 virtual bool Read(const IPC::Message* m, PickleIterator* iter) OVERRIDE {
83 return true;
84 }
85
Log(std::string * log) const86 virtual void Log(std::string* log) const OVERRIDE { }
87 };
88
89 } // namespace
90
91 namespace extensions {
92
APIPermission(const APIPermissionInfo * info)93 APIPermission::APIPermission(const APIPermissionInfo* info)
94 : info_(info) {
95 DCHECK(info_);
96 }
97
~APIPermission()98 APIPermission::~APIPermission() { }
99
id() const100 APIPermission::ID APIPermission::id() const {
101 return info()->id();
102 }
103
name() const104 const char* APIPermission::name() const {
105 return info()->name();
106 }
107
GetMessage_() const108 PermissionMessage APIPermission::GetMessage_() const {
109 return info()->GetMessage_();
110 }
111
112 //
113 // APIPermissionInfo
114 //
115
APIPermissionInfo(APIPermission::ID id,const char * name,int l10n_message_id,PermissionMessage::ID message_id,int flags,APIPermissionConstructor api_permission_constructor)116 APIPermissionInfo::APIPermissionInfo(
117 APIPermission::ID id,
118 const char* name,
119 int l10n_message_id,
120 PermissionMessage::ID message_id,
121 int flags,
122 APIPermissionConstructor api_permission_constructor)
123 : id_(id),
124 name_(name),
125 flags_(flags),
126 l10n_message_id_(l10n_message_id),
127 message_id_(message_id),
128 api_permission_constructor_(api_permission_constructor) { }
129
130
~APIPermissionInfo()131 APIPermissionInfo::~APIPermissionInfo() { }
132
CreateAPIPermission() const133 APIPermission* APIPermissionInfo::CreateAPIPermission() const {
134 return api_permission_constructor_ ?
135 api_permission_constructor_(this) : new SimpleAPIPermission(this);
136 }
137
GetMessage_() const138 PermissionMessage APIPermissionInfo::GetMessage_() const {
139 return PermissionMessage(
140 message_id_, l10n_util::GetStringUTF16(l10n_message_id_));
141 }
142
143 } // namespace extensions
144