14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "extensions/common/permissions/api_permission.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::APIPermission;
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::APIPermissionInfo;
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::PermissionMessage;
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using extensions::PermissionMessages;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SimpleAPIPermission : public APIPermission {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit SimpleAPIPermission(const APIPermissionInfo* permission)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : APIPermission(permission) { }
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~SimpleAPIPermission() { }
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool HasMessages() const OVERRIDE {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return info()->message_id() > PermissionMessage::kNone;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual PermissionMessages GetMessages() const OVERRIDE {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(HasMessages());
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PermissionMessages result;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result.push_back(GetMessage_());
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return result;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Check(
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const APIPermission::CheckParam* param) const OVERRIDE {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return !param;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Contains(const APIPermission* rhs) const OVERRIDE {
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CHECK_EQ(info(), rhs->info());
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equal(const APIPermission* rhs) const OVERRIDE {
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (this != rhs)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      CHECK_EQ(info(), rhs->info());
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
50c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual bool FromValue(
51c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      const base::Value* value,
52c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      std::string* /*error*/,
53c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      std::vector<std::string>* /*unhandled_permissions*/) OVERRIDE {
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return (value == NULL);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual scoped_ptr<base::Value> ToValue() const OVERRIDE {
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return scoped_ptr<base::Value>();
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual APIPermission* Clone() const OVERRIDE {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return new SimpleAPIPermission(info());
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual APIPermission* Diff(const APIPermission* rhs) const OVERRIDE {
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CHECK_EQ(info(), rhs->info());
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual APIPermission* Union(const APIPermission* rhs) const OVERRIDE {
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CHECK_EQ(info(), rhs->info());
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return new SimpleAPIPermission(info());
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual APIPermission* Intersect(const APIPermission* rhs) const OVERRIDE {
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CHECK_EQ(info(), rhs->info());
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return new SimpleAPIPermission(info());
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Write(IPC::Message* m) const OVERRIDE { }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Read(const IPC::Message* m, PickleIterator* iter) OVERRIDE {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Log(std::string* log) const OVERRIDE { }
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)APIPermission::APIPermission(const APIPermissionInfo* info)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  : info_(info) {
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(info_);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)APIPermission::~APIPermission() { }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)APIPermission::ID APIPermission::id() const {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return info()->id();
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* APIPermission::name() const {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return info()->name();
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PermissionMessage APIPermission::GetMessage_() const {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return info()->GetMessage_();
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// APIPermissionInfo
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)APIPermissionInfo::APIPermissionInfo(const APIPermissionInfo::InitInfo& info)
117010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    : id_(info.id),
118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      name_(info.name),
119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      flags_(info.flags),
120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      l10n_message_id_(info.l10n_message_id),
121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      message_id_(info.message_id ? info.message_id : PermissionMessage::kNone),
122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      api_permission_constructor_(info.constructor) {
123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)APIPermissionInfo::~APIPermissionInfo() { }
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)APIPermission* APIPermissionInfo::CreateAPIPermission() const {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return api_permission_constructor_ ?
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    api_permission_constructor_(this) : new SimpleAPIPermission(this);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PermissionMessage APIPermissionInfo::GetMessage_() const {
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return PermissionMessage(
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      message_id_, l10n_util::GetStringUTF16(l10n_message_id_));
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace extensions
138