OLD | NEW |
| (Empty) |
1 // JavaScript Library for Nacl Tests and Demos | |
2 | |
3 function NaclLib(embed_name, status_id, num_retries) { | |
4 this.embed_name_ = embed_name; | |
5 this.statusfield_ = document.getElementById(status_id); | |
6 this.status_ = "WAIT"; | |
7 this.message_ = ""; | |
8 this.handler_ = null; | |
9 this.retries_ = num_retries; | |
10 }; | |
11 | |
12 | |
13 NaclLib.prototype.getStatus = function() { | |
14 return this.status_; | |
15 }; | |
16 | |
17 | |
18 NaclLib.prototype.getMessage = function() { | |
19 return this.message_; | |
20 }; | |
21 | |
22 | |
23 NaclLib.prototype.cleanUp = function() { | |
24 if (this.handler_) { | |
25 clearInterval(this._handler); | |
26 this.handler_ = null; | |
27 } | |
28 }; | |
29 | |
30 | |
31 NaclLib.prototype.setStatus = function() { | |
32 this.statusfield_.innerHTML = | |
33 this.status_ + ": " + this.message_; | |
34 }; | |
35 | |
36 | |
37 NaclLib.prototype.setStatusWait = function(message) { | |
38 this.status_ = "WAIT"; | |
39 this.message_ = "" + this.retries_ + " " + message; | |
40 this.setStatus() | |
41 }; | |
42 | |
43 | |
44 NaclLib.prototype.setStatusError = function(message) { | |
45 this.status_ = "ERROR"; | |
46 this.message_ = message; | |
47 this.setStatus() | |
48 }; | |
49 | |
50 | |
51 NaclLib.prototype.setStatusSuccess = function(message) { | |
52 this.status_ = "SUCCESS"; | |
53 this.message_ = message; | |
54 this.setStatus() | |
55 }; | |
56 | |
57 | |
58 NaclLib.prototype.numModulesReady = function(modules) { | |
59 var count = 0; | |
60 for (var i = 0; i < modules.length; i++) { | |
61 if (modules[i].__moduleReady == 1) { | |
62 count += 1; | |
63 } | |
64 } | |
65 return count; | |
66 }; | |
67 | |
68 | |
69 NaclLib.prototype.areTherePluginProblems = function(modules) { | |
70 for (var i = 0; i < modules.length; i++) { | |
71 if (modules[i].__moduleReady == undefined) return 1; | |
72 } | |
73 return 0; | |
74 }; | |
75 | |
76 | |
77 NaclLib.prototype.checkModuleReadiness = function() { | |
78 // Work around bug that does not disable the handler. | |
79 if (!this.handler_) | |
80 return; | |
81 | |
82 if (this.retries_ == 0) { | |
83 this.cleanUp(); | |
84 this.setStatusError("The Native Client modules are loading too slowly"); | |
85 return; | |
86 } | |
87 this.retries_ -= 1; | |
88 | |
89 // Find all elements with name "this.embed_name_". This should be the list | |
90 // of all NaCl modules on the page. Note that passing in such a list at | |
91 // initialization time would sometimes pass the list of scriptable objects | |
92 // (as desired) but would sometimes pass the list of embed tags, depending | |
93 // on a start-up race condition. As such, pass the "name" attribute of the | |
94 // <embed> tags and then gather the list of all of those scriptable objects | |
95 // each time this method is invoked. | |
96 var module_list = document.getElementsByName(this.embed_name_); | |
97 var num_ready = this.numModulesReady(module_list); | |
98 | |
99 if (module_list.length == num_ready) { | |
100 if (this.wait) { | |
101 var result = this.wait(); | |
102 if (result) { | |
103 this.setStatusWait(result); | |
104 return; | |
105 } | |
106 } | |
107 | |
108 this.cleanUp(); | |
109 | |
110 var result; | |
111 try { | |
112 result = this.test(); | |
113 } catch(e) { | |
114 this.setStatusError(e); | |
115 return; | |
116 } | |
117 | |
118 if (result == "") { | |
119 this.setStatusSuccess(""); | |
120 } else { | |
121 this.setStatusError(result); | |
122 } | |
123 | |
124 return; | |
125 } | |
126 | |
127 this.setStatusWait("Loaded " + num_ready + "/" + module_list.length + | |
128 " modules"); | |
129 | |
130 if (this.areTherePluginProblems(module_list)) { | |
131 this.cleanUp(); | |
132 this.setStatusError("The Native Client plugin was unable to load"); | |
133 return; | |
134 } | |
135 }; | |
136 | |
137 | |
138 // Workaround for JS inconsistent scoping behavior | |
139 function wrapperForCheckModuleReadiness(that) { | |
140 that.checkModuleReadiness(); | |
141 } | |
142 | |
143 | |
144 NaclLib.prototype.waitForModulesAndRunTests = function() { | |
145 // avoid regsitering two handlers | |
146 if (!this.handler_) { | |
147 this.handler_ = setInterval(wrapperForCheckModuleReadiness, 100, this); | |
148 } | |
149 }; | |
OLD | NEW |