summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/devtools/front_end/host/ResourceLoader.js
blob: 501eb38d7f3e55251ab96c4ded0aed95763d053b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

WebInspector.ResourceLoader = {}

WebInspector.ResourceLoader._lastStreamId = 0;
/** @type {!Object.<number, !WebInspector.OutputStream>} */
WebInspector.ResourceLoader._boundStreams = {};

/**
 * @param {!WebInspector.OutputStream} stream
 * @return {number}
 */
WebInspector.ResourceLoader._bindOutputStream = function(stream)
{
    WebInspector.ResourceLoader._boundStreams[++WebInspector.ResourceLoader._lastStreamId] = stream;
    return WebInspector.ResourceLoader._lastStreamId;
}

/**
 * @param {number} id
 */
WebInspector.ResourceLoader._discardOutputStream = function(id)
{
    WebInspector.ResourceLoader._boundStreams[id].close();
    delete WebInspector.ResourceLoader._boundStreams[id];
}

/**
 * @param {number} id
 * @param {string} chunk
 */
WebInspector.ResourceLoader.streamWrite = function(id, chunk)
{
    WebInspector.ResourceLoader._boundStreams[id].write(chunk);
}

/**
 * @param {string} url
 * @param {?Object.<string, string>} headers
 * @param {function(number, !Object.<string, string>, string)} callback
 */
WebInspector.ResourceLoader.load = function(url, headers, callback)
{
    var stream = new WebInspector.StringOutputStream();
    WebInspector.ResourceLoader.loadAsStream(url, headers, stream, mycallback);

    /**
     * @param {number} statusCode
     * @param {!Object.<string, string>} headers
     */
    function mycallback(statusCode, headers)
    {
        callback(statusCode, headers, stream.data());
    }
}

/**
 * @param {string} url
 * @param {?Object.<string, string>} headers
 * @param {!WebInspector.OutputStream} stream
 * @param {function(number, !Object.<string, string>)=} callback
 */
WebInspector.ResourceLoader.loadAsStream = function(url, headers, stream, callback)
{
    var streamId = WebInspector.ResourceLoader._bindOutputStream(stream);
    var parsedURL = new WebInspector.ParsedURL(url);
    if (parsedURL.isDataURL()) {
        loadXHR(url)
            .then(dataURLDecodeSuccessful)
            .catch(dataURLDecodeFailed);
        return;
    }

    var rawHeaders = [];
    if (headers) {
        for (var key in headers)
            rawHeaders.push(key + ": " + headers[key]);
    }
    InspectorFrontendHost.loadNetworkResource(url, rawHeaders.join("\r\n"), streamId, finishedCallback);

    /**
     * @param {!InspectorFrontendHostAPI.LoadNetworkResourceResult} response
     */
    function finishedCallback(response)
    {
        if (callback)
            callback(response.statusCode, response.headers || {});
        WebInspector.ResourceLoader._discardOutputStream(streamId);
    }

    /**
     * @param {string} text
     */
    function dataURLDecodeSuccessful(text)
    {
        WebInspector.ResourceLoader.streamWrite(streamId, text);
        finishedCallback(/** @type {!InspectorFrontendHostAPI.LoadNetworkResourceResult} */ ({statusCode : 200}));
    }

    function dataURLDecodeFailed()
    {
        finishedCallback(/** @type {!InspectorFrontendHostAPI.LoadNetworkResourceResult} */ ({statusCode : 404}));
    }
}