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 // Variable to track if a captcha challenge was issued. If this gets set to 6 // true, it stays that way until we are told about successful login from 7 // the browser. This means subsequent errors (like invalid password) are 8 // rendered in the captcha state, which is basically identical except we 9 // don't show the top error blurb "Error Signing in" or the "Create 10 // account" link. 11 var g_is_captcha_challenge_active = false; 12 13 // Taken from new_new_tab.js. 14 // TODO(tim): Can this be unified? 15 function url(s) { 16 // http://www.w3.org/TR/css3-values/#uris 17 // Parentheses, commas, whitespace characters, single quotes (') and 18 // double quotes (") appearing in a URI must be escaped with a backslash 19 var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1'); 20 // WebKit has a bug when it comes to URLs that end with \ 21 // https://bugs.webkit.org/show_bug.cgi?id=28885 22 if (/\\\\$/.test(s2)) { 23 // Add a space to work around the WebKit bug. 24 s2 += ' '; 25 } 26 return 'url("' + s2 + '")'; 27 } 28 29 function gaia_setFocus() { 30 var form = document.getElementById("gaia-login-form"); 31 if (form.email && (form.email.value == null || form.email.value == "")) { 32 form.email.focus(); 33 } else if (form.passwd) { 34 form.passwd.focus(); 35 } 36 } 37 38 function showGaiaLogin(args) { 39 document.getElementById('logging-in-throbber').style.display = "none"; 40 41 document.getElementById('email').disabled = false; 42 document.getElementById('passwd').disabled = false; 43 44 var f = document.getElementById("gaia-login-form"); 45 if (f) { 46 if (args.user != undefined) { 47 if (f.email.value != args.user) 48 f.passwd.value = ""; // Reset the password field 49 f.email.value = args.user; 50 } 51 52 if (!args.editable_user) { 53 f.email.style.display = 'none'; 54 var span = document.getElementById('email-readonly'); 55 span.appendChild(document.createTextNode(f.email.value)); 56 span.style.display = 'inline'; 57 setElementDisplay("create-account-div", "none"); 58 } 59 60 f.accessCode.disabled = true; 61 } 62 63 if (1 == args.error) { 64 var access_code = document.getElementById('access-code'); 65 if (access_code.value && access_code.value != "") { 66 setElementDisplay("errormsg-0-access-code", 'block'); 67 showAccessCodeRequired(); 68 } else { 69 setElementDisplay("errormsg-1-password", 'table-row'); 70 } 71 setBlurbError(args.error_message); 72 } else if (3 == args.error) { 73 setElementDisplay("errormsg-0-connection", 'table-row'); 74 setBlurbError(args.error_message); 75 } else if (4 == args.error) { 76 showCaptcha(args); 77 } else if (8 == args.error) { 78 showAccessCodeRequired(); 79 } else if (args.error_message) { 80 setBlurbError(args.error_message); 81 } 82 83 document.getElementById("sign-in").disabled = false; 84 document.getElementById("sign-in").value = templateData['signin']; 85 gaia_setFocus(); 86 } 87 88 function showCaptcha(args) { 89 g_is_captcha_challenge_active = true; 90 91 // The captcha takes up lots of space, so make room. 92 setElementDisplay("top-blurb", "none"); 93 setElementDisplay("top-blurb-error", "none"); 94 setElementDisplay("create-account-div", "none"); 95 document.getElementById('create-account-cell').height = 0; 96 97 // It's showtime for the captcha now. 98 setElementDisplay("captcha-div", "block"); 99 document.getElementById('email').disabled = true; 100 document.getElementById('passwd').disabled = false; 101 document.getElementById('captcha-value').disabled = false; 102 document.getElementById('captcha-wrapper').style.backgroundImage = 103 url(args.captchaUrl); 104 } 105 106 function showAccessCodeRequired() { 107 setElementDisplay("password-row", "none"); 108 setElementDisplay("email-row", "none"); 109 document.getElementById("create-account-cell").style.visibility = 110 "hidden"; 111 112 setElementDisplay("access-code-label-row", "table-row"); 113 setElementDisplay("access-code-input-row", "table-row"); 114 setElementDisplay("access-code-help-row", "table-row"); 115 document.getElementById('access-code').disabled = false; 116 } 117 118 function CloseDialog() { 119 chrome.send("DialogClose", [""]); 120 } 121 122 function showGaiaSuccessAndClose() { 123 document.getElementById("sign-in").value = templateData['success']; 124 setTimeout(CloseDialog, 1600); 125 } 126 127 function showGaiaSuccessAndSettingUp() { 128 document.getElementById("sign-in").value = templateData['settingup']; 129 } 130 131 /** 132 * DOMContentLoaded handler, sets up the page. 133 */ 134 function load() { 135 var acct_text = document.getElementById("gaia-account-text"); 136 var translated_text = acct_text.textContent; 137 var posGoogle = translated_text.indexOf('Google'); 138 if (posGoogle != -1) { 139 var ltr = templateData['textdirection'] == 'ltr'; 140 var googleIsAtEndOfSentence = posGoogle != 0; 141 if (googleIsAtEndOfSentence == ltr) { 142 // We're in ltr and in the translation the word 'Google' is AFTER the 143 // word 'Account' OR we're in rtl and 'Google' is BEFORE 'Account'. 144 var logo_div = document.getElementById('gaia-logo'); 145 logo_div.parentNode.appendChild(logo_div); 146 } 147 acct_text.textContent = translated_text.replace('Google',''); 148 } 149 150 var loginForm = document.getElementById("gaia-login-form"); 151 loginForm.onsubmit = function() { 152 sendCredentialsAndClose(); 153 return false; 154 }; 155 156 var gaiaCancel = document.getElementById("gaia-cancel"); 157 gaiaCancel.onclick = function() { 158 CloseDialog(); 159 }; 160 161 var args = JSON.parse(chrome.dialogArguments); 162 showGaiaLogin(args); 163 } 164 165 function sendCredentialsAndClose() { 166 if (!setErrorVisibility()) 167 return false; 168 169 document.getElementById('email').disabled = true; 170 document.getElementById('passwd').disabled = true; 171 document.getElementById('captcha-value').disabled = true; 172 document.getElementById('access-code').disabled = true; 173 174 document.getElementById('logging-in-throbber').style.display = "block"; 175 176 var f = document.getElementById("gaia-login-form"); 177 var result = JSON.stringify({"user" : f.email.value, 178 "pass" : f.passwd.value, 179 "captcha" : f.captchaValue.value, 180 "access_code" : f.accessCode.value}); 181 document.getElementById("sign-in").disabled = true; 182 chrome.send("SubmitAuth", [result]); 183 } 184 185 function setElementDisplay(id, display) { 186 var d = document.getElementById(id); 187 if (d) 188 d.style.display = display; 189 } 190 191 function hideBlurb() { 192 setElementDisplay('top-blurb', 'none'); 193 } 194 195 function setBlurbError(error_message) { 196 if (g_is_captcha_challenge_active) 197 return; // No blurb in captcha challenge mode. 198 if (error_message) { 199 document.getElementById('error-signing-in').style.display = 'none'; 200 document.getElementById('error-custom').style.display = 'inline'; 201 document.getElementById('error-custom').textContent = error_message; 202 } else { 203 document.getElementById('error-signing-in').style.display = 'inline'; 204 document.getElementById('error-custom').style.display = 'none'; 205 } 206 document.getElementById("top-blurb-error").style.visibility = "visible"; 207 document.getElementById('email').disabled = false; 208 document.getElementById('passwd').disabled = false; 209 } 210 211 function resetErrorVisibility() { 212 setElementDisplay("errormsg-0-email", 'none'); 213 setElementDisplay("errormsg-0-password", 'none'); 214 setElementDisplay("errormsg-1-password", 'none'); 215 setElementDisplay("errormsg-0-connection", 'none'); 216 setElementDisplay("errormsg-0-access-code", 'none'); 217 } 218 219 function setErrorVisibility() { 220 resetErrorVisibility(); 221 var f = document.getElementById("gaia-login-form"); 222 if (null == f.email.value || "" == f.email.value) { 223 setElementDisplay("errormsg-0-email", 'table-row'); 224 setBlurbError(); 225 return false; 226 } 227 if (null == f.passwd.value || "" == f.passwd.value) { 228 setElementDisplay("errormsg-0-password", 'table-row'); 229 setBlurbError(); 230 return false; 231 } 232 if (!f.accessCode.disabled && (null == f.accessCode.value || 233 "" == f.accessCode.value)) { 234 setElementDisplay("errormsg-0-password", 'table-row'); 235 return false; 236 } 237 return true; 238 } 239 240 function onPreCreateAccount() { 241 return true; 242 } 243 244 function onPreLogin() { 245 if (window["onlogin"] != null) { 246 return onlogin(); 247 } else { 248 return true; 249 } 250 } 251 252 document.addEventListener('DOMContentLoaded', load); 253