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_EQ(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 CHECK_EQ(info(), rhs->info());
47 return true;
48 }
49
FromValue(const base::Value * value,std::string *,std::vector<std::string> *)50 virtual bool FromValue(
51 const base::Value* value,
52 std::string* /*error*/,
53 std::vector<std::string>* /*unhandled_permissions*/) OVERRIDE {
54 return (value == NULL);
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_EQ(info(), rhs->info());
67 return NULL;
68 }
69
Union(const APIPermission * rhs) const70 virtual APIPermission* Union(const APIPermission* rhs) const OVERRIDE {
71 CHECK_EQ(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_EQ(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(const APIPermissionInfo::InitInfo & info)116 APIPermissionInfo::APIPermissionInfo(const APIPermissionInfo::InitInfo& info)
117 : id_(info.id),
118 name_(info.name),
119 flags_(info.flags),
120 l10n_message_id_(info.l10n_message_id),
121 message_id_(info.message_id ? info.message_id : PermissionMessage::kNone),
122 api_permission_constructor_(info.constructor) {
123 }
124
~APIPermissionInfo()125 APIPermissionInfo::~APIPermissionInfo() { }
126
CreateAPIPermission() const127 APIPermission* APIPermissionInfo::CreateAPIPermission() const {
128 return api_permission_constructor_ ?
129 api_permission_constructor_(this) : new SimpleAPIPermission(this);
130 }
131
GetMessage_() const132 PermissionMessage APIPermissionInfo::GetMessage_() const {
133 return PermissionMessage(
134 message_id_, l10n_util::GetStringUTF16(l10n_message_id_));
135 }
136
137 } // namespace extensions
138