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(info() == rhs->info());
     41     return true;
     42   }
     43 
     44   virtual bool Equal(const APIPermission* rhs) const OVERRIDE {
     45     if (this == rhs)
     46       return true;
     47     CHECK(info() == rhs->info());
     48     return true;
     49   }
     50 
     51   virtual bool FromValue(const base::Value* value) OVERRIDE {
     52     if (value)
     53       return false;
     54     return true;
     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(info() == rhs->info());
     67     return NULL;
     68   }
     69 
     70   virtual APIPermission* Union(const APIPermission* rhs) const OVERRIDE {
     71     CHECK(info() == rhs->info());
     72     return new SimpleAPIPermission(info());
     73   }
     74 
     75   virtual APIPermission* Intersect(const APIPermission* rhs) const OVERRIDE {
     76     CHECK(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(
    117     APIPermission::ID id,
    118     const char* name,
    119     int l10n_message_id,
    120     PermissionMessage::ID message_id,
    121     int flags,
    122     APIPermissionConstructor api_permission_constructor)
    123     : id_(id),
    124       name_(name),
    125       flags_(flags),
    126       l10n_message_id_(l10n_message_id),
    127       message_id_(message_id),
    128       api_permission_constructor_(api_permission_constructor) { }
    129 
    130 
    131 APIPermissionInfo::~APIPermissionInfo() { }
    132 
    133 APIPermission* APIPermissionInfo::CreateAPIPermission() const {
    134   return api_permission_constructor_ ?
    135     api_permission_constructor_(this) : new SimpleAPIPermission(this);
    136 }
    137 
    138 PermissionMessage APIPermissionInfo::GetMessage_() const {
    139   return PermissionMessage(
    140       message_id_, l10n_util::GetStringUTF16(l10n_message_id_));
    141 }
    142 
    143 }  // namespace extensions
    144