Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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 "chrome/common/extensions/extension_messages.h"
      6 
      7 #include "chrome/common/extensions/extension.h"
      8 #include "chrome/common/extensions/extension_constants.h"
      9 #include "chrome/common/extensions/manifest.h"
     10 #include "chrome/common/extensions/permissions/permissions_data.h"
     11 #include "chrome/common/extensions/permissions/permissions_info.h"
     12 #include "content/public/common/common_param_traits.h"
     13 
     14 using extensions::APIPermission;
     15 using extensions::APIPermissionInfo;
     16 using extensions::APIPermissionMap;
     17 using extensions::APIPermissionSet;
     18 using extensions::Extension;
     19 using extensions::Manifest;
     20 using extensions::PermissionSet;
     21 using extensions::URLPatternSet;
     22 
     23 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params()
     24     : location(Manifest::INVALID_LOCATION),
     25       creation_flags(Extension::NO_FLAGS){}
     26 
     27 ExtensionMsg_Loaded_Params::~ExtensionMsg_Loaded_Params() {}
     28 
     29 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params(
     30     const Extension* extension)
     31     : manifest(extension->manifest()->value()->DeepCopy()),
     32       location(extension->location()),
     33       path(extension->path()),
     34       apis(extension->GetActivePermissions()->apis()),
     35       explicit_hosts(extension->GetActivePermissions()->explicit_hosts()),
     36       scriptable_hosts(extension->GetActivePermissions()->scriptable_hosts()),
     37       id(extension->id()),
     38       creation_flags(extension->creation_flags()) {
     39 }
     40 
     41 scoped_refptr<Extension> ExtensionMsg_Loaded_Params::ConvertToExtension(
     42     std::string* error) const {
     43   scoped_refptr<Extension> extension =
     44       Extension::Create(path, location, *manifest, creation_flags, error);
     45   if (extension.get()) {
     46     extensions::PermissionsData::SetActivePermissions(
     47         extension.get(),
     48         new PermissionSet(apis, explicit_hosts, scriptable_hosts));
     49   }
     50   return extension;
     51 }
     52 
     53 namespace IPC {
     54 
     55 template <>
     56 struct ParamTraits<Manifest::Location> {
     57   typedef Manifest::Location param_type;
     58   static void Write(Message* m, const param_type& p) {
     59     int val = static_cast<int>(p);
     60     WriteParam(m, val);
     61   }
     62   static bool Read(const Message* m, PickleIterator* iter, param_type* p) {
     63     int val = 0;
     64     if (!ReadParam(m, iter, &val) ||
     65         val < Manifest::INVALID_LOCATION ||
     66         val >= Manifest::NUM_LOCATIONS)
     67       return false;
     68     *p = static_cast<param_type>(val);
     69     return true;
     70   }
     71   static void Log(const param_type& p, std::string* l) {
     72     ParamTraits<int>::Log(static_cast<int>(p), l);
     73   }
     74 };
     75 
     76 void ParamTraits<URLPattern>::Write(Message* m, const param_type& p) {
     77   WriteParam(m, p.valid_schemes());
     78   WriteParam(m, p.GetAsString());
     79 }
     80 
     81 bool ParamTraits<URLPattern>::Read(const Message* m, PickleIterator* iter,
     82                                    param_type* p) {
     83   int valid_schemes;
     84   std::string spec;
     85   if (!ReadParam(m, iter, &valid_schemes) ||
     86       !ReadParam(m, iter, &spec))
     87     return false;
     88 
     89   // TODO(jstritar): We don't want the URLPattern to fail parsing when the
     90   // scheme is invalid. Instead, the pattern should parse but it should not
     91   // match the invalid patterns. We get around this by setting the valid
     92   // schemes after parsing the pattern. Update these method calls once we can
     93   // ignore scheme validation with URLPattern parse options. crbug.com/90544
     94   p->SetValidSchemes(URLPattern::SCHEME_ALL);
     95   URLPattern::ParseResult result = p->Parse(spec);
     96   p->SetValidSchemes(valid_schemes);
     97   return URLPattern::PARSE_SUCCESS == result;
     98 }
     99 
    100 void ParamTraits<URLPattern>::Log(const param_type& p, std::string* l) {
    101   LogParam(p.GetAsString(), l);
    102 }
    103 
    104 void ParamTraits<URLPatternSet>::Write(Message* m, const param_type& p) {
    105   WriteParam(m, p.patterns());
    106 }
    107 
    108 bool ParamTraits<URLPatternSet>::Read(const Message* m, PickleIterator* iter,
    109                                         param_type* p) {
    110   std::set<URLPattern> patterns;
    111   if (!ReadParam(m, iter, &patterns))
    112     return false;
    113 
    114   for (std::set<URLPattern>::iterator i = patterns.begin();
    115        i != patterns.end(); ++i)
    116     p->AddPattern(*i);
    117   return true;
    118 }
    119 
    120 void ParamTraits<URLPatternSet>::Log(const param_type& p, std::string* l) {
    121   LogParam(p.patterns(), l);
    122 }
    123 
    124 void ParamTraits<APIPermission::ID>::Write(
    125     Message* m, const param_type& p) {
    126   WriteParam(m, static_cast<int>(p));
    127 }
    128 
    129 bool ParamTraits<APIPermission::ID>::Read(
    130     const Message* m, PickleIterator* iter, param_type* p) {
    131   int api_id = -2;
    132   if (!ReadParam(m, iter, &api_id))
    133     return false;
    134 
    135   *p = static_cast<APIPermission::ID>(api_id);
    136   return true;
    137 }
    138 
    139 void ParamTraits<APIPermission::ID>::Log(
    140     const param_type& p, std::string* l) {
    141   LogParam(static_cast<int>(p), l);
    142 }
    143 
    144 void ParamTraits<APIPermission*>::Log(
    145     const param_type& p, std::string* l) {
    146   p->Log(l);
    147 }
    148 
    149 void ParamTraits<APIPermissionSet>::Write(
    150     Message* m, const param_type& p) {
    151   APIPermissionSet::const_iterator it = p.begin();
    152   const APIPermissionSet::const_iterator end = p.end();
    153   WriteParam(m, p.size());
    154   for (; it != end; ++it) {
    155     WriteParam(m, it->id());
    156     it->Write(m);
    157   }
    158 }
    159 
    160 bool ParamTraits<APIPermissionSet>::Read(
    161     const Message* m, PickleIterator* iter, param_type* r) {
    162   size_t size;
    163   if (!ReadParam(m, iter, &size))
    164     return false;
    165   for (size_t i = 0; i < size; ++i) {
    166     APIPermission::ID id;
    167     if (!ReadParam(m, iter, &id))
    168       return false;
    169     const APIPermissionInfo* permission_info =
    170       extensions::PermissionsInfo::GetInstance()->GetByID(id);
    171     if (!permission_info)
    172       return false;
    173     scoped_ptr<APIPermission> p(permission_info->CreateAPIPermission());
    174     if (!p->Read(m, iter))
    175       return false;
    176     r->insert(p.release());
    177   }
    178   return true;
    179 }
    180 
    181 void ParamTraits<APIPermissionSet>::Log(
    182     const param_type& p, std::string* l) {
    183   LogParam(p.map(), l);
    184 }
    185 
    186 void ParamTraits<ExtensionMsg_Loaded_Params>::Write(Message* m,
    187                                                     const param_type& p) {
    188   WriteParam(m, p.location);
    189   WriteParam(m, p.path);
    190   WriteParam(m, *(p.manifest));
    191   WriteParam(m, p.creation_flags);
    192   WriteParam(m, p.apis);
    193   WriteParam(m, p.explicit_hosts);
    194   WriteParam(m, p.scriptable_hosts);
    195 }
    196 
    197 bool ParamTraits<ExtensionMsg_Loaded_Params>::Read(const Message* m,
    198                                                    PickleIterator* iter,
    199                                                    param_type* p) {
    200   p->manifest.reset(new base::DictionaryValue());
    201   return ReadParam(m, iter, &p->location) &&
    202          ReadParam(m, iter, &p->path) &&
    203          ReadParam(m, iter, p->manifest.get()) &&
    204          ReadParam(m, iter, &p->creation_flags) &&
    205          ReadParam(m, iter, &p->apis) &&
    206          ReadParam(m, iter, &p->explicit_hosts) &&
    207          ReadParam(m, iter, &p->scriptable_hosts);
    208 }
    209 
    210 void ParamTraits<ExtensionMsg_Loaded_Params>::Log(const param_type& p,
    211                                                   std::string* l) {
    212   l->append(p.id);
    213 }
    214 
    215 }  // namespace IPC
    216