Home | History | Annotate | Download | only in permissions
      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:
     18   explicit SimpleAPIPermission(const APIPermissionInfo* permission)
     19     : APIPermission(permission) { }
     20 
     21   virtual ~SimpleAPIPermission() { }
     22 
     23   virtual bool HasMessages() const OVERRIDE {
     24     return info()->message_id() > PermissionMessage::kNone;
     25   }
     26 
     27   virtual PermissionMessages GetMessages() const OVERRIDE {
     28     DCHECK(HasMessages());
     29     PermissionMessages result;
     30     result.push_back(GetMessage_());
     31     return result;
     32   }
     33 
     34   virtual bool Check(
     35       const APIPermission::CheckParam* param) const OVERRIDE {
     36     return !param;
     37   }
     38 
     39   virtual bool Contains(const APIPermission* rhs) const OVERRIDE {
     40     CHECK_EQ(info(), rhs->info());
     41     return true;
     42   }
     43 
     44   virtual bool Equal(const APIPermission* rhs) const OVERRIDE {
     45     if (this != rhs)
     46       CHECK_EQ(info(), rhs->info());
     47     return true;
     48   }
     49 
     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 
     57   virtual scoped_ptr<base::Value> ToValue() const OVERRIDE {
     58     return scoped_ptr<base::Value>();
     59   }
     60 
     61   virtual APIPermission* Clone() const OVERRIDE {
     62     return new SimpleAPIPermission(info());
     63   }
     64 
     65   virtual APIPermission* Diff(const APIPermission* rhs) const OVERRIDE {
     66     CHECK_EQ(info(), rhs->info());
     67     return NULL;
     68   }
     69 
     70   virtual APIPermission* Union(const APIPermission* rhs) const OVERRIDE {
     71     CHECK_EQ(info(), rhs->info());
     72     return new SimpleAPIPermission(info());
     73   }
     74 
     75   virtual APIPermission* Intersect(const APIPermission* rhs) const OVERRIDE {
     76     CHECK_EQ(info(), rhs->info());
     77     return new SimpleAPIPermission(info());
     78   }
     79 
     80   virtual void Write(IPC::Message* m) const OVERRIDE { }
     81 
     82   virtual bool Read(const IPC::Message* m, PickleIterator* iter) OVERRIDE {
     83     return true;
     84   }
     85 
     86   virtual void Log(std::string* log) const OVERRIDE { }
     87 };
     88 
     89 }  // namespace
     90 
     91 namespace extensions {
     92 
     93 APIPermission::APIPermission(const APIPermissionInfo* info)
     94   : info_(info) {
     95   DCHECK(info_);
     96 }
     97 
     98 APIPermission::~APIPermission() { }
     99 
    100 APIPermission::ID APIPermission::id() const {
    101   return info()->id();
    102 }
    103 
    104 const char* APIPermission::name() const {
    105   return info()->name();
    106 }
    107 
    108 PermissionMessage APIPermission::GetMessage_() const {
    109   return info()->GetMessage_();
    110 }
    111 
    112 //
    113 // APIPermissionInfo
    114 //
    115 
    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 
    125 APIPermissionInfo::~APIPermissionInfo() { }
    126 
    127 APIPermission* APIPermissionInfo::CreateAPIPermission() const {
    128   return api_permission_constructor_ ?
    129     api_permission_constructor_(this) : new SimpleAPIPermission(this);
    130 }
    131 
    132 PermissionMessage APIPermissionInfo::GetMessage_() const {
    133   return PermissionMessage(
    134       message_id_, l10n_util::GetStringUTF16(l10n_message_id_));
    135 }
    136 
    137 }  // namespace extensions
    138