OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/sockets_udp/udp_socket_event_dispatcher.h" | 5 #include "extensions/browser/api/sockets_udp/udp_socket_event_dispatcher.h" |
6 | 6 |
7 #include "extensions/browser/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" |
(...skipping 10 matching lines...) Expand all Loading... |
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_CURRENTLY_ON(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) | 41 DCHECK(manager) |
(...skipping 10 matching lines...) Expand all Loading... |
52 | 52 |
53 UDPSocketEventDispatcher::ReceiveParams::~ReceiveParams() {} | 53 UDPSocketEventDispatcher::ReceiveParams::~ReceiveParams() {} |
54 | 54 |
55 void UDPSocketEventDispatcher::OnSocketBind(const std::string& extension_id, | 55 void UDPSocketEventDispatcher::OnSocketBind(const std::string& extension_id, |
56 int socket_id) { | 56 int socket_id) { |
57 OnSocketResume(extension_id, socket_id); | 57 OnSocketResume(extension_id, socket_id); |
58 } | 58 } |
59 | 59 |
60 void UDPSocketEventDispatcher::OnSocketResume(const std::string& extension_id, | 60 void UDPSocketEventDispatcher::OnSocketResume(const std::string& extension_id, |
61 int socket_id) { | 61 int socket_id) { |
62 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 62 DCHECK_CURRENTLY_ON(thread_id_); |
63 | 63 |
64 ReceiveParams params; | 64 ReceiveParams params; |
65 params.thread_id = thread_id_; | 65 params.thread_id = thread_id_; |
66 params.browser_context_id = browser_context_; | 66 params.browser_context_id = browser_context_; |
67 params.extension_id = extension_id; | 67 params.extension_id = extension_id; |
68 params.sockets = sockets_; | 68 params.sockets = sockets_; |
69 params.socket_id = socket_id; | 69 params.socket_id = socket_id; |
70 | 70 |
71 StartReceive(params); | 71 StartReceive(params); |
72 } | 72 } |
73 | 73 |
74 /* static */ | 74 /* static */ |
75 void UDPSocketEventDispatcher::StartReceive(const ReceiveParams& params) { | 75 void UDPSocketEventDispatcher::StartReceive(const ReceiveParams& params) { |
76 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 76 DCHECK_CURRENTLY_ON(params.thread_id); |
77 | 77 |
78 ResumableUDPSocket* socket = | 78 ResumableUDPSocket* socket = |
79 params.sockets->Get(params.extension_id, params.socket_id); | 79 params.sockets->Get(params.extension_id, params.socket_id); |
80 if (socket == NULL) { | 80 if (socket == NULL) { |
81 // 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. |
82 return; | 82 return; |
83 } | 83 } |
84 DCHECK(params.extension_id == socket->owner_extension_id()) | 84 DCHECK(params.extension_id == socket->owner_extension_id()) |
85 << "Socket has wrong owner."; | 85 << "Socket has wrong owner."; |
86 | 86 |
87 // Don't start another read if the socket has been paused. | 87 // Don't start another read if the socket has been paused. |
88 if (socket->paused()) | 88 if (socket->paused()) |
89 return; | 89 return; |
90 | 90 |
91 int buffer_size = (socket->buffer_size() <= 0 ? 4096 : socket->buffer_size()); | 91 int buffer_size = (socket->buffer_size() <= 0 ? 4096 : socket->buffer_size()); |
92 socket->RecvFrom( | 92 socket->RecvFrom( |
93 buffer_size, | 93 buffer_size, |
94 base::Bind(&UDPSocketEventDispatcher::ReceiveCallback, params)); | 94 base::Bind(&UDPSocketEventDispatcher::ReceiveCallback, params)); |
95 } | 95 } |
96 | 96 |
97 /* static */ | 97 /* static */ |
98 void UDPSocketEventDispatcher::ReceiveCallback( | 98 void UDPSocketEventDispatcher::ReceiveCallback( |
99 const ReceiveParams& params, | 99 const ReceiveParams& params, |
100 int bytes_read, | 100 int bytes_read, |
101 scoped_refptr<net::IOBuffer> io_buffer, | 101 scoped_refptr<net::IOBuffer> io_buffer, |
102 const std::string& address, | 102 const std::string& address, |
103 int port) { | 103 int port) { |
104 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 104 DCHECK_CURRENTLY_ON(params.thread_id); |
105 | 105 |
106 // If |bytes_read| == 0, the message contained no data. | 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 | 107 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value |
108 // from "net::ERR_". | 108 // from "net::ERR_". |
109 | 109 |
110 if (bytes_read >= 0) { | 110 if (bytes_read >= 0) { |
111 // Dispatch "onReceive" event. | 111 // Dispatch "onReceive" event. |
112 sockets_udp::ReceiveInfo receive_info; | 112 sockets_udp::ReceiveInfo receive_info; |
113 receive_info.socket_id = params.socket_id; | 113 receive_info.socket_id = params.socket_id; |
114 receive_info.data = std::string(io_buffer->data(), bytes_read); | 114 receive_info.data = std::string(io_buffer->data(), bytes_read); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 params.sockets->Get(params.extension_id, params.socket_id); | 147 params.sockets->Get(params.extension_id, params.socket_id); |
148 if (socket) { | 148 if (socket) { |
149 socket->set_paused(true); | 149 socket->set_paused(true); |
150 } | 150 } |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 /* static */ | 154 /* static */ |
155 void UDPSocketEventDispatcher::PostEvent(const ReceiveParams& params, | 155 void UDPSocketEventDispatcher::PostEvent(const ReceiveParams& params, |
156 scoped_ptr<Event> event) { | 156 scoped_ptr<Event> event) { |
157 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 157 DCHECK_CURRENTLY_ON(params.thread_id); |
158 | 158 |
159 BrowserThread::PostTask(BrowserThread::UI, | 159 BrowserThread::PostTask(BrowserThread::UI, |
160 FROM_HERE, | 160 FROM_HERE, |
161 base::Bind(&DispatchEvent, | 161 base::Bind(&DispatchEvent, |
162 params.browser_context_id, | 162 params.browser_context_id, |
163 params.extension_id, | 163 params.extension_id, |
164 base::Passed(event.Pass()))); | 164 base::Passed(event.Pass()))); |
165 } | 165 } |
166 | 166 |
167 /*static*/ | 167 /*static*/ |
168 void UDPSocketEventDispatcher::DispatchEvent(void* browser_context_id, | 168 void UDPSocketEventDispatcher::DispatchEvent(void* browser_context_id, |
169 const std::string& extension_id, | 169 const std::string& extension_id, |
170 scoped_ptr<Event> event) { | 170 scoped_ptr<Event> event) { |
171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
172 | 172 |
173 content::BrowserContext* context = | 173 content::BrowserContext* context = |
174 reinterpret_cast<content::BrowserContext*>(browser_context_id); | 174 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
175 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) | 175 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
176 return; | 176 return; |
177 EventRouter* router = ExtensionSystem::Get(context)->event_router(); | 177 EventRouter* router = ExtensionSystem::Get(context)->event_router(); |
178 if (router) | 178 if (router) |
179 router->DispatchEventToExtension(extension_id, event.Pass()); | 179 router->DispatchEventToExtension(extension_id, event.Pass()); |
180 } | 180 } |
181 | 181 |
182 } // namespace core_api | 182 } // namespace core_api |
183 } // namespace extensions | 183 } // namespace extensions |
OLD | NEW |