| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/json/json_writer.h" | 5 #include "base/json/json_writer.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/extensions/api/socket/socket_api_controller.h" | 8 #include "chrome/browser/extensions/api/socket/socket_api_controller.h" |
| 9 #include "chrome/browser/extensions/extension_event_router.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/base/rand_callback.h" | 13 #include "net/base/rand_callback.h" |
| 13 #include "net/udp/datagram_socket.h" | 14 #include "net/udp/datagram_socket.h" |
| 14 #include "net/udp/udp_client_socket.h" | 15 #include "net/udp/udp_client_socket.h" |
| 15 #include "net/udp/udp_socket.h" | 16 #include "net/udp/udp_socket.h" |
| 16 | 17 |
| 17 using namespace net; | 18 using namespace net; |
| 18 | 19 |
| 20 namespace events { |
| 21 const char kOnEvent[] = "experimental.socket.onEvent"; |
| 22 }; // namespace events |
| 23 |
| 19 namespace extensions { | 24 namespace extensions { |
| 20 | 25 |
| 26 enum SocketEventType { |
| 27 SOCKET_EVENT_WRITE_COMPLETE |
| 28 }; |
| 29 |
| 30 const char kEventTypeKey[] = "type"; |
| 31 const char kEventTypeWriteComplete[] = "writeComplete"; |
| 32 |
| 33 const char kSrcIdKey[] = "srcId"; |
| 34 const char kIsFinalEventKey[] = "isFinalEvent"; |
| 35 |
| 36 const char kResultCodeKey[] = "resultCode"; |
| 37 |
| 38 std::string SocketEventTypeToString(SocketEventType event_type) { |
| 39 switch (event_type) { |
| 40 case SOCKET_EVENT_WRITE_COMPLETE: |
| 41 return kEventTypeWriteComplete; |
| 42 default: |
| 43 NOTREACHED(); |
| 44 return std::string(); |
| 45 } |
| 46 } |
| 47 |
| 21 // A Socket wraps a low-level socket and includes housekeeping information that | 48 // A Socket wraps a low-level socket and includes housekeeping information that |
| 22 // we need to manage it in the context of an extension. | 49 // we need to manage it in the context of an extension. |
| 23 class Socket { | 50 class Socket { |
| 24 public: | 51 public: |
| 25 Socket(const Profile* profile, const std::string& src_extension_id, | 52 Socket(Profile* profile, const std::string& src_extension_id, int src_id, |
| 26 const GURL& src_url); | 53 const GURL& src_url); |
| 27 ~Socket(); | 54 ~Socket(); |
| 28 | 55 |
| 29 bool Connect(const net::IPEndPoint& ip_end_point); | 56 bool Connect(const net::IPEndPoint& ip_end_point); |
| 30 void Close(); | 57 void Close(); |
| 31 int Write(const std::string message); | 58 int Write(const std::string message); |
| 32 | 59 |
| 60 void OnSocketEvent(SocketEventType event_type, int result_code); |
| 61 |
| 33 private: | 62 private: |
| 34 // TODO(miket): this metadata will enable us to pass events back to the | |
| 35 // extension that created this Socket. | |
| 36 const Profile* profile_; | |
| 37 int id_; | 63 int id_; |
| 64 |
| 65 // This group of variables lets us send events back to the creator extension. |
| 66 Profile* profile_; |
| 38 std::string src_extension_id_; | 67 std::string src_extension_id_; |
| 68 int src_id_; |
| 39 GURL src_url_; | 69 GURL src_url_; |
| 40 | 70 |
| 41 scoped_ptr<net::UDPClientSocket> udp_client_socket_; | 71 scoped_ptr<net::UDPClientSocket> udp_client_socket_; |
| 42 bool is_connected_; | 72 bool is_connected_; |
| 43 | 73 |
| 44 // A callback required by UDPClientSocket::Write(). | 74 void OnWriteComplete(int result); |
| 45 void OnIOComplete(int result); | |
| 46 }; | 75 }; |
| 47 | 76 |
| 48 Socket::Socket(const Profile* profile, const std::string& src_extension_id, | 77 Socket::Socket(Profile* profile, const std::string& src_extension_id, |
| 49 const GURL& src_url) | 78 int src_id, const GURL& src_url) |
| 50 : profile_(profile), | 79 : id_(-1), |
| 80 profile_(profile), |
| 51 src_extension_id_(src_extension_id), | 81 src_extension_id_(src_extension_id), |
| 82 src_id_(src_id), |
| 52 src_url_(src_url), | 83 src_url_(src_url), |
| 53 udp_client_socket_(new UDPClientSocket( | 84 udp_client_socket_(new UDPClientSocket( |
| 54 DatagramSocket::DEFAULT_BIND, | 85 DatagramSocket::DEFAULT_BIND, |
| 55 RandIntCallback(), | 86 RandIntCallback(), |
| 56 NULL, | 87 NULL, |
| 57 NetLog::Source())), | 88 NetLog::Source())), |
| 58 is_connected_(false) {} | 89 is_connected_(false) {} |
| 59 | 90 |
| 60 Socket::~Socket() { | 91 Socket::~Socket() { |
| 61 if (is_connected_) { | 92 if (is_connected_) { |
| 62 Close(); | 93 Close(); |
| 63 } | 94 } |
| 64 } | 95 } |
| 65 | 96 |
| 66 void Socket::OnIOComplete(int result) { | 97 void Socket::OnSocketEvent(SocketEventType event_type, int result_code) { |
| 67 // We don't need to do anything. | 98 // Do we have a destination for this event? |
| 99 if (src_id_ < 0) |
| 100 return; |
| 101 |
| 102 std::string event_type_string = SocketEventTypeToString(event_type); |
| 103 |
| 104 ListValue args; |
| 105 DictionaryValue* event = new DictionaryValue(); |
| 106 event->SetString(kEventTypeKey, event_type_string); |
| 107 event->SetInteger(kSrcIdKey, src_id_); |
| 108 |
| 109 // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is |
| 110 // the framework we'll use, but we need to start using it. |
| 111 event->SetBoolean(kIsFinalEventKey, false); |
| 112 |
| 113 if (event_type == SOCKET_EVENT_WRITE_COMPLETE) { |
| 114 event->SetInteger(kResultCodeKey, result_code); |
| 115 } |
| 116 |
| 117 args.Set(0, event); |
| 118 std::string json_args; |
| 119 base::JSONWriter::Write(&args, false, &json_args); |
| 120 |
| 121 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 122 src_extension_id_, |
| 123 events::kOnEvent, |
| 124 json_args, |
| 125 profile_, |
| 126 src_url_); |
| 68 } | 127 } |
| 69 | 128 |
| 70 bool Socket::Connect(const net::IPEndPoint& ip_end_point) { | 129 bool Socket::Connect(const net::IPEndPoint& ip_end_point) { |
| 71 is_connected_ = udp_client_socket_->Connect(ip_end_point) == net::OK; | 130 is_connected_ = udp_client_socket_->Connect(ip_end_point) == net::OK; |
| 72 return is_connected_; | 131 return is_connected_; |
| 73 } | 132 } |
| 74 | 133 |
| 75 void Socket::Close() { | 134 void Socket::Close() { |
| 76 is_connected_ = false; | 135 is_connected_ = false; |
| 77 udp_client_socket_->Close(); | 136 udp_client_socket_->Close(); |
| 78 } | 137 } |
| 79 | 138 |
| 139 void Socket::OnWriteComplete(int result) { |
| 140 OnSocketEvent(SOCKET_EVENT_WRITE_COMPLETE, result); |
| 141 } |
| 142 |
| 80 int Socket::Write(const std::string message) { | 143 int Socket::Write(const std::string message) { |
| 81 int length = message.length(); | 144 int length = message.length(); |
| 82 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(message)); | 145 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(message)); |
| 83 scoped_refptr<DrainableIOBuffer> buffer( | 146 scoped_refptr<DrainableIOBuffer> buffer( |
| 84 new DrainableIOBuffer(io_buffer, length)); | 147 new DrainableIOBuffer(io_buffer, length)); |
| 85 | 148 |
| 86 int bytes_sent = 0; | 149 int bytes_sent = 0; |
| 87 while (buffer->BytesRemaining()) { | 150 while (buffer->BytesRemaining()) { |
| 88 int rv = udp_client_socket_->Write( | 151 int rv = udp_client_socket_->Write( |
| 89 buffer, buffer->BytesRemaining(), | 152 buffer, buffer->BytesRemaining(), |
| 90 base::Bind(&Socket::OnIOComplete, base::Unretained(this))); | 153 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); |
| 91 if (rv <= 0) { | 154 if (rv <= 0) { |
| 92 // We pass all errors, including ERROR_IO_PENDING, back to the caller. | 155 // We pass all errors, including ERROR_IO_PENDING, back to the caller. |
| 93 return bytes_sent > 0 ? bytes_sent : rv; | 156 return bytes_sent > 0 ? bytes_sent : rv; |
| 94 } | 157 } |
| 95 bytes_sent += rv; | 158 bytes_sent += rv; |
| 96 buffer->DidConsume(rv); | 159 buffer->DidConsume(rv); |
| 97 } | 160 } |
| 98 return bytes_sent; | 161 return bytes_sent; |
| 99 } | 162 } |
| 100 | 163 |
| 101 SocketController::SocketController() : next_socket_id_(1) { | 164 SocketController::SocketController() : next_socket_id_(1) { |
| 102 } | 165 } |
| 103 | 166 |
| 104 SocketController::~SocketController() {} | 167 SocketController::~SocketController() {} |
| 105 | 168 |
| 106 Socket* SocketController::GetSocket(int socket_id) { | 169 Socket* SocketController::GetSocket(int socket_id) { |
| 107 // TODO(miket): we should verify that the extension asking for the | 170 // TODO(miket): we should verify that the extension asking for the |
| 108 // socket is the same one that created it. | 171 // socket is the same one that created it. |
| 109 SocketMap::iterator i = socket_map_.find(socket_id); | 172 SocketMap::iterator i = socket_map_.find(socket_id); |
| 110 if (i != socket_map_.end()) | 173 if (i != socket_map_.end()) |
| 111 return i->second.get(); | 174 return i->second.get(); |
| 112 return NULL; | 175 return NULL; |
| 113 } | 176 } |
| 114 | 177 |
| 115 int SocketController::CreateUdp(const Profile* profile, | 178 int SocketController::CreateUdp(Profile* profile, |
| 116 const std::string& extension_id, | 179 const std::string& extension_id, |
| 180 int src_id, |
| 117 const GURL& src_url) { | 181 const GURL& src_url) { |
| 118 linked_ptr<Socket> socket(new Socket(profile, extension_id, src_url)); | 182 linked_ptr<Socket> socket(new Socket(profile, extension_id, src_id, |
| 183 src_url)); |
| 119 CHECK(socket.get()); | 184 CHECK(socket.get()); |
| 120 socket_map_[next_socket_id_] = socket; | 185 socket_map_[next_socket_id_] = socket; |
| 121 return next_socket_id_++; | 186 return next_socket_id_++; |
| 122 } | 187 } |
| 123 | 188 |
| 124 bool SocketController::DestroyUdp(int socket_id) { | 189 bool SocketController::DestroyUdp(int socket_id) { |
| 125 Socket* socket = GetSocket(socket_id); | 190 Socket* socket = GetSocket(socket_id); |
| 126 if (!socket) | 191 if (!socket) |
| 127 return false; | 192 return false; |
| 128 delete socket; | 193 delete socket; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 229 |
| 165 int SocketController::WriteUdp(int socket_id, const std::string message) { | 230 int SocketController::WriteUdp(int socket_id, const std::string message) { |
| 166 Socket* socket = GetSocket(socket_id); | 231 Socket* socket = GetSocket(socket_id); |
| 167 if (!socket) { | 232 if (!socket) { |
| 168 return -1; | 233 return -1; |
| 169 } | 234 } |
| 170 return socket->Write(message); | 235 return socket->Write(message); |
| 171 } | 236 } |
| 172 | 237 |
| 173 } // namespace extensions | 238 } // namespace extensions |
| OLD | NEW |