Home | History | Annotate | Download | only in net_internals
      1 // Copyright (c) 2012 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 
      5 /**
      6  * This view displays information on ChromeOS specific features.
      7  */
      8 var CrosView = (function() {
      9   'use strict';
     10 
     11   var fileContent;
     12   var passcode = '';
     13 
     14   /**
     15    *  Clear file input div
     16    *
     17    *  @private
     18    */
     19   function clearFileInput_() {
     20     $(CrosView.IMPORT_DIV_ID).innerHTML = $(CrosView.IMPORT_DIV_ID).innerHTML;
     21     $(CrosView.IMPORT_ONC_ID).addEventListener('change',
     22                                                handleFileChangeEvent_,
     23                                                false);
     24   }
     25 
     26   /**
     27    *  Send file contents and passcode to C++ cros network library.
     28    *
     29    *  @private
     30    */
     31   function importONCFile_() {
     32     clearParseStatus_();
     33     if (fileContent)
     34       g_browser.importONCFile(fileContent, passcode);
     35     else
     36       setParseStatus_('ONC file parse failed: cannot read file');
     37     clearFileInput_();
     38   }
     39 
     40   /**
     41    *  Set the passcode var, and trigger onc import.
     42    *
     43    *  @param {string} value The passcode value.
     44    *  @private
     45    */
     46   function setPasscode_(value) {
     47     passcode = value;
     48     if (passcode)
     49       importONCFile_();
     50   }
     51 
     52   /**
     53    *  Unhide the passcode prompt input field and give it focus.
     54    *
     55    *  @private
     56    */
     57   function promptForPasscode_() {
     58     $(CrosView.PASSCODE_ID).hidden = false;
     59     $(CrosView.PASSCODE_INPUT_ID).focus();
     60     $(CrosView.PASSCODE_INPUT_ID).select();
     61   }
     62 
     63   /**
     64    *  Set the fileContent var, and trigger onc import if the file appears to
     65    *  not be encrypted, or prompt for passcode if the file is encrypted.
     66    *
     67    *  @private
     68    *  @param {string} text contents of selected file.
     69    */
     70   function setFileContent_(result) {
     71     fileContent = result;
     72     // Parse the JSON to get at the top level "Type" property.
     73     var jsonObject;
     74     // Ignore any parse errors: they'll get handled in the C++ import code.
     75     try {
     76       jsonObject = JSON.parse(fileContent);
     77     } catch (error) {}
     78     // Check if file is encrypted.
     79     if (jsonObject &&
     80         jsonObject.hasOwnProperty('Type') &&
     81         jsonObject.Type == 'EncryptedConfiguration') {
     82       promptForPasscode_();
     83     } else {
     84       importONCFile_();
     85     }
     86   }
     87 
     88   /**
     89    *  Clear ONC file parse status.  Clears and hides the parse status div.
     90    *
     91    *  @private
     92    */
     93   function clearParseStatus_(error) {
     94     var parseStatus = $(CrosView.PARSE_STATUS_ID);
     95     parseStatus.hidden = true;
     96     parseStatus.textContent = '';
     97   }
     98 
     99   /**
    100    *  Set ONC file parse status.
    101    *
    102    *  @private
    103    */
    104   function setParseStatus_(error) {
    105     var parseStatus = $(CrosView.PARSE_STATUS_ID);
    106     parseStatus.hidden = false;
    107     parseStatus.textContent = error ?
    108         'ONC file parse failed: ' + error : 'ONC file successfully parsed';
    109     reset_();
    110   }
    111 
    112   /**
    113    *  Set storing debug logs status.
    114    *
    115    *  @private
    116    */
    117   function setStoreDebugLogsStatus_(status) {
    118     $(CrosView.STORE_DEBUG_LOGS_STATUS_ID).innerText = status;
    119   }
    120 
    121   /**
    122    *  Set status for current debug mode.
    123    *
    124    *  @private
    125    */
    126   function setNetworkDebugModeStatus_(status) {
    127     $(CrosView.DEBUG_STATUS_ID).innerText = status;
    128   }
    129 
    130   /**
    131    *  An event listener for the file selection field.
    132    *
    133    *  @private
    134    */
    135   function handleFileChangeEvent_(event) {
    136     clearParseStatus_();
    137     var file = event.target.files[0];
    138     var reader = new FileReader();
    139     reader.onloadend = function(e) {
    140       setFileContent_(reader.result);
    141     };
    142     reader.readAsText(file);
    143   }
    144 
    145   /**
    146    *  Add event listeners for the file selection, passcode input
    147    *  fields, for the button for debug logs storing and for buttons
    148    *  for debug mode selection.
    149    *
    150    *  @private
    151    */
    152   function addEventListeners_() {
    153     $(CrosView.IMPORT_ONC_ID).addEventListener('change',
    154                                                handleFileChangeEvent_,
    155                                                false);
    156 
    157     $(CrosView.PASSCODE_INPUT_ID).addEventListener('change', function(event) {
    158       setPasscode_(this.value);
    159     }, false);
    160 
    161     $(CrosView.STORE_DEBUG_LOGS_ID).addEventListener('click', function(event) {
    162       $(CrosView.STORE_DEBUG_LOGS_STATUS_ID).innerText = '';
    163       g_browser.storeDebugLogs();
    164     }, false);
    165 
    166     $(CrosView.DEBUG_WIFI_ID).addEventListener('click', function(event) {
    167         setNetworkDebugMode_('wifi');
    168     }, false);
    169     $(CrosView.DEBUG_ETHERNET_ID).addEventListener('click', function(event) {
    170         setNetworkDebugMode_('ethernet');
    171     }, false);
    172     $(CrosView.DEBUG_CELLULAR_ID).addEventListener('click', function(event) {
    173         setNetworkDebugMode_('cellular');
    174     }, false);
    175     $(CrosView.DEBUG_WIMAX_ID).addEventListener('click', function(event) {
    176         setNetworkDebugMode_('wimax');
    177     }, false);
    178     $(CrosView.DEBUG_NONE_ID).addEventListener('click', function(event) {
    179         setNetworkDebugMode_('none');
    180     }, false);
    181   }
    182 
    183   /**
    184    *  Reset fileContent and passcode vars.
    185    *
    186    *  @private
    187    */
    188   function reset_() {
    189     fileContent = undefined;
    190     passcode = '';
    191     $(CrosView.PASSCODE_ID).hidden = true;
    192   }
    193 
    194   /**
    195    *  Enables or disables debug mode for a specified subsystem.
    196    *
    197    *  @private
    198    */
    199   function setNetworkDebugMode_(subsystem) {
    200     $(CrosView.DEBUG_STATUS_ID).innerText = '';
    201     g_browser.setNetworkDebugMode(subsystem);
    202   }
    203 
    204   /**
    205    *  @constructor
    206    *  @extends {DivView}
    207    */
    208   function CrosView() {
    209     assertFirstConstructorCall(CrosView);
    210 
    211     // Call superclass's constructor.
    212     DivView.call(this, CrosView.MAIN_BOX_ID);
    213 
    214     g_browser.addCrosONCFileParseObserver(this);
    215     g_browser.addStoreDebugLogsObserver(this);
    216     g_browser.addSetNetworkDebugModeObserver(this);
    217     addEventListeners_();
    218   }
    219 
    220   CrosView.TAB_ID = 'tab-handle-chromeos';
    221   CrosView.TAB_NAME = 'ChromeOS';
    222   CrosView.TAB_HASH = '#chromeos';
    223 
    224   CrosView.MAIN_BOX_ID = 'chromeos-view-tab-content';
    225   CrosView.IMPORT_DIV_ID = 'chromeos-view-import-div';
    226   CrosView.IMPORT_ONC_ID = 'chromeos-view-import-onc';
    227   CrosView.PASSCODE_ID = 'chromeos-view-password-div';
    228   CrosView.PASSCODE_INPUT_ID = 'chromeos-view-onc-password';
    229   CrosView.PARSE_STATUS_ID = 'chromeos-view-parse-status';
    230   CrosView.STORE_DEBUG_LOGS_ID = 'chromeos-view-store-debug-logs';
    231   CrosView.STORE_DEBUG_LOGS_STATUS_ID = 'chromeos-view-store-debug-logs-status';
    232   CrosView.DEBUG_WIFI_ID = 'chromeos-view-network-debugging-wifi';
    233   CrosView.DEBUG_ETHERNET_ID = 'chromeos-view-network-debugging-ethernet';
    234   CrosView.DEBUG_CELLULAR_ID = 'chromeos-view-network-debugging-cellular';
    235   CrosView.DEBUG_WIMAX_ID = 'chromeos-view-network-debugging-wimax';
    236   CrosView.DEBUG_NONE_ID = 'chromeos-view-network-debugging-none';
    237   CrosView.DEBUG_STATUS_ID = 'chromeos-view-network-debugging-status';
    238 
    239   cr.addSingletonGetter(CrosView);
    240 
    241   CrosView.prototype = {
    242     // Inherit from DivView.
    243     __proto__: DivView.prototype,
    244 
    245     onONCFileParse: setParseStatus_,
    246     onStoreDebugLogs: setStoreDebugLogsStatus_,
    247     onSetNetworkDebugMode: setNetworkDebugModeStatus_,
    248   };
    249 
    250   return CrosView;
    251 })();
    252