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

Side by Side Diff: native_client_sdk/src/examples/hello_nacl_io/example.js

Issue 13488007: [NaCl SDK] Make the SDK examples buildable as a packaged app. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix license headers Created 7 years, 8 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 function moduleDidLoad() { 5 function moduleDidLoad() {
6 common.hideModule(); 6 common.hideModule();
7 } 7 }
8 8
9 // Called by the common.js module. 9 // Called by the common.js module.
10 function domContentLoaded(name, tc, config, width, height) { 10 function domContentLoaded(name, tc, config, width, height) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 function fopen(e) { 71 function fopen(e) {
72 var filename = document.getElementById('fopenFilename').value; 72 var filename = document.getElementById('fopenFilename').value;
73 var access = document.getElementById('fopenMode').value; 73 var access = document.getElementById('fopenMode').value;
74 nacl_module.postMessage(makeCall('fopen', filename, access)); 74 nacl_module.postMessage(makeCall('fopen', filename, access));
75 } 75 }
76 76
77 function fopen_result(filename, filehandle) { 77 function fopen_result(filename, filehandle) {
78 filehandle_map[filehandle] = filename; 78 filehandle_map[filehandle] = filename;
79 79
80 addFilenameToSelectElements(filehandle, filename) 80 addFilenameToSelectElements(filehandle, filename)
81 logMessage('File ' + filename + ' opened successfully.'); 81 common.logMessage('File ' + filename + ' opened successfully.\n');
82 } 82 }
83 83
84 function fclose(e) { 84 function fclose(e) {
85 var filehandle = document.getElementById('fcloseHandle').value; 85 var filehandle = document.getElementById('fcloseHandle').value;
86 nacl_module.postMessage(makeCall('fclose', filehandle)); 86 nacl_module.postMessage(makeCall('fclose', filehandle));
87 } 87 }
88 88
89 function fclose_result(filehandle) { 89 function fclose_result(filehandle) {
90 var filename = filehandle_map[filehandle]; 90 var filename = filehandle_map[filehandle];
91 removeFilenameFromSelectElements(filehandle, filename); 91 removeFilenameFromSelectElements(filehandle, filename);
92 logMessage('File ' + filename + ' closed successfully.'); 92 common.logMessage('File ' + filename + ' closed successfully.\n');
93 } 93 }
94 94
95 function fread(e) { 95 function fread(e) {
96 var filehandle = document.getElementById('freadHandle').value; 96 var filehandle = document.getElementById('freadHandle').value;
97 var numBytes = document.getElementById('freadBytes').value; 97 var numBytes = document.getElementById('freadBytes').value;
98 nacl_module.postMessage(makeCall('fread', filehandle, numBytes)); 98 nacl_module.postMessage(makeCall('fread', filehandle, numBytes));
99 } 99 }
100 100
101 function fread_result(filehandle, data) { 101 function fread_result(filehandle, data) {
102 var filename = filehandle_map[filehandle]; 102 var filename = filehandle_map[filehandle];
103 logMessage('Read "' + data + '" from file ' + filename + '.'); 103 common.logMessage('Read "' + data + '" from file ' + filename + '.\n');
104 } 104 }
105 105
106 function fwrite(e) { 106 function fwrite(e) {
107 var filehandle = document.getElementById('fwriteHandle').value; 107 var filehandle = document.getElementById('fwriteHandle').value;
108 var data = document.getElementById('fwriteData').value; 108 var data = document.getElementById('fwriteData').value;
109 nacl_module.postMessage(makeCall('fwrite', filehandle, data)); 109 nacl_module.postMessage(makeCall('fwrite', filehandle, data));
110 } 110 }
111 111
112 function fwrite_result(filehandle, bytes_written) { 112 function fwrite_result(filehandle, bytes_written) {
113 var filename = filehandle_map[filehandle]; 113 var filename = filehandle_map[filehandle];
114 logMessage('Wrote ' + bytes_written + ' bytes to file ' + filename + '.'); 114 common.logMessage('Wrote ' + bytes_written + ' bytes to file ' + filename +
115 '.\n');
115 } 116 }
116 117
117 function fseek(e) { 118 function fseek(e) {
118 var filehandle = document.getElementById('fseekHandle').value; 119 var filehandle = document.getElementById('fseekHandle').value;
119 var offset = document.getElementById('fseekOffset').value; 120 var offset = document.getElementById('fseekOffset').value;
120 var whence = document.getElementById('fseekWhence').value; 121 var whence = document.getElementById('fseekWhence').value;
121 nacl_module.postMessage(makeCall('fseek', filehandle, offset, whence)); 122 nacl_module.postMessage(makeCall('fseek', filehandle, offset, whence));
122 } 123 }
123 124
124 function fseek_result(filehandle, filepos) { 125 function fseek_result(filehandle, filepos) {
125 var filename = filehandle_map[filehandle]; 126 var filename = filehandle_map[filehandle];
126 logMessage('Seeked to location ' + filepos + ' in file ' + filename + '.'); 127 common.logMessage('Seeked to location ' + filepos + ' in file ' + filename +
128 '.\n');
127 } 129 }
128 130
129 /** 131 /**
130 * Return true when |s| starts with the string |prefix|. 132 * Return true when |s| starts with the string |prefix|.
131 * 133 *
132 * @param {string} s The string to search. 134 * @param {string} s The string to search.
133 * @param {string} prefix The prefix to search for in |s|. 135 * @param {string} prefix The prefix to search for in |s|.
134 */ 136 */
135 function startsWith(s, prefix) { 137 function startsWith(s, prefix) {
136 // indexOf would search the entire string, lastIndexOf(p, 0) only checks at 138 // indexOf would search the entire string, lastIndexOf(p, 0) only checks at
137 // the first index. See: http://stackoverflow.com/a/4579228 139 // the first index. See: http://stackoverflow.com/a/4579228
138 return s.lastIndexOf(prefix, 0) === 0; 140 return s.lastIndexOf(prefix, 0) === 0;
139 } 141 }
140 142
141 function logMessage(msg) {
142 var logEl = document.getElementById('log');
143
144 // Perform some basic escaping.
145 msg = msg.replace(/&/g, '&')
146 .replace(/</g, '&lt;')
147 .replace(/>/g, '&gt;')
148 .replace(/"/g, '&quot;')
149 .replace(/'/g, '&apos;');
150
151 logEl.innerHTML += msg + '<br>';
152 }
153
154 function makeCall(func) { 143 function makeCall(func) {
155 var message = func; 144 var message = func;
156 for (var i = 1; i < arguments.length; ++i) { 145 for (var i = 1; i < arguments.length; ++i) {
157 message += '\1' + arguments[i]; 146 message += '\1' + arguments[i];
158 } 147 }
159 148
160 return message; 149 return message;
161 } 150 }
162 151
163 // Called by the common.js module. 152 // Called by the common.js module.
164 function handleMessage(message_event) { 153 function handleMessage(message_event) {
165 var msg = message_event.data; 154 var msg = message_event.data;
166 if (startsWith(msg, 'Error:')) { 155 if (startsWith(msg, 'Error:')) {
167 logMessage(msg); 156 common.logMessage(msg + '\n');
168 } else { 157 } else {
169 // Result from a function call. 158 // Result from a function call.
170 var params = msg.split('\1'); 159 var params = msg.split('\1');
171 var func_name = params[0]; 160 var func_name = params[0];
172 var func_result_name = func_name + '_result'; 161 var func_result_name = func_name + '_result';
173 var result_func = window[func_result_name]; 162 var result_func = window[func_result_name];
174 163
175 if (!result_func) { 164 if (!result_func) {
176 logMessage('Error: Bad message received from NaCl module.'); 165 common.logMessage('Error: Bad message received from NaCl module.\n');
177 return; 166 return;
178 } 167 }
179 168
180 result_func.apply(null, params.slice(1)); 169 result_func.apply(null, params.slice(1));
181 } 170 }
182 } 171 }
OLDNEW
« no previous file with comments | « native_client_sdk/src/examples/hello_nacl_io/example.dsc ('k') | native_client_sdk/src/examples/hello_nacl_io/index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698