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