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. 4 5 #include "extensions/browser/api/sockets_udp/udp_socket_event_dispatcher.h" 6 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" 11 12 namespace extensions { 13 namespace core_api { 14 15 using content::BrowserThread; 16 17 static base::LazyInstance< 18 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher> > g_factory = 19 LAZY_INSTANCE_INITIALIZER; 20 21 // static 22 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>* 23 UDPSocketEventDispatcher::GetFactoryInstance() { 24 return g_factory.Pointer(); 25 } 26 27 // static 28 UDPSocketEventDispatcher* UDPSocketEventDispatcher::Get( 29 content::BrowserContext* context) { 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); 31 32 return BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>::Get(context); 33 } 34 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_); 40 DCHECK(manager) 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_; 46 } 47 48 UDPSocketEventDispatcher::~UDPSocketEventDispatcher() {} 49 50 UDPSocketEventDispatcher::ReceiveParams::ReceiveParams() {} 51 52 UDPSocketEventDispatcher::ReceiveParams::~ReceiveParams() {} 53 54 void UDPSocketEventDispatcher::OnSocketBind(const std::string& extension_id, 55 int socket_id) { 56 OnSocketResume(extension_id, socket_id); 57 } 58 59 void UDPSocketEventDispatcher::OnSocketResume(const std::string& extension_id, 60 int socket_id) { 61 DCHECK_CURRENTLY_ON(thread_id_); 62 63 ReceiveParams params; 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; 69 70 StartReceive(params); 71 } 72 73 /* static */ 74 void UDPSocketEventDispatcher::StartReceive(const ReceiveParams& params) { 75 DCHECK_CURRENTLY_ON(params.thread_id); 76 77 ResumableUDPSocket* socket = 78 params.sockets->Get(params.extension_id, params.socket_id); 79 if (socket == NULL) { 80 // This can happen if the socket is closed while our callback is active. 81 return; 82 } 83 DCHECK(params.extension_id == socket->owner_extension_id()) 84 << "Socket has wrong owner."; 85 86 // Don't start another read if the socket has been paused. 87 if (socket->paused()) 88 return; 89 90 int buffer_size = (socket->buffer_size() <= 0 ? 4096 : socket->buffer_size()); 91 socket->RecvFrom( 92 buffer_size, 93 base::Bind(&UDPSocketEventDispatcher::ReceiveCallback, params)); 94 } 95 96 /* static */ 97 void UDPSocketEventDispatcher::ReceiveCallback( 98 const ReceiveParams& params, 99 int bytes_read, 100 scoped_refptr<net::IOBuffer> io_buffer, 101 const std::string& address, 102 int port) { 103 DCHECK_CURRENTLY_ON(params.thread_id); 104 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 107 // from "net::ERR_". 108 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()); 121 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( 125 params.thread_id, 126 FROM_HERE, 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. 131 } else { 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()); 142 143 // Since we got an error, the socket is now "paused" until the application 144 // "resumes" it. 145 ResumableUDPSocket* socket = 146 params.sockets->Get(params.extension_id, params.socket_id); 147 if (socket) { 148 socket->set_paused(true); 149 } 150 } 151 } 152 153 /* static */ 154 void UDPSocketEventDispatcher::PostEvent(const ReceiveParams& params, 155 scoped_ptr<Event> event) { 156 DCHECK_CURRENTLY_ON(params.thread_id); 157 158 BrowserThread::PostTask(BrowserThread::UI, 159 FROM_HERE, 160 base::Bind(&DispatchEvent, 161 params.browser_context_id, 162 params.extension_id, 163 base::Passed(event.Pass()))); 164 } 165 166 /*static*/ 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); 171 172 content::BrowserContext* context = 173 reinterpret_cast<content::BrowserContext*>(browser_context_id); 174 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) 175 return; 176 EventRouter* router = EventRouter::Get(context); 177 if (router) 178 router->DispatchEventToExtension(extension_id, event.Pass()); 179 } 180 181 } // namespace core_api 182 } // namespace extensions 183