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

Side by Side Diff: extensions/renderer/display_source_custom_bindings.cc

Issue 1730583002: [chrome.displaySource] further implementation of call completion callbacks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from Antony Created 4 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 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 if (try_catch.HasCaught()) { 60 if (try_catch.HasCaught()) {
61 return v8::Null(isolate); 61 return v8::Null(isolate);
62 } 62 }
63 return child_v8; 63 return child_v8;
64 } 64 }
65 } 65 }
66 66
67 return v8::Null(isolate); 67 return v8::Null(isolate);
68 } 68 }
69 69
70 int32_t GetCallbackId() {
71 static int32_t sCallId = 0;
72 return ++sCallId;
73 }
74
70 } // namespace 75 } // namespace
71 76
72 void DisplaySourceCustomBindings::StartSession( 77 void DisplaySourceCustomBindings::StartSession(
73 const v8::FunctionCallbackInfo<v8::Value>& args) { 78 const v8::FunctionCallbackInfo<v8::Value>& args) {
74 CHECK_EQ(2, args.Length()); 79 CHECK_EQ(1, args.Length());
75 CHECK(args[0]->IsObject()); 80 CHECK(args[0]->IsObject());
76 CHECK(args[1]->IsFunction());
77 81
78 v8::Isolate* isolate = context()->isolate(); 82 v8::Isolate* isolate = context()->isolate();
79 v8::Local<v8::Object> start_info = args[0].As<v8::Object>(); 83 v8::Local<v8::Object> start_info = args[0].As<v8::Object>();
80 84
81 v8::Local<v8::Value> sink_id_val = 85 v8::Local<v8::Value> sink_id_val =
82 GetChildValue(start_info, "sinkId", isolate); 86 GetChildValue(start_info, "sinkId", isolate);
83 CHECK(sink_id_val->IsInt32()); 87 CHECK(sink_id_val->IsInt32());
84 const int sink_id = sink_id_val->ToInt32(isolate)->Value(); 88 const int sink_id = sink_id_val->ToInt32(isolate)->Value();
85 if (GetDisplaySession(sink_id)) { 89 if (GetDisplaySession(sink_id)) {
86 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( 90 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 session_params.auth_data = auth_info->data ? *auth_info->data : ""; 151 session_params.auth_data = auth_info->data ? *auth_info->data : "";
148 } 152 }
149 scoped_ptr<DisplaySourceSession> session = 153 scoped_ptr<DisplaySourceSession> session =
150 DisplaySourceSessionFactory::CreateSession(session_params); 154 DisplaySourceSessionFactory::CreateSession(session_params);
151 if (!session) { 155 if (!session) {
152 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( 156 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(
153 isolate, kErrorNotSupported))); 157 isolate, kErrorNotSupported)));
154 return; 158 return;
155 } 159 }
156 160
157 auto on_started_callback = base::Bind( 161 auto on_terminated_callback =
158 &DisplaySourceCustomBindings::OnSessionStarted, 162 base::Bind(&DisplaySourceCustomBindings::OnSessionTerminated,
159 weak_factory_.GetWeakPtr()); 163 weak_factory_.GetWeakPtr(), sink_id);
160 auto on_terminated_callback = base::Bind( 164 auto on_error_callback =
161 &DisplaySourceCustomBindings::OnSessionTerminated, 165 base::Bind(&DisplaySourceCustomBindings::OnSessionError,
162 weak_factory_.GetWeakPtr()); 166 weak_factory_.GetWeakPtr(), sink_id);
163 auto on_error_callback = base::Bind( 167 session->SetNotificationCallbacks(on_terminated_callback, on_error_callback);
164 &DisplaySourceCustomBindings::OnSessionError,
165 weak_factory_.GetWeakPtr());
166 session->SetCallbacks(on_started_callback,
167 on_terminated_callback,
168 on_error_callback);
169 168
170 CallbackInfo cb_info = GetCallbackInfo(kStarted, sink_id); 169 int32_t call_id = GetCallbackId();
171 args.GetReturnValue().Set(static_cast<int32_t>(cb_info.call_id)); 170 args.GetReturnValue().Set(call_id);
172 callbacks_.push_back(cb_info);
173 171
174 session->Start(); 172 auto on_call_completed =
173 base::Bind(&DisplaySourceCustomBindings::OnSessionStarted,
174 weak_factory_.GetWeakPtr(), sink_id, call_id);
175 session->Start(on_call_completed);
175 session_map_.insert(std::make_pair(sink_id, std::move(session))); 176 session_map_.insert(std::make_pair(sink_id, std::move(session)));
176 } 177 }
177 178
178 void DisplaySourceCustomBindings::TerminateSession( 179 void DisplaySourceCustomBindings::TerminateSession(
179 const v8::FunctionCallbackInfo<v8::Value>& args) { 180 const v8::FunctionCallbackInfo<v8::Value>& args) {
180 CHECK_EQ(2, args.Length()); 181 CHECK_EQ(1, args.Length());
181 CHECK(args[0]->IsInt32()); 182 CHECK(args[0]->IsInt32());
182 CHECK(args[1]->IsFunction());
183 183
184 v8::Isolate* isolate = context()->isolate(); 184 v8::Isolate* isolate = context()->isolate();
185 int sink_id = args[0]->ToInt32(args.GetIsolate())->Value(); 185 int sink_id = args[0]->ToInt32(args.GetIsolate())->Value();
186 DisplaySourceSession* session = GetDisplaySession(sink_id); 186 DisplaySourceSession* session = GetDisplaySession(sink_id);
187 if (!session) { 187 if (!session) {
188 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( 188 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(
189 isolate, kSessionNotFound))); 189 isolate, kSessionNotFound)));
190 return; 190 return;
191 } 191 }
192 192
193 if (session->state() == DisplaySourceSession::Terminating) { 193 DisplaySourceSession::State state = session->state();
194 DCHECK_NE(state, DisplaySourceSession::Idle);
195 if (state == DisplaySourceSession::Establishing) {
196 // 'session started' callback has not yet been invoked.
197 // This session is not existing for the user.
198 isolate->ThrowException(v8::Exception::Error(
199 v8::String::NewFromUtf8(isolate, kSessionNotFound)));
200 return;
201 }
202
203 if (state == DisplaySourceSession::Terminating) {
194 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8( 204 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(
195 isolate, kSessionAlreadyTerminating))); 205 isolate, kSessionAlreadyTerminating)));
196 return; 206 return;
197 } 207 }
198 208
199 CallbackInfo cb_info = GetCallbackInfo(kTerminated, sink_id); 209 int32_t call_id = GetCallbackId();
200 args.GetReturnValue().Set(static_cast<int32_t>(cb_info.call_id)); 210 args.GetReturnValue().Set(call_id);
201 callbacks_.push_back(cb_info);
202 211
212 auto on_call_completed =
213 base::Bind(&DisplaySourceCustomBindings::OnCallCompleted,
214 weak_factory_.GetWeakPtr(), call_id);
203 // The session will get removed from session_map_ in OnSessionTerminated. 215 // The session will get removed from session_map_ in OnSessionTerminated.
204 session->Terminate(); 216 session->Terminate(on_call_completed);
205 } 217 }
206 218
207 void DisplaySourceCustomBindings::CallCompletionCallback( 219 void DisplaySourceCustomBindings::OnCallCompleted(
208 int sink_id, 220 int call_id,
209 CallbackType type, 221 bool success,
210 const std::string& error_message) { 222 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
218 v8::Isolate* isolate = context()->isolate(); 223 v8::Isolate* isolate = context()->isolate();
219 ModuleSystem* module_system = context()->module_system(); 224 ModuleSystem* module_system = context()->module_system();
220 v8::HandleScope handle_scope(isolate); 225 v8::HandleScope handle_scope(isolate);
221 v8::Context::Scope context_scope(context()->v8_context()); 226 v8::Context::Scope context_scope(context()->v8_context());
222 227
223 v8::Local<v8::Value> callback_args[2]; 228 v8::Local<v8::Value> callback_args[2];
224 callback_args[0] = v8::Integer::New(isolate, it->call_id); 229 callback_args[0] = v8::Integer::New(isolate, call_id);
225 if (error_message.empty()) 230 if (success)
226 callback_args[1] = v8::Null(isolate); 231 callback_args[1] = v8::Null(isolate);
227 else 232 else
228 callback_args[1] = v8::String::NewFromUtf8(isolate, error_message.c_str()); 233 callback_args[1] = v8::String::NewFromUtf8(isolate, error_message.c_str());
229 234
230 module_system->CallModuleMethod("displaySource", "callCompletionCallback", 2, 235 module_system->CallModuleMethod("displaySource", "callCompletionCallback", 2,
231 callback_args); 236 callback_args);
232 } 237 }
233 238
239 void DisplaySourceCustomBindings::OnSessionStarted(
240 int sink_id,
241 int call_id,
242 bool success,
243 const std::string& error_message) {
244 CHECK(GetDisplaySession(sink_id));
245 if (!success) {
246 // Session has failed to start, removing it.
247 session_map_.erase(sink_id);
248 }
249 OnCallCompleted(call_id, success, error_message);
250 }
251
234 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const { 252 void DisplaySourceCustomBindings::DispatchSessionTerminated(int sink_id) const {
235 v8::Isolate* isolate = context()->isolate(); 253 v8::Isolate* isolate = context()->isolate();
236 v8::HandleScope handle_scope(isolate); 254 v8::HandleScope handle_scope(isolate);
237 v8::Context::Scope context_scope(context()->v8_context()); 255 v8::Context::Scope context_scope(context()->v8_context());
238 v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1); 256 v8::Local<v8::Array> event_args = v8::Array::New(isolate, 1);
239 event_args->Set(0, v8::Integer::New(isolate, sink_id)); 257 event_args->Set(0, v8::Integer::New(isolate, sink_id));
240 context()->DispatchEvent("displaySource.onSessionTerminated", event_args); 258 context()->DispatchEvent("displaySource.onSessionTerminated", event_args);
241 } 259 }
242 260
243 void DisplaySourceCustomBindings::DispatchSessionError( 261 void DisplaySourceCustomBindings::DispatchSessionError(
(...skipping 21 matching lines...) Expand all
265 } 283 }
266 284
267 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession( 285 DisplaySourceSession* DisplaySourceCustomBindings::GetDisplaySession(
268 int sink_id) const { 286 int sink_id) const {
269 auto iter = session_map_.find(sink_id); 287 auto iter = session_map_.find(sink_id);
270 if (iter != session_map_.end()) 288 if (iter != session_map_.end())
271 return iter->second.get(); 289 return iter->second.get();
272 return nullptr; 290 return nullptr;
273 } 291 }
274 292
275 void DisplaySourceCustomBindings::OnSessionStarted(int sink_id) {
276 CallCompletionCallback(sink_id, kStarted);
277 }
278
279 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) { 293 void DisplaySourceCustomBindings::OnSessionTerminated(int sink_id) {
280 DisplaySourceSession* session = GetDisplaySession(sink_id); 294 CHECK(GetDisplaySession(sink_id));
281 CHECK(session);
282 session_map_.erase(sink_id); 295 session_map_.erase(sink_id);
283 DispatchSessionTerminated(sink_id); 296 DispatchSessionTerminated(sink_id);
284 CallCompletionCallback(sink_id, kTerminated);
285 } 297 }
286 298
287 void DisplaySourceCustomBindings::OnSessionError(int sink_id, 299 void DisplaySourceCustomBindings::OnSessionError(int sink_id,
288 DisplaySourceErrorType type, 300 DisplaySourceErrorType type,
289 const std::string& message) { 301 const std::string& message) {
290 DisplaySourceSession* session = GetDisplaySession(sink_id); 302 CHECK(GetDisplaySession(sink_id));
291 CHECK(session);
292 DispatchSessionError(sink_id, type, message); 303 DispatchSessionError(sink_id, type, message);
293 } 304 }
294 305
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
303 } // extensions 306 } // extensions
OLDNEW
« no previous file with comments | « extensions/renderer/display_source_custom_bindings.h ('k') | extensions/renderer/resources/display_source_custom_bindings.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698