summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/settings/autofill_page/blocking_request_manager.js
blob: b82aa59e60bccf907466a55940cf484f544d5e38 (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Helper class for making blocking requests that are resolved
 * elsewhere in the DOM.
 */
cr.define('settings', function() {
  class BlockingRequestManager {
    /**
     * @param {Function=} makeRequest Function to initiate flow for request. If
     *     no function is provided, it defaults to this.resolve, i.e. it
     *     immediately resolves all requests.
     */
    constructor(makeRequest) {
      this.makeRequest_ = makeRequest || this.resolve;
      /**
       * @private {Function} callback Provided in requests and called when the
       *     request is resolved.
       */
      this.callback_ = null;
    }

    /**
     * Make a blocking request.
     * @param {Function} callback Function to be called if/when the request is
     *     successfully resolved.
     */
    request(callback) {
      this.callback_ = callback;
      this.makeRequest_();
    }

    /** Called if/when request is resolved successfully. */
    resolve() {
      this.callback_();
    }
  }

  return {
    BlockingRequestManager: BlockingRequestManager,
  };
});