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

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: Address comments 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 return Promise.resolve(new SharedWorker(moduleName + "_module.js", worke rName));
42
43 return loadResourcePromise(moduleName + "/module.json").then(start, start.bi nd(null, undefined));
44
45 /**
46 * @param {string=} content
47 */
48 function start(content)
49 {
50 if (!content)
51 throw new Error("Worker is not defined: " + moduleName + " " + new E rror().stack);
52 var scripts = JSON.parse(content)["scripts"];
53 if (scripts.length !== 1)
54 throw new Error("WorkerRuntime.startSharedWorker supports modules wi th only one script!");
55 return new SharedWorker(moduleName + "/" + scripts[0], workerName);
56 }
57 }
58
59 /**
60 * @param {string} moduleName
61 * @return {!Promise.<!Worker>}
62 */
63 WorkerRuntime.startWorker = function(moduleName)
64 {
65 if (Runtime.isReleaseMode())
66 return Promise.resolve(new Worker(moduleName + "_module.js"));
67
68 /**
69 * @suppress {checkTypes}
70 */
71 var loader = function() {
72 self.onmessage = function(event) {
73 self.onmessage = null;
74 var scripts = event.data;
75 for (var i = 0; i < scripts.length; ++i) {
76 var source = scripts[i]["source"];
77 self.eval(source + "\n//# sourceURL=" + scripts[i]["url"]);
78 }
79 };
80 };
81
82 return loadResourcePromise(moduleName + "/module.json").then(start, start.bi nd(null, undefined));
83
84 /**
85 * @param {string=} content
86 */
87 function start(content)
88 {
89 if (!content)
90 throw new Error("Worker is not defined: " + moduleName + " " + new E rror().stack);
91 var message = [];
92 var scripts = JSON.parse(content)["scripts"];
93 var promise = Promise.resolve();
94 for (var i = 0; i < scripts.length; ++i) {
95 var url = self._importScriptPathPrefix + moduleName + "/" + scripts[ i];
96 var parts = url.split("://");
97 url = parts.length === 1 ? url : parts[0] + "://" + normalizePath(pa rts[1]);
98 promise = promise.then(promiseGetter(loadResourcePromise(moduleName + "/" + scripts[i]))).then(pushSource.bind(null, url), pushSource.bind(null, nul l, null));
99 }
100
101 return promise.then(createWorker);
102
103 function promiseGetter(promise)
104 {
105 return function() {
106 return promise;
107 };
108 }
109
110 /**
111 * @param {?string} url
112 * @param {?string} source
113 */
114 function pushSource(url, source)
115 {
116 if (!url) {
117 console.error("Failed to load " + url);
118 return;
119 }
120 message.push({ source: source, url: url });
121 }
122
123 /**
124 * @return {!Worker}
125 */
126 function createWorker()
127 {
128 var blob = new Blob(["(" + loader.toString() + ")()\n//# sourceURL=" + moduleName], { type: "text/javascript" });
129 var workerURL = window.URL.createObjectURL(blob);
130 try {
131 var worker = new Worker(workerURL);
132 worker.postMessage(message);
133 return worker;
134 } finally {
135 window.URL.revokeObjectURL(workerURL);
136 }
137 }
138 }
139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698