By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / chrome / common / automation_messages.cc
blob1a600058cc707a524dc9c00cd3d2767dc20edec4
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.
5 #include "ui/base/models/menu_model.h"
7 // Get basic type definitions.
8 #define IPC_MESSAGE_IMPL
9 #include "chrome/common/automation_messages.h"
11 // Generate constructors.
12 #include "ipc/struct_constructor_macros.h"
13 #include "chrome/common/automation_messages.h"
15 // Generate destructors.
16 #include "ipc/struct_destructor_macros.h"
17 #include "chrome/common/automation_messages.h"
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
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
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
37 ContextMenuModel::ContextMenuModel() {
40 ContextMenuModel::~ContextMenuModel() {
41 for (size_t i = 0; i < items.size(); ++i)
42 delete items[i].submenu;
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) {
53 namespace IPC {
55 void ParamTraits<AutomationMouseEvent>::Write(Message* m,
56 const param_type& p) {
57 WriteParam(m, std::string(reinterpret_cast<const char*>(&p.mouse_event),
58 sizeof(p.mouse_event)));
59 WriteParam(m, p.location_script_chain);
62 bool ParamTraits<AutomationMouseEvent>::Read(const Message* m,
63 PickleIterator* iter,
64 param_type* p) {
65 std::string mouse_event;
66 if (!ReadParam(m, iter, &mouse_event))
67 return false;
68 memcpy(&p->mouse_event, mouse_event.c_str(), mouse_event.length());
69 if (!ReadParam(m, iter, &p->location_script_chain))
70 return false;
71 return true;
74 void ParamTraits<AutomationMouseEvent>::Log(const param_type& p,
75 std::string* l) {
76 l->append("(");
77 LogParam(std::string(reinterpret_cast<const char*>(&p.mouse_event),
78 sizeof(p.mouse_event)),
79 l);
80 l->append(", ");
81 LogParam(p.location_script_chain, l);
82 l->append(")");
85 void ParamTraits<ContextMenuModel>::Write(Message* m,
86 const param_type& p) {
87 WriteParam(m, p.items.size());
88 for (size_t i = 0; i < p.items.size(); ++i) {
89 WriteParam(m, static_cast<int>(p.items[i].type));
90 WriteParam(m, p.items[i].item_id);
91 WriteParam(m, p.items[i].label);
92 WriteParam(m, p.items[i].checked);
93 WriteParam(m, p.items[i].enabled);
95 if (p.items[i].type == static_cast<int>(ui::MenuModel::TYPE_SUBMENU)) {
96 Write(m, *p.items[i].submenu);
101 bool ParamTraits<ContextMenuModel>::Read(const Message* m,
102 PickleIterator* iter,
103 param_type* p) {
104 size_t item_count = 0;
105 if (!ReadParam(m, iter, &item_count))
106 return false;
108 p->items.reserve(item_count);
109 for (size_t i = 0; i < item_count; ++i) {
110 ContextMenuModel::Item item;
111 if (!ReadParam(m, iter, &item.type))
112 return false;
113 if (!ReadParam(m, iter, &item.item_id))
114 return false;
115 if (!ReadParam(m, iter, &item.label))
116 return false;
117 if (!ReadParam(m, iter, &item.checked))
118 return false;
119 if (!ReadParam(m, iter, &item.enabled))
120 return false;
122 if (item.type == static_cast<int>(ui::MenuModel::TYPE_SUBMENU)) {
123 item.submenu = new ContextMenuModel;
124 if (!Read(m, iter, item.submenu)) {
125 delete item.submenu;
126 item.submenu = NULL;
127 return false;
131 p->items.push_back(item);
134 return true;
137 void ParamTraits<ContextMenuModel>::Log(const param_type& p,
138 std::string* l) {
139 l->append("(");
140 for (size_t i = 0; i < p.items.size(); ++i) {
141 const ContextMenuModel::Item& item = p.items[i];
142 if (i)
143 l->append(", ");
144 l->append("(");
145 LogParam(item.type, l);
146 l->append(", ");
147 LogParam(item.item_id, l);
148 l->append(", ");
149 LogParam(item.label, l);
150 l->append(", ");
151 LogParam(item.checked, l);
152 l->append(", ");
153 LogParam(item.enabled, l);
154 if (item.type == ui::MenuModel::TYPE_SUBMENU) {
155 l->append(", ");
156 Log(*item.submenu, l);
158 l->append(")");
160 l->append(")");
163 // Only the net::UploadData ParamTraits<> definition needs this definition, so
164 // keep this in the implementation file so we can forward declare UploadData in
165 // the header.
166 template <>
167 struct ParamTraits<net::UploadElement> {
168 typedef net::UploadElement param_type;
169 static void Write(Message* m, const param_type& p) {
170 WriteParam(m, static_cast<int>(p.type()));
171 switch (p.type()) {
172 case net::UploadElement::TYPE_BYTES: {
173 m->WriteData(p.bytes(), static_cast<int>(p.bytes_length()));
174 break;
176 default: {
177 DCHECK(p.type() == net::UploadElement::TYPE_FILE);
178 WriteParam(m, p.file_path());
179 WriteParam(m, p.file_range_offset());
180 WriteParam(m, p.file_range_length());
181 WriteParam(m, p.expected_file_modification_time());
182 break;
186 static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
187 int type;
188 if (!ReadParam(m, iter, &type))
189 return false;
190 switch (type) {
191 case net::UploadElement::TYPE_BYTES: {
192 const char* data;
193 int len;
194 if (!m->ReadData(iter, &data, &len))
195 return false;
196 r->SetToBytes(data, len);
197 break;
199 default: {
200 DCHECK(type == net::UploadElement::TYPE_FILE);
201 FilePath file_path;
202 uint64 offset, length;
203 base::Time expected_modification_time;
204 if (!ReadParam(m, iter, &file_path))
205 return false;
206 if (!ReadParam(m, iter, &offset))
207 return false;
208 if (!ReadParam(m, iter, &length))
209 return false;
210 if (!ReadParam(m, iter, &expected_modification_time))
211 return false;
212 r->SetToFilePathRange(file_path, offset, length,
213 expected_modification_time);
214 break;
217 return true;
219 static void Log(const param_type& p, std::string* l) {
220 l->append("<net::UploadElement>");
224 void ParamTraits<scoped_refptr<net::UploadData> >::Write(Message* m,
225 const param_type& p) {
226 WriteParam(m, p.get() != NULL);
227 if (p) {
228 WriteParam(m, p->elements());
229 WriteParam(m, p->identifier());
230 WriteParam(m, p->is_chunked());
231 WriteParam(m, p->last_chunk_appended());
235 bool ParamTraits<scoped_refptr<net::UploadData> >::Read(const Message* m,
236 PickleIterator* iter,
237 param_type* r) {
238 bool has_object;
239 if (!ReadParam(m, iter, &has_object))
240 return false;
241 if (!has_object)
242 return true;
243 ScopedVector<net::UploadElement> elements;
244 if (!ReadParam(m, iter, &elements))
245 return false;
246 int64 identifier;
247 if (!ReadParam(m, iter, &identifier))
248 return false;
249 bool is_chunked = false;
250 if (!ReadParam(m, iter, &is_chunked))
251 return false;
252 bool last_chunk_appended = false;
253 if (!ReadParam(m, iter, &last_chunk_appended))
254 return false;
255 *r = new net::UploadData;
256 (*r)->swap_elements(&elements);
257 (*r)->set_identifier(identifier);
258 (*r)->set_is_chunked(is_chunked);
259 (*r)->set_last_chunk_appended(last_chunk_appended);
260 return true;
263 void ParamTraits<scoped_refptr<net::UploadData> >::Log(const param_type& p,
264 std::string* l) {
265 l->append("<net::UploadData>");
268 void ParamTraits<net::URLRequestStatus>::Write(Message* m,
269 const param_type& p) {
270 WriteParam(m, static_cast<int>(p.status()));
271 WriteParam(m, p.error());
274 bool ParamTraits<net::URLRequestStatus>::Read(const Message* m,
275 PickleIterator* iter,
276 param_type* r) {
277 int status, error;
278 if (!ReadParam(m, iter, &status) || !ReadParam(m, iter, &error))
279 return false;
280 r->set_status(static_cast<net::URLRequestStatus::Status>(status));
281 r->set_error(error);
282 return true;
285 void ParamTraits<net::URLRequestStatus>::Log(const param_type& p,
286 std::string* l) {
287 std::string status;
288 switch (p.status()) {
289 case net::URLRequestStatus::SUCCESS:
290 status = "SUCCESS";
291 break;
292 case net::URLRequestStatus::IO_PENDING:
293 status = "IO_PENDING ";
294 break;
295 case net::URLRequestStatus::CANCELED:
296 status = "CANCELED";
297 break;
298 case net::URLRequestStatus::FAILED:
299 status = "FAILED";
300 break;
301 default:
302 status = "UNKNOWN";
303 break;
305 if (p.status() == net::URLRequestStatus::FAILED)
306 l->append("(");
308 LogParam(status, l);
310 if (p.status() == net::URLRequestStatus::FAILED) {
311 l->append(", ");
312 LogParam(p.error(), l);
313 l->append(")");
317 } // namespace IPC