| 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_udp/udp_socket_event_dispatcher.h" | 5 #include "extensions/browser/api/sockets_udp/udp_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/udp_socket.h" | 10 #include "extensions/browser/api/socket/udp_socket.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value | 112 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value |
| 113 // from "net::ERR_". | 113 // from "net::ERR_". |
| 114 | 114 |
| 115 if (bytes_read >= 0) { | 115 if (bytes_read >= 0) { |
| 116 // Dispatch "onReceive" event. | 116 // Dispatch "onReceive" event. |
| 117 sockets_udp::ReceiveInfo receive_info; | 117 sockets_udp::ReceiveInfo receive_info; |
| 118 receive_info.socket_id = params.socket_id; | 118 receive_info.socket_id = params.socket_id; |
| 119 receive_info.data.assign(io_buffer->data(), io_buffer->data() + bytes_read); | 119 receive_info.data.assign(io_buffer->data(), io_buffer->data() + bytes_read); |
| 120 receive_info.remote_address = address; | 120 receive_info.remote_address = address; |
| 121 receive_info.remote_port = port; | 121 receive_info.remote_port = port; |
| 122 scoped_ptr<base::ListValue> args = | 122 std::unique_ptr<base::ListValue> args = |
| 123 sockets_udp::OnReceive::Create(receive_info); | 123 sockets_udp::OnReceive::Create(receive_info); |
| 124 scoped_ptr<Event> event(new Event(events::SOCKETS_UDP_ON_RECEIVE, | 124 std::unique_ptr<Event> event(new Event(events::SOCKETS_UDP_ON_RECEIVE, |
| 125 sockets_udp::OnReceive::kEventName, | 125 sockets_udp::OnReceive::kEventName, |
| 126 std::move(args))); | 126 std::move(args))); |
| 127 PostEvent(params, std::move(event)); | 127 PostEvent(params, std::move(event)); |
| 128 | 128 |
| 129 // Post a task to delay the read until the socket is available, as | 129 // Post a task to delay the read until the socket is available, as |
| 130 // calling StartReceive at this point would error with ERR_IO_PENDING. | 130 // calling StartReceive at this point would error with ERR_IO_PENDING. |
| 131 BrowserThread::PostTask( | 131 BrowserThread::PostTask( |
| 132 params.thread_id, | 132 params.thread_id, |
| 133 FROM_HERE, | 133 FROM_HERE, |
| 134 base::Bind(&UDPSocketEventDispatcher::StartReceive, params)); | 134 base::Bind(&UDPSocketEventDispatcher::StartReceive, params)); |
| 135 } else if (bytes_read == net::ERR_IO_PENDING) { | 135 } else if (bytes_read == net::ERR_IO_PENDING) { |
| 136 // This happens when resuming a socket which already had an | 136 // This happens when resuming a socket which already had an |
| 137 // active "recv" callback. | 137 // active "recv" callback. |
| 138 } else { | 138 } else { |
| 139 // Dispatch "onReceiveError" event but don't start another read to avoid | 139 // Dispatch "onReceiveError" event but don't start another read to avoid |
| 140 // potential infinite reads if we have a persistent network error. | 140 // potential infinite reads if we have a persistent network error. |
| 141 sockets_udp::ReceiveErrorInfo receive_error_info; | 141 sockets_udp::ReceiveErrorInfo receive_error_info; |
| 142 receive_error_info.socket_id = params.socket_id; | 142 receive_error_info.socket_id = params.socket_id; |
| 143 receive_error_info.result_code = bytes_read; | 143 receive_error_info.result_code = bytes_read; |
| 144 scoped_ptr<base::ListValue> args = | 144 std::unique_ptr<base::ListValue> args = |
| 145 sockets_udp::OnReceiveError::Create(receive_error_info); | 145 sockets_udp::OnReceiveError::Create(receive_error_info); |
| 146 scoped_ptr<Event> event(new Event(events::SOCKETS_UDP_ON_RECEIVE_ERROR, | 146 std::unique_ptr<Event> event( |
| 147 sockets_udp::OnReceiveError::kEventName, | 147 new Event(events::SOCKETS_UDP_ON_RECEIVE_ERROR, |
| 148 std::move(args))); | 148 sockets_udp::OnReceiveError::kEventName, std::move(args))); |
| 149 PostEvent(params, std::move(event)); | 149 PostEvent(params, std::move(event)); |
| 150 | 150 |
| 151 // Since we got an error, the socket is now "paused" until the application | 151 // Since we got an error, the socket is now "paused" until the application |
| 152 // "resumes" it. | 152 // "resumes" it. |
| 153 ResumableUDPSocket* socket = | 153 ResumableUDPSocket* socket = |
| 154 params.sockets->Get(params.extension_id, params.socket_id); | 154 params.sockets->Get(params.extension_id, params.socket_id); |
| 155 if (socket) { | 155 if (socket) { |
| 156 socket->set_paused(true); | 156 socket->set_paused(true); |
| 157 } | 157 } |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 | 160 |
| 161 /* static */ | 161 /* static */ |
| 162 void UDPSocketEventDispatcher::PostEvent(const ReceiveParams& params, | 162 void UDPSocketEventDispatcher::PostEvent(const ReceiveParams& params, |
| 163 scoped_ptr<Event> event) { | 163 std::unique_ptr<Event> event) { |
| 164 DCHECK_CURRENTLY_ON(params.thread_id); | 164 DCHECK_CURRENTLY_ON(params.thread_id); |
| 165 | 165 |
| 166 BrowserThread::PostTask( | 166 BrowserThread::PostTask( |
| 167 BrowserThread::UI, FROM_HERE, | 167 BrowserThread::UI, FROM_HERE, |
| 168 base::Bind(&DispatchEvent, params.browser_context_id, params.extension_id, | 168 base::Bind(&DispatchEvent, params.browser_context_id, params.extension_id, |
| 169 base::Passed(std::move(event)))); | 169 base::Passed(std::move(event)))); |
| 170 } | 170 } |
| 171 | 171 |
| 172 /*static*/ | 172 /*static*/ |
| 173 void UDPSocketEventDispatcher::DispatchEvent(void* browser_context_id, | 173 void UDPSocketEventDispatcher::DispatchEvent(void* browser_context_id, |
| 174 const std::string& extension_id, | 174 const std::string& extension_id, |
| 175 scoped_ptr<Event> event) { | 175 std::unique_ptr<Event> event) { |
| 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 177 | 177 |
| 178 content::BrowserContext* context = | 178 content::BrowserContext* context = |
| 179 reinterpret_cast<content::BrowserContext*>(browser_context_id); | 179 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
| 180 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) | 180 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 181 return; | 181 return; |
| 182 EventRouter* router = EventRouter::Get(context); | 182 EventRouter* router = EventRouter::Get(context); |
| 183 if (router) | 183 if (router) |
| 184 router->DispatchEventToExtension(extension_id, std::move(event)); | 184 router->DispatchEventToExtension(extension_id, std::move(event)); |
| 185 } | 185 } |
| 186 | 186 |
| 187 } // namespace api | 187 } // namespace api |
| 188 } // namespace extensions | 188 } // namespace extensions |
| OLD | NEW |