Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: chrome/browser/resources/settings/internet_page/network_nameservers.js

Issue 2179223004: MD Settings: Internet: Clean up network and proxy sections. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@issue_609156_internet_cleanup_3
Patch Set: Add MAX_NAMESERVERS Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 /** 5 /**
6 * @fileoverview Polymer element for displaying network nameserver options. 6 * @fileoverview Polymer element for displaying network nameserver options.
7 */ 7 */
8 (function() {
9 'use strict';
10
11 Polymer({ 8 Polymer({
12 is: 'network-nameservers', 9 is: 'network-nameservers',
13 10
14 properties: { 11 properties: {
15 /** 12 /**
16 * The network properties dictionary containing the nameserver properties to 13 * The network properties dictionary containing the nameserver properties to
17 * display and modify. 14 * display and modify.
18 * @type {!CrOnc.NetworkProperties|undefined} 15 * @type {!CrOnc.NetworkProperties|undefined}
19 */ 16 */
20 networkProperties: { 17 networkProperties: {
21 type: Object, 18 type: Object,
22 observer: 'networkPropertiesChanged_' 19 observer: 'networkPropertiesChanged_',
23 }, 20 },
24 21
25 /** 22 /**
26 * Whether or not the nameservers can be edited. 23 * Whether or not the nameservers can be edited.
27 */ 24 */
28 editable: { 25 editable: {
29 type: Boolean, 26 type: Boolean,
30 value: false 27 value: false,
31 }, 28 },
32 29
33 /** 30 /**
34 * Array of nameserver addresses stored as strings. 31 * Array of nameserver addresses stored as strings.
35 * @type {!Array<string>} 32 * @type {!Array<string>}
36 */ 33 */
37 nameservers: { 34 nameservers: {
38 type: Array, 35 type: Array,
39 value: function() { return []; } 36 value: function() {
37 return [];
38 },
40 }, 39 },
41 40
42 /** 41 /**
43 * The selected nameserver type. 42 * The selected nameserver type.
44 */ 43 */
45 nameserversType: { 44 nameserversType: {
46 type: String, 45 type: String,
47 value: 'automatic' 46 value: 'automatic',
48 }, 47 },
49 48
50 /** 49 /**
51 * Array of nameserver types. 50 * Array of nameserver types.
52 */ 51 */
53 nameserverTypeNames_: { 52 nameserverTypeNames_: {
54 type: Array, 53 type: Array,
55 value: ['automatic', 'google', 'custom'], 54 value: ['automatic', 'google', 'custom'],
56 readOnly: true 55 readOnly: true,
57 }, 56 },
58 }, 57 },
59 58
60 /** @const */ GoogleNameservers: ['8.8.4.4', '8.8.8.8'], 59 /** @const */
60 GOOGLE_NAMESERVERS: [
61 '8.8.4.4',
62 '8.8.8.8',
63 ],
64
65 /** @const */
66 MAX_NAMESERVERS: 4,
61 67
62 /** 68 /**
63 * Saved nameservers when switching to 'automatic'. 69 * Saved nameservers when switching to 'automatic'.
64 * @type {!Array<string>} 70 * @type {!Array<string>}
65 */ 71 */
66 savedNameservers_: [], 72 savedNameservers_: [],
67 73
68 /** 74 /** @private */
69 * Polymer networkProperties changed method.
70 */
71 networkPropertiesChanged_: function(newValue, oldValue) { 75 networkPropertiesChanged_: function(newValue, oldValue) {
72 if (!this.networkProperties) 76 if (!this.networkProperties)
73 return; 77 return;
74 78
75 if (!oldValue || newValue.GUID != oldValue.GUID) 79 if (!oldValue || newValue.GUID != oldValue.GUID)
76 this.savedNameservers_ = []; 80 this.savedNameservers_ = [];
77 81
78 // Update the 'nameservers' property. 82 // Update the 'nameservers' property.
79 var nameservers = []; 83 var nameservers = [];
80 var ipv4 = 84 var ipv4 =
81 CrOnc.getIPConfigForType(this.networkProperties, CrOnc.IPType.IPV4); 85 CrOnc.getIPConfigForType(this.networkProperties, CrOnc.IPType.IPV4);
82 if (ipv4 && ipv4.NameServers) 86 if (ipv4 && ipv4.NameServers)
83 nameservers = ipv4.NameServers; 87 nameservers = ipv4.NameServers;
84 88
85 // Update the 'nameserversType' property. 89 // Update the 'nameserversType' property.
86 var configType = 90 var configType =
87 CrOnc.getActiveValue(this.networkProperties.NameServersConfigType); 91 CrOnc.getActiveValue(this.networkProperties.NameServersConfigType);
88 var type; 92 var type;
89 if (configType == CrOnc.IPConfigType.STATIC) { 93 if (configType == CrOnc.IPConfigType.STATIC) {
90 if (nameservers.join(',') == this.GoogleNameservers.join(',')) 94 if (nameservers.join(',') == this.GOOGLE_NAMESERVERS.join(',')) {
91 type = 'google'; 95 type = 'google';
92 else 96 } else {
93 type = 'custom'; 97 type = 'custom';
98 }
94 } else { 99 } else {
95 type = 'automatic'; 100 type = 'automatic';
96 } 101 }
97 this.nameserversType = type; 102 this.setNameservers_(type, nameservers);
98 this.$$('#type').selectedIndex = this.getSelectedIndex_(type); 103 },
99 104
105 /**
106 * @param {string} nameserversType
107 * @param {!Array<string>} nameservers
108 * @private
109 */
110 setNameservers_: function(nameserversType, nameservers) {
111 this.nameserversType = nameserversType;
112 if (nameserversType == 'custom') {
113 // Add empty entries for unset custom nameservers.
114 for (let i = nameservers.length; i < this.MAX_NAMESERVERS; ++i)
115 nameservers[i] = '';
116 }
100 this.nameservers = nameservers; 117 this.nameservers = nameservers;
101 }, 118 },
102 119
103 /** 120 /**
104 * @param {string} nameserversType The nameservers type.
105 * @return {number} The selected index for |nameserversType|.
106 * @private
107 */
108 getSelectedIndex_: function(nameserversType) {
109 var idx = this.nameserverTypeNames_.indexOf(nameserversType);
110 if (idx != -1)
111 return idx;
112 console.error('Unexpected type: ' + nameserversType);
113 return 0;
114 },
115
116 /**
117 * @param {string} nameserversType The nameservers type. 121 * @param {string} nameserversType The nameservers type.
118 * @return {string} The description for |nameserversType|. 122 * @return {string} The description for |nameserversType|.
119 * @private 123 * @private
120 */ 124 */
121 nameserverTypeDesc_: function(nameserversType) { 125 nameserverTypeDesc_: function(nameserversType) {
122 // TODO(stevenjb): Translate. 126 // TODO(stevenjb): Translate.
123 if (nameserversType == 'custom') 127 if (nameserversType == 'custom')
124 return 'Custom name servers'; 128 return 'Custom name servers';
125 if (nameserversType == 'google') 129 if (nameserversType == 'google')
126 return 'Google name servers'; 130 return 'Google name servers';
127 return 'Automatic name servers'; 131 return 'Automatic name servers';
128 }, 132 },
129 133
130 /** 134 /**
131 * @param {boolean} editable The editable state. 135 * @param {boolean} editable The editable state.
132 * @param {string} nameserversType The nameservers type. 136 * @param {string} nameserversType The nameservers type.
133 * @return {boolean} True if the nameservers are editable. 137 * @return {boolean} True if the nameservers are editable.
134 * @private 138 * @private
135 */ 139 */
136 canEdit_: function(editable, nameserversType) { 140 canEdit_: function(editable, nameserversType) {
137 return editable && nameserversType == 'custom'; 141 return editable && nameserversType == 'custom';
138 }, 142 },
139 143
140 /** 144 /**
141 * Event triggered when the selected type changes. Updates nameservers and 145 * Event triggered when the selected type changes. Updates nameservers and
142 * sends the change value if necessary. 146 * sends the change value if necessary.
143 * @param {Event} event The select node change event. 147 * @param {!{detail: !{selected: string}}} e
144 * @private 148 * @private
145 */ 149 */
146 onTypeChange_: function(event) { 150 onTypeChange_: function(e) {
147 if (this.nameserversType == 'custom') 151 if (this.nameserversType == 'custom')
148 this.savedNameservers_ = this.nameservers; 152 this.savedNameservers_ = this.nameservers;
149 var type = this.nameserverTypeNames_[event.target.selectedIndex]; 153 var type = e.detail.selected;
150 this.nameserversType = type; 154 this.nameserversType = type;
151 if (type == 'custom') { 155 if (type == 'custom') {
156 // Restore the saved nameservers.
157 this.setNameservers_(type, this.savedNameservers_);
158 // Only send custom nameservers if they are not empty.
152 if (this.savedNameservers_.length == 0) 159 if (this.savedNameservers_.length == 0)
153 return; // Don't change nameservers until onValueChange_(). 160 return;
154 // Restore the saved nameservers and send them.
155 this.nameservers = this.savedNameservers_;
156 } 161 }
157 this.sendNameServers_(); 162 this.sendNameServers_();
158 }, 163 },
159 164
160 /** 165 /**
161 * Event triggered when a nameserver value changes. 166 * Event triggered when a nameserver value changes.
162 * @private 167 * @private
163 */ 168 */
164 onValueChange_: function() { 169 onValueChange_: function() {
165 if (this.nameserversType != 'custom') { 170 if (this.nameserversType != 'custom') {
166 // If a user inputs Google nameservers in the custom nameservers fields, 171 // If a user inputs Google nameservers in the custom nameservers fields,
167 // |nameserversType| will change to 'google' so don't send the values. 172 // |nameserversType| will change to 'google' so don't send the values.
168 return; 173 return;
169 } 174 }
170 this.sendNameServers_(); 175 this.sendNameServers_();
171 }, 176 },
172 177
173 /** 178 /**
174 * Sends the current nameservers type (for automatic) or value. 179 * Sends the current nameservers type (for automatic) or value.
175 * @private 180 * @private
176 */ 181 */
177 sendNameServers_: function() { 182 sendNameServers_: function() {
178 var type = this.nameserversType; 183 var type = this.nameserversType;
179 184
180 var nameservers;
181 if (type == 'custom') { 185 if (type == 'custom') {
182 nameservers = []; 186 let nameservers = [];
183 for (let i = 0; i < 4; ++i) { 187 for (let i = 0; i < this.MAX_NAMESERVERS; ++i) {
184 let id = 'nameserver' + i; 188 let id = 'nameserver' + i;
185 let nameserver = this.$$('#' + id).value; 189 let nameserverInput = this.$$('#' + id);
186 if (nameserver) 190 let nameserver = '';
187 nameservers.push(nameserver); 191 if (nameserverInput)
192 nameserver = this.$$('#' + id).value;
193 nameservers.push(nameserver);
188 } 194 }
189 this.fire('nameservers-change', { 195 this.fire('nameservers-change', {
190 field: 'NameServers', 196 field: 'NameServers',
191 value: nameservers 197 value: nameservers,
192 }); 198 });
193 } else if (type == 'google') { 199 } else if (type == 'google') {
194 nameservers = this.GoogleNameservers; 200 let nameservers = this.GOOGLE_NAMESERVERS;
195 this.fire('nameservers-change', { 201 this.fire('nameservers-change', {
196 field: 'NameServers', 202 field: 'NameServers',
197 value: nameservers 203 value: nameservers,
198 }); 204 });
199 } else { 205 } else {
200 // automatic 206 // automatic
201 this.fire('nameservers-change', { 207 this.fire('nameservers-change', {
202 field: 'NameServersConfigType', 208 field: 'NameServersConfigType',
203 value: CrOnc.IPConfigType.DHCP 209 value: CrOnc.IPConfigType.DHCP,
204 }); 210 });
205 } 211 }
206 }, 212 },
207 }); 213 });
208 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698