| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 | |
| 5 WebInspector.ResourceLoader = {}; | 4 WebInspector.ResourceLoader = {}; |
| 6 | 5 |
| 7 WebInspector.ResourceLoader._lastStreamId = 0; | 6 WebInspector.ResourceLoader._lastStreamId = 0; |
| 8 /** @type {!Object.<number, !WebInspector.OutputStream>} */ | 7 /** @type {!Object.<number, !WebInspector.OutputStream>} */ |
| 9 WebInspector.ResourceLoader._boundStreams = {}; | 8 WebInspector.ResourceLoader._boundStreams = {}; |
| 10 | 9 |
| 11 /** | 10 /** |
| 12 * @param {!WebInspector.OutputStream} stream | 11 * @param {!WebInspector.OutputStream} stream |
| 13 * @return {number} | 12 * @return {number} |
| 14 */ | 13 */ |
| 15 WebInspector.ResourceLoader._bindOutputStream = function(stream) | 14 WebInspector.ResourceLoader._bindOutputStream = function(stream) { |
| 16 { | 15 WebInspector.ResourceLoader._boundStreams[++WebInspector.ResourceLoader._lastS
treamId] = stream; |
| 17 WebInspector.ResourceLoader._boundStreams[++WebInspector.ResourceLoader._las
tStreamId] = stream; | 16 return WebInspector.ResourceLoader._lastStreamId; |
| 18 return WebInspector.ResourceLoader._lastStreamId; | |
| 19 }; | 17 }; |
| 20 | 18 |
| 21 /** | 19 /** |
| 22 * @param {number} id | 20 * @param {number} id |
| 23 */ | 21 */ |
| 24 WebInspector.ResourceLoader._discardOutputStream = function(id) | 22 WebInspector.ResourceLoader._discardOutputStream = function(id) { |
| 25 { | 23 WebInspector.ResourceLoader._boundStreams[id].close(); |
| 26 WebInspector.ResourceLoader._boundStreams[id].close(); | 24 delete WebInspector.ResourceLoader._boundStreams[id]; |
| 27 delete WebInspector.ResourceLoader._boundStreams[id]; | |
| 28 }; | 25 }; |
| 29 | 26 |
| 30 /** | 27 /** |
| 31 * @param {number} id | 28 * @param {number} id |
| 32 * @param {string} chunk | 29 * @param {string} chunk |
| 33 */ | 30 */ |
| 34 WebInspector.ResourceLoader.streamWrite = function(id, chunk) | 31 WebInspector.ResourceLoader.streamWrite = function(id, chunk) { |
| 35 { | 32 WebInspector.ResourceLoader._boundStreams[id].write(chunk); |
| 36 WebInspector.ResourceLoader._boundStreams[id].write(chunk); | |
| 37 }; | 33 }; |
| 38 | 34 |
| 39 /** | 35 /** |
| 40 * @param {string} url | 36 * @param {string} url |
| 41 * @param {?Object.<string, string>} headers | 37 * @param {?Object.<string, string>} headers |
| 42 * @param {function(number, !Object.<string, string>, string)} callback | 38 * @param {function(number, !Object.<string, string>, string)} callback |
| 43 */ | 39 */ |
| 44 WebInspector.ResourceLoader.load = function(url, headers, callback) | 40 WebInspector.ResourceLoader.load = function(url, headers, callback) { |
| 45 { | 41 var stream = new WebInspector.StringOutputStream(); |
| 46 var stream = new WebInspector.StringOutputStream(); | 42 WebInspector.ResourceLoader.loadAsStream(url, headers, stream, mycallback); |
| 47 WebInspector.ResourceLoader.loadAsStream(url, headers, stream, mycallback); | |
| 48 | 43 |
| 49 /** | 44 /** |
| 50 * @param {number} statusCode | 45 * @param {number} statusCode |
| 51 * @param {!Object.<string, string>} headers | 46 * @param {!Object.<string, string>} headers |
| 52 */ | 47 */ |
| 53 function mycallback(statusCode, headers) | 48 function mycallback(statusCode, headers) { |
| 54 { | 49 callback(statusCode, headers, stream.data()); |
| 55 callback(statusCode, headers, stream.data()); | 50 } |
| 56 } | |
| 57 }; | 51 }; |
| 58 | 52 |
| 59 /** | 53 /** |
| 60 * @param {string} url | 54 * @param {string} url |
| 61 * @param {?Object.<string, string>} headers | 55 * @param {?Object.<string, string>} headers |
| 62 * @param {!WebInspector.OutputStream} stream | 56 * @param {!WebInspector.OutputStream} stream |
| 63 * @param {function(number, !Object.<string, string>)=} callback | 57 * @param {function(number, !Object.<string, string>)=} callback |
| 64 */ | 58 */ |
| 65 WebInspector.ResourceLoader.loadAsStream = function(url, headers, stream, callba
ck) | 59 WebInspector.ResourceLoader.loadAsStream = function(url, headers, stream, callba
ck) { |
| 66 { | 60 var streamId = WebInspector.ResourceLoader._bindOutputStream(stream); |
| 67 var streamId = WebInspector.ResourceLoader._bindOutputStream(stream); | 61 var parsedURL = new WebInspector.ParsedURL(url); |
| 68 var parsedURL = new WebInspector.ParsedURL(url); | 62 if (parsedURL.isDataURL()) { |
| 69 if (parsedURL.isDataURL()) { | 63 loadXHR(url).then(dataURLDecodeSuccessful).catch(dataURLDecodeFailed); |
| 70 loadXHR(url) | 64 return; |
| 71 .then(dataURLDecodeSuccessful) | 65 } |
| 72 .catch(dataURLDecodeFailed); | |
| 73 return; | |
| 74 } | |
| 75 | 66 |
| 76 var rawHeaders = []; | 67 var rawHeaders = []; |
| 77 if (headers) { | 68 if (headers) { |
| 78 for (var key in headers) | 69 for (var key in headers) |
| 79 rawHeaders.push(key + ": " + headers[key]); | 70 rawHeaders.push(key + ': ' + headers[key]); |
| 80 } | 71 } |
| 81 InspectorFrontendHost.loadNetworkResource(url, rawHeaders.join("\r\n"), stre
amId, finishedCallback); | 72 InspectorFrontendHost.loadNetworkResource(url, rawHeaders.join('\r\n'), stream
Id, finishedCallback); |
| 82 | 73 |
| 83 /** | 74 /** |
| 84 * @param {!InspectorFrontendHostAPI.LoadNetworkResourceResult} response | 75 * @param {!InspectorFrontendHostAPI.LoadNetworkResourceResult} response |
| 85 */ | 76 */ |
| 86 function finishedCallback(response) | 77 function finishedCallback(response) { |
| 87 { | 78 if (callback) |
| 88 if (callback) | 79 callback(response.statusCode, response.headers || {}); |
| 89 callback(response.statusCode, response.headers || {}); | 80 WebInspector.ResourceLoader._discardOutputStream(streamId); |
| 90 WebInspector.ResourceLoader._discardOutputStream(streamId); | 81 } |
| 91 } | |
| 92 | 82 |
| 93 /** | 83 /** |
| 94 * @param {string} text | 84 * @param {string} text |
| 95 */ | 85 */ |
| 96 function dataURLDecodeSuccessful(text) | 86 function dataURLDecodeSuccessful(text) { |
| 97 { | 87 WebInspector.ResourceLoader.streamWrite(streamId, text); |
| 98 WebInspector.ResourceLoader.streamWrite(streamId, text); | 88 finishedCallback(/** @type {!InspectorFrontendHostAPI.LoadNetworkResourceRes
ult} */ ({statusCode: 200})); |
| 99 finishedCallback(/** @type {!InspectorFrontendHostAPI.LoadNetworkResourc
eResult} */ ({statusCode : 200})); | 89 } |
| 100 } | |
| 101 | 90 |
| 102 function dataURLDecodeFailed() | 91 function dataURLDecodeFailed() { |
| 103 { | 92 finishedCallback(/** @type {!InspectorFrontendHostAPI.LoadNetworkResourceRes
ult} */ ({statusCode: 404})); |
| 104 finishedCallback(/** @type {!InspectorFrontendHostAPI.LoadNetworkResourc
eResult} */ ({statusCode : 404})); | 93 } |
| 105 } | |
| 106 }; | 94 }; |
| OLD | NEW |