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_tcp/tcp_socket_event_dispatcher.h"
7 #include "extensions/browser/api/socket/tcp_socket.h"
8 #include "extensions/browser/event_router.h"
9 #include "extensions/browser/extension_system.h"
10 #include "extensions/browser/extensions_browser_client.h"
11 #include "net/base/net_errors.h"
14 int kDefaultBufferSize = 4096;
17 namespace extensions {
20 using content::BrowserThread;
22 static base::LazyInstance<
23 BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher> > g_factory =
24 LAZY_INSTANCE_INITIALIZER;
27 BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>*
28 TCPSocketEventDispatcher::GetFactoryInstance() {
29 return g_factory.Pointer();
33 TCPSocketEventDispatcher* TCPSocketEventDispatcher::Get(
34 content::BrowserContext* context) {
35 DCHECK_CURRENTLY_ON(BrowserThread::UI);
37 return BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>::Get(context);
40 TCPSocketEventDispatcher::TCPSocketEventDispatcher(
41 content::BrowserContext* context)
42 : thread_id_(Socket::kThreadId), browser_context_(context) {
43 ApiResourceManager<ResumableTCPSocket>* manager =
44 ApiResourceManager<ResumableTCPSocket>::Get(browser_context_);
46 << "There is no socket manager. "
47 "If this assertion is failing during a test, then it is likely that "
48 "TestExtensionSystem is failing to provide an instance of "
49 "ApiResourceManager<ResumableTCPSocket>.";
50 sockets_ = manager->data_;
53 TCPSocketEventDispatcher::~TCPSocketEventDispatcher() {}
55 TCPSocketEventDispatcher::ReadParams::ReadParams() {}
57 TCPSocketEventDispatcher::ReadParams::~ReadParams() {}
59 void TCPSocketEventDispatcher::OnSocketConnect(const std::string& extension_id,
61 DCHECK_CURRENTLY_ON(thread_id_);
63 StartSocketRead(extension_id, socket_id);
66 void TCPSocketEventDispatcher::OnSocketResume(const std::string& extension_id,
68 DCHECK_CURRENTLY_ON(thread_id_);
70 StartSocketRead(extension_id, socket_id);
73 void TCPSocketEventDispatcher::StartSocketRead(const std::string& extension_id,
75 DCHECK_CURRENTLY_ON(thread_id_);
78 params.thread_id = thread_id_;
79 params.browser_context_id = browser_context_;
80 params.extension_id = extension_id;
81 params.sockets = sockets_;
82 params.socket_id = socket_id;
88 void TCPSocketEventDispatcher::StartRead(const ReadParams& params) {
89 DCHECK_CURRENTLY_ON(params.thread_id);
91 ResumableTCPSocket* socket =
92 params.sockets->Get(params.extension_id, params.socket_id);
94 // This can happen if the socket is closed while our callback is active.
97 DCHECK(params.extension_id == socket->owner_extension_id())
98 << "Socket has wrong owner.";
100 // Don't start another read if the socket has been paused.
101 if (socket->paused())
104 int buffer_size = socket->buffer_size();
105 if (buffer_size <= 0)
106 buffer_size = kDefaultBufferSize;
107 socket->Read(buffer_size,
108 base::Bind(&TCPSocketEventDispatcher::ReadCallback, params));
112 void TCPSocketEventDispatcher::ReadCallback(
113 const ReadParams& params,
115 scoped_refptr<net::IOBuffer> io_buffer) {
116 DCHECK_CURRENTLY_ON(params.thread_id);
118 // If |bytes_read| == 0, the connection has been closed by the peer.
119 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value
122 if (bytes_read == 0) {
123 bytes_read = net::ERR_CONNECTION_CLOSED;
126 if (bytes_read > 0) {
127 // Dispatch "onReceive" event.
128 sockets_tcp::ReceiveInfo receive_info;
129 receive_info.socket_id = params.socket_id;
130 receive_info.data = std::string(io_buffer->data(), bytes_read);
131 scoped_ptr<base::ListValue> args =
132 sockets_tcp::OnReceive::Create(receive_info);
133 scoped_ptr<Event> event(
134 new Event(sockets_tcp::OnReceive::kEventName, args.Pass()));
135 PostEvent(params, event.Pass());
137 // Post a task to delay the read until the socket is available, as
138 // calling StartReceive at this point would error with ERR_IO_PENDING.
139 BrowserThread::PostTask(
142 base::Bind(&TCPSocketEventDispatcher::StartRead, params));
143 } else if (bytes_read == net::ERR_IO_PENDING) {
144 // This happens when resuming a socket which already had an
145 // active "read" callback.
147 // Dispatch "onReceiveError" event but don't start another read to avoid
148 // potential infinite reads if we have a persistent network error.
149 sockets_tcp::ReceiveErrorInfo receive_error_info;
150 receive_error_info.socket_id = params.socket_id;
151 receive_error_info.result_code = bytes_read;
152 scoped_ptr<base::ListValue> args =
153 sockets_tcp::OnReceiveError::Create(receive_error_info);
154 scoped_ptr<Event> event(
155 new Event(sockets_tcp::OnReceiveError::kEventName, args.Pass()));
156 PostEvent(params, event.Pass());
158 // Since we got an error, the socket is now "paused" until the application
160 ResumableTCPSocket* socket =
161 params.sockets->Get(params.extension_id, params.socket_id);
163 socket->set_paused(true);
169 void TCPSocketEventDispatcher::PostEvent(const ReadParams& params,
170 scoped_ptr<Event> event) {
171 DCHECK_CURRENTLY_ON(params.thread_id);
173 BrowserThread::PostTask(BrowserThread::UI,
175 base::Bind(&DispatchEvent,
176 params.browser_context_id,
178 base::Passed(event.Pass())));
182 void TCPSocketEventDispatcher::DispatchEvent(void* browser_context_id,
183 const std::string& extension_id,
184 scoped_ptr<Event> event) {
185 DCHECK_CURRENTLY_ON(BrowserThread::UI);
187 content::BrowserContext* context =
188 reinterpret_cast<content::BrowserContext*>(browser_context_id);
189 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context))
192 EventRouter* event_router = EventRouter::Get(context);
194 event_router->DispatchEventToExtension(extension_id, event.Pass());
197 } // namespace core_api
198 } // namespace extensions