|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +const uuidGen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator); |
| 6 | +const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); |
| 7 | + |
| 8 | +class Helper { |
| 9 | + addObserver(handler, topic) { |
| 10 | + Services.obs.addObserver(handler, topic); |
| 11 | + return () => Services.obs.removeObserver(handler, topic); |
| 12 | + } |
| 13 | + |
| 14 | + addMessageListener(receiver, eventName, handler) { |
| 15 | + receiver.addMessageListener(eventName, handler); |
| 16 | + return () => receiver.removeMessageListener(eventName, handler); |
| 17 | + } |
| 18 | + |
| 19 | + addEventListener(receiver, eventName, handler) { |
| 20 | + receiver.addEventListener(eventName, handler); |
| 21 | + return () => receiver.removeEventListener(eventName, handler); |
| 22 | + } |
| 23 | + |
| 24 | + on(receiver, eventName, handler) { |
| 25 | + // The toolkit/modules/EventEmitter.jsm dispatches event name as a first argument. |
| 26 | + // Fire event listeners without it for convenience. |
| 27 | + const handlerWrapper = (_, ...args) => handler(...args); |
| 28 | + receiver.on(eventName, handlerWrapper); |
| 29 | + return () => receiver.off(eventName, handlerWrapper); |
| 30 | + } |
| 31 | + |
| 32 | + addProgressListener(progress, listener, flags) { |
| 33 | + progress.addProgressListener(listener, flags); |
| 34 | + return () => progress.removeProgressListener(listener); |
| 35 | + } |
| 36 | + |
| 37 | + removeListeners(listeners) { |
| 38 | + for (const tearDown of listeners) |
| 39 | + tearDown.call(null); |
| 40 | + listeners.splice(0, listeners.length); |
| 41 | + } |
| 42 | + |
| 43 | + generateId() { |
| 44 | + const string = uuidGen.generateUUID().toString(); |
| 45 | + return string.substring(1, string.length - 1); |
| 46 | + } |
| 47 | + |
| 48 | + getLoadContext(channel) { |
| 49 | + let loadContext = null; |
| 50 | + try { |
| 51 | + if (channel.notificationCallbacks) |
| 52 | + loadContext = channel.notificationCallbacks.getInterface(Ci.nsILoadContext); |
| 53 | + } catch (e) {} |
| 54 | + try { |
| 55 | + if (!loadContext && channel.loadGroup) |
| 56 | + loadContext = channel.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext); |
| 57 | + } catch (e) { } |
| 58 | + return loadContext; |
| 59 | + } |
| 60 | + |
| 61 | + getNetworkErrorStatusText(status) { |
| 62 | + if (!status) |
| 63 | + return null; |
| 64 | + for (const key of Object.keys(Cr)) { |
| 65 | + if (Cr[key] === status) |
| 66 | + return key; |
| 67 | + } |
| 68 | + // Security module. The following is taken from |
| 69 | + // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/How_to_check_the_secruity_state_of_an_XMLHTTPRequest_over_SSL |
| 70 | + if ((status & 0xff0000) === 0x5a0000) { |
| 71 | + // NSS_SEC errors (happen below the base value because of negative vals) |
| 72 | + if ((status & 0xffff) < Math.abs(Ci.nsINSSErrorsService.NSS_SEC_ERROR_BASE)) { |
| 73 | + // The bases are actually negative, so in our positive numeric space, we |
| 74 | + // need to subtract the base off our value. |
| 75 | + const nssErr = Math.abs(Ci.nsINSSErrorsService.NSS_SEC_ERROR_BASE) - (status & 0xffff); |
| 76 | + switch (nssErr) { |
| 77 | + case 11: |
| 78 | + return 'SEC_ERROR_EXPIRED_CERTIFICATE'; |
| 79 | + case 12: |
| 80 | + return 'SEC_ERROR_REVOKED_CERTIFICATE'; |
| 81 | + case 13: |
| 82 | + return 'SEC_ERROR_UNKNOWN_ISSUER'; |
| 83 | + case 20: |
| 84 | + return 'SEC_ERROR_UNTRUSTED_ISSUER'; |
| 85 | + case 21: |
| 86 | + return 'SEC_ERROR_UNTRUSTED_CERT'; |
| 87 | + case 36: |
| 88 | + return 'SEC_ERROR_CA_CERT_INVALID'; |
| 89 | + case 90: |
| 90 | + return 'SEC_ERROR_INADEQUATE_KEY_USAGE'; |
| 91 | + case 176: |
| 92 | + return 'SEC_ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED'; |
| 93 | + default: |
| 94 | + return 'SEC_ERROR_UNKNOWN'; |
| 95 | + } |
| 96 | + } |
| 97 | + const sslErr = Math.abs(Ci.nsINSSErrorsService.NSS_SSL_ERROR_BASE) - (status & 0xffff); |
| 98 | + switch (sslErr) { |
| 99 | + case 3: |
| 100 | + return 'SSL_ERROR_NO_CERTIFICATE'; |
| 101 | + case 4: |
| 102 | + return 'SSL_ERROR_BAD_CERTIFICATE'; |
| 103 | + case 8: |
| 104 | + return 'SSL_ERROR_UNSUPPORTED_CERTIFICATE_TYPE'; |
| 105 | + case 9: |
| 106 | + return 'SSL_ERROR_UNSUPPORTED_VERSION'; |
| 107 | + case 12: |
| 108 | + return 'SSL_ERROR_BAD_CERT_DOMAIN'; |
| 109 | + default: |
| 110 | + return 'SSL_ERROR_UNKNOWN'; |
| 111 | + } |
| 112 | + } |
| 113 | + return '<unknown error>'; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +var EXPORTED_SYMBOLS = [ "Helper" ]; |
| 118 | +this.Helper = Helper; |
| 119 | + |
0 commit comments