| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "extensions/browser/api/socket/tcp_socket.h" | 10 #include "extensions/browser/api/socket/tcp_socket.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 | 127 |
| 128 if (bytes_read == 0) { | 128 if (bytes_read == 0) { |
| 129 bytes_read = net::ERR_CONNECTION_CLOSED; | 129 bytes_read = net::ERR_CONNECTION_CLOSED; |
| 130 } | 130 } |
| 131 | 131 |
| 132 if (bytes_read > 0) { | 132 if (bytes_read > 0) { |
| 133 // Dispatch "onReceive" event. | 133 // Dispatch "onReceive" event. |
| 134 sockets_tcp::ReceiveInfo receive_info; | 134 sockets_tcp::ReceiveInfo receive_info; |
| 135 receive_info.socket_id = params.socket_id; | 135 receive_info.socket_id = params.socket_id; |
| 136 receive_info.data.assign(io_buffer->data(), io_buffer->data() + bytes_read); | 136 receive_info.data.assign(io_buffer->data(), io_buffer->data() + bytes_read); |
| 137 scoped_ptr<base::ListValue> args = | 137 std::unique_ptr<base::ListValue> args = |
| 138 sockets_tcp::OnReceive::Create(receive_info); | 138 sockets_tcp::OnReceive::Create(receive_info); |
| 139 scoped_ptr<Event> event(new Event(events::SOCKETS_TCP_ON_RECEIVE, | 139 std::unique_ptr<Event> event(new Event(events::SOCKETS_TCP_ON_RECEIVE, |
| 140 sockets_tcp::OnReceive::kEventName, | 140 sockets_tcp::OnReceive::kEventName, |
| 141 std::move(args))); | 141 std::move(args))); |
| 142 PostEvent(params, std::move(event)); | 142 PostEvent(params, std::move(event)); |
| 143 | 143 |
| 144 // Post a task to delay the read until the socket is available, as | 144 // Post a task to delay the read until the socket is available, as |
| 145 // calling StartReceive at this point would error with ERR_IO_PENDING. | 145 // calling StartReceive at this point would error with ERR_IO_PENDING. |
| 146 BrowserThread::PostTask( | 146 BrowserThread::PostTask( |
| 147 params.thread_id, | 147 params.thread_id, |
| 148 FROM_HERE, | 148 FROM_HERE, |
| 149 base::Bind(&TCPSocketEventDispatcher::StartRead, params)); | 149 base::Bind(&TCPSocketEventDispatcher::StartRead, params)); |
| 150 } else if (bytes_read == net::ERR_IO_PENDING) { | 150 } else if (bytes_read == net::ERR_IO_PENDING) { |
| 151 // This happens when resuming a socket which already had an | 151 // This happens when resuming a socket which already had an |
| 152 // active "read" callback. | 152 // active "read" callback. |
| 153 } else { | 153 } else { |
| 154 // Dispatch "onReceiveError" event but don't start another read to avoid | 154 // Dispatch "onReceiveError" event but don't start another read to avoid |
| 155 // potential infinite reads if we have a persistent network error. | 155 // potential infinite reads if we have a persistent network error. |
| 156 sockets_tcp::ReceiveErrorInfo receive_error_info; | 156 sockets_tcp::ReceiveErrorInfo receive_error_info; |
| 157 receive_error_info.socket_id = params.socket_id; | 157 receive_error_info.socket_id = params.socket_id; |
| 158 receive_error_info.result_code = bytes_read; | 158 receive_error_info.result_code = bytes_read; |
| 159 scoped_ptr<base::ListValue> args = | 159 std::unique_ptr<base::ListValue> args = |
| 160 sockets_tcp::OnReceiveError::Create(receive_error_info); | 160 sockets_tcp::OnReceiveError::Create(receive_error_info); |
| 161 scoped_ptr<Event> event(new Event(events::SOCKETS_TCP_ON_RECEIVE_ERROR, | 161 std::unique_ptr<Event> event( |
| 162 sockets_tcp::OnReceiveError::kEventName, | 162 new Event(events::SOCKETS_TCP_ON_RECEIVE_ERROR, |
| 163 std::move(args))); | 163 sockets_tcp::OnReceiveError::kEventName, std::move(args))); |
| 164 PostEvent(params, std::move(event)); | 164 PostEvent(params, std::move(event)); |
| 165 | 165 |
| 166 // Since we got an error, the socket is now "paused" until the application | 166 // Since we got an error, the socket is now "paused" until the application |
| 167 // "resumes" it. | 167 // "resumes" it. |
| 168 ResumableTCPSocket* socket = | 168 ResumableTCPSocket* socket = |
| 169 params.sockets->Get(params.extension_id, params.socket_id); | 169 params.sockets->Get(params.extension_id, params.socket_id); |
| 170 if (socket) { | 170 if (socket) { |
| 171 socket->set_paused(true); | 171 socket->set_paused(true); |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 } | 174 } |
| 175 | 175 |
| 176 // static | 176 // static |
| 177 void TCPSocketEventDispatcher::PostEvent(const ReadParams& params, | 177 void TCPSocketEventDispatcher::PostEvent(const ReadParams& params, |
| 178 scoped_ptr<Event> event) { | 178 std::unique_ptr<Event> event) { |
| 179 DCHECK_CURRENTLY_ON(params.thread_id); | 179 DCHECK_CURRENTLY_ON(params.thread_id); |
| 180 | 180 |
| 181 BrowserThread::PostTask( | 181 BrowserThread::PostTask( |
| 182 BrowserThread::UI, FROM_HERE, | 182 BrowserThread::UI, FROM_HERE, |
| 183 base::Bind(&DispatchEvent, params.browser_context_id, params.extension_id, | 183 base::Bind(&DispatchEvent, params.browser_context_id, params.extension_id, |
| 184 base::Passed(std::move(event)))); | 184 base::Passed(std::move(event)))); |
| 185 } | 185 } |
| 186 | 186 |
| 187 // static | 187 // static |
| 188 void TCPSocketEventDispatcher::DispatchEvent(void* browser_context_id, | 188 void TCPSocketEventDispatcher::DispatchEvent(void* browser_context_id, |
| 189 const std::string& extension_id, | 189 const std::string& extension_id, |
| 190 scoped_ptr<Event> event) { | 190 std::unique_ptr<Event> event) { |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 192 | 192 |
| 193 content::BrowserContext* context = | 193 content::BrowserContext* context = |
| 194 reinterpret_cast<content::BrowserContext*>(browser_context_id); | 194 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
| 195 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) | 195 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 196 return; | 196 return; |
| 197 | 197 |
| 198 EventRouter* event_router = EventRouter::Get(context); | 198 EventRouter* event_router = EventRouter::Get(context); |
| 199 if (event_router) | 199 if (event_router) |
| 200 event_router->DispatchEventToExtension(extension_id, std::move(event)); | 200 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 } // namespace api | 203 } // namespace api |
| 204 } // namespace extensions | 204 } // namespace extensions |
| OLD | NEW |