Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / permissions / api_permission.cc
blob7d8f04e151cc5748d3592c112b577a32fbe892f1
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"
10 namespace {
12 using extensions::APIPermission;
13 using extensions::APIPermissionInfo;
14 using extensions::PermissionMessage;
15 using extensions::PermissionMessages;
17 class SimpleAPIPermission : public APIPermission {
18 public:
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());
27 return permissions;
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_());
38 return result;
41 bool Check(const APIPermission::CheckParam* param) const override {
42 return !param;
45 bool Contains(const APIPermission* rhs) const override {
46 CHECK_EQ(info(), rhs->info());
47 return true;
50 bool Equal(const APIPermission* rhs) const override {
51 if (this != rhs)
52 CHECK_EQ(info(), rhs->info());
53 return true;
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());
72 return NULL;
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 {
88 return true;
91 void Log(std::string* log) const override {}
94 } // namespace
96 namespace extensions {
98 APIPermission::APIPermission(const APIPermissionInfo* info)
99 : info_(info) {
100 DCHECK(info_);
103 APIPermission::~APIPermission() { }
105 APIPermission::ID APIPermission::id() const {
106 return info()->id();
109 const char* APIPermission::name() const {
110 return info()->name();
113 PermissionMessage APIPermission::GetMessage_() const {
114 return info()->GetMessage_();
118 // APIPermissionInfo
121 APIPermissionInfo::APIPermissionInfo(const APIPermissionInfo::InitInfo& info)
122 : id_(info.id),
123 name_(info.name),
124 flags_(info.flags),
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