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