Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / extensions / common / permissions / api_permission.cc
blobd5fd5a1f3d7d5068505c7d0e90c1985977b4f2da
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 "ui/base/l10n/l10n_util.h"
9 namespace {
11 using extensions::APIPermission;
12 using extensions::APIPermissionInfo;
13 using extensions::PermissionMessage;
14 using extensions::PermissionMessages;
16 class SimpleAPIPermission : public APIPermission {
17 public:
18 explicit SimpleAPIPermission(const APIPermissionInfo* permission)
19 : APIPermission(permission) { }
21 ~SimpleAPIPermission() override {}
23 bool HasMessages() const override {
24 return info()->message_id() > PermissionMessage::kNone;
27 PermissionMessages GetMessages() const override {
28 DCHECK(HasMessages());
29 PermissionMessages result;
30 result.push_back(GetMessage_());
31 return result;
34 bool Check(const APIPermission::CheckParam* param) const override {
35 return !param;
38 bool Contains(const APIPermission* rhs) const override {
39 CHECK_EQ(info(), rhs->info());
40 return true;
43 bool Equal(const APIPermission* rhs) const override {
44 if (this != rhs)
45 CHECK_EQ(info(), rhs->info());
46 return true;
49 bool FromValue(const base::Value* value,
50 std::string* /*error*/,
51 std::vector<std::string>* /*unhandled_permissions*/) override {
52 return (value == NULL);
55 scoped_ptr<base::Value> ToValue() const override {
56 return scoped_ptr<base::Value>();
59 APIPermission* Clone() const override {
60 return new SimpleAPIPermission(info());
63 APIPermission* Diff(const APIPermission* rhs) const override {
64 CHECK_EQ(info(), rhs->info());
65 return NULL;
68 APIPermission* Union(const APIPermission* rhs) const override {
69 CHECK_EQ(info(), rhs->info());
70 return new SimpleAPIPermission(info());
73 APIPermission* Intersect(const APIPermission* rhs) const override {
74 CHECK_EQ(info(), rhs->info());
75 return new SimpleAPIPermission(info());
78 void Write(IPC::Message* m) const override {}
80 bool Read(const IPC::Message* m, PickleIterator* iter) override {
81 return true;
84 void Log(std::string* log) const override {}
87 } // namespace
89 namespace extensions {
91 APIPermission::APIPermission(const APIPermissionInfo* info)
92 : info_(info) {
93 DCHECK(info_);
96 APIPermission::~APIPermission() { }
98 APIPermission::ID APIPermission::id() const {
99 return info()->id();
102 const char* APIPermission::name() const {
103 return info()->name();
106 PermissionMessage APIPermission::GetMessage_() const {
107 return info()->GetMessage_();
111 // APIPermissionInfo
114 APIPermissionInfo::APIPermissionInfo(const APIPermissionInfo::InitInfo& info)
115 : id_(info.id),
116 name_(info.name),
117 flags_(info.flags),
118 l10n_message_id_(info.l10n_message_id),
119 message_id_(info.message_id ? info.message_id : PermissionMessage::kNone),
120 api_permission_constructor_(info.constructor) {
123 APIPermissionInfo::~APIPermissionInfo() { }
125 APIPermission* APIPermissionInfo::CreateAPIPermission() const {
126 return api_permission_constructor_ ?
127 api_permission_constructor_(this) : new SimpleAPIPermission(this);
130 PermissionMessage APIPermissionInfo::GetMessage_() const {
131 return PermissionMessage(
132 message_id_, l10n_util::GetStringUTF16(l10n_message_id_));
135 } // namespace extensions