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

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

Issue 2925053005: [cast_channel] Implement CastSocketService::OpenSocket() (Closed)
Patch Set: move |logger_| from cast_channel_api to cast_socket_service Created 3 years, 5 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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 } 112 }
113 113
114 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) { 114 bool IsValidConnectInfoIpAddress(const ConnectInfo& connect_info) {
115 net::IPAddress ip_address; 115 net::IPAddress ip_address;
116 return ip_address.AssignFromIPLiteral(connect_info.ip_address); 116 return ip_address.AssignFromIPLiteral(connect_info.ip_address);
117 } 117 }
118 118
119 } // namespace 119 } // namespace
120 120
121 CastChannelAPI::CastChannelAPI(content::BrowserContext* context) 121 CastChannelAPI::CastChannelAPI(content::BrowserContext* context)
122 : browser_context_(context), logger_(new Logger()) { 122 : browser_context_(context) {
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() {
132 return logger_;
133 }
134
135 void CastChannelAPI::SendEvent(const std::string& extension_id, 131 void CastChannelAPI::SendEvent(const std::string& extension_id,
136 std::unique_ptr<Event> event) { 132 std::unique_ptr<Event> event) {
137 DCHECK_CURRENTLY_ON(BrowserThread::UI); 133 DCHECK_CURRENTLY_ON(BrowserThread::UI);
138 EventRouter* event_router = EventRouter::Get(GetBrowserContext()); 134 EventRouter* event_router = EventRouter::Get(GetBrowserContext());
139 if (event_router) { 135 if (event_router) {
140 event_router->DispatchEventToExtension(extension_id, std::move(event)); 136 event_router->DispatchEventToExtension(extension_id, std::move(event));
141 } 137 }
142 } 138 }
143 139
144 static base::LazyInstance< 140 static base::LazyInstance<
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 SetResultFromChannelInfo(channel_info); 205 SetResultFromChannelInfo(channel_info);
210 SetError("Channel error = " + base::IntToString(error)); 206 SetError("Channel error = " + base::IntToString(error));
211 } 207 }
212 208
213 void CastChannelAsyncApiFunction::SetResultFromChannelInfo( 209 void CastChannelAsyncApiFunction::SetResultFromChannelInfo(
214 const ChannelInfo& channel_info) { 210 const ChannelInfo& channel_info) {
215 DCHECK_CURRENTLY_ON(BrowserThread::IO); 211 DCHECK_CURRENTLY_ON(BrowserThread::IO);
216 SetResult(channel_info.ToValue()); 212 SetResult(channel_info.ToValue());
217 } 213 }
218 214
219 CastChannelOpenFunction::CastChannelOpenFunction() 215 CastChannelOpenFunction::CastChannelOpenFunction() {}
220 : new_channel_id_(0) {
221 }
222 216
223 CastChannelOpenFunction::~CastChannelOpenFunction() { } 217 CastChannelOpenFunction::~CastChannelOpenFunction() { }
224 218
225 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( 219 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo(
226 const ConnectInfo& connect_info) { 220 const ConnectInfo& connect_info) {
227 net::IPAddress ip_address; 221 net::IPAddress ip_address;
228 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address)); 222 CHECK(ip_address.AssignFromIPLiteral(connect_info.ip_address));
229 return new net::IPEndPoint(ip_address, 223 return new net::IPEndPoint(ip_address,
230 static_cast<uint16_t>(connect_info.port)); 224 static_cast<uint16_t>(connect_info.port));
231 } 225 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 } 265 }
272 266
273 ip_endpoint_.reset(ParseConnectInfo(connect_info)); 267 ip_endpoint_.reset(ParseConnectInfo(connect_info));
274 return true; 268 return true;
275 } 269 }
276 270
277 void CastChannelOpenFunction::AsyncWorkStart() { 271 void CastChannelOpenFunction::AsyncWorkStart() {
278 DCHECK(api_); 272 DCHECK(api_);
279 DCHECK(ip_endpoint_.get()); 273 DCHECK(ip_endpoint_.get());
280 const ConnectInfo& connect_info = params_->connect_info; 274 const ConnectInfo& connect_info = params_->connect_info;
281 CastSocket* socket; 275
282 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest(); 276 std::unique_ptr<CastSocket> test_socket = api_->GetSocketForTest();
283 if (test_socket.get()) { 277 if (test_socket.get())
284 socket = test_socket.release(); 278 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 279
297 auto* observer = cast_socket_service_->GetObserver(extension_->id()); 280 auto* observer = cast_socket_service_->GetObserver(extension_->id());
298 if (!observer) { 281 if (!observer) {
299 observer = cast_socket_service_->AddObserver( 282 observer = cast_socket_service_->AddObserver(
300 extension_->id(), base::MakeUnique<CastMessageHandler>( 283 extension_->id(), base::MakeUnique<CastMessageHandler>(
301 base::Bind(&CastChannelAPI::SendEvent, 284 base::Bind(&CastChannelAPI::SendEvent,
302 api_->AsWeakPtr(), extension_->id()), 285 api_->AsWeakPtr(), extension_->id()),
303 api_->GetLogger())); 286 cast_socket_service_->GetLogger()));
304 } 287 }
305 288
306 socket->AddObserver(observer); 289 cast_socket_service_->OpenSocket(
307 // Construct read delegates. 290 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(),
308 socket->Connect(base::Bind(&CastChannelOpenFunction::OnOpen, this)); 291 base::TimeDelta::FromMilliseconds(connect_info.timeout.get()
292 ? *connect_info.timeout
293 : kDefaultConnectTimeoutMillis),
294 liveness_timeout_, ping_interval_,
295 connect_info.capabilities.get() ? *connect_info.capabilities
296 : CastDeviceCapability::NONE,
297 base::Bind(&CastChannelOpenFunction::OnOpen, this), observer);
309 } 298 }
310 299
311 void CastChannelOpenFunction::OnOpen(ChannelError result) { 300 void CastChannelOpenFunction::OnOpen(int channel_id, ChannelError result) {
312 DCHECK_CURRENTLY_ON(BrowserThread::IO); 301 DCHECK_CURRENTLY_ON(BrowserThread::IO);
313 VLOG(1) << "Connect finished, OnOpen invoked."; 302 VLOG(1) << "Connect finished, OnOpen invoked.";
314 // TODO: If we failed to open the CastSocket, we may want to clean up here, 303 // 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 304 // rather than relying on the extension to call close(). This can be done by
316 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id). 305 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id).
317 if (result != ChannelError::UNKNOWN) { 306 if (result != ChannelError::UNKNOWN) {
318 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_); 307 CastSocket* socket = cast_socket_service_->GetSocket(channel_id);
319 CHECK(socket); 308 CHECK(socket);
320 SetResultFromSocket(*socket); 309 SetResultFromSocket(*socket);
321 } else { 310 } else {
322 // The socket is being destroyed. 311 // The socket is being destroyed.
323 SetResultFromError(new_channel_id_, 312 SetResultFromError(channel_id, api::cast_channel::CHANNEL_ERROR_UNKNOWN);
324 api::cast_channel::CHANNEL_ERROR_UNKNOWN);
325 } 313 }
326 314
327 AsyncWorkCompleted(); 315 AsyncWorkCompleted();
328 } 316 }
329 317
330 CastChannelSendFunction::CastChannelSendFunction() { } 318 CastChannelSendFunction::CastChannelSendFunction() { }
331 319
332 CastChannelSendFunction::~CastChannelSendFunction() { } 320 CastChannelSendFunction::~CastChannelSendFunction() { }
333 321
334 bool CastChannelSendFunction::Prepare() { 322 bool CastChannelSendFunction::Prepare() {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result; 410 VLOG(1) << "CastChannelCloseFunction::OnClose result = " << result;
423 int channel_id = params_->channel.channel_id; 411 int channel_id = params_->channel.channel_id;
424 CastSocket* socket = cast_socket_service_->GetSocket(channel_id); 412 CastSocket* socket = cast_socket_service_->GetSocket(channel_id);
425 if (result < 0 || !socket) { 413 if (result < 0 || !socket) {
426 SetResultFromError(channel_id, 414 SetResultFromError(channel_id,
427 api::cast_channel::CHANNEL_ERROR_SOCKET_ERROR); 415 api::cast_channel::CHANNEL_ERROR_SOCKET_ERROR);
428 } else { 416 } else {
429 SetResultFromSocket(*socket); 417 SetResultFromSocket(*socket);
430 // This will delete |socket|. 418 // This will delete |socket|.
431 cast_socket_service_->RemoveSocket(channel_id); 419 cast_socket_service_->RemoveSocket(channel_id);
432 api_->GetLogger()->ClearLastError(channel_id); 420 cast_socket_service_->GetLogger()->ClearLastError(channel_id);
433 } 421 }
434 AsyncWorkCompleted(); 422 AsyncWorkCompleted();
435 } 423 }
436 424
437 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler( 425 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler(
438 const EventDispatchCallback& ui_dispatch_cb, 426 const EventDispatchCallback& ui_dispatch_cb,
439 scoped_refptr<Logger> logger) 427 scoped_refptr<Logger> logger)
440 : ui_dispatch_cb_(ui_dispatch_cb), logger_(logger) { 428 : ui_dispatch_cb_(ui_dispatch_cb), logger_(logger) {
441 DCHECK(logger_); 429 DCHECK(logger_);
442 } 430 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 OnMessage::Create(channel_info, message_info); 469 OnMessage::Create(channel_info, message_info);
482 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, 470 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE,
483 OnMessage::kEventName, 471 OnMessage::kEventName,
484 std::move(results))); 472 std::move(results)));
485 BrowserThread::PostTask( 473 BrowserThread::PostTask(
486 BrowserThread::UI, FROM_HERE, 474 BrowserThread::UI, FROM_HERE,
487 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); 475 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event))));
488 } 476 }
489 477
490 } // namespace extensions 478 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/cast_channel/cast_channel_api.h ('k') | extensions/browser/api/cast_channel/cast_channel_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698