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_tcp/tcp_socket_event_dispatcher.h" | 5 #include "extensions/browser/api/sockets_tcp/tcp_socket_event_dispatcher.h" |
6 | 6 |
7 #include "extensions/browser/api/socket/tcp_socket.h" | 7 #include "extensions/browser/api/socket/tcp_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 14 matching lines...) Expand all Loading... |
25 | 25 |
26 // static | 26 // static |
27 BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>* | 27 BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>* |
28 TCPSocketEventDispatcher::GetFactoryInstance() { | 28 TCPSocketEventDispatcher::GetFactoryInstance() { |
29 return g_factory.Pointer(); | 29 return g_factory.Pointer(); |
30 } | 30 } |
31 | 31 |
32 // static | 32 // static |
33 TCPSocketEventDispatcher* TCPSocketEventDispatcher::Get( | 33 TCPSocketEventDispatcher* TCPSocketEventDispatcher::Get( |
34 content::BrowserContext* context) { | 34 content::BrowserContext* context) { |
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 35 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
36 | 36 |
37 return BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>::Get(context); | 37 return BrowserContextKeyedAPIFactory<TCPSocketEventDispatcher>::Get(context); |
38 } | 38 } |
39 | 39 |
40 TCPSocketEventDispatcher::TCPSocketEventDispatcher( | 40 TCPSocketEventDispatcher::TCPSocketEventDispatcher( |
41 content::BrowserContext* context) | 41 content::BrowserContext* context) |
42 : thread_id_(Socket::kThreadId), browser_context_(context) { | 42 : thread_id_(Socket::kThreadId), browser_context_(context) { |
43 ApiResourceManager<ResumableTCPSocket>* manager = | 43 ApiResourceManager<ResumableTCPSocket>* manager = |
44 ApiResourceManager<ResumableTCPSocket>::Get(browser_context_); | 44 ApiResourceManager<ResumableTCPSocket>::Get(browser_context_); |
45 DCHECK(manager) | 45 DCHECK(manager) |
46 << "There is no socket manager. " | 46 << "There is no socket manager. " |
47 "If this assertion is failing during a test, then it is likely that " | 47 "If this assertion is failing during a test, then it is likely that " |
48 "TestExtensionSystem is failing to provide an instance of " | 48 "TestExtensionSystem is failing to provide an instance of " |
49 "ApiResourceManager<ResumableTCPSocket>."; | 49 "ApiResourceManager<ResumableTCPSocket>."; |
50 sockets_ = manager->data_; | 50 sockets_ = manager->data_; |
51 } | 51 } |
52 | 52 |
53 TCPSocketEventDispatcher::~TCPSocketEventDispatcher() {} | 53 TCPSocketEventDispatcher::~TCPSocketEventDispatcher() {} |
54 | 54 |
55 TCPSocketEventDispatcher::ReadParams::ReadParams() {} | 55 TCPSocketEventDispatcher::ReadParams::ReadParams() {} |
56 | 56 |
57 TCPSocketEventDispatcher::ReadParams::~ReadParams() {} | 57 TCPSocketEventDispatcher::ReadParams::~ReadParams() {} |
58 | 58 |
59 void TCPSocketEventDispatcher::OnSocketConnect(const std::string& extension_id, | 59 void TCPSocketEventDispatcher::OnSocketConnect(const std::string& extension_id, |
60 int socket_id) { | 60 int socket_id) { |
61 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 61 DCHECK_CURRENTLY_ON(thread_id_); |
62 | 62 |
63 StartSocketRead(extension_id, socket_id); | 63 StartSocketRead(extension_id, socket_id); |
64 } | 64 } |
65 | 65 |
66 void TCPSocketEventDispatcher::OnSocketResume(const std::string& extension_id, | 66 void TCPSocketEventDispatcher::OnSocketResume(const std::string& extension_id, |
67 int socket_id) { | 67 int socket_id) { |
68 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 68 DCHECK_CURRENTLY_ON(thread_id_); |
69 | 69 |
70 StartSocketRead(extension_id, socket_id); | 70 StartSocketRead(extension_id, socket_id); |
71 } | 71 } |
72 | 72 |
73 void TCPSocketEventDispatcher::StartSocketRead(const std::string& extension_id, | 73 void TCPSocketEventDispatcher::StartSocketRead(const std::string& extension_id, |
74 int socket_id) { | 74 int socket_id) { |
75 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 75 DCHECK_CURRENTLY_ON(thread_id_); |
76 | 76 |
77 ReadParams params; | 77 ReadParams params; |
78 params.thread_id = thread_id_; | 78 params.thread_id = thread_id_; |
79 params.browser_context_id = browser_context_; | 79 params.browser_context_id = browser_context_; |
80 params.extension_id = extension_id; | 80 params.extension_id = extension_id; |
81 params.sockets = sockets_; | 81 params.sockets = sockets_; |
82 params.socket_id = socket_id; | 82 params.socket_id = socket_id; |
83 | 83 |
84 StartRead(params); | 84 StartRead(params); |
85 } | 85 } |
86 | 86 |
87 // static | 87 // static |
88 void TCPSocketEventDispatcher::StartRead(const ReadParams& params) { | 88 void TCPSocketEventDispatcher::StartRead(const ReadParams& params) { |
89 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 89 DCHECK_CURRENTLY_ON(params.thread_id); |
90 | 90 |
91 ResumableTCPSocket* socket = | 91 ResumableTCPSocket* socket = |
92 params.sockets->Get(params.extension_id, params.socket_id); | 92 params.sockets->Get(params.extension_id, params.socket_id); |
93 if (!socket) { | 93 if (!socket) { |
94 // This can happen if the socket is closed while our callback is active. | 94 // This can happen if the socket is closed while our callback is active. |
95 return; | 95 return; |
96 } | 96 } |
97 DCHECK(params.extension_id == socket->owner_extension_id()) | 97 DCHECK(params.extension_id == socket->owner_extension_id()) |
98 << "Socket has wrong owner."; | 98 << "Socket has wrong owner."; |
99 | 99 |
100 // Don't start another read if the socket has been paused. | 100 // Don't start another read if the socket has been paused. |
101 if (socket->paused()) | 101 if (socket->paused()) |
102 return; | 102 return; |
103 | 103 |
104 int buffer_size = socket->buffer_size(); | 104 int buffer_size = socket->buffer_size(); |
105 if (buffer_size <= 0) | 105 if (buffer_size <= 0) |
106 buffer_size = kDefaultBufferSize; | 106 buffer_size = kDefaultBufferSize; |
107 socket->Read(buffer_size, | 107 socket->Read(buffer_size, |
108 base::Bind(&TCPSocketEventDispatcher::ReadCallback, params)); | 108 base::Bind(&TCPSocketEventDispatcher::ReadCallback, params)); |
109 } | 109 } |
110 | 110 |
111 // static | 111 // static |
112 void TCPSocketEventDispatcher::ReadCallback( | 112 void TCPSocketEventDispatcher::ReadCallback( |
113 const ReadParams& params, | 113 const ReadParams& params, |
114 int bytes_read, | 114 int bytes_read, |
115 scoped_refptr<net::IOBuffer> io_buffer) { | 115 scoped_refptr<net::IOBuffer> io_buffer) { |
116 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 116 DCHECK_CURRENTLY_ON(params.thread_id); |
117 | 117 |
118 // If |bytes_read| == 0, the connection has been closed by the peer. | 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 | 119 // If |bytes_read| < 0, there was a network error, and |bytes_read| is a value |
120 // from "net::ERR_". | 120 // from "net::ERR_". |
121 | 121 |
122 if (bytes_read == 0) { | 122 if (bytes_read == 0) { |
123 bytes_read = net::ERR_CONNECTION_CLOSED; | 123 bytes_read = net::ERR_CONNECTION_CLOSED; |
124 } | 124 } |
125 | 125 |
126 if (bytes_read > 0) { | 126 if (bytes_read > 0) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 params.sockets->Get(params.extension_id, params.socket_id); | 161 params.sockets->Get(params.extension_id, params.socket_id); |
162 if (socket) { | 162 if (socket) { |
163 socket->set_paused(true); | 163 socket->set_paused(true); |
164 } | 164 } |
165 } | 165 } |
166 } | 166 } |
167 | 167 |
168 // static | 168 // static |
169 void TCPSocketEventDispatcher::PostEvent(const ReadParams& params, | 169 void TCPSocketEventDispatcher::PostEvent(const ReadParams& params, |
170 scoped_ptr<Event> event) { | 170 scoped_ptr<Event> event) { |
171 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); | 171 DCHECK_CURRENTLY_ON(params.thread_id); |
172 | 172 |
173 BrowserThread::PostTask(BrowserThread::UI, | 173 BrowserThread::PostTask(BrowserThread::UI, |
174 FROM_HERE, | 174 FROM_HERE, |
175 base::Bind(&DispatchEvent, | 175 base::Bind(&DispatchEvent, |
176 params.browser_context_id, | 176 params.browser_context_id, |
177 params.extension_id, | 177 params.extension_id, |
178 base::Passed(event.Pass()))); | 178 base::Passed(event.Pass()))); |
179 } | 179 } |
180 | 180 |
181 // static | 181 // static |
182 void TCPSocketEventDispatcher::DispatchEvent(void* browser_context_id, | 182 void TCPSocketEventDispatcher::DispatchEvent(void* browser_context_id, |
183 const std::string& extension_id, | 183 const std::string& extension_id, |
184 scoped_ptr<Event> event) { | 184 scoped_ptr<Event> event) { |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 185 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
186 | 186 |
187 content::BrowserContext* context = | 187 content::BrowserContext* context = |
188 reinterpret_cast<content::BrowserContext*>(browser_context_id); | 188 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
189 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) | 189 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
190 return; | 190 return; |
191 | 191 |
192 EventRouter* router = ExtensionSystem::Get(context)->event_router(); | 192 EventRouter* router = ExtensionSystem::Get(context)->event_router(); |
193 if (router) | 193 if (router) |
194 router->DispatchEventToExtension(extension_id, event.Pass()); | 194 router->DispatchEventToExtension(extension_id, event.Pass()); |
195 } | 195 } |
196 | 196 |
197 } // namespace core_api | 197 } // namespace core_api |
198 } // namespace extensions | 198 } // namespace extensions |
OLD | NEW |