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

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

Issue 10273016: Refactor the socket API to remove onEvent callback in socket.create() function. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Update Created 8 years, 7 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.h" 5 #include "chrome/browser/extensions/api/socket/socket.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h"
9 #include "net/base/address_list.h" 9 #include "net/base/address_list.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
11 #include "net/base/ip_endpoint.h" 11 #include "net/base/ip_endpoint.h"
12 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
13 #include "net/socket/socket.h" 13 #include "net/socket/socket.h"
14 14
15 namespace extensions { 15 namespace extensions {
16 16
17 Socket::Socket(APIResourceEventNotifier* event_notifier) 17 Socket::Socket(APIResourceEventNotifier* event_notifier)
18 : APIResource(APIResource::SocketResource, event_notifier), 18 : APIResource(APIResource::SocketResource, event_notifier),
19 port_(0), 19 port_(0),
20 is_connected_(false) { 20 is_connected_(false) {
21 } 21 }
22 22
23 Socket::~Socket() { 23 Socket::~Socket() {
24 // Derived destructors should make sure the socket has been closed. 24 // Derived destructors should make sure the socket has been closed.
25 DCHECK(!is_connected_); 25 DCHECK(!is_connected_);
26 } 26 }
27 27
28 void Socket::OnDataRead(scoped_refptr<net::IOBuffer> io_buffer,
29 net::IPEndPoint* address,
30 int result) {
31 // OnDataRead will take ownership of data_value.
32 ListValue* data_value = new ListValue();
33 if (result >= 0) {
34 size_t bytes_size = static_cast<size_t>(result);
35 const char* io_buffer_start = io_buffer->data();
36 for (size_t i = 0; i < bytes_size; ++i) {
37 data_value->Set(i, Value::CreateIntegerValue(io_buffer_start[i]));
38 }
39 }
40
41 std::string ip_address_str;
42 int port = 0;
43 if (address)
44 IPEndPointToStringAndPort(*address, &ip_address_str, &port);
45 event_notifier()->OnDataRead(result, data_value, ip_address_str, port);
46 }
47
48 void Socket::OnWriteComplete(int result) {
49 event_notifier()->OnWriteComplete(result);
50 }
51
52 // static 28 // static
53 bool Socket::StringAndPortToIPEndPoint(const std::string& ip_address_str, 29 bool Socket::StringAndPortToIPEndPoint(const std::string& ip_address_str,
54 int port, 30 int port,
55 net::IPEndPoint* ip_end_point) { 31 net::IPEndPoint* ip_end_point) {
56 DCHECK(ip_end_point); 32 DCHECK(ip_end_point);
57 net::IPAddressNumber ip_number; 33 net::IPAddressNumber ip_number;
58 if (!net::ParseIPLiteralToNumber(ip_address_str, &ip_number)) 34 if (!net::ParseIPLiteralToNumber(ip_address_str, &ip_number))
59 return false; 35 return false;
60 36
61 *ip_end_point = net::IPEndPoint(ip_number, port); 37 *ip_end_point = net::IPEndPoint(ip_number, port);
(...skipping 24 matching lines...) Expand all
86 *ip_address_str = net::NetAddressToString( 62 *ip_address_str = net::NetAddressToString(
87 reinterpret_cast<struct sockaddr*>(&addr), addr_len); 63 reinterpret_cast<struct sockaddr*>(&addr), addr_len);
88 *port = address.port(); 64 *port = address.port();
89 } else { 65 } else {
90 *ip_address_str = ""; 66 *ip_address_str = "";
91 *port = 0; 67 *port = 0;
92 } 68 }
93 } 69 }
94 70
95 } // namespace extensions 71 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/socket.h ('k') | chrome/browser/extensions/api/socket/socket_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698