|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @fileoverview Utilities for Auth test app features. |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * Initializes the widget for toggling reCAPTCHA size. |
| 24 | + * @param {function(string):void} callback The callback to call when the |
| 25 | + * size toggler is changed, which takes in the new reCAPTCHA size. |
| 26 | + */ |
| 27 | +function initRecaptchaToggle(callback) { |
| 28 | + // Listen to recaptcha config togglers. |
| 29 | + var $recaptchaConfigTogglers = $('.toggleRecaptcha'); |
| 30 | + $recaptchaConfigTogglers.click(function(e) { |
| 31 | + // Remove currently active option. |
| 32 | + $recaptchaConfigTogglers.removeClass('active'); |
| 33 | + // Set currently selected option. |
| 34 | + $(this).addClass('active'); |
| 35 | + // Get the current reCAPTCHA setting label. |
| 36 | + var size = $(e.target) |
| 37 | + .text() |
| 38 | + .toLowerCase(); |
| 39 | + callback(size); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +// Install servicerWorker if supported. |
| 44 | +if ('serviceWorker' in navigator) { |
| 45 | + navigator.serviceWorker |
| 46 | + .register('/service-worker.js', { scope: '/' }) |
| 47 | + .then(function(reg) { |
| 48 | + // Registration worked. |
| 49 | + console.log('Registration succeeded. Scope is ' + reg.scope); |
| 50 | + }) |
| 51 | + .catch(function(error) { |
| 52 | + // Registration failed. |
| 53 | + console.log('Registration failed with ' + error.message); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +var webWorker = null; |
| 58 | +if (window.Worker) { |
| 59 | + webWorker = new Worker('/web-worker.js'); |
| 60 | + /** |
| 61 | + * Handles the incoming message from the web worker. |
| 62 | + * @param {!Object} e The message event received. |
| 63 | + */ |
| 64 | + webWorker.onmessage = function(e) { |
| 65 | + console.log('User data passed through web worker: ', e.data); |
| 66 | + switch (e.data.type) { |
| 67 | + case 'GET_USER_INFO': |
| 68 | + alertSuccess( |
| 69 | + 'User data passed through web worker: ' + JSON.stringify(e.data) |
| 70 | + ); |
| 71 | + break; |
| 72 | + case 'RUN_TESTS': |
| 73 | + if (e.data.status == 'success') { |
| 74 | + alertSuccess('Web worker tests ran successfully!'); |
| 75 | + } else { |
| 76 | + alertError('Error: ' + JSON.stringify(e.data.error)); |
| 77 | + } |
| 78 | + break; |
| 79 | + default: |
| 80 | + return; |
| 81 | + } |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Asks the web worker, if supported in current browser, to return the user info |
| 87 | + * corresponding to the currentUser as seen within the worker. |
| 88 | + */ |
| 89 | +function onGetCurrentUserDataFromWebWorker() { |
| 90 | + if (webWorker) { |
| 91 | + webWorker.postMessage({ type: 'GET_USER_INFO' }); |
| 92 | + } else { |
| 93 | + alertError('Error: Web workers are not supported in the current browser!'); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Runs various Firebase Auth tests in a web worker environment and confirms the |
| 99 | + * expected behavior. This is useful for manual testing in different browsers. |
| 100 | + * @param {string} googleIdToken The Google ID token to sign in with. |
| 101 | + */ |
| 102 | +function runWebWorkerTests(googleIdToken) { |
| 103 | + if (webWorker) { |
| 104 | + webWorker.postMessage({ |
| 105 | + type: 'RUN_TESTS', |
| 106 | + googleIdToken: googleIdToken |
| 107 | + }); |
| 108 | + } else { |
| 109 | + alertError('Error: Web workers are not supported in the current browser!'); |
| 110 | + } |
| 111 | +} |
0 commit comments