summaryrefslogtreecommitdiff
path: root/chromium/ui/webui/resources/cr_elements/chromeos/network/cr_network_listener_behavior.js
blob: c467b9aa0937c44e40ec964f404d578bd94b944b (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 2018 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 Polymer behavior for alerting specified child elements of
 * changes to the devices network data.
 */

/** @polymerBehavior */
const CrNetworkListenerBehavior = {
  properties: {
    /**
     * Array of selectors specifying all children to alert of changes to the
     * network list.
     * @private {!Array<string>}
     */
    networkListChangeSubscriberSelectors_: Array,

    /**
     * Array of selectors specifying all children to alert of important changes
     * to the specific networks.
     * @private {!Array<string>}
     */
    networksChangeSubscriberSelectors_: Array,

    /** @type {!NetworkingPrivate} */
    networkingPrivate: Object,
  },

  /** @private {?function(!Array<string>)} */
  networkListChangedListener_: null,

  /** @private {?function(!Array<string>)} */
  networksChangedListener_: null,

  /** @override */
  attached: function() {
    this.networkListChangedListener_ = this.networkListChangedListener_ ||
        this.onNetworkListChanged_.bind(this);
    this.networkingPrivate.onNetworkListChanged.addListener(
        this.networkListChangedListener_);

    this.networksChangedListener_ =
        this.networksChangedListener_ || this.onNetworksChanged_.bind(this);
    this.networkingPrivate.onNetworksChanged.addListener(
        this.networksChangedListener_);
  },

  /** @override */
  detached: function() {
    this.networkingPrivate.onNetworkListChanged.removeListener(
        assert(this.networkListChangedListener_));
    this.networkingPrivate.onNetworksChanged.removeListener(
        assert(this.networksChangedListener_));
  },

  /**
   * This event is triggered when the list of networks changes.
   * |networkIds| contains the ids for all visible or configured networks.
   * networkingPrivate.onNetworkListChanged event callback.
   * @param {!Array<string>} networkIds
   * @private
   */
  onNetworkListChanged_: function(networkIds) {
    const event = new CustomEvent('network-list-changed', {detail: networkIds});
    for (let i = 0; i < this.networkListChangeSubscriberSelectors_.length;
         i++) {
      this.maybeDispatchEvent_(
          this.networkListChangeSubscriberSelectors_[i], event);
    }
  },

  /**
   * This event is triggered when interesting properties of a network change.
   * |networkIds| contains the ids for networks whose properties have changed.
   * networkingPrivate.onNetworksChanged event callback.
   * @param {!Array<string>} networkIds
   * @private
   */
  onNetworksChanged_: function(networkIds) {
    const event = new CustomEvent('networks-changed', {detail: networkIds});
    for (let i = 0; i < this.networksChangeSubscriberSelectors_.length; i++) {
      this.maybeDispatchEvent_(
          this.networksChangeSubscriberSelectors_[i], event);
    }
  },

  /**
   * @param {!Event} event
   * @private
   */
  maybeDispatchEvent_: function(selectors, event) {
    const element = this.$$(selectors);
    if (!element)
      return;
    element.dispatchEvent(event);
  },
};