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

Side by Side Diff: Source/devtools/front_end/common/WorkerRuntime.js

Issue 685203003: DevTools: Get rid of synchronous XHRs in the frontend code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Annotate code, fix tests Created 6 years, 1 month 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
(Empty)
1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 var WorkerRuntime = {};
32
33 /**
34 * @param {string} moduleName
35 * @param {string} workerName
36 * @return {!Promise.<!SharedWorker>}
37 */
38 WorkerRuntime.startSharedWorker = function(moduleName, workerName)
39 {
40 if (Runtime.isReleaseMode()) {
41 try {
42 var worker = new SharedWorker(moduleName + "_module.js", workerName) ;
43 return Promise.resolve(worker);
44 } catch (e) {
45 return Promise.reject(e);
46 }
47 }
48
49 return loadResourcePromise(moduleName + "/module.json").then(start, start.bi nd(null, undefined));
50
51 /**
52 * @param {string=} content
53 * @return {!SharedWorker}
54 */
55 function start(content)
56 {
57 if (!content)
58 throw new Error("Worker is not defined: " + moduleName + " " + new E rror().stack);
59 var scripts = JSON.parse(content)["scripts"];
60 if (scripts.length !== 1)
61 throw new Error("WorkerRuntime.startSharedWorker supports modules wi th only one script!");
62 return new SharedWorker(moduleName + "/" + scripts[0], workerName);
63 }
64 }
65
66 /**
67 * @param {string} moduleName
68 * @return {!Promise.<!Worker>}
69 */
70 WorkerRuntime.startWorker = function(moduleName)
71 {
72 if (Runtime.isReleaseMode())
73 return Promise.resolve(new Worker(moduleName + "_module.js"));
74
75 /**
76 * @suppress {checkTypes}
77 */
78 var loader = function() {
79 self.onmessage = function(event) {
80 self.onmessage = null;
81 var scripts = event.data;
82 for (var i = 0; i < scripts.length; ++i) {
83 var source = scripts[i]["source"];
84 self.eval(source + "\n//# sourceURL=" + scripts[i]["url"]);
85 }
86 };
87 };
88
89 return loadResourcePromise(moduleName + "/module.json").then(start, start.bi nd(null, undefined));
90
91 /**
92 * @param {string=} content
93 */
94 function start(content)
95 {
96 if (!content)
97 throw new Error("Worker is not defined: " + moduleName + " " + new E rror().stack);
98 var message = [];
99 var scripts = JSON.parse(content)["scripts"];
100 var promise = Promise.resolve();
101 for (var i = 0; i < scripts.length; ++i) {
102 var url = self._importScriptPathPrefix + moduleName + "/" + scripts[ i];
103 var parts = url.split("://");
104 url = parts.length === 1 ? url : parts[0] + "://" + normalizePath(pa rts[1]);
105 promise = promise.then(promiseGetter(loadResourcePromise(moduleName + "/" + scripts[i]))).then(pushSource.bind(null, url), pushSource.bind(null, nul l, null));
106 }
107
108 return promise.then(createWorker);
109
110 function promiseGetter(promise)
111 {
112 return function() {
113 return promise;
114 };
115 }
116
117 /**
118 * @param {?string} url
119 * @param {?string} source
120 */
121 function pushSource(url, source)
122 {
123 if (!url) {
124 console.error("Failed to load " + url);
125 return;
126 }
127 message.push({ source: source, url: url });
128 }
129
130 /**
131 * @return {!Worker}
132 */
133 function createWorker()
134 {
135 var blob = new Blob(["(" + loader.toString() + ")()\n//# sourceURL=" + moduleName], { type: "text/javascript" });
136 var workerURL = window.URL.createObjectURL(blob);
137 try {
138 var worker = new Worker(workerURL);
139 worker.postMessage(message);
140 return worker;
141 } finally {
142 window.URL.revokeObjectURL(workerURL);
143 }
144 }
145 }
146 }
147
148 /**
149 * @constructor
150 * @param {string} moduleName
151 * @param {string=} workerName
152 */
153 WorkerRuntime.Worker = function(moduleName, workerName)
154 {
155 this._isSharedWorker = !!workerName;
156 this._workerPromise = workerName ? WorkerRuntime.startSharedWorker(moduleNam e, /** @type {string} */ (workerName)) : WorkerRuntime.startWorker(moduleName);
157 }
158
159 WorkerRuntime.Worker.prototype = {
160 /**
161 * @param {*} message
162 */
163 postMessage: function(message)
164 {
165 this._workerPromise.then(postToWorker.bind(this));
166
167 /**
168 * @param {!Worker|!SharedWorker} worker
169 * @this {WorkerRuntime.Worker}
170 */
171 function postToWorker(worker)
172 {
173 if (!this._disposed)
174 worker.postMessage(message);
175 }
176 },
177
178 dispose: function()
179 {
180 this._disposed = true;
181 this._workerPromise.then(terminate);
182
183 /**
184 * @param {!Worker|!SharedWorker} worker
185 */
186 function terminate(worker)
187 {
188 worker.terminate();
189 }
190 },
191
192 terminate: function()
193 {
194 this.dispose();
195 },
196
197 /**
198 * @param {?function(!MessageEvent.<*>)} listener
199 */
200 set onmessage(listener)
201 {
202 this._workerPromise.then(setOnMessage);
203
204 /**
205 * @param {!Worker|!SharedWorker} worker
206 */
207 function setOnMessage(worker)
208 {
209 worker.onmessage = listener;
210 }
211 },
212
213 /**
214 * @param {?function(!Event)} listener
215 */
216 set onerror(listener)
217 {
218 this._workerPromise.then(setOnError);
219
220 /**
221 * @param {!Worker|!SharedWorker} worker
222 */
223 function setOnError(worker)
224 {
225 worker.onerror = listener;
226 }
227 },
228
229 get port()
230 {
231 return new WorkerRuntime.Worker.FuturePort(this);
232 }
233 }
234
235 /**
236 * @constructor
237 * @param {!WorkerRuntime.Worker} worker
238 */
239 WorkerRuntime.Worker.FuturePort = function(worker)
240 {
241 this._worker = worker;
242 }
243
244 WorkerRuntime.Worker.FuturePort.prototype = {
245 /**
246 * @param {?function(!MessageEvent.<?>)} listener
247 */
248 set onmessage(listener)
249 {
250 this._worker._workerPromise.then(setOnMessage);
251
252 /**
253 * @param {!SharedWorker} worker
254 */
255 function setOnMessage(worker)
256 {
257 worker.port.onmessage = listener;
258 }
259 },
260
261 /**
262 * @param {?function(!Event)} listener
263 */
264 set onerror(listener)
265 {
266 this._worker._workerPromise.then(setOnError);
267
268 /**
269 * @param {!SharedWorker} worker
270 */
271 function setOnError(worker)
272 {
273 worker.port.onerror = listener;
274 }
275 }
276 }
OLDNEW
« no previous file with comments | « Source/devtools/front_end/bindings/TempFile.js ('k') | Source/devtools/front_end/common/module.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698