Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <html> |
michael@0 | 2 | <head> |
michael@0 | 3 | <script type="application/javascript;version=1.8"> |
michael@0 | 4 | function TestData(aOpts) { |
michael@0 | 5 | for (var opt in aOpts) { |
michael@0 | 6 | if (aOpts.hasOwnProperty(opt)) { |
michael@0 | 7 | this[opt] = aOpts[opt]; |
michael@0 | 8 | } |
michael@0 | 9 | } |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | TestData.prototype = { |
michael@0 | 13 | getObj: function() { |
michael@0 | 14 | if (!this.obj) { |
michael@0 | 15 | return null; |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | // only one of the 2 should be set |
michael@0 | 19 | if ((this.idl && this.webidl) || |
michael@0 | 20 | (!this.idl && !this.webidl)) { |
michael@0 | 21 | return null; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | var obj = window.navigator[this.obj]; |
michael@0 | 25 | |
michael@0 | 26 | if ((this.webidl && obj instanceof window[this.webidl]) || |
michael@0 | 27 | (this.idl && obj instanceof SpecialPowers.Ci[this.idl])) { |
michael@0 | 28 | return obj; |
michael@0 | 29 | } else { |
michael@0 | 30 | return null; |
michael@0 | 31 | } |
michael@0 | 32 | }, |
michael@0 | 33 | |
michael@0 | 34 | // default verifier |
michael@0 | 35 | verifier: function(success, failure) { |
michael@0 | 36 | try { |
michael@0 | 37 | if (this.getObj()) { |
michael@0 | 38 | success(this.perm); |
michael@0 | 39 | } else { |
michael@0 | 40 | failure("Did not receive proper object"); |
michael@0 | 41 | } |
michael@0 | 42 | } catch (e) { |
michael@0 | 43 | failure("Received exception!: " + e); |
michael@0 | 44 | } |
michael@0 | 45 | }, |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function receiveMessage(e) { |
michael@0 | 49 | var src = e.source; |
michael@0 | 50 | var step = e.data.step; |
michael@0 | 51 | var id = e.data.id; |
michael@0 | 52 | var timer = window.setTimeout(timeout, 10000); |
michael@0 | 53 | var data = new TestData(e.data.testdata); |
michael@0 | 54 | var success, failure; |
michael@0 | 55 | |
michael@0 | 56 | function reply(res, msg) { |
michael@0 | 57 | window.clearTimeout(timer); |
michael@0 | 58 | window.removeEventListener("message", receiveMessage, false); |
michael@0 | 59 | src.postMessage({result: res, msg: msg, |
michael@0 | 60 | id: id}, "*"); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function _success(msg) { |
michael@0 | 64 | reply(true, msg); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function _failure(msg) { |
michael@0 | 68 | reply(false, msg); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function timeout() { |
michael@0 | 72 | reply(false, "Test timed out", false); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | // flip success and failure around for precheck |
michael@0 | 76 | if (step == 0) { |
michael@0 | 77 | success = _failure; |
michael@0 | 78 | failure = _success; |
michael@0 | 79 | } else { |
michael@0 | 80 | success = _success; |
michael@0 | 81 | failure = _failure; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | if (data.verifier instanceof Function) { |
michael@0 | 85 | data.verifier(success, failure); |
michael@0 | 86 | } else { |
michael@0 | 87 | // import toSource() function to global |
michael@0 | 88 | eval(data.verifier); |
michael@0 | 89 | verifier.bind(data, success, failure)(); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | window.addEventListener("message", receiveMessage, false); |
michael@0 | 94 | </script> |
michael@0 | 95 | </head> |
michael@0 | 96 | <body> |
michael@0 | 97 | <div id="content" style="display: none"></div> |
michael@0 | 98 | </body> |
michael@0 | 99 | </html> |