Chromium Code Reviews| 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/renderer/extensions/cast_streaming_native_handler.h" | 5 #include "chrome/renderer/extensions/cast_streaming_native_handler.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 base::Bind(&CastStreamingNativeHandler::CallGetRawEventsCallback, | 452 base::Bind(&CastStreamingNativeHandler::CallGetRawEventsCallback, |
| 453 weak_factory_.GetWeakPtr(), | 453 weak_factory_.GetWeakPtr(), |
| 454 transport_id)); | 454 transport_id)); |
| 455 } | 455 } |
| 456 | 456 |
| 457 void CastStreamingNativeHandler::GetStats( | 457 void CastStreamingNativeHandler::GetStats( |
| 458 const v8::FunctionCallbackInfo<v8::Value>& args) { | 458 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 459 CHECK_EQ(2, args.Length()); | 459 CHECK_EQ(2, args.Length()); |
| 460 CHECK(args[0]->IsInt32()); | 460 CHECK(args[0]->IsInt32()); |
| 461 CHECK(args[1]->IsFunction()); | 461 CHECK(args[1]->IsFunction()); |
| 462 const int transport_id = args[0]->ToInt32()->Value(); | |
| 463 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); | |
| 464 if (!transport) | |
| 465 return; | |
| 462 | 466 |
| 463 // TODO(imcheng): Implement this. | 467 linked_ptr<extensions::ScopedPersistent<v8::Function> > callback( |
| 468 new extensions::ScopedPersistent<v8::Function>); | |
| 469 callback->reset(args[1].As<v8::Function>()); | |
| 470 get_stats_callbacks_.insert(std::make_pair(transport_id, callback)); | |
| 471 | |
| 472 transport->GetStats( | |
| 473 base::Bind(&CastStreamingNativeHandler::CallGetStatsCallback, | |
| 474 weak_factory_.GetWeakPtr(), | |
| 475 transport_id)); | |
| 464 } | 476 } |
| 465 | 477 |
| 466 void CastStreamingNativeHandler::CallGetRawEventsCallback( | 478 void CastStreamingNativeHandler::CallGetRawEventsCallback( |
| 467 int transport_id, | 479 int transport_id, |
| 468 scoped_ptr<std::string> raw_events) { | 480 scoped_ptr<std::string> raw_events) { |
| 469 v8::Isolate* isolate = context()->isolate(); | 481 v8::Isolate* isolate = context()->isolate(); |
| 470 v8::HandleScope handle_scope(isolate); | 482 v8::HandleScope handle_scope(isolate); |
| 471 v8::Context::Scope context_scope(context()->v8_context()); | 483 v8::Context::Scope context_scope(context()->v8_context()); |
| 472 | 484 |
| 473 RtpStreamCallbackMap::iterator it = | 485 RtpStreamCallbackMap::iterator it = |
| 474 get_raw_events_callbacks_.find(transport_id); | 486 get_raw_events_callbacks_.find(transport_id); |
| 475 if (it != get_raw_events_callbacks_.end()) { | 487 if (it != get_raw_events_callbacks_.end()) { |
|
Alpha Left Google
2014/03/03 07:11:39
nit: Early return if it is not found. This the fol
imcheng
2014/03/04 02:06:24
Done.
| |
| 476 v8::Handle<v8::Value> callback_args[1]; | 488 v8::Handle<v8::Value> callback_args[1]; |
| 477 callback_args[0] = v8::String::NewFromUtf8(isolate, | 489 callback_args[0] = v8::String::NewFromUtf8(isolate, |
| 478 raw_events->data(), | 490 raw_events->data(), |
| 479 v8::String::kNormalString, | 491 v8::String::kNormalString, |
| 480 raw_events->size()); | 492 raw_events->size()); |
| 481 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); | 493 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); |
| 482 get_raw_events_callbacks_.erase(it); | 494 get_raw_events_callbacks_.erase(it); |
| 483 } | 495 } |
| 484 } | 496 } |
| 485 | 497 |
| 498 void CastStreamingNativeHandler::CallGetStatsCallback( | |
| 499 int transport_id, | |
| 500 scoped_ptr<std::string> stats) { | |
| 501 v8::Isolate* isolate = context()->isolate(); | |
| 502 v8::HandleScope handle_scope(isolate); | |
| 503 v8::Context::Scope context_scope(context()->v8_context()); | |
| 504 | |
| 505 RtpStreamCallbackMap::iterator it = get_stats_callbacks_.find(transport_id); | |
| 506 if (it != get_stats_callbacks_.end()) { | |
|
Alpha Left Google
2014/03/03 07:11:39
nit: Early return if it is not found. This the fol
imcheng
2014/03/04 02:06:24
Done.
| |
| 507 v8::Handle<v8::Value> callback_args[1]; | |
| 508 callback_args[0] = v8::String::NewFromUtf8( | |
|
Alpha Left Google
2014/03/03 07:11:39
I think it's odd that we pass the javascript API w
imcheng
2014/03/04 02:06:24
Per offline chat discussion, this api will return
| |
| 509 isolate, stats->data(), v8::String::kNormalString, stats->size()); | |
| 510 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); | |
| 511 get_stats_callbacks_.erase(it); | |
| 512 } | |
| 513 } | |
| 514 | |
| 486 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( | 515 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( |
| 487 int transport_id) const { | 516 int transport_id) const { |
| 488 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( | 517 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( |
| 489 transport_id); | 518 transport_id); |
| 490 if (iter != rtp_stream_map_.end()) | 519 if (iter != rtp_stream_map_.end()) |
| 491 return iter->second.get(); | 520 return iter->second.get(); |
| 492 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 521 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 493 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( | 522 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( |
| 494 isolate, kRtpStreamNotFound))); | 523 isolate, kRtpStreamNotFound))); |
| 495 return NULL; | 524 return NULL; |
| 496 } | 525 } |
| 497 | 526 |
| 498 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( | 527 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( |
| 499 int transport_id) const { | 528 int transport_id) const { |
| 500 UdpTransportMap::const_iterator iter = udp_transport_map_.find( | 529 UdpTransportMap::const_iterator iter = udp_transport_map_.find( |
| 501 transport_id); | 530 transport_id); |
| 502 if (iter != udp_transport_map_.end()) | 531 if (iter != udp_transport_map_.end()) |
| 503 return iter->second.get(); | 532 return iter->second.get(); |
| 504 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 533 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 505 isolate->ThrowException(v8::Exception::RangeError( | 534 isolate->ThrowException(v8::Exception::RangeError( |
| 506 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); | 535 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); |
| 507 return NULL; | 536 return NULL; |
| 508 } | 537 } |
| 509 | 538 |
| 510 } // namespace extensions | 539 } // namespace extensions |
| OLD | NEW |