Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: native_client_sdk/src/gonacl_appengine/static/pnacl-demo/example.js

Issue 22503002: [NaCl SDK] Earth demo modifications for gonacl PNaCl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge master Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 var naclModule = null; 7 var naclModule = null;
8 8
9 /** 9 /**
10 * MIME type for PNaCl 10 * MIME type for PNaCl
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 var listenerDiv = document.getElementById('listener'); 59 var listenerDiv = document.getElementById('listener');
60 listenerDiv.appendChild(moduleEl); 60 listenerDiv.appendChild(moduleEl);
61 } 61 }
62 62
63 /** 63 /**
64 * Add the default event listeners to the element with id "listener". 64 * Add the default event listeners to the element with id "listener".
65 */ 65 */
66 function attachDefaultListeners() { 66 function attachDefaultListeners() {
67 var listenerDiv = document.getElementById('listener'); 67 var listenerDiv = document.getElementById('listener');
68 listenerDiv.addEventListener('load', moduleDidLoad, true); 68 listenerDiv.addEventListener('load', moduleDidLoad, true);
69 listenerDiv.addEventListener('error', moduleLoadError, true);
69 listenerDiv.addEventListener('progress', moduleLoadProgress, true); 70 listenerDiv.addEventListener('progress', moduleLoadProgress, true);
70 listenerDiv.addEventListener('message', handleMessage, true); 71 listenerDiv.addEventListener('message', handleMessage, true);
71 listenerDiv.addEventListener('crash', handleCrash, true); 72 listenerDiv.addEventListener('crash', handleCrash, true);
72 if (typeof window.attachListeners !== 'undefined') { 73 if (typeof window.attachListeners !== 'undefined') {
73 window.attachListeners(); 74 window.attachListeners();
74 } 75 }
75 } 76 }
76 77
77 /** 78 /**
78 * Called when the Browser can not communicate with the Module 79 * Called when the Browser can not communicate with the Module
(...skipping 16 matching lines...) Expand all
95 /** 96 /**
96 * Called when the NaCl module is loaded. 97 * Called when the NaCl module is loaded.
97 * 98 *
98 * This event listener is registered in attachDefaultListeners above. 99 * This event listener is registered in attachDefaultListeners above.
99 */ 100 */
100 function moduleDidLoad() { 101 function moduleDidLoad() {
101 var bar = document.getElementById('progress'); 102 var bar = document.getElementById('progress');
102 bar.value = 100; 103 bar.value = 100;
103 bar.max = 100; 104 bar.max = 100;
104 naclModule = document.getElementById('nacl_module'); 105 naclModule = document.getElementById('nacl_module');
105 updateStatus('RUNNING'); 106 hideStatus();
107 }
106 108
107 setAuxElementsVisibility(true); 109 function hideStatus() {
110 document.getElementById('statusField').style.display = 'none';
111 document.getElementById('progress').style.display = 'none';
112 }
113
114 function moduleLoadError(event) {
115 updateStatus('Load failed.');
108 } 116 }
109 117
110 /** 118 /**
111 * Called when the plugin reports progress events. 119 * Called when the plugin reports progress events.
112 * 120 *
113 * @param {Object} event 121 * @param {Object} event
114 */ 122 */
115 function moduleLoadProgress(event) { 123 function moduleLoadProgress(event) {
124 document.getElementById('progress').style.display = 'block';
125
116 var loadPercent = 0.0; 126 var loadPercent = 0.0;
117 var bar = document.getElementById('progress'); 127 var bar = document.getElementById('progress');
118 bar.max = 100; 128 bar.max = 100;
119 if (event.lengthComputable && event.total > 0) { 129 if (event.lengthComputable && event.total > 0) {
120 loadPercent = event.loaded / event.total * 100.0; 130 loadPercent = event.loaded / event.total * 100.0;
121 } else { 131 } else {
122 // The total length is not yet known. 132 // The total length is not yet known.
123 loadPercent = -1.0; 133 loadPercent = -1.0;
124 } 134 }
125 bar.value = loadPercent; 135 bar.value = loadPercent;
126 } 136 }
127 137
128 /** 138 /**
129 * Return true when |s| starts with the string |prefix|. 139 * Return true when |s| starts with the string |prefix|.
130 * 140 *
131 * @param {string} s The string to search. 141 * @param {string} s The string to search.
132 * @param {string} prefix The prefix to search for in |s|. 142 * @param {string} prefix The prefix to search for in |s|.
133 * @return {bool} 143 * @return {bool}
134 */ 144 */
135 function startsWith(s, prefix) { 145 function startsWith(s, prefix) {
136 // indexOf would search the entire string, lastIndexOf(p, 0) only checks at 146 // indexOf would search the entire string, lastIndexOf(p, 0) only checks at
137 // the first index. See: http://stackoverflow.com/a/4579228 147 // the first index. See: http://stackoverflow.com/a/4579228
138 return s.lastIndexOf(prefix, 0) === 0; 148 return s.lastIndexOf(prefix, 0) === 0;
139 } 149 }
140 150
141 /** 151 /**
142 * Add a message to an element with id "log".
143 *
144 * This function is used by the default "log:" message handler.
145 *
146 * @param {string} message The message to log.
147 */
148 function logMessage(message) {
149 document.getElementById('log').textContent = logMessageArray.join('');
150 console.log(message);
151 }
152
153 var defaultMessageTypes = {
154 'alert': alert,
155 'log': logMessage
156 };
157
158 /**
159 * Called when the NaCl module sends a message to JavaScript (via
160 * PPB_Messaging.PostMessage())
161 *
162 * This event listener is registered in createNaClModule above.
163 *
164 * @param {Event} message_event A message event. message_event.data contains
165 * the data sent from the NaCl module.
166 */
167 function handleMessage(message_event) {
168 if (typeof message_event.data === 'string') {
169 for (var type in defaultMessageTypes) {
170 if (defaultMessageTypes.hasOwnProperty(type)) {
171 if (startsWith(message_event.data, type + ':')) {
172 func = defaultMessageTypes[type];
173 func(message_event.data.slice(type.length + 1));
174 return;
175 }
176 }
177 }
178 }
179
180 if (typeof window.handleMessage !== 'undefined') {
181 window.handleMessage(message_event);
182 return;
183 }
184
185 logMessage('Unhandled message: ' + message_event.data + '\n');
186 }
187
188 /**
189 * Called when the DOM content has loaded; i.e. the page's document is fully 152 * Called when the DOM content has loaded; i.e. the page's document is fully
190 * parsed. At this point, we can safely query any elements in the document via 153 * parsed. At this point, we can safely query any elements in the document via
191 * document.querySelector, document.getElementById, etc. 154 * document.querySelector, document.getElementById, etc.
192 * 155 *
193 * @param {string} name The name of the example. 156 * @param {string} name The name of the example.
194 * @param {number} width The width to create the plugin. 157 * @param {number} width The width to create the plugin.
195 * @param {number} height The height to create the plugin. 158 * @param {number} height The height to create the plugin.
196 * @param {Object} attrs Optional dictionary of additional attributes. 159 * @param {Object} attrs Optional dictionary of additional attributes.
197 */ 160 */
198 function domContentLoaded(name, width, height, attrs) { 161 function domContentLoaded(name, width, height, attrs) {
199 // If the page loads before the Native Client module loads, then set the 162 updateStatus('Loading...');
200 // status message indicating that the module is still loading. Otherwise,
201 // do not change the status message.
202 setAuxElementsVisibility(false);
203 updateStatus('Page loaded.');
204 if (!browserSupportsPNaCl()) { 163 if (!browserSupportsPNaCl()) {
205 updateStatus('Browser does not support PNaCl or PNaCl is disabled'); 164 updateStatus('Browser does not support PNaCl or PNaCl is disabled');
206 } else if (naclModule == null) { 165 } else if (naclModule == null) {
207 // We use a non-zero sized embed to give Chrome space to place the bad 166 // We use a non-zero sized embed to give Chrome space to place the bad
208 // plug-in graphic, if there is a problem. 167 // plug-in graphic, if there is a problem.
209 width = typeof width !== 'undefined' ? width : 200; 168 width = typeof width !== 'undefined' ? width : 200;
210 height = typeof height !== 'undefined' ? height : 200; 169 height = typeof height !== 'undefined' ? height : 200;
211 createNaClModule(name, width, height, attrs); 170 createNaClModule(name, width, height, attrs);
212 attachDefaultListeners(); 171 attachDefaultListeners();
213 } else { 172 } else {
214 // It's possible that the Native Client module onload event fired 173 // It's possible that the Native Client module onload event fired
215 // before the page's onload event. In this case, the status message 174 // before the page's onload event. In this case, the status message
216 // will reflect 'SUCCESS', but won't be displayed. This call will 175 // will reflect 'SUCCESS', but won't be displayed. This call will
217 // display the current message. 176 // display the current message.
218 updateStatus('Waiting.'); 177 updateStatus('Waiting.');
219 } 178 }
220 } 179 }
221 180
222 /** 181 /**
223 * If the element with id 'statusField' exists, then set its HTML to the status 182 * If the element with id 'statusField' exists, then set its HTML to the status
224 * message as well. 183 * message as well.
225 * 184 *
226 * @param {string} opt_message The message to set. 185 * @param {string} opt_message The message to set.
227 */ 186 */
228 function updateStatus(opt_message) { 187 function updateStatus(opt_message) {
229 var statusField = document.getElementById('statusField'); 188 var statusField = document.getElementById('statusField');
230 if (statusField) { 189 if (statusField) {
231 statusField.innerHTML = opt_message; 190 statusField.style.display = 'block';
232 } 191 statusField.textContent = opt_message;
233 }
234
235 function postThreadFunc(numThreads) {
236 return function() {
237 naclModule.postMessage({'message' : 'set_threads',
238 'value' : numThreads});
239 } 192 }
240 } 193 }
241 194
242 // Add event listeners after the NaCl module has loaded. These listeners will 195 // Add event listeners after the NaCl module has loaded. These listeners will
243 // forward messages to the NaCl module via postMessage() 196 // forward messages to the NaCl module via postMessage()
244 function attachListeners() { 197 function attachListeners() {
245 var threads = [0, 1, 2, 4, 6, 8, 12, 16, 24, 32]; 198 document.getElementById('threadCount').addEventListener('change',
246 for (var i = 0; i < threads.length; i++) { 199 function() {
247 document.getElementById('radio' + i).addEventListener('click', 200 var value = parseInt(document.getElementById('threadCount').value);
248 postThreadFunc(threads[i])); 201 naclModule.postMessage({'message': 'set_threads',
249 } 202 'value': value});
203 });
250 document.getElementById('zoomRange').addEventListener('change', 204 document.getElementById('zoomRange').addEventListener('change',
251 function() { 205 function() {
252 var value = parseFloat(document.getElementById('zoomRange').value); 206 var value = parseFloat(document.getElementById('zoomRange').value);
253 naclModule.postMessage({'message' : 'set_zoom', 207 naclModule.postMessage({'message' : 'set_zoom',
254 'value' : value}); 208 'value' : value});
255 }); 209 });
256 document.getElementById('lightRange').addEventListener('change', 210 document.getElementById('lightRange').addEventListener('change',
257 function() { 211 function() {
258 var value = parseFloat(document.getElementById('lightRange').value); 212 var value = parseFloat(document.getElementById('lightRange').value);
259 naclModule.postMessage({'message' : 'set_light', 213 naclModule.postMessage({'message' : 'set_light',
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 var value = attr[1]; 294 var value = attr[1];
341 attrs[key] = value; 295 attrs[key] = value;
342 } 296 }
343 } 297 }
344 298
345 domContentLoaded(body.dataset.name, 299 domContentLoaded(body.dataset.name,
346 body.dataset.width, 300 body.dataset.width,
347 body.dataset.height, attrs); 301 body.dataset.height, attrs);
348 } 302 }
349 }); 303 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698