| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/sockets_udp/udp_socket_event_dispatcher.
h" | 5 #include "extensions/browser/api/sockets_udp/udp_socket_event_dispatcher.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/socket/udp_socket.h" | 7 #include "extensions/browser/api/socket/udp_socket.h" |
| 8 #include "extensions/browser/event_router.h" | 8 #include "extensions/browser/event_router.h" |
| 9 #include "extensions/browser/extension_system.h" | 9 #include "extensions/browser/extension_system.h" |
| 10 #include "extensions/browser/extensions_browser_client.h" | 10 #include "extensions/browser/extensions_browser_client.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 | 12 |
| 13 namespace extensions { | 13 namespace extensions { |
| 14 namespace api { | 14 namespace core_api { |
| 15 | 15 |
| 16 using content::BrowserThread; | 16 using content::BrowserThread; |
| 17 | 17 |
| 18 static base::LazyInstance< | 18 static base::LazyInstance< |
| 19 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher> > g_factory = | 19 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher> > g_factory = |
| 20 LAZY_INSTANCE_INITIALIZER; | 20 LAZY_INSTANCE_INITIALIZER; |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>* | 23 BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>* |
| 24 UDPSocketEventDispatcher::GetFactoryInstance() { | 24 UDPSocketEventDispatcher::GetFactoryInstance() { |
| 25 return g_factory.Pointer(); | 25 return g_factory.Pointer(); |
| 26 } | 26 } |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 UDPSocketEventDispatcher* UDPSocketEventDispatcher::Get( | 29 UDPSocketEventDispatcher* UDPSocketEventDispatcher::Get( |
| 30 content::BrowserContext* context) { | 30 content::BrowserContext* context) { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 | 32 |
| 33 return BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>::Get(context); | 33 return BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>::Get(context); |
| 34 } | 34 } |
| 35 | 35 |
| 36 UDPSocketEventDispatcher::UDPSocketEventDispatcher( | 36 UDPSocketEventDispatcher::UDPSocketEventDispatcher( |
| 37 content::BrowserContext* context) | 37 content::BrowserContext* context) |
| 38 : thread_id_(Socket::kThreadId), browser_context_(context) { | 38 : thread_id_(Socket::kThreadId), browser_context_(context) { |
| 39 ApiResourceManager<ResumableUDPSocket>* manager = | 39 ApiResourceManager<ResumableUDPSocket>* manager = |
| 40 ApiResourceManager<ResumableUDPSocket>::Get(browser_context_); | 40 ApiResourceManager<ResumableUDPSocket>::Get(browser_context_); |
| 41 DCHECK(manager) << "There is no socket manager. " | 41 DCHECK(manager) |
| 42 "If this assertion is failing during a test, then it is likely that " | 42 << "There is no socket manager. " |
| 43 "TestExtensionSystem is failing to provide an instance of " | 43 "If this assertion is failing during a test, then it is likely that " |
| 44 "ApiResourceManager<ResumableUDPSocket>."; | 44 "TestExtensionSystem is failing to provide an instance of " |
| 45 "ApiResourceManager<ResumableUDPSocket>."; |
| 45 sockets_ = manager->data_; | 46 sockets_ = manager->data_; |
| 46 } | 47 } |
| 47 | 48 |
| 48 UDPSocketEventDispatcher::~UDPSocketEventDispatcher() {} | 49 UDPSocketEventDispatcher::~UDPSocketEventDispatcher() {} |
| 49 | 50 |
| 50 UDPSocketEventDispatcher::ReceiveParams::ReceiveParams() {} | 51 UDPSocketEventDispatcher::ReceiveParams::ReceiveParams() {} |
| 51 | 52 |
| 52 UDPSocketEventDispatcher::ReceiveParams::~ReceiveParams() {} | 53 UDPSocketEventDispatcher::ReceiveParams::~ReceiveParams() {} |
| 53 | 54 |
| 54 void UDPSocketEventDispatcher::OnSocketBind(const std::string& extension_id, | 55 void UDPSocketEventDispatcher::OnSocketBind(const std::string& extension_id, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 74 void UDPSocketEventDispatcher::StartReceive(const ReceiveParams& params) { | 75 void UDPSocketEventDispatcher::StartReceive(const ReceiveParams& params) { |
| 75 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 76 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); |
| 76 | 77 |
| 77 ResumableUDPSocket* socket = | 78 ResumableUDPSocket* socket = |
| 78 params.sockets->Get(params.extension_id, params.socket_id); | 79 params.sockets->Get(params.extension_id, params.socket_id); |
| 79 if (socket == NULL) { | 80 if (socket == NULL) { |
| 80 // This can happen if the socket is closed while our callback is active. | 81 // This can happen if the socket is closed while our callback is active. |
| 81 return; | 82 return; |
| 82 } | 83 } |
| 83 DCHECK(params.extension_id == socket->owner_extension_id()) | 84 DCHECK(params.extension_id == socket->owner_extension_id()) |
| 84 << "Socket has wrong owner."; | 85 << "Socket has wrong owner."; |
| 85 | 86 |
| 86 // Don't start another read if the socket has been paused. | 87 // Don't start another read if the socket has been paused. |
| 87 if (socket->paused()) | 88 if (socket->paused()) |
| 88 return; | 89 return; |
| 89 | 90 |
| 90 int buffer_size = (socket->buffer_size() <= 0 ? 4096 : socket->buffer_size()); | 91 int buffer_size = (socket->buffer_size() <= 0 ? 4096 : socket->buffer_size()); |
| 91 socket->RecvFrom(buffer_size, | 92 socket->RecvFrom( |
| 92 base::Bind(&UDPSocketEventDispatcher::ReceiveCallback, | 93 buffer_size, |
| 93 params)); | 94 base::Bind(&UDPSocketEventDispatcher::ReceiveCallback, params)); |
| 94 } | 95 } |
| 95 | 96 |
| 96 /* static */ | 97 /* static */ |
| 97 void UDPSocketEventDispatcher::ReceiveCallback( | 98 void UDPSocketEventDispatcher::ReceiveCallback( |
| 98 const ReceiveParams& params, | 99 const ReceiveParams& params, |
| 99 int bytes_read, | 100 int bytes_read, |
| 100 scoped_refptr<net::IOBuffer> io_buffer, | 101 scoped_refptr<net::IOBuffer> io_buffer, |
| 101 const std::string& address, | 102 const std::string& address, |
| 102 int port) { | 103 int port) { |
| 103 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 104 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); |
| 104 | 105 |
| 105 // If |bytes_read| == 0, the message contained no data. | 106 // 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 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value |
| 107 // from "net::ERR_". | 108 // from "net::ERR_". |
| 108 | 109 |
| 109 if (bytes_read >= 0) { | 110 if (bytes_read >= 0) { |
| 110 // Dispatch "onReceive" event. | 111 // Dispatch "onReceive" event. |
| 111 sockets_udp::ReceiveInfo receive_info; | 112 sockets_udp::ReceiveInfo receive_info; |
| 112 receive_info.socket_id = params.socket_id; | 113 receive_info.socket_id = params.socket_id; |
| 113 receive_info.data = std::string(io_buffer->data(), bytes_read); | 114 receive_info.data = std::string(io_buffer->data(), bytes_read); |
| 114 receive_info.remote_address = address; | 115 receive_info.remote_address = address; |
| 115 receive_info.remote_port = port; | 116 receive_info.remote_port = port; |
| 116 scoped_ptr<base::ListValue> args = | 117 scoped_ptr<base::ListValue> args = |
| 117 sockets_udp::OnReceive::Create(receive_info); | 118 sockets_udp::OnReceive::Create(receive_info); |
| 118 scoped_ptr<Event> event( | 119 scoped_ptr<Event> event( |
| 119 new Event(sockets_udp::OnReceive::kEventName, args.Pass())); | 120 new Event(sockets_udp::OnReceive::kEventName, args.Pass())); |
| 120 PostEvent(params, event.Pass()); | 121 PostEvent(params, event.Pass()); |
| 121 | 122 |
| 122 // Post a task to delay the read until the socket is available, as | 123 // 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 // calling StartReceive at this point would error with ERR_IO_PENDING. |
| 124 BrowserThread::PostTask( | 125 BrowserThread::PostTask( |
| 125 params.thread_id, FROM_HERE, | 126 params.thread_id, |
| 127 FROM_HERE, |
| 126 base::Bind(&UDPSocketEventDispatcher::StartReceive, params)); | 128 base::Bind(&UDPSocketEventDispatcher::StartReceive, params)); |
| 127 } else if (bytes_read == net::ERR_IO_PENDING) { | 129 } else if (bytes_read == net::ERR_IO_PENDING) { |
| 128 // This happens when resuming a socket which already had an | 130 // This happens when resuming a socket which already had an |
| 129 // active "recv" callback. | 131 // active "recv" callback. |
| 130 } else { | 132 } else { |
| 131 // Dispatch "onReceiveError" event but don't start another read to avoid | 133 // Dispatch "onReceiveError" event but don't start another read to avoid |
| 132 // potential infinite reads if we have a persistent network error. | 134 // potential infinite reads if we have a persistent network error. |
| 133 sockets_udp::ReceiveErrorInfo receive_error_info; | 135 sockets_udp::ReceiveErrorInfo receive_error_info; |
| 134 receive_error_info.socket_id = params.socket_id; | 136 receive_error_info.socket_id = params.socket_id; |
| 135 receive_error_info.result_code = bytes_read; | 137 receive_error_info.result_code = bytes_read; |
| 136 scoped_ptr<base::ListValue> args = | 138 scoped_ptr<base::ListValue> args = |
| 137 sockets_udp::OnReceiveError::Create(receive_error_info); | 139 sockets_udp::OnReceiveError::Create(receive_error_info); |
| 138 scoped_ptr<Event> event( | 140 scoped_ptr<Event> event( |
| 139 new Event(sockets_udp::OnReceiveError::kEventName, args.Pass())); | 141 new Event(sockets_udp::OnReceiveError::kEventName, args.Pass())); |
| 140 PostEvent(params, event.Pass()); | 142 PostEvent(params, event.Pass()); |
| 141 | 143 |
| 142 // Since we got an error, the socket is now "paused" until the application | 144 // Since we got an error, the socket is now "paused" until the application |
| 143 // "resumes" it. | 145 // "resumes" it. |
| 144 ResumableUDPSocket* socket = | 146 ResumableUDPSocket* socket = |
| 145 params.sockets->Get(params.extension_id, params.socket_id); | 147 params.sockets->Get(params.extension_id, params.socket_id); |
| 146 if (socket) { | 148 if (socket) { |
| 147 socket->set_paused(true); | 149 socket->set_paused(true); |
| 148 } | 150 } |
| 149 } | 151 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 170 | 172 |
| 171 content::BrowserContext* context = | 173 content::BrowserContext* context = |
| 172 reinterpret_cast<content::BrowserContext*>(browser_context_id); | 174 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
| 173 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) | 175 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 174 return; | 176 return; |
| 175 EventRouter* router = ExtensionSystem::Get(context)->event_router(); | 177 EventRouter* router = ExtensionSystem::Get(context)->event_router(); |
| 176 if (router) | 178 if (router) |
| 177 router->DispatchEventToExtension(extension_id, event.Pass()); | 179 router->DispatchEventToExtension(extension_id, event.Pass()); |
| 178 } | 180 } |
| 179 | 181 |
| 180 } // namespace api | 182 } // namespace core_api |
| 181 } // namespace extensions | 183 } // namespace extensions |
| OLD | NEW |