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

Side by Side Diff: chrome/renderer/extensions/cast_streaming_native_handler.cc

Issue 170063006: Cast: Add JS API to get raw events logs from cast extension. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Alpha's comments Created 6 years, 9 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 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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 base::Unretained(this))); 161 base::Unretained(this)));
162 RouteFunction("StopCastRtpStream", 162 RouteFunction("StopCastRtpStream",
163 base::Bind(&CastStreamingNativeHandler::StopCastRtpStream, 163 base::Bind(&CastStreamingNativeHandler::StopCastRtpStream,
164 base::Unretained(this))); 164 base::Unretained(this)));
165 RouteFunction("DestroyCastUdpTransport", 165 RouteFunction("DestroyCastUdpTransport",
166 base::Bind(&CastStreamingNativeHandler::DestroyCastUdpTransport, 166 base::Bind(&CastStreamingNativeHandler::DestroyCastUdpTransport,
167 base::Unretained(this))); 167 base::Unretained(this)));
168 RouteFunction("SetDestinationCastUdpTransport", 168 RouteFunction("SetDestinationCastUdpTransport",
169 base::Bind(&CastStreamingNativeHandler::SetDestinationCastUdpTransport, 169 base::Bind(&CastStreamingNativeHandler::SetDestinationCastUdpTransport,
170 base::Unretained(this))); 170 base::Unretained(this)));
171 RouteFunction("ToggleLogging",
172 base::Bind(&CastStreamingNativeHandler::ToggleLogging,
173 base::Unretained(this)));
174 RouteFunction("GetRawEvents",
175 base::Bind(&CastStreamingNativeHandler::GetRawEvents,
176 base::Unretained(this)));
177 RouteFunction("GetStats",
178 base::Bind(&CastStreamingNativeHandler::GetStats,
179 base::Unretained(this)));
171 } 180 }
172 181
173 CastStreamingNativeHandler::~CastStreamingNativeHandler() { 182 CastStreamingNativeHandler::~CastStreamingNativeHandler() {
174 } 183 }
175 184
176 void CastStreamingNativeHandler::CreateCastSession( 185 void CastStreamingNativeHandler::CreateCastSession(
177 const v8::FunctionCallbackInfo<v8::Value>& args) { 186 const v8::FunctionCallbackInfo<v8::Value>& args) {
178 CHECK_EQ(3, args.Length()); 187 CHECK_EQ(3, args.Length());
179 CHECK(args[0]->IsObject()); 188 CHECK(args[0]->IsObject());
180 CHECK(args[1]->IsObject()); 189 CHECK(args[1]->IsObject());
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 } 411 }
403 net::IPAddressNumber ip; 412 net::IPAddressNumber ip;
404 if (!net::ParseIPLiteralToNumber(destination->address, &ip)) { 413 if (!net::ParseIPLiteralToNumber(destination->address, &ip)) {
405 args.GetIsolate()->ThrowException(v8::Exception::TypeError( 414 args.GetIsolate()->ThrowException(v8::Exception::TypeError(
406 v8::String::NewFromUtf8(args.GetIsolate(), kInvalidDestination))); 415 v8::String::NewFromUtf8(args.GetIsolate(), kInvalidDestination)));
407 return; 416 return;
408 } 417 }
409 transport->SetDestination(net::IPEndPoint(ip, destination->port)); 418 transport->SetDestination(net::IPEndPoint(ip, destination->port));
410 } 419 }
411 420
421 void CastStreamingNativeHandler::ToggleLogging(
422 const v8::FunctionCallbackInfo<v8::Value>& args) {
423 CHECK_EQ(2, args.Length());
424 CHECK(args[0]->IsInt32());
425 CHECK(args[1]->IsBoolean());
426
427 const int stream_id = args[0]->ToInt32()->Value();
428 CastRtpStream* stream = GetRtpStreamOrThrow(stream_id);
429 if (!stream)
430 return;
431
432 const bool enable = args[1]->ToBoolean()->Value();
433 stream->ToggleLogging(enable);
434 }
435
436 void CastStreamingNativeHandler::GetRawEvents(
437 const v8::FunctionCallbackInfo<v8::Value>& args) {
438 CHECK_EQ(2, args.Length());
439 CHECK(args[0]->IsInt32());
440 CHECK(args[1]->IsFunction());
441 const int transport_id = args[0]->ToInt32()->Value();
442 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id);
443 if (!transport)
444 return;
445
446 linked_ptr<extensions::ScopedPersistent<v8::Function> > callback(
447 new extensions::ScopedPersistent<v8::Function>);
448 callback->reset(args[1].As<v8::Function>());
449 get_raw_events_callbacks_.insert(std::make_pair(transport_id, callback));
450
451 transport->GetRawEvents(
452 base::Bind(&CastStreamingNativeHandler::CallGetRawEventsCallback,
453 weak_factory_.GetWeakPtr(),
454 transport_id));
455 }
456
457 void CastStreamingNativeHandler::GetStats(
458 const v8::FunctionCallbackInfo<v8::Value>& args) {
459 CHECK_EQ(2, args.Length());
460 CHECK(args[0]->IsInt32());
461 CHECK(args[1]->IsFunction());
462
463 // TODO(imcheng): Implement this.
464 }
465
466 void CastStreamingNativeHandler::CallGetRawEventsCallback(
467 int transport_id,
468 scoped_ptr<std::string> raw_events) {
469 v8::Isolate* isolate = context()->isolate();
470 v8::HandleScope handle_scope(isolate);
471 v8::Context::Scope context_scope(context()->v8_context());
472
473 RtpStreamCallbackMap::iterator it =
474 get_raw_events_callbacks_.find(transport_id);
475 if (it != get_raw_events_callbacks_.end()) {
476 v8::Handle<v8::Value> callback_args[1];
477 callback_args[0] = v8::String::NewFromUtf8(isolate,
478 raw_events->data(),
479 v8::String::kNormalString,
480 raw_events->size());
481 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args);
482 get_raw_events_callbacks_.erase(it);
483 }
484 }
485
412 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( 486 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow(
413 int transport_id) const { 487 int transport_id) const {
414 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( 488 RtpStreamMap::const_iterator iter = rtp_stream_map_.find(
415 transport_id); 489 transport_id);
416 if (iter != rtp_stream_map_.end()) 490 if (iter != rtp_stream_map_.end())
417 return iter->second.get(); 491 return iter->second.get();
418 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); 492 v8::Isolate* isolate = context()->v8_context()->GetIsolate();
419 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( 493 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8(
420 isolate, kRtpStreamNotFound))); 494 isolate, kRtpStreamNotFound)));
421 return NULL; 495 return NULL;
422 } 496 }
423 497
424 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( 498 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow(
425 int transport_id) const { 499 int transport_id) const {
426 UdpTransportMap::const_iterator iter = udp_transport_map_.find( 500 UdpTransportMap::const_iterator iter = udp_transport_map_.find(
427 transport_id); 501 transport_id);
428 if (iter != udp_transport_map_.end()) 502 if (iter != udp_transport_map_.end())
429 return iter->second.get(); 503 return iter->second.get();
430 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); 504 v8::Isolate* isolate = context()->v8_context()->GetIsolate();
431 isolate->ThrowException(v8::Exception::RangeError( 505 isolate->ThrowException(v8::Exception::RangeError(
432 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); 506 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound)));
433 return NULL; 507 return NULL;
434 } 508 }
435 509
436 } // namespace extensions 510 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698