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