summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/chromeos/network/network_config.js
blob: 97784c7ce508ebb711e46bc98183ff13d6329a58 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2014 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.

'use strict';

/**
 * @fileoverview This object provides a similar API to chrome.networkingPrivate.
 * It simulates the extension callback model by storing callbacks in a member
 * object and invoking them when the corresponding method is called by Chrome in
 * response to a chrome.send call.
 */

var networkConfig = {
  /**
   * Return the property associated with a key (which may reference a
   * sub-object).
   *
   * @param {Object} properties The object containing the network properties.
   * @param {string} key The ONC key for the property. May refer to a nested
   *     propety, e.g. 'WiFi.Security'.
   * @return {*} The value associated with the property.
   */
  getValueFromProperties: function(properties, key) {
    if (!key) {
      console.error('Empty key');
      return undefined;
    }
    var dot = key.indexOf('.');
    if (dot > 0) {
      var key1 = key.substring(0, dot);
      var key2 = key.substring(dot + 1);
      var subobject = properties[key1];
      if (subobject)
        return this.getValueFromProperties(subobject, key2);
    }
    return properties[key];
  },

  /**
   * Generate a unique id for 'callback' and store it for future retrieval.
   *
   * @param {function} callback The associated callback.
   * @return {integer} The id of the callback.
   * @private
   */
  callbackId: 1,
  callbackMap: {},
  storeCallback_: function(callback) {
    var id = this.callbackId++;
    this.callbackMap[id] = callback;
    return id;
  },

  /**
   * Retrieve the callback associated with |id| and remove it from the map.
   *
   * @param {integer} id The id of the callback.
   * @return {function} The associated callback.
   * @private
   */
  retrieveCallback_: function(id) {
    var callback = this.callbackMap[id];
    delete this.callbackMap[id];
    return callback;
  },

  /**
   * Callback invoked by Chrome.
   *
   * @param {Array} args A list of arguments passed to the callback. The first
   *   entry must be the callbackId passed to chrome.send.
   */
  chromeCallbackSuccess: function(args) {
    var callbackId = args.shift();
    var callback = this.retrieveCallback_(callbackId);
    this.lastError = '';
    if (callback)
      callback.apply(null, args);
    else
      console.error('Callback not found for id: ' + callbackId);
  },

  /**
   * Error Callback invoked by Chrome. Sets lastError and logs to the console.
   *
   * @param {Args} args A list of arguments. The first entry must be the
   *   callbackId passed to chrome.send.
   */
  lastError: '',
  chromeCallbackError: function(args) {
    var callbackId = args.shift();
    this.lastError = args.shift();
    console.error('Callback error: "' + this.lastError);
    // We still invoke the callback, but with null args. The callback should
    // check this.lastError and handle that.
    var callback = this.retrieveCallback_(callbackId);
    if (callback)
      callback.apply(null, null);
  },

  /**
   * Implement networkingPrivate.getProperties. See networking_private.json.
   *
   * @param {string} guid The guid identifying the network.
   * @param {function()} callback The callback to call on completion.
   */
  getProperties: function(guid, callback) {
    var callbackId = this.storeCallback_(callback);
    chrome.send('networkConfig.getProperties', [callbackId, guid]);
  },

  /**
   * Implement networkingPrivate.getNetworks. See networking_private.json.
   *
   * @param {string} guid The guid identifying the network.
   * @param {function()} callback The callback to call on completion.
   */
  getNetworks: function(filter, callback) {
    var callbackId = this.storeCallback_(callback);
    chrome.send('networkConfig.getNetworks', [callbackId, filter]);
  }
};