| 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" | 
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 64     } | 64     } | 
| 65   } | 65   } | 
| 66 | 66 | 
| 67   return v8::Null(isolate); | 67   return v8::Null(isolate); | 
| 68 } | 68 } | 
| 69 | 69 | 
| 70 }  // namespace | 70 }  // namespace | 
| 71 | 71 | 
| 72 void DisplaySourceCustomBindings::StartSession( | 72 void DisplaySourceCustomBindings::StartSession( | 
| 73     const v8::FunctionCallbackInfo<v8::Value>& args) { | 73     const v8::FunctionCallbackInfo<v8::Value>& args) { | 
| 74   CHECK_EQ(1, args.Length()); | 74   CHECK_EQ(2, args.Length()); | 
| 75   CHECK(args[0]->IsObject()); | 75   CHECK(args[0]->IsObject()); | 
|  | 76   CHECK(args[1]->IsFunction()); | 
|  | 77 | 
| 76   v8::Isolate* isolate = context()->isolate(); | 78   v8::Isolate* isolate = context()->isolate(); | 
| 77   v8::Local<v8::Object> start_info = args[0].As<v8::Object>(); | 79   v8::Local<v8::Object> start_info = args[0].As<v8::Object>(); | 
| 78 | 80 | 
| 79   v8::Local<v8::Value> sink_id_val = | 81   v8::Local<v8::Value> sink_id_val = | 
| 80       GetChildValue(start_info, "sinkId", isolate); | 82       GetChildValue(start_info, "sinkId", isolate); | 
| 81   CHECK(sink_id_val->IsInt32()); | 83   CHECK(sink_id_val->IsInt32()); | 
| 82   const int sink_id = sink_id_val->ToInt32(isolate)->Value(); | 84   const int sink_id = sink_id_val->ToInt32(isolate)->Value(); | 
| 83   if (GetDisplaySession(sink_id)) { | 85   if (GetDisplaySession(sink_id)) { | 
| 84     isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 86     isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 
| 85         isolate, kSessionAlreadyStarted))); | 87         isolate, kSessionAlreadyStarted))); | 
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 157       weak_factory_.GetWeakPtr()); | 159       weak_factory_.GetWeakPtr()); | 
| 158   auto on_terminated_callback = base::Bind( | 160   auto on_terminated_callback = base::Bind( | 
| 159       &DisplaySourceCustomBindings::OnSessionTerminated, | 161       &DisplaySourceCustomBindings::OnSessionTerminated, | 
| 160       weak_factory_.GetWeakPtr()); | 162       weak_factory_.GetWeakPtr()); | 
| 161   auto on_error_callback = base::Bind( | 163   auto on_error_callback = base::Bind( | 
| 162       &DisplaySourceCustomBindings::OnSessionError, | 164       &DisplaySourceCustomBindings::OnSessionError, | 
| 163       weak_factory_.GetWeakPtr()); | 165       weak_factory_.GetWeakPtr()); | 
| 164   session->SetCallbacks(on_started_callback, | 166   session->SetCallbacks(on_started_callback, | 
| 165                         on_terminated_callback, | 167                         on_terminated_callback, | 
| 166                         on_error_callback); | 168                         on_error_callback); | 
|  | 169 | 
|  | 170   CallbackInfo cb_info = GetCallbackInfo(kStarted, sink_id); | 
|  | 171   args.GetReturnValue().Set(static_cast<int32_t>(cb_info.call_id)); | 
|  | 172   callbacks_.push_back(cb_info); | 
|  | 173 | 
| 167   session->Start(); | 174   session->Start(); | 
| 168   session_map_.insert(std::make_pair(sink_id, std::move(session))); | 175   session_map_.insert(std::make_pair(sink_id, std::move(session))); | 
| 169 } | 176 } | 
| 170 | 177 | 
| 171 void DisplaySourceCustomBindings::TerminateSession( | 178 void DisplaySourceCustomBindings::TerminateSession( | 
| 172     const v8::FunctionCallbackInfo<v8::Value>& args) { | 179     const v8::FunctionCallbackInfo<v8::Value>& args) { | 
| 173   CHECK_EQ(1, args.Length()); | 180   CHECK_EQ(2, args.Length()); | 
| 174   CHECK(args[0]->IsInt32()); | 181   CHECK(args[0]->IsInt32()); | 
|  | 182   CHECK(args[1]->IsFunction()); | 
| 175 | 183 | 
| 176   v8::Isolate* isolate = context()->isolate(); | 184   v8::Isolate* isolate = context()->isolate(); | 
| 177   int sink_id = args[0]->ToInt32(args.GetIsolate())->Value(); | 185   int sink_id = args[0]->ToInt32(args.GetIsolate())->Value(); | 
| 178   DisplaySourceSession* session = GetDisplaySession(sink_id); | 186   DisplaySourceSession* session = GetDisplaySession(sink_id); | 
| 179   if (!session) { | 187   if (!session) { | 
| 180     isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 188     isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 
| 181         isolate, kSessionNotFound))); | 189         isolate, kSessionNotFound))); | 
| 182     return; | 190     return; | 
| 183   } | 191   } | 
| 184 | 192 | 
| 185   if (session->state() == DisplaySourceSession::Terminating) { | 193   if (session->state() == DisplaySourceSession::Terminating) { | 
| 186     isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 194     isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 
| 187         isolate, kSessionAlreadyTerminating))); | 195         isolate, kSessionAlreadyTerminating))); | 
| 188     return; | 196     return; | 
| 189   } | 197   } | 
|  | 198 | 
|  | 199   CallbackInfo cb_info = GetCallbackInfo(kTerminated, sink_id); | 
|  | 200   args.GetReturnValue().Set(static_cast<int32_t>(cb_info.call_id)); | 
|  | 201   callbacks_.push_back(cb_info); | 
|  | 202 | 
| 190   // The session will get removed from session_map_ in OnSessionTerminated. | 203   // The session will get removed from session_map_ in OnSessionTerminated. | 
| 191   session->Terminate(); | 204   session->Terminate(); | 
| 192 } | 205 } | 
| 193 | 206 | 
| 194 void DisplaySourceCustomBindings::DispatchSessionStarted(int sink_id) const { | 207 void DisplaySourceCustomBindings::CallCompletionCallback( | 
|  | 208     int sink_id, | 
|  | 209     CallbackType type, | 
|  | 210     const std::string& error_message) { | 
|  | 211   auto predicate = [sink_id, type](const CallbackInfo& info) -> bool { | 
|  | 212     return info.sink_id == sink_id && info.type == type; | 
|  | 213   }; | 
|  | 214   auto it = std::find_if(callbacks_.begin(), callbacks_.end(), predicate); | 
|  | 215   if (it == callbacks_.end()) | 
|  | 216     return; | 
|  | 217 | 
| 195   v8::Isolate* isolate = context()->isolate(); | 218   v8::Isolate* isolate = context()->isolate(); | 
|  | 219   ModuleSystem* module_system = context()->module_system(); | 
| 196   v8::HandleScope handle_scope(isolate); | 220   v8::HandleScope handle_scope(isolate); | 
| 197   v8::Context::Scope context_scope(context()->v8_context()); | 221   v8::Context::Scope context_scope(context()->v8_context()); | 
| 198   v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1); | 222 | 
| 199   event_args->Set(0, v8::Integer::New(isolate, sink_id)); | 223   v8::Local<v8::Value> callback_args[2]; | 
| 200   context()->DispatchEvent("displaySource.onSessionStarted", event_args); | 224   callback_args[0] = v8::Integer::New(isolate, it->call_id); | 
|  | 225   if (error_message.empty()) | 
|  | 226     callback_args[1] = v8::Null(isolate); | 
|  | 227   else | 
|  | 228     callback_args[1] = v8::String::NewFromUtf8(isolate, error_message.c_str()); | 
|  | 229 | 
|  | 230   module_system->CallModuleMethod("displaySource", "callCompletionCallback", 2, | 
|  | 231                                   callback_args); | 
| 201 } | 232 } | 
| 202 | 233 | 
| 203 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const { | 234 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const { | 
| 204   v8::Isolate* isolate = context()->isolate(); | 235   v8::Isolate* isolate = context()->isolate(); | 
| 205   v8::HandleScope handle_scope(isolate); | 236   v8::HandleScope handle_scope(isolate); | 
| 206   v8::Context::Scope context_scope(context()->v8_context()); | 237   v8::Context::Scope context_scope(context()->v8_context()); | 
| 207   v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1); | 238   v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1); | 
| 208   event_args->Set(0, v8::Integer::New(isolate, sink_id)); | 239   event_args->Set(0, v8::Integer::New(isolate, sink_id)); | 
| 209   context()->DispatchEvent("displaySource.onSessionTerminated", event_args); | 240   context()->DispatchEvent("displaySource.onSessionTerminated", event_args); | 
| 210 } | 241 } | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
| 235 | 266 | 
| 236 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession( | 267 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession( | 
| 237     int sink_id) const { | 268     int sink_id) const { | 
| 238   auto iter = session_map_.find(sink_id); | 269   auto iter = session_map_.find(sink_id); | 
| 239   if (iter != session_map_.end()) | 270   if (iter != session_map_.end()) | 
| 240     return iter->second.get(); | 271     return iter->second.get(); | 
| 241   return nullptr; | 272   return nullptr; | 
| 242 } | 273 } | 
| 243 | 274 | 
| 244 void DisplaySourceCustomBindings::OnSessionStarted(int sink_id) { | 275 void DisplaySourceCustomBindings::OnSessionStarted(int sink_id) { | 
| 245   DispatchSessionStarted(sink_id); | 276   CallCompletionCallback(sink_id, kStarted); | 
| 246 } | 277 } | 
| 247 | 278 | 
| 248 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) { | 279 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) { | 
| 249   DisplaySourceSession* session = GetDisplaySession(sink_id); | 280   DisplaySourceSession* session = GetDisplaySession(sink_id); | 
| 250   CHECK(session); | 281   CHECK(session); | 
| 251   session_map_.erase(sink_id); | 282   session_map_.erase(sink_id); | 
| 252   DispatchSessionTerminated(sink_id); | 283   DispatchSessionTerminated(sink_id); | 
|  | 284   CallCompletionCallback(sink_id, kTerminated); | 
| 253 } | 285 } | 
| 254 | 286 | 
| 255 void DisplaySourceCustomBindings::OnSessionError(int sink_id, | 287 void DisplaySourceCustomBindings::OnSessionError(int sink_id, | 
| 256                                                  DisplaySourceErrorType type, | 288                                                  DisplaySourceErrorType type, | 
| 257                                                  const std::string& message) { | 289                                                  const std::string& message) { | 
| 258   DisplaySourceSession* session = GetDisplaySession(sink_id); | 290   DisplaySourceSession* session = GetDisplaySession(sink_id); | 
| 259   CHECK(session); | 291   CHECK(session); | 
| 260   if (session->state() != DisplaySourceSession::Established && |  | 
| 261       session->state() != DisplaySourceSession::Terminating) { |  | 
| 262     // Error has occured before the session has actually started, |  | 
| 263     // no need to wait for session termination notification. |  | 
| 264     session_map_.erase(sink_id); |  | 
| 265   } |  | 
| 266 |  | 
| 267   DispatchSessionError(sink_id, type, message); | 292   DispatchSessionError(sink_id, type, message); | 
| 268 } | 293 } | 
| 269 | 294 | 
|  | 295 DisplaySourceCustomBindings::CallbackInfo | 
|  | 296 DisplaySourceCustomBindings::GetCallbackInfo( | 
|  | 297     DisplaySourceCustomBindings::CallbackType type, | 
|  | 298     int sink_id) const { | 
|  | 299   static int sCallId = 0; | 
|  | 300   return {type, sink_id, ++sCallId}; | 
|  | 301 } | 
|  | 302 | 
| 270 }  // extensions | 303 }  // extensions | 
| OLD | NEW | 
|---|