Chromium Code Reviews| 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 v8::Global<v8::Function> callback(isolate, args[1].As<v8::Function>()); | |
| 170 session_started_cb_map_.insert(std::make_pair(sink_id, std::move(callback))); | |
| 171 | |
| 167 session->Start(); | 172 session->Start(); |
| 168 session_map_.insert(std::make_pair(sink_id, std::move(session))); | 173 session_map_.insert(std::make_pair(sink_id, std::move(session))); |
| 169 } | 174 } |
| 170 | 175 |
| 171 void DisplaySourceCustomBindings::TerminateSession( | 176 void DisplaySourceCustomBindings::TerminateSession( |
| 172 const v8::FunctionCallbackInfo<v8::Value>& args) { | 177 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 173 CHECK_EQ(1, args.Length()); | 178 CHECK_EQ(2, args.Length()); |
| 174 CHECK(args[0]->IsInt32()); | 179 CHECK(args[0]->IsInt32()); |
| 180 CHECK(args[1]->IsFunction()); | |
| 175 | 181 |
| 176 v8::Isolate* isolate = context()->isolate(); | 182 v8::Isolate* isolate = context()->isolate(); |
| 177 int sink_id = args[0]->ToInt32(args.GetIsolate())->Value(); | 183 int sink_id = args[0]->ToInt32(args.GetIsolate())->Value(); |
| 178 DisplaySourceSession* session = GetDisplaySession(sink_id); | 184 DisplaySourceSession* session = GetDisplaySession(sink_id); |
| 179 if (!session) { | 185 if (!session) { |
| 180 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 186 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( |
| 181 isolate, kSessionNotFound))); | 187 isolate, kSessionNotFound))); |
| 182 return; | 188 return; |
| 183 } | 189 } |
| 184 | 190 |
| 185 if (session->state() == DisplaySourceSession::Terminating) { | 191 if (session->state() == DisplaySourceSession::Terminating) { |
| 186 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( | 192 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( |
| 187 isolate, kSessionAlreadyTerminating))); | 193 isolate, kSessionAlreadyTerminating))); |
| 188 return; | 194 return; |
| 189 } | 195 } |
| 196 v8::Global<v8::Function> callback(isolate, args[1].As<v8::Function>()); | |
| 197 session_finished_cb_map_.insert(std::make_pair(sink_id, std::move(callback))); | |
| 190 // The session will get removed from session_map_ in OnSessionTerminated. | 198 // The session will get removed from session_map_ in OnSessionTerminated. |
| 191 session->Terminate(); | 199 session->Terminate(); |
| 192 } | 200 } |
| 193 | 201 |
| 202 void DisplaySourceCustomBindings::CallSessionStartedCallback( | |
| 203 int sink_id, | |
| 204 const std::string& error_message) { | |
| 205 auto iter = session_started_cb_map_.find(sink_id); | |
| 206 if (iter == session_started_cb_map_.end()) | |
| 207 return; | |
| 208 CallCompletionCallback(iter->second, error_message); | |
| 209 session_started_cb_map_.erase(iter); | |
| 210 } | |
| 211 | |
| 212 void DisplaySourceCustomBindings::CallSessionTerminatedCallback( | |
| 213 int sink_id, | |
| 214 const std::string& error_message) { | |
| 215 auto iter = session_finished_cb_map_.find(sink_id); | |
| 216 if (iter == session_finished_cb_map_.end()) | |
| 217 return; | |
| 218 CallCompletionCallback(iter->second, error_message); | |
| 219 session_finished_cb_map_.erase(iter); | |
| 220 } | |
| 221 | |
| 222 void DisplaySourceCustomBindings::CallCompletionCallback( | |
| 223 const v8::Global<v8::Function>& callback, | |
| 224 const std::string& error_message) { | |
| 225 v8::Isolate* isolate = context()->isolate(); | |
| 226 v8::HandleScope handle_scope(isolate); | |
| 227 v8::Context::Scope context_scope(context()->v8_context()); | |
| 228 | |
| 229 v8::Local<v8::Value> callback_args[1]; | |
| 230 if (error_message.empty()) | |
| 231 callback_args[0] = v8::Null(isolate); | |
| 232 else | |
| 233 callback_args[0] = v8::String::NewFromUtf8(isolate, error_message.c_str()); | |
| 234 context()->CallFunction(v8::Local<v8::Function>::New(isolate, callback), 1, | |
| 235 callback_args); | |
| 236 } | |
| 237 | |
| 194 void DisplaySourceCustomBindings::DispatchSessionStarted(int sink_id) const { | 238 void DisplaySourceCustomBindings::DispatchSessionStarted(int sink_id) const { |
| 195 v8::Isolate* isolate = context()->isolate(); | 239 v8::Isolate* isolate = context()->isolate(); |
| 196 v8::HandleScope handle_scope(isolate); | 240 v8::HandleScope handle_scope(isolate); |
| 197 v8::Context::Scope context_scope(context()->v8_context()); | 241 v8::Context::Scope context_scope(context()->v8_context()); |
| 198 v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1); | 242 v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1); |
| 199 event_args->Set(0, v8::Integer::New(isolate, sink_id)); | 243 event_args->Set(0, v8::Integer::New(isolate, sink_id)); |
| 200 context()->DispatchEvent("displaySource.onSessionStarted", event_args); | 244 context()->DispatchEvent("displaySource.onSessionStarted", event_args); |
| 201 } | 245 } |
| 202 | 246 |
| 203 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const { | 247 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession( | 280 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession( |
| 237 int sink_id) const { | 281 int sink_id) const { |
| 238 auto iter = session_map_.find(sink_id); | 282 auto iter = session_map_.find(sink_id); |
| 239 if (iter != session_map_.end()) | 283 if (iter != session_map_.end()) |
| 240 return iter->second.get(); | 284 return iter->second.get(); |
| 241 return nullptr; | 285 return nullptr; |
| 242 } | 286 } |
| 243 | 287 |
| 244 void DisplaySourceCustomBindings::OnSessionStarted(int sink_id) { | 288 void DisplaySourceCustomBindings::OnSessionStarted(int sink_id) { |
| 245 DispatchSessionStarted(sink_id); | 289 DispatchSessionStarted(sink_id); |
| 290 CallSessionStartedCallback(sink_id); | |
|
asargent_no_longer_on_chrome
2016/02/09 00:07:56
One question that came to mind seeing this - does
Mikhail
2016/02/09 14:26:42
Good point. Unlike session errors or termination,
asargent_no_longer_on_chrome
2016/02/09 17:04:05
Since the API is still only available in dev chann
Mikhail
2016/02/10 15:58:51
The 'chrome.displaySource' is removed now.
| |
| 246 } | 291 } |
| 247 | 292 |
| 248 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) { | 293 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) { |
| 249 DisplaySourceSession* session = GetDisplaySession(sink_id); | 294 DisplaySourceSession* session = GetDisplaySession(sink_id); |
| 250 CHECK(session); | 295 CHECK(session); |
| 251 session_map_.erase(sink_id); | 296 session_map_.erase(sink_id); |
| 252 DispatchSessionTerminated(sink_id); | 297 DispatchSessionTerminated(sink_id); |
| 298 CallSessionTerminatedCallback(sink_id); | |
| 253 } | 299 } |
| 254 | 300 |
| 255 void DisplaySourceCustomBindings::OnSessionError(int sink_id, | 301 void DisplaySourceCustomBindings::OnSessionError(int sink_id, |
| 256 DisplaySourceErrorType type, | 302 DisplaySourceErrorType type, |
| 257 const std::string& message) { | 303 const std::string& message) { |
| 258 DisplaySourceSession* session = GetDisplaySession(sink_id); | 304 DisplaySourceSession* session = GetDisplaySession(sink_id); |
| 259 CHECK(session); | 305 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); | 306 DispatchSessionError(sink_id, type, message); |
| 268 } | 307 } |
| 269 | 308 |
| 270 } // extensions | 309 } // extensions |
| OLD | NEW |