| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/sockets_tcp/tcp_socket_event_dispatcher.h" | 5 #include "extensions/browser/api/sockets_tcp/tcp_socket_event_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "extensions/browser/api/socket/tcp_socket.h" | 8 #include "extensions/browser/api/socket/tcp_socket.h" |
| 9 #include "extensions/browser/event_router.h" | 9 #include "extensions/browser/event_router.h" |
| 10 #include "extensions/browser/extension_system.h" | 10 #include "extensions/browser/extension_system.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 bytes_read = net::ERR_CONNECTION_CLOSED; | 124 bytes_read = net::ERR_CONNECTION_CLOSED; |
| 125 } | 125 } |
| 126 | 126 |
| 127 if (bytes_read > 0) { | 127 if (bytes_read > 0) { |
| 128 // Dispatch "onReceive" event. | 128 // Dispatch "onReceive" event. |
| 129 sockets_tcp::ReceiveInfo receive_info; | 129 sockets_tcp::ReceiveInfo receive_info; |
| 130 receive_info.socket_id = params.socket_id; | 130 receive_info.socket_id = params.socket_id; |
| 131 receive_info.data.assign(io_buffer->data(), io_buffer->data() + bytes_read); | 131 receive_info.data.assign(io_buffer->data(), io_buffer->data() + bytes_read); |
| 132 scoped_ptr<base::ListValue> args = | 132 scoped_ptr<base::ListValue> args = |
| 133 sockets_tcp::OnReceive::Create(receive_info); | 133 sockets_tcp::OnReceive::Create(receive_info); |
| 134 scoped_ptr<Event> event( | 134 scoped_ptr<Event> event(new Event( |
| 135 new Event(sockets_tcp::OnReceive::kEventName, args.Pass())); | 135 events::UNKNOWN, sockets_tcp::OnReceive::kEventName, args.Pass())); |
| 136 PostEvent(params, event.Pass()); | 136 PostEvent(params, event.Pass()); |
| 137 | 137 |
| 138 // Post a task to delay the read until the socket is available, as | 138 // Post a task to delay the read until the socket is available, as |
| 139 // calling StartReceive at this point would error with ERR_IO_PENDING. | 139 // calling StartReceive at this point would error with ERR_IO_PENDING. |
| 140 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
| 141 params.thread_id, | 141 params.thread_id, |
| 142 FROM_HERE, | 142 FROM_HERE, |
| 143 base::Bind(&TCPSocketEventDispatcher::StartRead, params)); | 143 base::Bind(&TCPSocketEventDispatcher::StartRead, params)); |
| 144 } else if (bytes_read == net::ERR_IO_PENDING) { | 144 } else if (bytes_read == net::ERR_IO_PENDING) { |
| 145 // This happens when resuming a socket which already had an | 145 // This happens when resuming a socket which already had an |
| 146 // active "read" callback. | 146 // active "read" callback. |
| 147 } else { | 147 } else { |
| 148 // Dispatch "onReceiveError" event but don't start another read to avoid | 148 // Dispatch "onReceiveError" event but don't start another read to avoid |
| 149 // potential infinite reads if we have a persistent network error. | 149 // potential infinite reads if we have a persistent network error. |
| 150 sockets_tcp::ReceiveErrorInfo receive_error_info; | 150 sockets_tcp::ReceiveErrorInfo receive_error_info; |
| 151 receive_error_info.socket_id = params.socket_id; | 151 receive_error_info.socket_id = params.socket_id; |
| 152 receive_error_info.result_code = bytes_read; | 152 receive_error_info.result_code = bytes_read; |
| 153 scoped_ptr<base::ListValue> args = | 153 scoped_ptr<base::ListValue> args = |
| 154 sockets_tcp::OnReceiveError::Create(receive_error_info); | 154 sockets_tcp::OnReceiveError::Create(receive_error_info); |
| 155 scoped_ptr<Event> event( | 155 scoped_ptr<Event> event(new Event( |
| 156 new Event(sockets_tcp::OnReceiveError::kEventName, args.Pass())); | 156 events::UNKNOWN, sockets_tcp::OnReceiveError::kEventName, args.Pass())); |
| 157 PostEvent(params, event.Pass()); | 157 PostEvent(params, event.Pass()); |
| 158 | 158 |
| 159 // Since we got an error, the socket is now "paused" until the application | 159 // Since we got an error, the socket is now "paused" until the application |
| 160 // "resumes" it. | 160 // "resumes" it. |
| 161 ResumableTCPSocket* socket = | 161 ResumableTCPSocket* socket = |
| 162 params.sockets->Get(params.extension_id, params.socket_id); | 162 params.sockets->Get(params.extension_id, params.socket_id); |
| 163 if (socket) { | 163 if (socket) { |
| 164 socket->set_paused(true); | 164 socket->set_paused(true); |
| 165 } | 165 } |
| 166 } | 166 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 190 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) | 190 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 191 return; | 191 return; |
| 192 | 192 |
| 193 EventRouter* event_router = EventRouter::Get(context); | 193 EventRouter* event_router = EventRouter::Get(context); |
| 194 if (event_router) | 194 if (event_router) |
| 195 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 195 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
| 196 } | 196 } |
| 197 | 197 |
| 198 } // namespace core_api | 198 } // namespace core_api |
| 199 } // namespace extensions | 199 } // namespace extensions |
| OLD | NEW |