Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: chrome/browser/extensions/api/socket/socket_api.cc

Issue 10694106: Added support for multiple parameters to Extension API callbacks. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Synced. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/socket/socket_api.h" 5 #include "chrome/browser/extensions/api/socket/socket_api.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/extensions/api/api_resource_controller.h" 9 #include "chrome/browser/extensions/api/api_resource_controller.h"
10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 Socket* socket = NULL; 116 Socket* socket = NULL;
117 if (socket_type_ == kSocketTypeTCP) { 117 if (socket_type_ == kSocketTypeTCP) {
118 socket = new TCPSocket(event_notifier_); 118 socket = new TCPSocket(event_notifier_);
119 } else if (socket_type_== kSocketTypeUDP) { 119 } else if (socket_type_== kSocketTypeUDP) {
120 socket = new UDPSocket(event_notifier_); 120 socket = new UDPSocket(event_notifier_);
121 } 121 }
122 DCHECK(socket); 122 DCHECK(socket);
123 123
124 DictionaryValue* result = new DictionaryValue(); 124 DictionaryValue* result = new DictionaryValue();
125 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); 125 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket));
126 result_.reset(result); 126 SetResult(result);
127 } 127 }
128 128
129 bool SocketDestroyFunction::Prepare() { 129 bool SocketDestroyFunction::Prepare() {
130 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 130 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
131 return true; 131 return true;
132 } 132 }
133 133
134 void SocketDestroyFunction::Work() { 134 void SocketDestroyFunction::Work() {
135 if (!controller()->RemoveSocket(socket_id_)) 135 if (!controller()->RemoveSocket(socket_id_))
136 error_ = kSocketNotFoundError; 136 error_ = kSocketNotFoundError;
(...skipping 13 matching lines...) Expand all
150 } 150 }
151 151
152 void SocketConnectFunction::AsyncWorkStart() { 152 void SocketConnectFunction::AsyncWorkStart() {
153 StartDnsLookup(hostname_); 153 StartDnsLookup(hostname_);
154 } 154 }
155 155
156 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { 156 void SocketConnectFunction::AfterDnsLookup(int lookup_result) {
157 if (lookup_result == net::OK) { 157 if (lookup_result == net::OK) {
158 StartConnect(); 158 StartConnect();
159 } else { 159 } else {
160 result_.reset(Value::CreateIntegerValue(lookup_result)); 160 SetResult(Value::CreateIntegerValue(lookup_result));
161 AsyncWorkCompleted(); 161 AsyncWorkCompleted();
162 } 162 }
163 } 163 }
164 164
165 void SocketConnectFunction::StartConnect() { 165 void SocketConnectFunction::StartConnect() {
166 Socket* socket = controller()->GetSocket(socket_id_); 166 Socket* socket = controller()->GetSocket(socket_id_);
167 if (!socket) { 167 if (!socket) {
168 error_ = kSocketNotFoundError; 168 error_ = kSocketNotFoundError;
169 OnConnect(-1); 169 OnConnect(-1);
170 return; 170 return;
171 } 171 }
172 172
173 socket->Connect(resolved_address_, port_, 173 socket->Connect(resolved_address_, port_,
174 base::Bind(&SocketConnectFunction::OnConnect, this)); 174 base::Bind(&SocketConnectFunction::OnConnect, this));
175 } 175 }
176 176
177 void SocketConnectFunction::OnConnect(int result) { 177 void SocketConnectFunction::OnConnect(int result) {
178 result_.reset(Value::CreateIntegerValue(result)); 178 SetResult(Value::CreateIntegerValue(result));
179 AsyncWorkCompleted(); 179 AsyncWorkCompleted();
180 } 180 }
181 181
182 bool SocketDisconnectFunction::Prepare() { 182 bool SocketDisconnectFunction::Prepare() {
183 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 183 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
184 return true; 184 return true;
185 } 185 }
186 186
187 void SocketDisconnectFunction::Work() { 187 void SocketDisconnectFunction::Work() {
188 Socket* socket = controller()->GetSocket(socket_id_); 188 Socket* socket = controller()->GetSocket(socket_id_);
189 if (socket) 189 if (socket)
190 socket->Disconnect(); 190 socket->Disconnect();
191 else 191 else
192 error_ = kSocketNotFoundError; 192 error_ = kSocketNotFoundError;
193 result_.reset(Value::CreateNullValue()); 193 SetResult(Value::CreateNullValue());
194 } 194 }
195 195
196 bool SocketBindFunction::Prepare() { 196 bool SocketBindFunction::Prepare() {
197 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 197 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
198 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); 198 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_));
199 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); 199 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_));
200 return true; 200 return true;
201 } 201 }
202 202
203 void SocketBindFunction::Work() { 203 void SocketBindFunction::Work() {
204 int result = -1; 204 int result = -1;
205 Socket* socket = controller()->GetSocket(socket_id_); 205 Socket* socket = controller()->GetSocket(socket_id_);
206 if (socket) 206 if (socket)
207 result = socket->Bind(address_, port_); 207 result = socket->Bind(address_, port_);
208 else 208 else
209 error_ = kSocketNotFoundError; 209 error_ = kSocketNotFoundError;
210 210
211 result_.reset(Value::CreateIntegerValue(result)); 211 SetResult(Value::CreateIntegerValue(result));
212 } 212 }
213 213
214 SocketReadFunction::SocketReadFunction() 214 SocketReadFunction::SocketReadFunction()
215 : params_(NULL) { 215 : params_(NULL) {
216 } 216 }
217 217
218 SocketReadFunction::~SocketReadFunction() {} 218 SocketReadFunction::~SocketReadFunction() {}
219 219
220 bool SocketReadFunction::Prepare() { 220 bool SocketReadFunction::Prepare() {
221 params_ = api::experimental_socket::Read::Params::Create(*args_); 221 params_ = api::experimental_socket::Read::Params::Create(*args_);
(...skipping 19 matching lines...) Expand all
241 result->SetInteger(kResultCodeKey, bytes_read); 241 result->SetInteger(kResultCodeKey, bytes_read);
242 if (bytes_read > 0) { 242 if (bytes_read > 0) {
243 result->Set(kDataKey, 243 result->Set(kDataKey,
244 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), 244 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(),
245 bytes_read)); 245 bytes_read));
246 } else { 246 } else {
247 // BinaryValue does not support NULL buffer. Workaround it with new char[1]. 247 // BinaryValue does not support NULL buffer. Workaround it with new char[1].
248 // http://crbug.com/127630 248 // http://crbug.com/127630
249 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0)); 249 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0));
250 } 250 }
251 result_.reset(result); 251 SetResult(result);
252 252
253 AsyncWorkCompleted(); 253 AsyncWorkCompleted();
254 } 254 }
255 255
256 SocketWriteFunction::SocketWriteFunction() 256 SocketWriteFunction::SocketWriteFunction()
257 : socket_id_(0), 257 : socket_id_(0),
258 io_buffer_(NULL), 258 io_buffer_(NULL),
259 io_buffer_size_(0) { 259 io_buffer_size_(0) {
260 } 260 }
261 261
(...skipping 18 matching lines...) Expand all
280 return; 280 return;
281 } 281 }
282 282
283 socket->Write(io_buffer_, io_buffer_size_, 283 socket->Write(io_buffer_, io_buffer_size_,
284 base::Bind(&SocketWriteFunction::OnCompleted, this)); 284 base::Bind(&SocketWriteFunction::OnCompleted, this));
285 } 285 }
286 286
287 void SocketWriteFunction::OnCompleted(int bytes_written) { 287 void SocketWriteFunction::OnCompleted(int bytes_written) {
288 DictionaryValue* result = new DictionaryValue(); 288 DictionaryValue* result = new DictionaryValue();
289 result->SetInteger(kBytesWrittenKey, bytes_written); 289 result->SetInteger(kBytesWrittenKey, bytes_written);
290 result_.reset(result); 290 SetResult(result);
291 291
292 AsyncWorkCompleted(); 292 AsyncWorkCompleted();
293 } 293 }
294 294
295 SocketRecvFromFunction::SocketRecvFromFunction() 295 SocketRecvFromFunction::SocketRecvFromFunction()
296 : params_(NULL) { 296 : params_(NULL) {
297 } 297 }
298 298
299 SocketRecvFromFunction::~SocketRecvFromFunction() {} 299 SocketRecvFromFunction::~SocketRecvFromFunction() {}
300 300
(...skipping 25 matching lines...) Expand all
326 result->Set(kDataKey, 326 result->Set(kDataKey,
327 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), 327 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(),
328 bytes_read)); 328 bytes_read));
329 } else { 329 } else {
330 // BinaryValue does not support NULL buffer. Workaround it with new char[1]. 330 // BinaryValue does not support NULL buffer. Workaround it with new char[1].
331 // http://crbug.com/127630 331 // http://crbug.com/127630
332 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0)); 332 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0));
333 } 333 }
334 result->SetString(kAddressKey, address); 334 result->SetString(kAddressKey, address);
335 result->SetInteger(kPortKey, port); 335 result->SetInteger(kPortKey, port);
336 result_.reset(result); 336 SetResult(result);
337 337
338 AsyncWorkCompleted(); 338 AsyncWorkCompleted();
339 } 339 }
340 340
341 SocketSendToFunction::SocketSendToFunction() 341 SocketSendToFunction::SocketSendToFunction()
342 : socket_id_(0), 342 : socket_id_(0),
343 io_buffer_(NULL), 343 io_buffer_(NULL),
344 io_buffer_size_(0) { 344 io_buffer_size_(0) {
345 } 345 }
346 346
(...skipping 12 matching lines...) Expand all
359 } 359 }
360 360
361 void SocketSendToFunction::AsyncWorkStart() { 361 void SocketSendToFunction::AsyncWorkStart() {
362 StartDnsLookup(hostname_); 362 StartDnsLookup(hostname_);
363 } 363 }
364 364
365 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { 365 void SocketSendToFunction::AfterDnsLookup(int lookup_result) {
366 if (lookup_result == net::OK) { 366 if (lookup_result == net::OK) {
367 StartSendTo(); 367 StartSendTo();
368 } else { 368 } else {
369 result_.reset(Value::CreateIntegerValue(lookup_result)); 369 SetResult(Value::CreateIntegerValue(lookup_result));
370 AsyncWorkCompleted(); 370 AsyncWorkCompleted();
371 } 371 }
372 } 372 }
373 373
374 void SocketSendToFunction::StartSendTo() { 374 void SocketSendToFunction::StartSendTo() {
375 Socket* socket = controller()->GetSocket(socket_id_); 375 Socket* socket = controller()->GetSocket(socket_id_);
376 if (!socket) { 376 if (!socket) {
377 error_ = kSocketNotFoundError; 377 error_ = kSocketNotFoundError;
378 OnCompleted(-1); 378 OnCompleted(-1);
379 return; 379 return;
380 } 380 }
381 381
382 socket->SendTo(io_buffer_, io_buffer_size_, resolved_address_, port_, 382 socket->SendTo(io_buffer_, io_buffer_size_, resolved_address_, port_,
383 base::Bind(&SocketSendToFunction::OnCompleted, this)); 383 base::Bind(&SocketSendToFunction::OnCompleted, this));
384 } 384 }
385 385
386 void SocketSendToFunction::OnCompleted(int bytes_written) { 386 void SocketSendToFunction::OnCompleted(int bytes_written) {
387 DictionaryValue* result = new DictionaryValue(); 387 DictionaryValue* result = new DictionaryValue();
388 result->SetInteger(kBytesWrittenKey, bytes_written); 388 result->SetInteger(kBytesWrittenKey, bytes_written);
389 result_.reset(result); 389 SetResult(result);
390 390
391 AsyncWorkCompleted(); 391 AsyncWorkCompleted();
392 } 392 }
393 393
394 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() 394 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction()
395 : params_(NULL) { 395 : params_(NULL) {
396 } 396 }
397 397
398 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} 398 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {}
399 399
400 bool SocketSetKeepAliveFunction::Prepare() { 400 bool SocketSetKeepAliveFunction::Prepare() {
401 params_ = api::experimental_socket::SetKeepAlive::Params::Create(*args_); 401 params_ = api::experimental_socket::SetKeepAlive::Params::Create(*args_);
402 EXTENSION_FUNCTION_VALIDATE(params_.get()); 402 EXTENSION_FUNCTION_VALIDATE(params_.get());
403 return true; 403 return true;
404 } 404 }
405 405
406 void SocketSetKeepAliveFunction::Work() { 406 void SocketSetKeepAliveFunction::Work() {
407 bool result = false; 407 bool result = false;
408 Socket* socket = controller()->GetSocket(params_->socket_id); 408 Socket* socket = controller()->GetSocket(params_->socket_id);
409 if (socket) { 409 if (socket) {
410 int delay = 0; 410 int delay = 0;
411 if (params_->delay.get()) 411 if (params_->delay.get())
412 delay = *params_->delay; 412 delay = *params_->delay;
413 result = socket->SetKeepAlive(params_->enable, delay); 413 result = socket->SetKeepAlive(params_->enable, delay);
414 } else { 414 } else {
415 error_ = kSocketNotFoundError; 415 error_ = kSocketNotFoundError;
416 } 416 }
417 result_.reset(Value::CreateBooleanValue(result)); 417 SetResult(Value::CreateBooleanValue(result));
418 } 418 }
419 419
420 SocketSetNoDelayFunction::SocketSetNoDelayFunction() 420 SocketSetNoDelayFunction::SocketSetNoDelayFunction()
421 : params_(NULL) { 421 : params_(NULL) {
422 } 422 }
423 423
424 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} 424 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {}
425 425
426 bool SocketSetNoDelayFunction::Prepare() { 426 bool SocketSetNoDelayFunction::Prepare() {
427 params_ = api::experimental_socket::SetNoDelay::Params::Create(*args_); 427 params_ = api::experimental_socket::SetNoDelay::Params::Create(*args_);
428 EXTENSION_FUNCTION_VALIDATE(params_.get()); 428 EXTENSION_FUNCTION_VALIDATE(params_.get());
429 return true; 429 return true;
430 } 430 }
431 431
432 void SocketSetNoDelayFunction::Work() { 432 void SocketSetNoDelayFunction::Work() {
433 bool result = false; 433 bool result = false;
434 Socket* socket = controller()->GetSocket(params_->socket_id); 434 Socket* socket = controller()->GetSocket(params_->socket_id);
435 if (socket) 435 if (socket)
436 result = socket->SetNoDelay(params_->no_delay); 436 result = socket->SetNoDelay(params_->no_delay);
437 else 437 else
438 error_ = kSocketNotFoundError; 438 error_ = kSocketNotFoundError;
439 result_.reset(Value::CreateBooleanValue(result)); 439 SetResult(Value::CreateBooleanValue(result));
440 } 440 }
441 441
442 } // namespace extensions 442 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698