File: api_request_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (670 lines) | stat: -rw-r--r-- 27,044 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/bindings/api_request_handler.h"

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "base/values.h"
#include "content/public/renderer/v8_value_converter.h"
#include "extensions/renderer/bindings/api_binding_util.h"
#include "extensions/renderer/bindings/api_response_validator.h"
#include "extensions/renderer/bindings/exception_handler.h"
#include "extensions/renderer/bindings/js_runner.h"
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/data_object_builder.h"
#include "third_party/blink/public/web/web_blob.h"

namespace extensions {

// Keys used for passing data back through a custom callback;
constexpr const char kErrorKey[] = "error";
constexpr const char kResolverKey[] = "resolver";
constexpr const char kExceptionHandlerKey[] = "exceptionHandler";

// A helper class to adapt base::Value-style response arguments to v8 arguments
// lazily, or simply return v8 arguments directly (depending on which style of
// arguments were used in construction).
class APIRequestHandler::ArgumentAdapter {
 public:
  ArgumentAdapter(const base::Value::List* base_argumements,
                  mojom::ExtraResponseDataPtr extra_data);
  explicit ArgumentAdapter(const v8::LocalVector<v8::Value>& v8_arguments);

  ArgumentAdapter(ArgumentAdapter&) = delete;
  ArgumentAdapter& operator=(const ArgumentAdapter&) = delete;
  ArgumentAdapter(ArgumentAdapter&&) = default;
  ArgumentAdapter& operator=(ArgumentAdapter&&) = default;

  ~ArgumentAdapter();

  const v8::LocalVector<v8::Value>& GetArguments(
      v8::Local<v8::Context> context) const;

  mojom::ExtraResponseDataPtr TakeExtraData() { return std::move(extra_data_); }

 private:
  raw_ptr<const base::Value::List> base_arguments_ = nullptr;
  mutable std::optional<v8::LocalVector<v8::Value>> v8_arguments_;
  mojom::ExtraResponseDataPtr extra_data_ = nullptr;
};

APIRequestHandler::ArgumentAdapter::ArgumentAdapter(
    const base::Value::List* base_arguments,
    mojom::ExtraResponseDataPtr extra_data)
    : base_arguments_(base_arguments), extra_data_(std::move(extra_data)) {}
APIRequestHandler::ArgumentAdapter::ArgumentAdapter(
    const v8::LocalVector<v8::Value>& v8_arguments)
    : v8_arguments_(v8_arguments) {}
APIRequestHandler::ArgumentAdapter::~ArgumentAdapter() = default;

const v8::LocalVector<v8::Value>&
APIRequestHandler::ArgumentAdapter::GetArguments(
    v8::Local<v8::Context> context) const {
  v8::Isolate* isolate = context->GetIsolate();
  DCHECK(isolate->GetCurrentContext() == context);

  if (base_arguments_) {
    DCHECK(!v8_arguments_.has_value())
        << "GetArguments() should only be called once.";
    std::unique_ptr<content::V8ValueConverter> converter =
        content::V8ValueConverter::Create();
    v8_arguments_.emplace(isolate);
    v8_arguments_->reserve(base_arguments_->size());
    for (const auto& arg : *base_arguments_) {
      v8_arguments_->push_back(converter->ToV8Value(arg, context));
    }
  }

  DCHECK(v8_arguments_.has_value());
  return v8_arguments_.value();
}

// A helper class to handler delivering the results of an API call to a handler,
// which can be either a callback or a promise.
// TODO(devlin): The overlap in this class for handling a promise vs a callback
// is pretty minimal, leading to a lot of if/else branching. This might be
// cleaner with separate versions for the two cases.
class APIRequestHandler::AsyncResultHandler {
 public:
  // A callback-based result handler.
  AsyncResultHandler(v8::Isolate* isolate,
                     v8::Local<v8::Function> callback,
                     v8::Local<v8::Function> custom_callback,
                     binding::ResultModifierFunction result_modifier,
                     ExceptionHandler* exception_handler);
  // A promise-based result handler.
  AsyncResultHandler(v8::Isolate* isolate,
                     v8::Local<v8::Promise::Resolver> promise_resolver,
                     v8::Local<v8::Function> custom_callback,
                     binding::ResultModifierFunction result_modifier);

  AsyncResultHandler(const AsyncResultHandler&) = delete;
  AsyncResultHandler& operator=(const AsyncResultHandler&) = delete;

  ~AsyncResultHandler();

  // Resolve the request.
  // - Sets last error, if it's not promise-based.
  // - If the request had a custom callback, this calls the custom callback,
  // with a handle to then resolve the extension's callback or promise.
  // - Else, invokes the extension's callback or resolves the promise
  // immediately.
  void ResolveRequest(v8::Local<v8::Context> context,
                      APILastError* last_error,
                      const v8::LocalVector<v8::Value>& response_args,
                      const std::string& error,
                      mojom::ExtraResponseDataPtr extra_data);

  // Returns true if the request handler is using a custom callback.
  bool has_custom_callback() const { return !custom_callback_.IsEmpty(); }

 private:
  // Delivers the result to the promise resolver.
  static void ResolvePromise(v8::Local<v8::Context> context,
                             const v8::LocalVector<v8::Value>& response_args,
                             const std::string& error,
                             v8::Local<v8::Promise::Resolver> resolver);

  // Delivers the result to the callback provided by the extension.
  static void CallExtensionCallback(v8::Local<v8::Context> context,
                                    v8::LocalVector<v8::Value> response_args,
                                    v8::Local<v8::Function> extension_callback,
                                    ExceptionHandler* exception_handler);

  // Helper function to handle the result after the bindings' custom callback
  // has completed.
  static void CustomCallbackAdaptor(
      const v8::FunctionCallbackInfo<v8::Value>& info);

  // Delivers the result to the custom callback.
  void CallCustomCallback(v8::Local<v8::Context> context,
                          const v8::LocalVector<v8::Value>& response_args,
                          const std::string& error);

  // The type of asynchronous response this handler is for.
  const binding::AsyncResponseType async_type_;

  // Callback-based handlers. Mutually exclusive with promise-based handlers.
  v8::Global<v8::Function> extension_callback_;

  // Promise-based handlers. Mutually exclusive with callback-based handlers.
  v8::Global<v8::Promise::Resolver> promise_resolver_;

  // The ExceptionHandler used to catch any exceptions thrown when handling the
  // extension's callback. Only non-null for callback-based requests.
  // This is guaranteed to be valid while the AsyncResultHandler is valid
  // because the ExceptionHandler lives for the duration of the bindings
  // system, similar to the APIRequestHandler (which owns this).
  raw_ptr<ExceptionHandler> exception_handler_ = nullptr;

  // Custom callback handlers.
  v8::Global<v8::Function> custom_callback_;

  // A OnceCallback that can be used to modify the return arguments.
  binding::ResultModifierFunction result_modifier_;
};

APIRequestHandler::AsyncResultHandler::AsyncResultHandler(
    v8::Isolate* isolate,
    v8::Local<v8::Function> extension_callback,
    v8::Local<v8::Function> custom_callback,
    binding::ResultModifierFunction result_modifier,
    ExceptionHandler* exception_handler)
    : async_type_(binding::AsyncResponseType::kCallback),
      exception_handler_(exception_handler),
      result_modifier_(std::move(result_modifier)) {
  DCHECK(!extension_callback.IsEmpty() || !custom_callback.IsEmpty());
  DCHECK(exception_handler_);
  if (!extension_callback.IsEmpty()) {
    extension_callback_.Reset(isolate, extension_callback);
  }
  if (!custom_callback.IsEmpty()) {
    custom_callback_.Reset(isolate, custom_callback);
  }
}

APIRequestHandler::AsyncResultHandler::AsyncResultHandler(
    v8::Isolate* isolate,
    v8::Local<v8::Promise::Resolver> promise_resolver,
    v8::Local<v8::Function> custom_callback,
    binding::ResultModifierFunction result_modifier)
    : async_type_(binding::AsyncResponseType::kPromise),
      result_modifier_(std::move(result_modifier)) {
  // NOTE(devlin): We'll need to handle an empty promise resolver if
  // v8::Promise::Resolver::New() isn't guaranteed.
  DCHECK(!promise_resolver.IsEmpty());
  promise_resolver_.Reset(isolate, promise_resolver);
  if (!custom_callback.IsEmpty()) {
    custom_callback_.Reset(isolate, custom_callback);
  }
}

APIRequestHandler::AsyncResultHandler::~AsyncResultHandler() = default;

void APIRequestHandler::AsyncResultHandler::ResolveRequest(
    v8::Local<v8::Context> context,
    APILastError* last_error,
    const v8::LocalVector<v8::Value>& response_args,
    const std::string& error,
    mojom::ExtraResponseDataPtr extra_data) {
  v8::Isolate* isolate = context->GetIsolate();

  // Set runtime.lastError if there is an error and this isn't a promise-based
  // request (promise-based requests instead reject to indicate failure).
  bool set_last_error = promise_resolver_.IsEmpty() && !error.empty();
  if (set_last_error) {
    last_error->SetError(context, error);
  }

  // If there is a result modifier for this async request and the response args
  // are not empty, run the result modifier and allow it to massage the return
  // arguments before we send them back.
  // Note: a request can end up with a result modifier and be returning an empty
  // set of args if we are responding that an error occurred.
  v8::LocalVector<v8::Value> args =
      result_modifier_.is_null() || response_args.empty()
          ? response_args
          : std::move(result_modifier_)
                .Run(response_args, context, async_type_);

  if (has_custom_callback()) {
    // Blobs that are part of the response are passed in as an extra parameter
    // to the custom callback. The custom callback can then incorporate these
    // blobs appropriately in its response.
    if (extra_data) {
      v8::LocalVector<v8::Value> v8_blobs(isolate);
      for (auto& blob : extra_data->blobs) {
        auto web_blob =
            blink::WebBlob::CreateFromSerializedBlob(std::move(blob));
        v8_blobs.push_back(web_blob.ToV8Value(context->GetIsolate()));
      }
      auto blobs = v8::Array::New(context->GetIsolate(), v8_blobs.data(),
                                  v8_blobs.size());
      args.push_back(std::move(blobs));
    }

    // Custom callback case; the custom callback will invoke a curried-in
    // callback, which will trigger the response in the extension (either
    // promise or callback).
    CallCustomCallback(context, args, error);
  } else if (!promise_resolver_.IsEmpty()) {  // Promise-based request.
    DCHECK(extension_callback_.IsEmpty());
    ResolvePromise(context, args, error, promise_resolver_.Get(isolate));
  } else {  // Callback-based request.
    DCHECK(!extension_callback_.IsEmpty());
    DCHECK(exception_handler_);
    CallExtensionCallback(context, std::move(args),
                          extension_callback_.Get(isolate), exception_handler_);
  }

  // Since arbitrary JS was run the context might have been invalidated, so only
  // clear last error if the context is still valid.
  if (set_last_error && binding::IsContextValid(context)) {
    last_error->ClearError(context, true);
  }
}

// static
void APIRequestHandler::AsyncResultHandler::ResolvePromise(
    v8::Local<v8::Context> context,
    const v8::LocalVector<v8::Value>& response_args,
    const std::string& error,
    v8::Local<v8::Promise::Resolver> resolver) {
  DCHECK_LE(response_args.size(), 1u);

  v8::Isolate* isolate = context->GetIsolate();
  v8::MicrotasksScope microtasks_scope(
      isolate, context->GetMicrotaskQueue(),
      v8::MicrotasksScope::kDoNotRunMicrotasks);

  if (error.empty()) {
    v8::Local<v8::Value> result;
    if (!response_args.empty()) {
      result = response_args[0];
    } else {
      result = v8::Undefined(isolate);
    }

    v8::Maybe<bool> promise_result = resolver->Resolve(context, result);
    // TODO(devlin): It's potentially possible that this could throw if V8
    // is terminating on a worker thread; however, it's unclear what happens in
    // that scenario (we may appropriately shutdown the thread, or any future
    // access of v8 may cause crashes). Make this a CHECK() to flush out any
    // situations in which this is a concern. If there are no crashes after
    // some time, we may be able to downgrade this.
    CHECK(promise_result.IsJust());
  } else {
    v8::Local<v8::Value> v8_error =
        v8::Exception::Error(gin::StringToV8(isolate, error));
    v8::Maybe<bool> promise_result = resolver->Reject(context, v8_error);
    // See comment above.
    CHECK(promise_result.IsJust());
  }
}

// static
void APIRequestHandler::AsyncResultHandler::CallExtensionCallback(
    v8::Local<v8::Context> context,
    v8::LocalVector<v8::Value> args,
    v8::Local<v8::Function> extension_callback,
    ExceptionHandler* exception_handler) {
  DCHECK(exception_handler);
  // TODO(devlin): Integrate the API method name in the error message. This
  // will require currying it around a bit more.
  exception_handler->RunExtensionCallback(
      context, extension_callback, std::move(args), "Error handling response");
}

// static
void APIRequestHandler::AsyncResultHandler::CustomCallbackAdaptor(
    const v8::FunctionCallbackInfo<v8::Value>& info) {
  gin::Arguments arguments(info);
  v8::Isolate* isolate = arguments.isolate();
  v8::HandleScope handle_scope(isolate);
  v8::Local<v8::Context> context = isolate->GetCurrentContext();

  v8::Local<v8::Object> data = info.Data().As<v8::Object>();

  v8::Local<v8::Value> resolver =
      data->Get(context, gin::StringToSymbol(isolate, kResolverKey))
          .ToLocalChecked();
  if (resolver->IsFunction()) {
    v8::Local<v8::Value> exception_handler_value =
        data->Get(context, gin::StringToSymbol(isolate, kExceptionHandlerKey))
            .ToLocalChecked();
    ExceptionHandler* exception_handler =
        ExceptionHandler::FromV8Wrapper(isolate, exception_handler_value);
    // `exception_handler` could be null if the context were invalidated.
    // Since this can be invoked arbitrarily from running JS, we need to
    // handle this case gracefully.
    if (!exception_handler) {
      return;
    }

    CallExtensionCallback(context, arguments.GetAll(),
                          resolver.As<v8::Function>(), exception_handler);
  } else {
    v8::Local<v8::Value> error_value =
        data->Get(context, gin::StringToSymbol(isolate, kErrorKey))
            .ToLocalChecked();
    const std::string error = gin::V8ToString(isolate, error_value);

    CHECK(resolver->IsPromise());
    ResolvePromise(context, arguments.GetAll(), error,
                   resolver.As<v8::Promise::Resolver>());
  }
}

void APIRequestHandler::AsyncResultHandler::CallCustomCallback(
    v8::Local<v8::Context> context,
    const v8::LocalVector<v8::Value>& response_args,
    const std::string& error) {
  v8::Isolate* isolate = context->GetIsolate();

  v8::Local<v8::Value> callback_to_pass = v8::Undefined(isolate);
  if (!extension_callback_.IsEmpty() || !promise_resolver_.IsEmpty()) {
    gin::DataObjectBuilder data_builder(isolate);
    v8::Local<v8::Value> resolver_value;
    if (!promise_resolver_.IsEmpty()) {
      resolver_value = promise_resolver_.Get(isolate);
    } else {
      DCHECK(exception_handler_);
      resolver_value = extension_callback_.Get(isolate);
      data_builder.Set(kExceptionHandlerKey,
                       exception_handler_->GetV8Wrapper(isolate));
    }
    v8::Local<v8::Object> data = data_builder.Set(kResolverKey, resolver_value)
                                     .Set(kErrorKey, error)
                                     .Build();
    // Rather than passing the original callback, we create a function which
    // calls back to an adpator which will invoke the original callback or
    // resolve the promises, depending on the type of request that was made to
    // the API.
    callback_to_pass = v8::Function::New(context, &CustomCallbackAdaptor, data)
                           .ToLocalChecked();
  }

  // Custom callbacks in the JS bindings are called with the arguments of the
  // callback function and the response from the API.
  v8::LocalVector<v8::Value> custom_callback_args(isolate);
  custom_callback_args.reserve(1 + response_args.size());
  custom_callback_args.push_back(callback_to_pass);
  custom_callback_args.insert(custom_callback_args.end(), response_args.begin(),
                              response_args.end());

  JSRunner::Get(context)->RunJSFunction(custom_callback_.Get(isolate), context,
                                        custom_callback_args);
}

APIRequestHandler::Request::Request() = default;
APIRequestHandler::Request::~Request() = default;

APIRequestHandler::RequestDetails::RequestDetails(
    int request_id,
    v8::Local<v8::Promise> promise)
    : request_id(request_id), promise(promise) {}
APIRequestHandler::RequestDetails::~RequestDetails() = default;
APIRequestHandler::RequestDetails::RequestDetails(const RequestDetails& other) =
    default;

APIRequestHandler::PendingRequest::PendingRequest(
    v8::Isolate* isolate,
    v8::Local<v8::Context> context,
    const std::string& method_name,
    std::unique_ptr<AsyncResultHandler> async_handler,
    std::unique_ptr<InteractionProvider::Token> gesture_token)
    : isolate(isolate),
      context(isolate, context),
      method_name(method_name),
      async_handler(std::move(async_handler)) {
  // Only curry the user gesture through if there's something to handle the
  // response.
  if (this->async_handler) {
    user_gesture_token = std::move(gesture_token);
  }
}

APIRequestHandler::PendingRequest::~PendingRequest() = default;
APIRequestHandler::PendingRequest::PendingRequest(PendingRequest&&) = default;
APIRequestHandler::PendingRequest& APIRequestHandler::PendingRequest::operator=(
    PendingRequest&&) = default;

APIRequestHandler::APIRequestHandler(
    SendRequestMethod send_request,
    APILastError last_error,
    ExceptionHandler* exception_handler,
    const InteractionProvider* interaction_provider)
    : send_request_(std::move(send_request)),
      last_error_(std::move(last_error)),
      exception_handler_(exception_handler),
      interaction_provider_(interaction_provider) {}

APIRequestHandler::~APIRequestHandler() = default;

v8::Local<v8::Promise> APIRequestHandler::StartRequest(
    v8::Local<v8::Context> context,
    const std::string& method,
    base::Value::List arguments_list,
    binding::AsyncResponseType async_type,
    v8::Local<v8::Function> callback,
    v8::Local<v8::Function> custom_callback,
    binding::ResultModifierFunction result_modifier) {
  v8::Isolate* isolate = context->GetIsolate();

  v8::Local<v8::Promise> promise;
  std::unique_ptr<AsyncResultHandler> async_handler =
      GetAsyncResultHandler(context, async_type, callback, custom_callback,
                            std::move(result_modifier), &promise);
  DCHECK_EQ(async_type == binding::AsyncResponseType::kPromise,
            !promise.IsEmpty());

  int request_id = GetNextRequestId();
  auto request = std::make_unique<Request>();
  request->request_id = request_id;

  std::unique_ptr<InteractionProvider::Token> user_gesture_token;
  if (async_handler) {
    user_gesture_token = interaction_provider_->GetCurrentToken(context);
    request->has_async_response_handler = true;
  }

  pending_requests_.emplace(
      request_id,
      PendingRequest(isolate, context, method, std::move(async_handler),
                     std::move(user_gesture_token)));

  request->has_user_gesture =
      interaction_provider_->HasActiveInteraction(context);
  request->arguments_list = std::move(arguments_list);
  request->method_name = method;

  last_sent_request_id_ = request_id;
  send_request_.Run(std::move(request), context);

  return promise;
}

void APIRequestHandler::CompleteRequest(
    int request_id,
    const base::Value::List& response_args,
    const std::string& error,
    mojom::ExtraResponseDataPtr extra_data) {
  CompleteRequestImpl(request_id,
                      ArgumentAdapter(&response_args, std::move(extra_data)),
                      error);
}

void APIRequestHandler::CompleteRequest(
    int request_id,
    const v8::LocalVector<v8::Value>& response_args,
    const std::string& error) {
  CompleteRequestImpl(request_id, ArgumentAdapter(response_args), error);
}

APIRequestHandler::RequestDetails APIRequestHandler::AddPendingRequest(
    v8::Local<v8::Context> context,
    binding::AsyncResponseType async_type,
    v8::Local<v8::Function> callback,
    binding::ResultModifierFunction result_modifier) {
  v8::Isolate* isolate = context->GetIsolate();
  v8::Local<v8::Promise> promise;
  std::unique_ptr<AsyncResultHandler> async_handler = GetAsyncResultHandler(
      context, async_type, callback, v8::Local<v8::Function>(),
      std::move(result_modifier), &promise);
  DCHECK_EQ(async_type == binding::AsyncResponseType::kPromise,
            !promise.IsEmpty());

  int request_id = GetNextRequestId();

  // NOTE(devlin): We ignore the UserGestureToken for synthesized requests like
  // these that aren't sent to the browser. It is the caller's responsibility to
  // handle any user gesture behavior. This prevents an issue where messaging
  // handling would create an extra scoped user gesture, causing issues. See
  // https://crbug.com/921141.
  std::unique_ptr<InteractionProvider::Token> null_user_gesture_token;

  pending_requests_.emplace(
      request_id,
      PendingRequest(isolate, context, std::string(), std::move(async_handler),
                     std::move(null_user_gesture_token)));

  return RequestDetails(request_id, promise);
}

void APIRequestHandler::InvalidateContext(v8::Local<v8::Context> context) {
  for (auto iter = pending_requests_.begin();
       iter != pending_requests_.end();) {
    if (iter->second.context == context) {
      iter = pending_requests_.erase(iter);
    } else {
      ++iter;
    }
  }
}

void APIRequestHandler::SetResponseValidator(
    std::unique_ptr<APIResponseValidator> response_validator) {
  DCHECK(!response_validator_);
  response_validator_ = std::move(response_validator);
}

std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const {
  std::set<int> result;
  for (const auto& pair : pending_requests_) {
    result.insert(pair.first);
  }
  return result;
}

int APIRequestHandler::GetNextRequestId() {
  // The request id is primarily used in the renderer to associate an API
  // request with the associated callback, but it's also used in the browser as
  // an identifier for the extension function (e.g. by the pageCapture API).
  // TODO(devlin): We should probably fix this, since the request id is only
  // unique per-isolate, rather than globally.
  // TODO(devlin): We could *probably* get away with just using an integer
  // here, but it's a little less foolproof. How slow is GenerateGUID? Should
  // we use that instead? It means updating the IPC
  // (ExtensionHostMsg_Request).
  // base::UnguessableToken is another good option.
  return next_request_id_++;
}

std::unique_ptr<APIRequestHandler::AsyncResultHandler>
APIRequestHandler::GetAsyncResultHandler(
    v8::Local<v8::Context> context,
    binding::AsyncResponseType async_type,
    v8::Local<v8::Function> extension_callback,
    v8::Local<v8::Function> custom_callback,
    binding::ResultModifierFunction result_modifier,
    v8::Local<v8::Promise>* promise_out) {
  v8::Isolate* isolate = context->GetIsolate();

  std::unique_ptr<AsyncResultHandler> async_handler;
  if (async_type == binding::AsyncResponseType::kPromise) {
    DCHECK(extension_callback.IsEmpty())
        << "Promise based requests should never be "
           "started with a callback being passed in.";
    v8::Local<v8::Promise::Resolver> resolver =
        v8::Promise::Resolver::New(context).ToLocalChecked();
    async_handler = std::make_unique<AsyncResultHandler>(
        isolate, resolver, custom_callback, std::move(result_modifier));
    *promise_out = resolver->GetPromise();
  } else if (!custom_callback.IsEmpty() || !extension_callback.IsEmpty()) {
    async_handler = std::make_unique<AsyncResultHandler>(
        isolate, extension_callback, custom_callback,
        std::move(result_modifier), exception_handler_);
  }
  return async_handler;
}

void APIRequestHandler::CompleteRequestImpl(int request_id,
                                            ArgumentAdapter arguments,
                                            const std::string& error) {
  auto iter = pending_requests_.find(request_id);
  // The request may have been removed if the context was invalidated before a
  // response is ready.
  if (iter == pending_requests_.end()) {
    return;
  }

  PendingRequest pending_request = std::move(iter->second);
  pending_requests_.erase(iter);

  v8::Isolate* isolate = pending_request.isolate;
  v8::HandleScope handle_scope(isolate);
  v8::Local<v8::Context> context = pending_request.context.Get(isolate);
  v8::Context::Scope context_scope(context);

  if (!pending_request.async_handler) {
    // If there's no async handler associated with the request, but there is an
    // error, report the error as if it were unchecked.
    if (!error.empty()) {
      // TODO(devlin): Use pending_requeset.method_name here?
      last_error_.ReportUncheckedError(context, error);
    }
    // No async handler to trigger, so we're done!
    return;
  }

  const v8::LocalVector<v8::Value>& response_args =
      arguments.GetArguments(context);

  std::unique_ptr<InteractionProvider::Scope> user_gesture;
  if (pending_request.user_gesture_token) {
    user_gesture = interaction_provider_->CreateScopedInteraction(
        context, std::move(pending_request.user_gesture_token));
  }

  if (response_validator_) {
    bool has_custom_callback =
        pending_request.async_handler->has_custom_callback();
    response_validator_->ValidateResponse(
        context, pending_request.method_name, response_args, error,
        has_custom_callback
            ? APIResponseValidator::CallbackType::kAPIProvided
            : APIResponseValidator::CallbackType::kCallerProvided);
  }

  v8::TryCatch try_catch(isolate);
  pending_request.async_handler->ResolveRequest(
      context, &last_error_, response_args, error, arguments.TakeExtraData());

  // Since arbitrary JS has ran, the context may have been invalidated. If it
  // was, bail.
  if (!binding::IsContextValid(context)) {
    return;
  }

  if (try_catch.HasCaught()) {
    exception_handler_->HandleException(context, "Error handling response",
                                        &try_catch);
  }
}

}  // namespace extensions