summaryrefslogtreecommitdiff
path: root/chromium/ui/webui/resources/js/ios/web_ui.js
blob: e49caedd152e8c0265c6a7b806dcbec3108b0fda (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
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
// 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.

window['chrome'] = window['chrome'] || {};

/**
 * Sends messages to the browser. See
 * https://chromium.googlesource.com/chromium/src/+/main/docs/webui_explainer.md#chrome_send
 *
 * @param {string} message name to be passed to the browser.
 * @param {Array=} args optional.
 */
window['chrome']['send'] = function(message, args) {
  __gCrWeb.message.invokeOnHost({
    'command': 'webui.chromeSend',
    'message': message,
    'arguments': args || [],
  });
};


try {
  new EventTarget().constructor;
} catch {
  /**
   * Minimal EventTarget polyfill for webui.
   * TODO(crbug.com/1173902): delete once iOS 13 is deprecated.
   * @constructor
   */
  const EventTarget = function() {
    /**
     * @type {Object}
     */
    this.listeners_ = {};
  };

  /**
   * Registers an event handler of a specific event type on the EventTarget.
   * @param {string} type event type.
   * @param {function} listener event callback.
   * @param {object|boolean|null} options event options.
   */
  EventTarget.prototype.addEventListener = function(type, listener, options) {
    if (!(type in this.listeners_)) {
      this.listeners_[type] = [];
    }
    this.listeners_[type].push({listener: listener, options: options});
  };

  /**
   * Removes an event listener from the EventTarget.
   * @param {string} type event type.
   * @param {function} listener event callback.
   * @param {object|boolean|null} options event options.
   */
  EventTarget.prototype.removeEventListener = function(
      type, listener, options) {
    if (!(type in this.listeners_)) {
      return;
    }

    const stubs = this.listeners_[type];
    let i = 0;
    while (i < stubs.length) {
      if (stubs[i].listener === listener &&
          JSON.stringify(stubs[i].options) === JSON.stringify(options)) {
        stubs.splice(i, 1);
      } else {
        i++;
      }
    }
  };

  /**
   * Dispatches an event to this EventTarget.
   * @param {!Event} event event to dispatch.
   * @return {boolean}
   */
  EventTarget.prototype.dispatchEvent = function(event) {
    if (!(event.type in this.listeners)) {
      return true;
    }

    // Iterate on copy, in case options.once requires removing listener.
    const stubs = this.listeners_[type].slice(0);
    for (let i = 0; i < stubs.length; i++) {
      stubs[i].listener.call(this, event);
      if (typeof stubs[i].options === 'object' && stubs[i].options.once) {
        this.removeEventListener(
            event.type, stubs[i].listener, stubs[i].options);
      }
    }

    return !event.defaultPrevented;
  };

  window.EventTarget = EventTarget;
}