1// Copyright (c) 2011 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('options', function() {
6  var OptionsPage = options.OptionsPage;
7
8  /////////////////////////////////////////////////////////////////////////////
9  // InternetOptions class:
10
11  /**
12   * Encapsulated handling of ChromeOS internet options page.
13   * @constructor
14   */
15  function InternetOptions() {
16    OptionsPage.call(this, 'internet', templateData.internetPageTabTitle,
17                     'internetPage');
18  }
19
20  cr.addSingletonGetter(InternetOptions);
21
22  // Inherit InternetOptions from OptionsPage.
23  InternetOptions.prototype = {
24    __proto__: OptionsPage.prototype,
25
26    /**
27     * Initializes InternetOptions page.
28     * Calls base class implementation to starts preference initialization.
29     */
30    initializePage: function() {
31      OptionsPage.prototype.initializePage.call(this);
32
33      if (templateData.accessLocked) {
34        var page = $('internetPage');
35        page.setAttribute('accesslocked', true);
36      }
37
38      options.internet.NetworkElement.decorate($('wired-list'));
39      $('wired-list').load(templateData.wiredList);
40      options.internet.NetworkElement.decorate($('wireless-list'));
41      $('wireless-list').load(templateData.wirelessList);
42      options.internet.NetworkElement.decorate($('remembered-list'));
43      $('remembered-list').load(templateData.rememberedList);
44
45      options.internet.CellularPlanElement.decorate($('planList'));
46
47      $('wired-section').hidden = (templateData.wiredList.length == 0);
48      $('wireless-section').hidden = (templateData.wirelessList.length == 0);
49      $('remembered-section').hidden =
50          (templateData.rememberedList.length == 0);
51      InternetOptions.setupAttributes(templateData);
52      $('detailsInternetDismiss').addEventListener('click', function(event) {
53        InternetOptions.setDetails();
54      });
55      $('detailsInternetLogin').addEventListener('click', function(event) {
56        InternetOptions.loginFromDetails();
57      });
58      $('activateDetails').addEventListener('click', function(event) {
59        InternetOptions.activateFromDetails();
60      });
61      $('enable-wifi').addEventListener('click', function(event) {
62        event.target.disabled = true;
63        chrome.send('enableWifi', []);
64      });
65      $('disable-wifi').addEventListener('click', function(event) {
66        event.target.disabled = true;
67        chrome.send('disableWifi', []);
68      });
69      $('enable-cellular').addEventListener('click', function(event) {
70        event.target.disabled = true;
71        chrome.send('enableCellular', []);
72      });
73      $('disable-cellular').addEventListener('click', function(event) {
74        event.target.disabled = true;
75        chrome.send('disableCellular', []);
76      });
77      $('buyplanDetails').addEventListener('click', function(event) {
78        chrome.send('buyDataPlan', []);
79        OptionsPage.closeOverlay();
80      });
81      $('cellularApnClear').addEventListener('click', function(event) {
82        $('cellularApn').value = "";
83        $('cellularApnUsername').value = "";
84        $('cellularApnPassword').value = "";
85        var data = $('inetAddress').data;
86        chrome.send('setApn', [String(data.servicePath),
87                               String($('cellularApn').value),
88                               String($('cellularApnUsername').value),
89                               String($('cellularApnPassword').value)]);
90      });
91      $('cellularApnSet').addEventListener('click', function(event) {
92        var data = $('inetAddress').data;
93        chrome.send('setApn', [String(data.servicePath),
94                               String($('cellularApn').value),
95                               String($('cellularApnUsername').value),
96                               String($('cellularApnPassword').value)]);
97      });
98      $('sim-card-lock-enabled').addEventListener('click', function(event) {
99        var newValue = $('sim-card-lock-enabled').checked;
100        // Leave value as is because user needs to enter PIN code first.
101        // When PIN will be entered and value changed,
102        // we'll update UI to reflect that change.
103        $('sim-card-lock-enabled').checked = !newValue;
104        InternetOptions.enableSecurityTab(false);
105        chrome.send('setSimCardLock', [newValue]);
106      });
107      $('change-pin').addEventListener('click', function(event) {
108        chrome.send('changePin');
109      });
110      this.showNetworkDetails_();
111    },
112
113    showNetworkDetails_: function() {
114      var params = parseQueryParams(window.location);
115      var servicePath = params.servicePath;
116      var networkType = params.networkType;
117      if (!servicePath || !servicePath.length ||
118          !networkType || !networkType.length)
119        return;
120      chrome.send('buttonClickCallback',
121          [networkType, servicePath, "options"]);
122    }
123  };
124
125  // A boolean flag from InternerOptionsHandler to indicate whether to use
126  // inline WebUI for ethernet/wifi login/options.
127  InternetOptions.useSettingsUI = false;
128
129  // Network status update will be blocked while typing in WEP password etc.
130  InternetOptions.updateLocked = false;
131  InternetOptions.updatePending = false;
132  InternetOptions.updataData = null;
133
134  InternetOptions.loginFromDetails = function () {
135    var data = $('inetAddress').data;
136    var servicePath = data.servicePath;
137    if (data.type == options.internet.Constants.TYPE_WIFI) {
138      if (data.certInPkcs) {
139        chrome.send('loginToCertNetwork',[String(servicePath),
140                                          String(data.certPath),
141                                          String(data.ident)]);
142      } else {
143        chrome.send('loginToCertNetwork',[String(servicePath),
144                                          String($('inetCert').value),
145                                          String($('inetIdent').value),
146                                          String($('inetCertPass').value)]);
147      }
148    } else if (data.type == options.internet.Constants.TYPE_CELLULAR) {
149        chrome.send('buttonClickCallback', [String(data.type),
150                                            servicePath,
151                                            'connect']);
152    }
153    OptionsPage.closeOverlay();
154  };
155
156  InternetOptions.activateFromDetails = function () {
157    var data = $('inetAddress').data;
158    var servicePath = data.servicePath;
159    if (data.type == options.internet.Constants.TYPE_CELLULAR) {
160      chrome.send('buttonClickCallback', [String(data.type),
161                                          String(servicePath),
162                                          'activate']);
163    }
164    OptionsPage.closeOverlay();
165  };
166
167  InternetOptions.setDetails = function () {
168    var data = $('inetAddress').data;
169    var servicePath = data.servicePath;
170    if (data.type == options.internet.Constants.TYPE_WIFI) {
171      chrome.send('setDetails',[String(servicePath),
172                                $('autoConnectNetwork').checked ?
173                                  "true" : "false"]);
174    }
175    OptionsPage.closeOverlay();
176  };
177
178  InternetOptions.enableSecurityTab = function(enabled) {
179    $('sim-card-lock-enabled').disabled = !enabled;
180    $('change-pin').disabled = !enabled;
181  };
182
183  InternetOptions.setupAttributes = function(data) {
184    var buttons = $('wireless-buttons');
185    if (data.wifiEnabled) {
186      $('disable-wifi').disabled = false;
187      $('disable-wifi').hidden = false;
188      $('enable-wifi').hidden = true;
189    } else {
190      $('enable-wifi').disabled = false;
191      $('enable-wifi').hidden = false;
192      $('disable-wifi').hidden = true;
193    }
194    if (data.cellularAvailable) {
195      if (data.cellularEnabled) {
196        $('disable-cellular').disabled = false;
197        $('disable-cellular').hidden = false;
198        $('enable-cellular').hidden = true;
199      } else {
200        $('enable-cellular').disabled = false;
201        $('enable-cellular').hidden = false;
202        $('disable-cellular').hidden = true;
203      }
204      if (!AccountsOptions.currentUserIsOwner())
205        $('internet-owner-only-warning').hidden = false;
206    } else {
207      $('enable-cellular').hidden = true;
208      $('disable-cellular').hidden = true;
209      $('data-roaming').hidden = true;
210    }
211
212    InternetOptions.useSettingsUI = data.networkUseSettingsUI;
213  };
214
215  // Prevent clobbering of password input field.
216  InternetOptions.lockUpdates = function () {
217    InternetOptions.updateLocked = true;
218  };
219
220  InternetOptions.unlockUpdates = function () {
221    InternetOptions.updateLocked = false;
222    if (InternetOptions.updatePending) {
223      InternetOptions.refreshNetworkData(InternetOptions.updateData);
224    }
225  };
226
227  //
228  //Chrome callbacks
229  //
230  InternetOptions.refreshNetworkData = function (data) {
231    var page = $('internetPage');
232    if (data.accessLocked) {
233      page.setAttribute('accesslocked', true);
234      return;
235    }
236    page.removeAttribute('accesslocked');
237    if (InternetOptions.updateLocked) {
238      InternetOptions.updateData = data;
239      InternetOptions.updatePending = true;
240    } else {
241      $('wired-list').load(data.wiredList);
242      $('wireless-list').load(data.wirelessList);
243      $('remembered-list').load(data.rememberedList);
244
245      $('wired-section').hidden = (data.wiredList.length == 0);
246      $('wireless-section').hidden = (data.wirelessList.length == 0);
247      InternetOptions.setupAttributes(data);
248      $('remembered-section').hidden = (data.rememberedList.length == 0);
249      InternetOptions.updateData = null;
250      InternetOptions.updatePending = false;
251    }
252  };
253
254  InternetOptions.updateCellularPlans = function (data) {
255    var page = $('detailsInternetPage');
256    page.removeAttribute('cellplanloading');
257    if (data.plans && data.plans.length) {
258      page.removeAttribute('nocellplan');
259      page.setAttribute('hascellplan', true);
260      $('planList').load(data.plans);
261    } else {
262      page.setAttribute('nocellplan', true);
263      page.removeAttribute('hascellplan');
264    }
265
266    if (!data.needsPlan)
267      page.setAttribute('hasactiveplan', true);
268    else
269      page.removeAttribute('hasactiveplan');
270
271    if (data.activated) {
272      page.setAttribute('activated', true);
273    } else {
274      page.removeAttribute('activated');
275      $('detailsInternetLogin').classList.add('hidden');
276    }
277
278    // CSS selectors don't like me anymore, switching to classList
279    if (data.showBuyButton)
280      $('buyplanDetails').classList.remove('hidden');
281    else
282      $('buyplanDetails').classList.add('hidden');
283
284    if (data.showActivateButton)
285      $('activateDetails').classList.remove('hidden');
286    else
287      $('activateDetails').classList.add('hidden');
288
289    // Nudge webkit so that it redraws the details overlay page.
290    // See http://crosbug.com/9616 for details.
291    // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=50176
292    var dummy = page.ownerDocument.createTextNode(' ');
293    page.appendChild(dummy);
294    page.removeChild(dummy);
295  };
296
297  InternetOptions.updateSecurityTab = function(data) {
298    InternetOptions.enableSecurityTab(true);
299    $('sim-card-lock-enabled').checked = data.requirePin;
300  };
301
302  InternetOptions.showPasswordEntry = function (data) {
303    var element = $(data.servicePath);
304    element.showPassword();
305  };
306
307  InternetOptions.showDetailedInfo = function (data) {
308    var page = $('detailsInternetPage');
309    $('buyplanDetails').classList.add('hidden');
310    $('activateDetails').classList.add('hidden');
311    $('detailsInternetLogin').classList.add('hidden');
312    if (data.connecting) {
313      page.setAttribute('connecting', data.connecting);
314    } else {
315      page.removeAttribute('connecting');
316    }
317    if (data.connected) {
318      page.setAttribute('connected', data.connected);
319      $('inetTitle').textContent = localStrings.getString('inetStatus');
320    } else {
321      page.removeAttribute('connected');
322      $('inetTitle').textContent = localStrings.getString('inetConnect');
323      $('detailsInternetLogin').classList.remove('hidden');
324    }
325    $('connectionState').textContent = data.connectionState;
326    var address = $('inetAddress');
327    address.data = data;
328    if (data.ipconfigs && data.ipconfigs.length) {
329      // We will be displaying only the first ipconfig info for now until we
330      // start supporting multiple IP addresses per connection.
331      address.textContent = data.ipconfigs[0].address;
332      $('inetSubnetAddress').textContent = data.ipconfigs[0].subnetAddress;
333      $('inetGateway').textContent = data.ipconfigs[0].gateway;
334      $('inetDns').textContent = data.ipconfigs[0].dns;
335    } else {
336      // This is most likely a transient state due to device still connecting.
337      address.textContent = '?';
338      $('inetSubnetAddress').textContent = '?';
339      $('inetGateway').textContent = '?';
340      $('inetDns').textContent = '?';
341    }
342    if (data.hardwareAddress) {
343      $('hardwareAddress').textContent = data.hardwareAddress;
344      $('hardwareAddressRow').style.display = 'table-row';
345    } else {
346      // This is most likely a device without a hardware address.
347      $('hardwareAddressRow').style.display = 'none';
348    }
349    if (data.type == 2) {
350      OptionsPage.showTab($('wifiNetworkNavTab'));
351      page.setAttribute('wireless', true);
352      page.removeAttribute('ethernet');
353      page.removeAttribute('cellular');
354      page.removeAttribute('gsm');
355      $('inetSsid').textContent = data.ssid;
356      $('autoConnectNetwork').checked = data.autoConnect;
357      if (!AccountsOptions.currentUserIsOwner()) {
358        // Disable this for guest non-Owners.
359        $('autoConnectNetwork').disabled = true;
360      }
361      page.removeAttribute('password');
362      if (data.encrypted) {
363        page.setAttribute('password', true);
364      }
365    } else if(data.type == 5) {
366      if (!data.gsm)
367        OptionsPage.showTab($('cellularPlanNavTab'));
368      else
369        OptionsPage.showTab($('cellularConnNavTab'));
370      page.removeAttribute('ethernet');
371      page.removeAttribute('wireless');
372      page.setAttribute('cellular', true);
373      if (data.carrierUrl) {
374        var a = $('carrierUrl');
375        if (!a) {
376          a = document.createElement('a');
377          $('serviceName').appendChild(a);
378          a.id = 'carrierUrl';
379          a.target = "_blank";
380        }
381        a.href = data.carrierUrl;
382        a.textContent = data.serviceName;
383      } else {
384        $('serviceName').textContent = data.serviceName;
385      }
386      $('networkTechnology').textContent = data.networkTechnology;
387      $('activationState').textContent = data.activationState;
388      $('roamingState').textContent = data.roamingState;
389      $('restrictedPool').textContent = data.restrictedPool;
390      $('errorState').textContent = data.errorState;
391      $('manufacturer').textContent = data.manufacturer;
392      $('modelId').textContent = data.modelId;
393      $('firmwareRevision').textContent = data.firmwareRevision;
394      $('hardwareRevision').textContent = data.hardwareRevision;
395      $('prlVersion').textContent = data.prlVersion;
396      $('meid').textContent = data.meid;
397      $('imei').textContent = data.imei;
398      $('mdn').textContent = data.mdn;
399      $('esn').textContent = data.esn;
400      $('min').textContent = data.min;
401      if (!data.gsm) {
402        page.removeAttribute('gsm');
403      } else {
404        $('operatorName').textContent = data.operatorName;
405        $('operatorCode').textContent = data.operatorCode;
406        $('imsi').textContent = data.imsi;
407        // If there's no custom APN show default APN that is used. These will be
408        // displayed differently when http://crosbug.com/14290 is fixed.
409        // See also http://crosbug.com/p/4058 for context.
410        if (data.apn != '') {
411          $('cellularApn').value = data.apn;
412          $('cellularApnUsername').value = data.apn_username;
413          $('cellularApnPassword').value = data.apn_password;
414        } else {
415          $('cellularApn').value = data.last_good_apn;
416          $('cellularApnUsername').value = data.last_good_apn_username;
417          $('cellularApnPassword').value = data.last_good_apn_password;
418        }
419        $('sim-card-lock-enabled').checked = data.simCardLockEnabled;
420        InternetOptions.enableSecurityTab(true);
421        page.setAttribute('gsm', true);
422      }
423
424      // CSS selectors don't like me anymore, switching to classList
425      if (data.showBuyButton)
426        $('buyplanDetails').classList.remove('hidden');
427      else
428        $('buyplanDetails').classList.add('hidden');
429
430      if (data.showActivateButton) {
431        $('activateDetails').classList.remove('hidden')
432        $('detailsInternetLogin').classList.add('hidden');
433      } else {
434        $('activateDetails').classList.add('hidden');
435      }
436
437      page.removeAttribute('hascellplan');
438      if (data.connected) {
439        page.removeAttribute('nocellplan');
440        page.setAttribute('cellplanloading', true);
441        chrome.send('refreshCellularPlan', [data.servicePath])
442      } else {
443        page.setAttribute('nocellplan', true);
444        page.removeAttribute('cellplanloading');
445      }
446    } else {
447      OptionsPage.showTab($('internetNavTab'));
448      page.setAttribute('ethernet', true);
449      page.removeAttribute('wireless');
450      page.removeAttribute('cellular');
451      page.removeAttribute('gsm');
452    }
453    OptionsPage.navigateToPage('detailsInternetPage');
454  };
455
456  // Export
457  return {
458    InternetOptions: InternetOptions
459  };
460});
461