Home | History | Annotate | Download | only in common
      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 "ui/base/models/menu_model.h"
      6 
      7 // Get basic type definitions.
      8 #define IPC_MESSAGE_IMPL
      9 #include "chrome/common/automation_messages.h"
     10 
     11 // Generate constructors.
     12 #include "ipc/struct_constructor_macros.h"
     13 #include "chrome/common/automation_messages.h"
     14 
     15 // Generate destructors.
     16 #include "ipc/struct_destructor_macros.h"
     17 #include "chrome/common/automation_messages.h"
     18 
     19 // Generate param traits write methods.
     20 #include "ipc/param_traits_write_macros.h"
     21 namespace IPC {
     22 #include "chrome/common/automation_messages.h"
     23 }  // namespace IPC
     24 
     25 // Generate param traits read methods.
     26 #include "ipc/param_traits_read_macros.h"
     27 namespace IPC {
     28 #include "chrome/common/automation_messages.h"
     29 }  // namespace IPC
     30 
     31 // Generate param traits log methods.
     32 #include "ipc/param_traits_log_macros.h"
     33 namespace IPC {
     34 #include "chrome/common/automation_messages.h"
     35 }  // namespace IPC
     36 
     37 ContextMenuModel::ContextMenuModel() {
     38 }
     39 
     40 ContextMenuModel::~ContextMenuModel() {
     41   for (size_t i = 0; i < items.size(); ++i)
     42     delete items[i].submenu;
     43 }
     44 
     45 ContextMenuModel::Item::Item()
     46     : type(static_cast<int>(ui::MenuModel::TYPE_COMMAND)),
     47       item_id(0),
     48       checked(false),
     49       enabled(true),
     50       submenu(NULL) {
     51 }
     52 
     53 namespace IPC {
     54 
     55 void ParamTraits<ContextMenuModel>::Write(Message* m,
     56                                           const param_type& p) {
     57   WriteParam(m, p.items.size());
     58   for (size_t i = 0; i < p.items.size(); ++i) {
     59     WriteParam(m, static_cast<int>(p.items[i].type));
     60     WriteParam(m, p.items[i].item_id);
     61     WriteParam(m, p.items[i].label);
     62     WriteParam(m, p.items[i].checked);
     63     WriteParam(m, p.items[i].enabled);
     64 
     65     if (p.items[i].type == static_cast<int>(ui::MenuModel::TYPE_SUBMENU)) {
     66       Write(m, *p.items[i].submenu);
     67     }
     68   }
     69 }
     70 
     71 bool ParamTraits<ContextMenuModel>::Read(const Message* m,
     72                                          PickleIterator* iter,
     73                                          param_type* p) {
     74   size_t item_count = 0;
     75   if (!ReadParam(m, iter, &item_count))
     76     return false;
     77 
     78   p->items.reserve(item_count);
     79   for (size_t i = 0; i < item_count; ++i) {
     80     ContextMenuModel::Item item;
     81     if (!ReadParam(m, iter, &item.type))
     82       return false;
     83     if (!ReadParam(m, iter, &item.item_id))
     84       return false;
     85     if (!ReadParam(m, iter, &item.label))
     86       return false;
     87     if (!ReadParam(m, iter, &item.checked))
     88       return false;
     89     if (!ReadParam(m, iter, &item.enabled))
     90       return false;
     91 
     92     if (item.type == static_cast<int>(ui::MenuModel::TYPE_SUBMENU)) {
     93       item.submenu = new ContextMenuModel;
     94       if (!Read(m, iter, item.submenu)) {
     95         delete item.submenu;
     96         item.submenu = NULL;
     97         return false;
     98       }
     99     }
    100 
    101     p->items.push_back(item);
    102   }
    103 
    104   return true;
    105 }
    106 
    107 void ParamTraits<ContextMenuModel>::Log(const param_type& p,
    108                                         std::string* l) {
    109   l->append("(");
    110   for (size_t i = 0; i < p.items.size(); ++i) {
    111     const ContextMenuModel::Item& item = p.items[i];
    112     if (i)
    113       l->append(", ");
    114     l->append("(");
    115     LogParam(item.type, l);
    116     l->append(", ");
    117     LogParam(item.item_id, l);
    118     l->append(", ");
    119     LogParam(item.label, l);
    120     l->append(", ");
    121     LogParam(item.checked, l);
    122     l->append(", ");
    123     LogParam(item.enabled, l);
    124     if (item.type == ui::MenuModel::TYPE_SUBMENU) {
    125       l->append(", ");
    126       Log(*item.submenu, l);
    127     }
    128     l->append(")");
    129   }
    130   l->append(")");
    131 }
    132 
    133 // Only the net::UploadData ParamTraits<> definition needs this definition, so
    134 // keep this in the implementation file so we can forward declare UploadData in
    135 // the header.
    136 template <>
    137 struct ParamTraits<net::UploadElement> {
    138   typedef net::UploadElement param_type;
    139   static void Write(Message* m, const param_type& p) {
    140     WriteParam(m, static_cast<int>(p.type()));
    141     switch (p.type()) {
    142       case net::UploadElement::TYPE_BYTES: {
    143         m->WriteData(p.bytes(), static_cast<int>(p.bytes_length()));
    144         break;
    145       }
    146       default: {
    147         DCHECK(p.type() == net::UploadElement::TYPE_FILE);
    148         WriteParam(m, p.file_path());
    149         WriteParam(m, p.file_range_offset());
    150         WriteParam(m, p.file_range_length());
    151         WriteParam(m, p.expected_file_modification_time());
    152         break;
    153       }
    154     }
    155   }
    156   static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
    157     int type;
    158     if (!ReadParam(m, iter, &type))
    159       return false;
    160     switch (type) {
    161       case net::UploadElement::TYPE_BYTES: {
    162         const char* data;
    163         int len;
    164         if (!m->ReadData(iter, &data, &len))
    165           return false;
    166         r->SetToBytes(data, len);
    167         break;
    168       }
    169       default: {
    170         DCHECK(type == net::UploadElement::TYPE_FILE);
    171         base::FilePath file_path;
    172         uint64 offset, length;
    173         base::Time expected_modification_time;
    174         if (!ReadParam(m, iter, &file_path))
    175           return false;
    176         if (!ReadParam(m, iter, &offset))
    177           return false;
    178         if (!ReadParam(m, iter, &length))
    179           return false;
    180         if (!ReadParam(m, iter, &expected_modification_time))
    181           return false;
    182         r->SetToFilePathRange(file_path, offset, length,
    183                               expected_modification_time);
    184         break;
    185       }
    186     }
    187     return true;
    188   }
    189   static void Log(const param_type& p, std::string* l) {
    190     l->append("<net::UploadElement>");
    191   }
    192 };
    193 
    194 void ParamTraits<scoped_refptr<net::UploadData> >::Write(Message* m,
    195                                                          const param_type& p) {
    196   WriteParam(m, p.get() != NULL);
    197   if (p.get()) {
    198     WriteParam(m, p->elements());
    199     WriteParam(m, p->identifier());
    200     WriteParam(m, p->is_chunked());
    201     WriteParam(m, p->last_chunk_appended());
    202   }
    203 }
    204 
    205 bool ParamTraits<scoped_refptr<net::UploadData> >::Read(const Message* m,
    206                                                         PickleIterator* iter,
    207                                                         param_type* r) {
    208   bool has_object;
    209   if (!ReadParam(m, iter, &has_object))
    210     return false;
    211   if (!has_object)
    212     return true;
    213   ScopedVector<net::UploadElement> elements;
    214   if (!ReadParam(m, iter, &elements))
    215     return false;
    216   int64 identifier;
    217   if (!ReadParam(m, iter, &identifier))
    218     return false;
    219   bool is_chunked = false;
    220   if (!ReadParam(m, iter, &is_chunked))
    221     return false;
    222   bool last_chunk_appended = false;
    223   if (!ReadParam(m, iter, &last_chunk_appended))
    224     return false;
    225   *r = new net::UploadData;
    226   (*r)->swap_elements(&elements);
    227   (*r)->set_identifier(identifier);
    228   (*r)->set_is_chunked(is_chunked);
    229   (*r)->set_last_chunk_appended(last_chunk_appended);
    230   return true;
    231 }
    232 
    233 void ParamTraits<scoped_refptr<net::UploadData> >::Log(const param_type& p,
    234                                                        std::string* l) {
    235   l->append("<net::UploadData>");
    236 }
    237 
    238 void ParamTraits<net::URLRequestStatus>::Write(Message* m,
    239                                                const param_type& p) {
    240   WriteParam(m, static_cast<int>(p.status()));
    241   WriteParam(m, p.error());
    242 }
    243 
    244 bool ParamTraits<net::URLRequestStatus>::Read(const Message* m,
    245                                               PickleIterator* iter,
    246                                               param_type* r) {
    247   int status, error;
    248   if (!ReadParam(m, iter, &status) || !ReadParam(m, iter, &error))
    249     return false;
    250   r->set_status(static_cast<net::URLRequestStatus::Status>(status));
    251   r->set_error(error);
    252   return true;
    253 }
    254 
    255 void ParamTraits<net::URLRequestStatus>::Log(const param_type& p,
    256                                              std::string* l) {
    257   std::string status;
    258   switch (p.status()) {
    259     case net::URLRequestStatus::SUCCESS:
    260       status = "SUCCESS";
    261       break;
    262     case net::URLRequestStatus::IO_PENDING:
    263       status = "IO_PENDING ";
    264       break;
    265     case net::URLRequestStatus::CANCELED:
    266       status = "CANCELED";
    267       break;
    268     case net::URLRequestStatus::FAILED:
    269       status = "FAILED";
    270       break;
    271     default:
    272       status = "UNKNOWN";
    273       break;
    274   }
    275   if (p.status() == net::URLRequestStatus::FAILED)
    276     l->append("(");
    277 
    278   LogParam(status, l);
    279 
    280   if (p.status() == net::URLRequestStatus::FAILED) {
    281     l->append(", ");
    282     LogParam(p.error(), l);
    283     l->append(")");
    284   }
    285 }
    286 
    287 }  // namespace IPC
    288