| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/renderer/display_source_custom_bindings.h" | 5 #include "extensions/renderer/display_source_custom_bindings.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
| 11 #include "extensions/renderer/extension_bindings_system.h" |
| 11 #include "extensions/renderer/script_context.h" | 12 #include "extensions/renderer/script_context.h" |
| 12 #include "third_party/WebKit/public/platform/WebMediaStream.h" | 13 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
| 13 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 14 #include "third_party/WebKit/public/web/WebDOMMediaStreamTrack.h" | 15 #include "third_party/WebKit/public/web/WebDOMMediaStreamTrack.h" |
| 15 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 19 using content::V8ValueConverter; | 20 using content::V8ValueConverter; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 const char kErrorNotSupported[] = "Not supported"; | 23 const char kErrorNotSupported[] = "Not supported"; |
| 23 const char kInvalidStreamArgs[] = "Invalid stream arguments"; | 24 const char kInvalidStreamArgs[] = "Invalid stream arguments"; |
| 24 const char kSessionAlreadyStarted[] = "The session has been already started"; | 25 const char kSessionAlreadyStarted[] = "The session has been already started"; |
| 25 const char kSessionAlreadyTerminating[] = "The session is already terminating"; | 26 const char kSessionAlreadyTerminating[] = "The session is already terminating"; |
| 26 const char kSessionNotFound[] = "Session not found"; | 27 const char kSessionNotFound[] = "Session not found"; |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 DisplaySourceCustomBindings::DisplaySourceCustomBindings(ScriptContext* context) | 30 DisplaySourceCustomBindings::DisplaySourceCustomBindings( |
| 31 ScriptContext* context, |
| 32 ExtensionBindingsSystem* bindings_system) |
| 30 : ObjectBackedNativeHandler(context), | 33 : ObjectBackedNativeHandler(context), |
| 34 bindings_system_(bindings_system), |
| 31 weak_factory_(this) { | 35 weak_factory_(this) { |
| 32 RouteFunction("StartSession", "displaySource", | 36 RouteFunction("StartSession", "displaySource", |
| 33 base::Bind(&DisplaySourceCustomBindings::StartSession, | 37 base::Bind(&DisplaySourceCustomBindings::StartSession, |
| 34 weak_factory_.GetWeakPtr())); | 38 weak_factory_.GetWeakPtr())); |
| 35 RouteFunction("TerminateSession", "displaySource", | 39 RouteFunction("TerminateSession", "displaySource", |
| 36 base::Bind(&DisplaySourceCustomBindings::TerminateSession, | 40 base::Bind(&DisplaySourceCustomBindings::TerminateSession, |
| 37 weak_factory_.GetWeakPtr())); | 41 weak_factory_.GetWeakPtr())); |
| 38 } | 42 } |
| 39 | 43 |
| 40 DisplaySourceCustomBindings::~DisplaySourceCustomBindings() { | 44 DisplaySourceCustomBindings::~DisplaySourceCustomBindings() { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 const std::string& error_message) { | 247 const std::string& error_message) { |
| 244 CHECK(GetDisplaySession(sink_id)); | 248 CHECK(GetDisplaySession(sink_id)); |
| 245 if (!success) { | 249 if (!success) { |
| 246 // Session has failed to start, removing it. | 250 // Session has failed to start, removing it. |
| 247 session_map_.erase(sink_id); | 251 session_map_.erase(sink_id); |
| 248 } | 252 } |
| 249 OnCallCompleted(call_id, success, error_message); | 253 OnCallCompleted(call_id, success, error_message); |
| 250 } | 254 } |
| 251 | 255 |
| 252 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const { | 256 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const { |
| 253 v8::Isolate* isolate = context()->isolate(); | 257 base::ListValue event_args; |
| 254 v8::HandleScope handle_scope(isolate); | 258 event_args.AppendInteger(sink_id); |
| 255 v8::Context::Scope context_scope(context()->v8_context()); | 259 bindings_system_->DispatchEventInContext("displaySource.onSessionTerminated", |
| 256 v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1); | 260 &event_args, nullptr, context()); |
| 257 event_args->Set(0, v8::Integer::New(isolate, sink_id)); | |
| 258 context()->DispatchEvent("displaySource.onSessionTerminated", event_args); | |
| 259 } | 261 } |
| 260 | 262 |
| 261 void DisplaySourceCustomBindings::DispatchSessionError( | 263 void DisplaySourceCustomBindings::DispatchSessionError( |
| 262 int sink_id, | 264 int sink_id, |
| 263 DisplaySourceErrorType type, | 265 DisplaySourceErrorType type, |
| 264 const std::string& message) const { | 266 const std::string& message) const { |
| 265 v8::Isolate* isolate = context()->isolate(); | |
| 266 v8::HandleScope handle_scope(isolate); | |
| 267 v8::Context::Scope context_scope(context()->v8_context()); | |
| 268 | |
| 269 api::display_source::ErrorInfo error_info; | 267 api::display_source::ErrorInfo error_info; |
| 270 error_info.type = type; | 268 error_info.type = type; |
| 271 if (!message.empty()) | 269 if (!message.empty()) |
| 272 error_info.description.reset(new std::string(message)); | 270 error_info.description.reset(new std::string(message)); |
| 273 | 271 |
| 274 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 272 base::ListValue event_args; |
| 275 v8::Local<v8::Value> info_arg = | 273 event_args.AppendInteger(sink_id); |
| 276 converter->ToV8Value(error_info.ToValue().get(), | 274 event_args.Append(error_info.ToValue()); |
| 277 context()->v8_context()); | 275 bindings_system_->DispatchEventInContext( |
| 278 | 276 "displaySource.onSessionErrorOccured", &event_args, nullptr, context()); |
| 279 v8::Local<v8::Array> event_args = v8::Array::New(isolate, 2); | |
| 280 event_args->Set(0, v8::Integer::New(isolate, sink_id)); | |
| 281 event_args->Set(1, info_arg); | |
| 282 context()->DispatchEvent("displaySource.onSessionErrorOccured", event_args); | |
| 283 } | 277 } |
| 284 | 278 |
| 285 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession( | 279 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession( |
| 286 int sink_id) const { | 280 int sink_id) const { |
| 287 auto iter = session_map_.find(sink_id); | 281 auto iter = session_map_.find(sink_id); |
| 288 if (iter != session_map_.end()) | 282 if (iter != session_map_.end()) |
| 289 return iter->second.get(); | 283 return iter->second.get(); |
| 290 return nullptr; | 284 return nullptr; |
| 291 } | 285 } |
| 292 | 286 |
| 293 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) { | 287 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) { |
| 294 CHECK(GetDisplaySession(sink_id)); | 288 CHECK(GetDisplaySession(sink_id)); |
| 295 session_map_.erase(sink_id); | 289 session_map_.erase(sink_id); |
| 296 DispatchSessionTerminated(sink_id); | 290 DispatchSessionTerminated(sink_id); |
| 297 } | 291 } |
| 298 | 292 |
| 299 void DisplaySourceCustomBindings::OnSessionError(int sink_id, | 293 void DisplaySourceCustomBindings::OnSessionError(int sink_id, |
| 300 DisplaySourceErrorType type, | 294 DisplaySourceErrorType type, |
| 301 const std::string& message) { | 295 const std::string& message) { |
| 302 CHECK(GetDisplaySession(sink_id)); | 296 CHECK(GetDisplaySession(sink_id)); |
| 303 DispatchSessionError(sink_id, type, message); | 297 DispatchSessionError(sink_id, type, message); |
| 304 } | 298 } |
| 305 | 299 |
| 306 } // extensions | 300 } // extensions |
| OLD | NEW |