OLD | NEW |
(Empty) | |
| 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 |
| 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 * Send file contents and passcode to C++ cros network library. |
| 16 * |
| 17 * @private |
| 18 */ |
| 19 function importONCFile_() { |
| 20 if (fileContent) |
| 21 g_browser.importONCFile(fileContent, passcode); |
| 22 else |
| 23 setParseStatus_(false); |
| 24 } |
| 25 |
| 26 /** |
| 27 * Set the passcode var, and trigger onc import. |
| 28 * |
| 29 * @private |
| 30 * @param {string} passcode |
| 31 */ |
| 32 function setPasscode_(value) { |
| 33 passcode = value; |
| 34 if (passcode) |
| 35 importONCFile_(); |
| 36 } |
| 37 |
| 38 /** |
| 39 * Unhide the passcode prompt input field and give it focus. |
| 40 * |
| 41 * @private |
| 42 */ |
| 43 function promptForPasscode_() { |
| 44 $(CrosView.PASSCODE_ID).hidden = false; |
| 45 $(CrosView.PASSCODE_INPUT_ID).focus(); |
| 46 $(CrosView.PASSCODE_INPUT_ID).select(); |
| 47 } |
| 48 |
| 49 /** |
| 50 * Set the fileContent var, and trigger onc import if the file appears to |
| 51 * not be encrypted, or prompt for passcode if the file is encrypted. |
| 52 * |
| 53 * @private |
| 54 * @param {string} text contents of selected file. |
| 55 */ |
| 56 function setFileContent_(result) { |
| 57 fileContent = result; |
| 58 // Check if file is encrypted. |
| 59 if (fileContent.search(/begin pgp message/i) == -1) { |
| 60 // Not encrypted, don't need passcode. |
| 61 importONCFile_(); |
| 62 } else { |
| 63 promptForPasscode_(); |
| 64 } |
| 65 } |
| 66 |
| 67 /** |
| 68 * Set ONC file parse status. |
| 69 * |
| 70 * @private |
| 71 */ |
| 72 function setParseStatus_(success) { |
| 73 var parseStatus = $(CrosView.PARSE_STATUS_ID); |
| 74 parseStatus.hidden = false; |
| 75 parseStatus.textContent = status ? |
| 76 "ONC file successfully parsed" : "ONC file parse failed"; |
| 77 reset_(); |
| 78 } |
| 79 |
| 80 /** |
| 81 * Add event listeners for the file selection and passcode input fields. |
| 82 * |
| 83 * @private |
| 84 */ |
| 85 function addEventListeners_() { |
| 86 $(CrosView.IMPORT_ONC_ID).addEventListener('change', function(event) { |
| 87 $(CrosView.PARSE_STATUS_ID).hidden = true; |
| 88 var file = event.target.files[0]; |
| 89 var reader = new FileReader(); |
| 90 reader.onloadend = function(e) { |
| 91 setFileContent_(this.result); |
| 92 }; |
| 93 reader.readAsText(file); |
| 94 }, false); |
| 95 |
| 96 $(CrosView.PASSCODE_INPUT_ID).addEventListener('change', function(event) { |
| 97 setPasscode_(this.value); |
| 98 }, false); |
| 99 } |
| 100 |
| 101 /** |
| 102 * Reset fileContent and passcode vars. |
| 103 * |
| 104 * @private |
| 105 */ |
| 106 function reset_() { |
| 107 fileContent = undefined; |
| 108 passcode = ''; |
| 109 $(CrosView.PASSCODE_ID).hidden = true; |
| 110 } |
| 111 |
| 112 /** |
| 113 * @constructor |
| 114 * @extends {DivView} |
| 115 */ |
| 116 function CrosView() { |
| 117 assertFirstConstructorCall(CrosView); |
| 118 |
| 119 // Call superclass's constructor. |
| 120 DivView.call(this, CrosView.MAIN_BOX_ID); |
| 121 |
| 122 g_browser.addCrosONCFileParseObserver(this); |
| 123 addEventListeners_(); |
| 124 } |
| 125 |
| 126 // ID for special HTML element in category_tabs.html |
| 127 CrosView.TAB_HANDLE_ID = 'tab-handle-chromeos'; |
| 128 |
| 129 CrosView.MAIN_BOX_ID = 'chromeos-view-tab-content'; |
| 130 CrosView.IMPORT_ONC_ID = 'chromeos-view-import-onc'; |
| 131 CrosView.PASSCODE_ID = 'chromeos-view-password-div'; |
| 132 CrosView.PASSCODE_INPUT_ID = 'chromeos-view-onc-password'; |
| 133 CrosView.PARSE_STATUS_ID = 'chromeos-view-parse-status'; |
| 134 |
| 135 cr.addSingletonGetter(CrosView); |
| 136 |
| 137 CrosView.prototype = { |
| 138 // Inherit from DivView. |
| 139 __proto__: DivView.prototype, |
| 140 |
| 141 onONCFileParse: setParseStatus_, |
| 142 }; |
| 143 |
| 144 return CrosView; |
| 145 })(); |
OLD | NEW |