Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: extensions/browser/api/cast_channel/cast_channel_api.cc

Issue 2925053005: [cast_channel] Implement CastSocketService::OpenSocket() (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/cast_channel/cast_channel_api.h" 5 #include "extensions/browser/api/cast_channel/cast_channel_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 : browser_context_(context), logger_(new Logger()) { 122 : browser_context_(context), logger_(new Logger()) {
123 DCHECK(browser_context_); 123 DCHECK(browser_context_);
124 } 124 }
125 125
126 // static 126 // static
127 CastChannelAPI* CastChannelAPI::Get(content::BrowserContext* context) { 127 CastChannelAPI* CastChannelAPI::Get(content::BrowserContext* context) {
128 return BrowserContextKeyedAPIFactory<CastChannelAPI>::Get(context); 128 return BrowserContextKeyedAPIFactory<CastChannelAPI>::Get(context);
129 } 129 }
130 130
131 scoped_refptr<Logger> CastChannelAPI::GetLogger() { 131 scoped_refptr<Logger> CastChannelAPI::GetLogger() {
132 return logger_; 132 return logger_;
imcheng 2017/06/24 01:29:36 We now have 2 Loggers - one here and one in CastSo
zhaobin 2017/06/26 19:04:09 |logger_| is used in several places here (e.g. in
133 } 133 }
134 134
135 void CastChannelAPI::SendEvent(const std::string& extension_id, 135 void CastChannelAPI::SendEvent(const std::string& extension_id,
136 std::unique_ptr<Event> event) { 136 std::unique_ptr<Event> event) {
137 DCHECK_CURRENTLY_ON(BrowserThread::UI); 137 DCHECK_CURRENTLY_ON(BrowserThread::UI);
138 EventRouter* event_router = EventRouter::Get(GetBrowserContext()); 138 EventRouter* event_router = EventRouter::Get(GetBrowserContext());
139 if (event_router) { 139 if (event_router) {
140 event_router->DispatchEventToExtension(extension_id, std::move(event)); 140 event_router->DispatchEventToExtension(extension_id, std::move(event));
141 } 141 }
142 } 142 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 SetResultFromChannelInfo(channel_info); 209 SetResultFromChannelInfo(channel_info);
210 SetError("Channel error = " + base::IntToString(error)); 210 SetError("Channel error = " + base::IntToString(error));
211 } 211 }
212 212
213 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( 213 void CastChannelAsyncApiFunction::SetResultFromChannelInfo(
214 const ChannelInfo& channel_info) { 214 const ChannelInfo& channel_info) {
215 DCHECK_CURRENTLY_ON(BrowserThread::IO); 215 DCHECK_CURRENTLY_ON(BrowserThread::IO);
216 SetResult(channel_info.ToValue()); 216 SetResult(channel_info.ToValue());
217 } 217 }
218 218
219 CastChannelOpenFunction::CastChannelOpenFunction() 219 CastChannelOpenFunction::CastChannelOpenFunction() {}
220 : new_channel_id_(0) {
221 }
222 220
223 CastChannelOpenFunction::~CastChannelOpenFunction() { } 221 CastChannelOpenFunction::~CastChannelOpenFunction() { }
224 222
225 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( 223 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo(
226 const ConnectInfo& connect_info) { 224 const ConnectInfo& connect_info) {
227 net::IPAddress ip_address; 225 net::IPAddress ip_address;
228 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address)); 226 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address));
229 return new net::IPEndPoint(ip_address, 227 return new net::IPEndPoint(ip_address,
230 static_cast<uint16_t>(connect_info.port)); 228 static_cast<uint16_t>(connect_info.port));
231 } 229 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 } 269 }
272 270
273 ip_endpoint_.reset(ParseConnectInfo(connect_info)); 271 ip_endpoint_.reset(ParseConnectInfo(connect_info));
274 return true; 272 return true;
275 } 273 }
276 274
277 void CastChannelOpenFunction::AsyncWorkStart() { 275 void CastChannelOpenFunction::AsyncWorkStart() {
278 DCHECK(api_); 276 DCHECK(api_);
279 DCHECK(ip_endpoint_.get()); 277 DCHECK(ip_endpoint_.get());
280 const ConnectInfo& connect_info = params_->connect_info; 278 const ConnectInfo& connect_info = params_->connect_info;
281 CastSocket* socket; 279
282 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); 280 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest();
283 if (test_socket.get()) { 281 if (test_socket.get())
284 socket = test_socket.release(); 282 cast_socket_service_->SetSocketForTest(std::move(test_socket));
285 } else {
286 socket = new CastSocketImpl(
287 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(),
288 base::TimeDelta::FromMilliseconds(connect_info.timeout.get()
289 ? *connect_info.timeout
290 : kDefaultConnectTimeoutMillis),
291 liveness_timeout_, ping_interval_, api_->GetLogger(),
292 connect_info.capabilities.get() ? *connect_info.capabilities
293 : CastDeviceCapability::NONE);
294 }
295 new_channel_id_ = cast_socket_service_->AddSocket(base::WrapUnique(socket));
296 283
297 auto* observer = cast_socket_service_->GetObserver(extension_->id()); 284 auto* observer = cast_socket_service_->GetObserver(extension_->id());
298 if (!observer) { 285 if (!observer) {
299 observer = cast_socket_service_->AddObserver( 286 observer = cast_socket_service_->AddObserver(
300 extension_->id(), base::MakeUnique<CastMessageHandler>( 287 extension_->id(), base::MakeUnique<CastMessageHandler>(
301 base::Bind(&CastChannelAPI::SendEvent, 288 base::Bind(&CastChannelAPI::SendEvent,
302 api_->AsWeakPtr(), extension_->id()), 289 api_->AsWeakPtr(), extension_->id()),
303 api_->GetLogger())); 290 api_->GetLogger()));
304 } 291 }
305 292
306 socket->AddObserver(observer); 293 cast_socket_service_->OpenSocket(
307 // Construct read delegates. 294 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(),
308 socket->Connect(base::Bind(&CastChannelOpenFunction::OnOpen, this)); 295 base::TimeDelta::FromMilliseconds(connect_info.timeout.get()
296 ? *connect_info.timeout
297 : kDefaultConnectTimeoutMillis),
298 liveness_timeout_, ping_interval_, api_->GetLogger(),
299 connect_info.capabilities.get() ? *connect_info.capabilities
300 : CastDeviceCapability::NONE,
301 base::Bind(&CastChannelOpenFunction::OnOpen, this), observer);
309 } 302 }
310 303
311 void CastChannelOpenFunction::OnOpen(ChannelError result) { 304 void CastChannelOpenFunction::OnOpen(int channel_id, ChannelError result) {
312 DCHECK_CURRENTLY_ON(BrowserThread::IO); 305 DCHECK_CURRENTLY_ON(BrowserThread::IO);
313 VLOG(1) << "Connect finished, OnOpen invoked."; 306 VLOG(1) << "Connect finished, OnOpen invoked.";
314 // TODO: If we failed to open the CastSocket, we may want to clean up here, 307 // TODO: If we failed to open the CastSocket, we may want to clean up here,
315 // rather than relying on the extension to call close(). This can be done by 308 // rather than relying on the extension to call close(). This can be done by
316 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id). 309 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id).
317 if (result != ChannelError::UNKNOWN) { 310 if (result != ChannelError::UNKNOWN) {
318 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_); 311 CastSocket* socket = cast_socket_service_->GetSocket(channel_id);
319 CHECK(socket); 312 CHECK(socket);
320 SetResultFromSocket(*socket); 313 SetResultFromSocket(*socket);
321 } else { 314 } else {
322 // The socket is being destroyed. 315 // The socket is being destroyed.
323 SetResultFromError(new_channel_id_, 316 SetResultFromError(channel_id, api::cast_channel::CHANNEL_ERROR_UNKNOWN);
324 api::cast_channel::CHANNEL_ERROR_UNKNOWN);
325 } 317 }
326 318
327 AsyncWorkCompleted(); 319 AsyncWorkCompleted();
328 } 320 }
329 321
330 CastChannelSendFunction::CastChannelSendFunction() { } 322 CastChannelSendFunction::CastChannelSendFunction() { }
331 323
332 CastChannelSendFunction::~CastChannelSendFunction() { } 324 CastChannelSendFunction::~CastChannelSendFunction() { }
333 325
334 bool CastChannelSendFunction::Prepare() { 326 bool CastChannelSendFunction::Prepare() {
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 OnMessage::Create(channel_info, message_info); 473 OnMessage::Create(channel_info, message_info);
482 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, 474 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE,
483 OnMessage::kEventName, 475 OnMessage::kEventName,
484 std::move(results))); 476 std::move(results)));
485 BrowserThread::PostTask( 477 BrowserThread::PostTask(
486 BrowserThread::UI, FROM_HERE, 478 BrowserThread::UI, FROM_HERE,
487 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); 479 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event))));
488 } 480 }
489 481
490 } // namespace extensions 482 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698