OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
3 * Copyright (C) 2008, 2009 Anthony Ricaud <rik@webkit.org> | 3 * Copyright (C) 2008, 2009 Anthony Ricaud <rik@webkit.org> |
4 * Copyright (C) 2011 Google Inc. All rights reserved. | 4 * Copyright (C) 2011 Google Inc. All rights reserved. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * | 9 * |
10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
11 * notice, this list of conditions and the following disclaimer. | 11 * notice, this list of conditions and the following disclaimer. |
12 * 2. Redistributions in binary form must reproduce the above copyright | 12 * 2. Redistributions in binary form must reproduce the above copyright |
13 * notice, this list of conditions and the following disclaimer in the | 13 * notice, this list of conditions and the following disclaimer in the |
14 * documentation and/or other materials provided with the distribution. | 14 * documentation and/or other materials provided with the distribution. |
15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | 15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
16 * its contributors may be used to endorse or promote products derived | 16 * its contributors may be used to endorse or promote products derived |
17 * from this software without specific prior written permission. | 17 * from this software without specific prior written permission. |
18 * | 18 * |
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | 22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
| 30 /** |
| 31 * @unrestricted |
| 32 */ |
| 33 WebInspector.HARWriter = class { |
| 34 /** |
| 35 * @param {!WebInspector.OutputStream} stream |
| 36 * @param {!Array.<!WebInspector.NetworkRequest>} requests |
| 37 * @param {!WebInspector.Progress} progress |
| 38 */ |
| 39 write(stream, requests, progress) { |
| 40 this._stream = stream; |
| 41 this._harLog = (new WebInspector.HARLog(requests)).build(); |
| 42 this._pendingRequests = 1; // Guard against completing resource transfer be
fore all requests are made. |
| 43 var entries = this._harLog.entries; |
| 44 for (var i = 0; i < entries.length; ++i) { |
| 45 var content = requests[i].content; |
| 46 if (typeof content === 'undefined' && requests[i].finished) { |
| 47 ++this._pendingRequests; |
| 48 requests[i].requestContent().then(this._onContentAvailable.bind(this, en
tries[i], requests[i])); |
| 49 } else if (content !== null) |
| 50 this._setEntryContent(entries[i], requests[i]); |
| 51 } |
| 52 var compositeProgress = new WebInspector.CompositeProgress(progress); |
| 53 this._writeProgress = compositeProgress.createSubProgress(); |
| 54 if (--this._pendingRequests) { |
| 55 this._requestsProgress = compositeProgress.createSubProgress(); |
| 56 this._requestsProgress.setTitle(WebInspector.UIString('Collecting content…
')); |
| 57 this._requestsProgress.setTotalWork(this._pendingRequests); |
| 58 } else |
| 59 this._beginWrite(); |
| 60 } |
30 | 61 |
31 /** | 62 /** |
32 * @constructor | 63 * @param {!Object} entry |
33 */ | 64 * @param {!WebInspector.NetworkRequest} request |
34 WebInspector.HARWriter = function() | 65 */ |
35 { | 66 _setEntryContent(entry, request) { |
| 67 if (request.content !== null) |
| 68 entry.response.content.text = request.content; |
| 69 if (request.contentEncoded) |
| 70 entry.response.content.encoding = 'base64'; |
| 71 } |
| 72 |
| 73 /** |
| 74 * @param {!Object} entry |
| 75 * @param {!WebInspector.NetworkRequest} request |
| 76 * @param {?string} content |
| 77 */ |
| 78 _onContentAvailable(entry, request, content) { |
| 79 this._setEntryContent(entry, request); |
| 80 if (this._requestsProgress) |
| 81 this._requestsProgress.worked(); |
| 82 if (!--this._pendingRequests) { |
| 83 this._requestsProgress.done(); |
| 84 this._beginWrite(); |
| 85 } |
| 86 } |
| 87 |
| 88 _beginWrite() { |
| 89 const jsonIndent = 2; |
| 90 this._text = JSON.stringify({log: this._harLog}, null, jsonIndent); |
| 91 this._writeProgress.setTitle(WebInspector.UIString('Writing file…')); |
| 92 this._writeProgress.setTotalWork(this._text.length); |
| 93 this._bytesWritten = 0; |
| 94 this._writeNextChunk(this._stream); |
| 95 } |
| 96 |
| 97 /** |
| 98 * @param {!WebInspector.OutputStream} stream |
| 99 * @param {string=} error |
| 100 */ |
| 101 _writeNextChunk(stream, error) { |
| 102 if (this._bytesWritten >= this._text.length || error) { |
| 103 stream.close(); |
| 104 this._writeProgress.done(); |
| 105 return; |
| 106 } |
| 107 const chunkSize = 100000; |
| 108 var text = this._text.substring(this._bytesWritten, this._bytesWritten + chu
nkSize); |
| 109 this._bytesWritten += text.length; |
| 110 stream.write(text, this._writeNextChunk.bind(this)); |
| 111 this._writeProgress.setWorked(this._bytesWritten); |
| 112 } |
36 }; | 113 }; |
37 | |
38 WebInspector.HARWriter.prototype = { | |
39 /** | |
40 * @param {!WebInspector.OutputStream} stream | |
41 * @param {!Array.<!WebInspector.NetworkRequest>} requests | |
42 * @param {!WebInspector.Progress} progress | |
43 */ | |
44 write: function(stream, requests, progress) | |
45 { | |
46 this._stream = stream; | |
47 this._harLog = (new WebInspector.HARLog(requests)).build(); | |
48 this._pendingRequests = 1; // Guard against completing resource transfer
before all requests are made. | |
49 var entries = this._harLog.entries; | |
50 for (var i = 0; i < entries.length; ++i) { | |
51 var content = requests[i].content; | |
52 if (typeof content === "undefined" && requests[i].finished) { | |
53 ++this._pendingRequests; | |
54 requests[i].requestContent().then(this._onContentAvailable.bind(
this, entries[i], requests[i])); | |
55 } else if (content !== null) | |
56 this._setEntryContent(entries[i], requests[i]); | |
57 } | |
58 var compositeProgress = new WebInspector.CompositeProgress(progress); | |
59 this._writeProgress = compositeProgress.createSubProgress(); | |
60 if (--this._pendingRequests) { | |
61 this._requestsProgress = compositeProgress.createSubProgress(); | |
62 this._requestsProgress.setTitle(WebInspector.UIString("Collecting co
ntent…")); | |
63 this._requestsProgress.setTotalWork(this._pendingRequests); | |
64 } else | |
65 this._beginWrite(); | |
66 }, | |
67 | |
68 /** | |
69 * @param {!Object} entry | |
70 * @param {!WebInspector.NetworkRequest} request | |
71 */ | |
72 _setEntryContent: function(entry, request) | |
73 { | |
74 if (request.content !== null) | |
75 entry.response.content.text = request.content; | |
76 if (request.contentEncoded) | |
77 entry.response.content.encoding = "base64"; | |
78 }, | |
79 | |
80 /** | |
81 * @param {!Object} entry | |
82 * @param {!WebInspector.NetworkRequest} request | |
83 * @param {?string} content | |
84 */ | |
85 _onContentAvailable: function(entry, request, content) | |
86 { | |
87 this._setEntryContent(entry, request); | |
88 if (this._requestsProgress) | |
89 this._requestsProgress.worked(); | |
90 if (!--this._pendingRequests) { | |
91 this._requestsProgress.done(); | |
92 this._beginWrite(); | |
93 } | |
94 }, | |
95 | |
96 _beginWrite: function() | |
97 { | |
98 const jsonIndent = 2; | |
99 this._text = JSON.stringify({log: this._harLog}, null, jsonIndent); | |
100 this._writeProgress.setTitle(WebInspector.UIString("Writing file…")); | |
101 this._writeProgress.setTotalWork(this._text.length); | |
102 this._bytesWritten = 0; | |
103 this._writeNextChunk(this._stream); | |
104 }, | |
105 | |
106 /** | |
107 * @param {!WebInspector.OutputStream} stream | |
108 * @param {string=} error | |
109 */ | |
110 _writeNextChunk: function(stream, error) | |
111 { | |
112 if (this._bytesWritten >= this._text.length || error) { | |
113 stream.close(); | |
114 this._writeProgress.done(); | |
115 return; | |
116 } | |
117 const chunkSize = 100000; | |
118 var text = this._text.substring(this._bytesWritten, this._bytesWritten +
chunkSize); | |
119 this._bytesWritten += text.length; | |
120 stream.write(text, this._writeNextChunk.bind(this)); | |
121 this._writeProgress.setWorked(this._bytesWritten); | |
122 } | |
123 }; | |
OLD | NEW |