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 "base/lazy_instance.h"
8 #include "extensions/browser/api/socket/udp_socket.h"
9 #include "extensions/browser/event_router.h"
10 #include "extensions/browser/extensions_browser_client.h"
11 #include "net/base/net_errors.h"
13 namespace extensions
{
16 using content::BrowserThread
;
18 static base::LazyInstance
<
19 BrowserContextKeyedAPIFactory
<UDPSocketEventDispatcher
> > g_factory
=
20 LAZY_INSTANCE_INITIALIZER
;
23 BrowserContextKeyedAPIFactory
<UDPSocketEventDispatcher
>*
24 UDPSocketEventDispatcher::GetFactoryInstance() {
25 return g_factory
.Pointer();
29 UDPSocketEventDispatcher
* UDPSocketEventDispatcher::Get(
30 content::BrowserContext
* context
) {
31 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
33 return BrowserContextKeyedAPIFactory
<UDPSocketEventDispatcher
>::Get(context
);
36 UDPSocketEventDispatcher::UDPSocketEventDispatcher(
37 content::BrowserContext
* context
)
38 : thread_id_(Socket::kThreadId
), browser_context_(context
) {
39 ApiResourceManager
<ResumableUDPSocket
>* manager
=
40 ApiResourceManager
<ResumableUDPSocket
>::Get(browser_context_
);
42 << "There is no socket manager. "
43 "If this assertion is failing during a test, then it is likely that "
44 "TestExtensionSystem is failing to provide an instance of "
45 "ApiResourceManager<ResumableUDPSocket>.";
46 sockets_
= manager
->data_
;
49 UDPSocketEventDispatcher::~UDPSocketEventDispatcher() {}
51 UDPSocketEventDispatcher::ReceiveParams::ReceiveParams() {}
53 UDPSocketEventDispatcher::ReceiveParams::~ReceiveParams() {}
55 void UDPSocketEventDispatcher::OnSocketBind(const std::string
& extension_id
,
57 OnSocketResume(extension_id
, socket_id
);
60 void UDPSocketEventDispatcher::OnSocketResume(const std::string
& extension_id
,
62 DCHECK_CURRENTLY_ON(thread_id_
);
65 params
.thread_id
= thread_id_
;
66 params
.browser_context_id
= browser_context_
;
67 params
.extension_id
= extension_id
;
68 params
.sockets
= sockets_
;
69 params
.socket_id
= socket_id
;
75 void UDPSocketEventDispatcher::StartReceive(const ReceiveParams
& params
) {
76 DCHECK_CURRENTLY_ON(params
.thread_id
);
78 ResumableUDPSocket
* socket
=
79 params
.sockets
->Get(params
.extension_id
, params
.socket_id
);
81 // This can happen if the socket is closed while our callback is active.
84 DCHECK(params
.extension_id
== socket
->owner_extension_id())
85 << "Socket has wrong owner.";
87 // Don't start another read if the socket has been paused.
91 int buffer_size
= (socket
->buffer_size() <= 0 ? 4096 : socket
->buffer_size());
94 base::Bind(&UDPSocketEventDispatcher::ReceiveCallback
, params
));
98 void UDPSocketEventDispatcher::ReceiveCallback(
99 const ReceiveParams
& params
,
101 scoped_refptr
<net::IOBuffer
> io_buffer
,
102 const std::string
& address
,
104 DCHECK_CURRENTLY_ON(params
.thread_id
);
106 // If |bytes_read| == 0, the message contained no data.
107 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value
110 if (bytes_read
>= 0) {
111 // Dispatch "onReceive" event.
112 sockets_udp::ReceiveInfo receive_info
;
113 receive_info
.socket_id
= params
.socket_id
;
114 receive_info
.data
.assign(io_buffer
->data(), io_buffer
->data() + bytes_read
);
115 receive_info
.remote_address
= address
;
116 receive_info
.remote_port
= port
;
117 scoped_ptr
<base::ListValue
> args
=
118 sockets_udp::OnReceive::Create(receive_info
);
119 scoped_ptr
<Event
> event(
120 new Event(sockets_udp::OnReceive::kEventName
, args
.Pass()));
121 PostEvent(params
, event
.Pass());
123 // Post a task to delay the read until the socket is available, as
124 // calling StartReceive at this point would error with ERR_IO_PENDING.
125 BrowserThread::PostTask(
128 base::Bind(&UDPSocketEventDispatcher::StartReceive
, params
));
129 } else if (bytes_read
== net::ERR_IO_PENDING
) {
130 // This happens when resuming a socket which already had an
131 // active "recv" callback.
133 // Dispatch "onReceiveError" event but don't start another read to avoid
134 // potential infinite reads if we have a persistent network error.
135 sockets_udp::ReceiveErrorInfo receive_error_info
;
136 receive_error_info
.socket_id
= params
.socket_id
;
137 receive_error_info
.result_code
= bytes_read
;
138 scoped_ptr
<base::ListValue
> args
=
139 sockets_udp::OnReceiveError::Create(receive_error_info
);
140 scoped_ptr
<Event
> event(
141 new Event(sockets_udp::OnReceiveError::kEventName
, args
.Pass()));
142 PostEvent(params
, event
.Pass());
144 // Since we got an error, the socket is now "paused" until the application
146 ResumableUDPSocket
* socket
=
147 params
.sockets
->Get(params
.extension_id
, params
.socket_id
);
149 socket
->set_paused(true);
155 void UDPSocketEventDispatcher::PostEvent(const ReceiveParams
& params
,
156 scoped_ptr
<Event
> event
) {
157 DCHECK_CURRENTLY_ON(params
.thread_id
);
159 BrowserThread::PostTask(BrowserThread::UI
,
161 base::Bind(&DispatchEvent
,
162 params
.browser_context_id
,
164 base::Passed(event
.Pass())));
168 void UDPSocketEventDispatcher::DispatchEvent(void* browser_context_id
,
169 const std::string
& extension_id
,
170 scoped_ptr
<Event
> event
) {
171 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
173 content::BrowserContext
* context
=
174 reinterpret_cast<content::BrowserContext
*>(browser_context_id
);
175 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context
))
177 EventRouter
* router
= EventRouter::Get(context
);
179 router
->DispatchEventToExtension(extension_id
, event
.Pass());
182 } // namespace core_api
183 } // namespace extensions