file_shim.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. TestData.prototype = {
  12. getObj: function() {
  13. if (!this.obj) {
  14. return null;
  15. }
  16. // only one of the 2 should be set
  17. if ((this.idl && this.webidl) ||
  18. (!this.idl && !this.webidl)) {
  19. return null;
  20. }
  21. // split on . to allow nested props
  22. var props = this.obj.split(".");
  23. var obj = window.navigator;
  24. for (var i = 0; i < props.length && obj !== undefined; i++) {
  25. obj = obj[props[i]];
  26. }
  27. if ((this.webidl && obj instanceof window[this.webidl]) ||
  28. (this.idl && obj instanceof SpecialPowers.Ci[this.idl])) {
  29. return obj;
  30. } else {
  31. return null;
  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. function receiveMessage(e) {
  48. var src = e.source;
  49. var step = e.data.step;
  50. var id = e.data.id;
  51. var data = new TestData(e.data.testdata);
  52. var success, failure;
  53. function reply(res, msg) {
  54. window.removeEventListener("message", receiveMessage, false);
  55. src.postMessage({result: res, msg: msg,
  56. id: id}, "*");
  57. }
  58. function _success(msg) {
  59. reply(true, msg);
  60. }
  61. function _failure(msg) {
  62. reply(false, msg);
  63. }
  64. // flip success and failure around for precheck
  65. if (step == 0) {
  66. success = _failure;
  67. failure = _success;
  68. } else {
  69. success = _success;
  70. failure = _failure;
  71. }
  72. if (data.verifier instanceof Function) {
  73. data.verifier(success, failure);
  74. } else {
  75. // import toSource() function to global
  76. eval(data.verifier);
  77. verifier.bind(data, success, failure)();
  78. }
  79. }
  80. window.addEventListener("message", receiveMessage, false);
  81. </script>
  82. </head>
  83. <body>
  84. <div id="content" style="display: none"></div>
  85. </body>
  86. </html>