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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/sockets_udp/udp_socket_event_dispatcher.h"
7 #include "extensions/browser/api/socket/udp_socket.h"
8 #include "extensions/browser/event_router.h"
9 #include "extensions/browser/extensions_browser_client.h"
10 #include "net/base/net_errors.h"
12 namespace extensions {
15 using content::BrowserThread;
17 static base::LazyInstance<
18 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher> > g_factory =
19 LAZY_INSTANCE_INITIALIZER;
22 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>*
23 UDPSocketEventDispatcher::GetFactoryInstance() {
24 return g_factory.Pointer();
28 UDPSocketEventDispatcher* UDPSocketEventDispatcher::Get(
29 content::BrowserContext* context) {
30 DCHECK_CURRENTLY_ON(BrowserThread::UI);
32 return BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>::Get(context);
35 UDPSocketEventDispatcher::UDPSocketEventDispatcher(
36 content::BrowserContext* context)
37 : thread_id_(Socket::kThreadId), browser_context_(context) {
38 ApiResourceManager<ResumableUDPSocket>* manager =
39 ApiResourceManager<ResumableUDPSocket>::Get(browser_context_);
41 << "There is no socket manager. "
42 "If this assertion is failing during a test, then it is likely that "
43 "TestExtensionSystem is failing to provide an instance of "
44 "ApiResourceManager<ResumableUDPSocket>.";
45 sockets_ = manager->data_;
48 UDPSocketEventDispatcher::~UDPSocketEventDispatcher() {}
50 UDPSocketEventDispatcher::ReceiveParams::ReceiveParams() {}
52 UDPSocketEventDispatcher::ReceiveParams::~ReceiveParams() {}
54 void UDPSocketEventDispatcher::OnSocketBind(const std::string& extension_id,
56 OnSocketResume(extension_id, socket_id);
59 void UDPSocketEventDispatcher::OnSocketResume(const std::string& extension_id,
61 DCHECK_CURRENTLY_ON(thread_id_);
64 params.thread_id = thread_id_;
65 params.browser_context_id = browser_context_;
66 params.extension_id = extension_id;
67 params.sockets = sockets_;
68 params.socket_id = socket_id;
74 void UDPSocketEventDispatcher::StartReceive(const ReceiveParams& params) {
75 DCHECK_CURRENTLY_ON(params.thread_id);
77 ResumableUDPSocket* socket =
78 params.sockets->Get(params.extension_id, params.socket_id);
80 // This can happen if the socket is closed while our callback is active.
83 DCHECK(params.extension_id == socket->owner_extension_id())
84 << "Socket has wrong owner.";
86 // Don't start another read if the socket has been paused.
90 int buffer_size = (socket->buffer_size() <= 0 ? 4096 : socket->buffer_size());
93 base::Bind(&UDPSocketEventDispatcher::ReceiveCallback, params));
97 void UDPSocketEventDispatcher::ReceiveCallback(
98 const ReceiveParams& params,
100 scoped_refptr<net::IOBuffer> io_buffer,
101 const std::string& address,
103 DCHECK_CURRENTLY_ON(params.thread_id);
105 // If |bytes_read| == 0, the message contained no data.
106 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value
109 if (bytes_read >= 0) {
110 // Dispatch "onReceive" event.
111 sockets_udp::ReceiveInfo receive_info;
112 receive_info.socket_id = params.socket_id;
113 receive_info.data = std::string(io_buffer->data(), bytes_read);
114 receive_info.remote_address = address;
115 receive_info.remote_port = port;
116 scoped_ptr<base::ListValue> args =
117 sockets_udp::OnReceive::Create(receive_info);
118 scoped_ptr<Event> event(
119 new Event(sockets_udp::OnReceive::kEventName, args.Pass()));
120 PostEvent(params, event.Pass());
122 // Post a task to delay the read until the socket is available, as
123 // calling StartReceive at this point would error with ERR_IO_PENDING.
124 BrowserThread::PostTask(
127 base::Bind(&UDPSocketEventDispatcher::StartReceive, params));
128 } else if (bytes_read == net::ERR_IO_PENDING) {
129 // This happens when resuming a socket which already had an
130 // active "recv" callback.
132 // Dispatch "onReceiveError" event but don't start another read to avoid
133 // potential infinite reads if we have a persistent network error.
134 sockets_udp::ReceiveErrorInfo receive_error_info;
135 receive_error_info.socket_id = params.socket_id;
136 receive_error_info.result_code = bytes_read;
137 scoped_ptr<base::ListValue> args =
138 sockets_udp::OnReceiveError::Create(receive_error_info);
139 scoped_ptr<Event> event(
140 new Event(sockets_udp::OnReceiveError::kEventName, args.Pass()));
141 PostEvent(params, event.Pass());
143 // Since we got an error, the socket is now "paused" until the application
145 ResumableUDPSocket* socket =
146 params.sockets->Get(params.extension_id, params.socket_id);
148 socket->set_paused(true);
154 void UDPSocketEventDispatcher::PostEvent(const ReceiveParams& params,
155 scoped_ptr<Event> event) {
156 DCHECK_CURRENTLY_ON(params.thread_id);
158 BrowserThread::PostTask(BrowserThread::UI,
160 base::Bind(&DispatchEvent,
161 params.browser_context_id,
163 base::Passed(event.Pass())));
167 void UDPSocketEventDispatcher::DispatchEvent(void* browser_context_id,
168 const std::string& extension_id,
169 scoped_ptr<Event> event) {
170 DCHECK_CURRENTLY_ON(BrowserThread::UI);
172 content::BrowserContext* context =
173 reinterpret_cast<content::BrowserContext*>(browser_context_id);
174 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context))
176 EventRouter* router = EventRouter::Get(context);
178 router->DispatchEventToExtension(extension_id, event.Pass());
181 } // namespace core_api
182 } // namespace extensions