1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5cr.define('network.config', function() {
6  var NetworkConfig = cr.ui.define('div');
7
8  NetworkConfig.prototype = {
9    __proto__: HTMLDivElement.prototype,
10    decorate: function() {
11      var params = parseQueryParams(window.location);
12      this.networkId_ = params.network;
13      this.activeArea_ = null;
14      this.userArea_ = null;
15      this.managedArea_ = null;
16      this.stateArea_ = null;
17      this.updateDom_();
18      this.fetchProperties_();
19    },
20
21    fetchProperties_: function() {
22      chrome.networkingPrivate.getProperties(
23          this.networkId_,
24          this.updateActiveSettings_.bind(this));
25      chrome.networkingPrivate.getManagedProperties(
26          this.networkId_,
27          this.updateManagedSettings_.bind(this));
28      chrome.networkingPrivate.getState(
29          this.networkId_,
30          this.updateState_.bind(this));
31    },
32
33    stringifyJSON_: function(properties) {
34      return JSON.stringify(properties, undefined, 2);
35    },
36
37    updateActiveSettings_: function(properties) {
38      this.activeArea_.value = this.stringifyJSON_(properties);
39    },
40
41    updateManagedSettings_: function(properties) {
42      var error = chrome.runtime.lastError;
43      if (error) {
44        this.managedArea_.value = error.message;
45        this.userArea_.value = 'undefined';
46      } else {
47        this.managedArea_.value = this.stringifyJSON_(properties);
48        this.userArea_.value = this.stringifyJSON_(
49            this.extractUserSettings_(properties));
50      }
51    },
52
53    updateState_: function(properties) {
54      this.stateArea_.value = this.stringifyJSON_(properties);
55    },
56
57    extractUserSettings_: function(properties) {
58      if ('UserSetting' in properties)
59        return properties['UserSetting'];
60
61      if ('SharedSetting' in properties)
62        return properties['SharedSetting'];
63
64      var result = {};
65      for (var fieldName in properties) {
66        var entry = properties[fieldName];
67        if (typeof entry === 'object') {
68          var nestedResult = this.extractUserSettings_(entry);
69          if (nestedResult)
70            result[fieldName] = nestedResult;
71        }
72      }
73      if (Object.keys(result).length)
74        return result;
75      else
76        return undefined;
77    },
78
79    updateDom_: function() {
80      var div = document.createElement('div');
81
82      this.activeArea_ = function() {
83        var label = document.createElement('h4');
84        label.textContent = 'Active Settings (getProperties)';
85        div.appendChild(label);
86        var area = document.createElement('textarea');
87        div.appendChild(area);
88        return area;
89      }();
90
91      this.userArea_ = function() {
92        var label = document.createElement('h4');
93        label.textContent = 'User Settings';
94        div.appendChild(label);
95        var area = document.createElement('textarea');
96        div.appendChild(area);
97        return area;
98      }();
99
100      this.managedArea_ = function() {
101        var label = document.createElement('h4');
102        label.textContent = 'Managed Settings (getManagedProperties)';
103        div.appendChild(label);
104        var area = document.createElement('textarea');
105        div.appendChild(area);
106        return area;
107      }();
108
109      this.stateArea_ = function() {
110        var label = document.createElement('h4');
111        label.textContent = 'State (getState)';
112        div.appendChild(label);
113        var area = document.createElement('textarea');
114        div.appendChild(area);
115        return area;
116      }();
117
118      this.appendChild(div);
119    },
120
121    get userSettings() {
122      return JSON.parse(this.userArea_.value);
123    },
124
125    get networkId() {
126      return this.networkId_;
127    }
128  };
129
130  return {
131    NetworkConfig: NetworkConfig
132  };
133});
134