| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 * @return {number} | 348 * @return {number} |
| 349 */ | 349 */ |
| 350 _pageEventTime: function(page, time) | 350 _pageEventTime: function(page, time) |
| 351 { | 351 { |
| 352 var startTime = page.startTime; | 352 var startTime = page.startTime; |
| 353 if (time === -1 || startTime === -1) | 353 if (time === -1 || startTime === -1) |
| 354 return -1; | 354 return -1; |
| 355 return WebInspector.HAREntry._toMilliseconds(time - startTime); | 355 return WebInspector.HAREntry._toMilliseconds(time - startTime); |
| 356 } | 356 } |
| 357 } | 357 } |
| 358 | |
| 359 /** | |
| 360 * @constructor | |
| 361 */ | |
| 362 WebInspector.HARWriter = function() | |
| 363 { | |
| 364 } | |
| 365 | |
| 366 WebInspector.HARWriter.prototype = { | |
| 367 /** | |
| 368 * @param {!WebInspector.OutputStream} stream | |
| 369 * @param {!Array.<!WebInspector.NetworkRequest>} requests | |
| 370 * @param {!WebInspector.Progress} progress | |
| 371 */ | |
| 372 write: function(stream, requests, progress) | |
| 373 { | |
| 374 this._stream = stream; | |
| 375 this._harLog = (new WebInspector.HARLog(requests)).build(); | |
| 376 this._pendingRequests = 1; // Guard against completing resource transfer
before all requests are made. | |
| 377 var entries = this._harLog.entries; | |
| 378 for (var i = 0; i < entries.length; ++i) { | |
| 379 var content = requests[i].content; | |
| 380 if (typeof content === "undefined" && requests[i].finished) { | |
| 381 ++this._pendingRequests; | |
| 382 requests[i].requestContent(this._onContentAvailable.bind(this, e
ntries[i])); | |
| 383 } else if (content !== null) | |
| 384 entries[i].response.content.text = content; | |
| 385 } | |
| 386 var compositeProgress = new WebInspector.CompositeProgress(progress); | |
| 387 this._writeProgress = compositeProgress.createSubProgress(); | |
| 388 if (--this._pendingRequests) { | |
| 389 this._requestsProgress = compositeProgress.createSubProgress(); | |
| 390 this._requestsProgress.setTitle(WebInspector.UIString("Collecting co
ntent…")); | |
| 391 this._requestsProgress.setTotalWork(this._pendingRequests); | |
| 392 } else | |
| 393 this._beginWrite(); | |
| 394 }, | |
| 395 | |
| 396 /** | |
| 397 * @param {!Object} entry | |
| 398 * @param {?string} content | |
| 399 */ | |
| 400 _onContentAvailable: function(entry, content) | |
| 401 { | |
| 402 if (content !== null) | |
| 403 entry.response.content.text = content; | |
| 404 if (this._requestsProgress) | |
| 405 this._requestsProgress.worked(); | |
| 406 if (!--this._pendingRequests) { | |
| 407 this._requestsProgress.done(); | |
| 408 this._beginWrite(); | |
| 409 } | |
| 410 }, | |
| 411 | |
| 412 _beginWrite: function() | |
| 413 { | |
| 414 const jsonIndent = 2; | |
| 415 this._text = JSON.stringify({log: this._harLog}, null, jsonIndent); | |
| 416 this._writeProgress.setTitle(WebInspector.UIString("Writing file…")); | |
| 417 this._writeProgress.setTotalWork(this._text.length); | |
| 418 this._bytesWritten = 0; | |
| 419 this._writeNextChunk(this._stream); | |
| 420 }, | |
| 421 | |
| 422 /** | |
| 423 * @param {!WebInspector.OutputStream} stream | |
| 424 * @param {string=} error | |
| 425 */ | |
| 426 _writeNextChunk: function(stream, error) | |
| 427 { | |
| 428 if (this._bytesWritten >= this._text.length || error) { | |
| 429 stream.close(); | |
| 430 this._writeProgress.done(); | |
| 431 return; | |
| 432 } | |
| 433 const chunkSize = 100000; | |
| 434 var text = this._text.substring(this._bytesWritten, this._bytesWritten +
chunkSize); | |
| 435 this._bytesWritten += text.length; | |
| 436 stream.write(text, this._writeNextChunk.bind(this)); | |
| 437 this._writeProgress.setWorked(this._bytesWritten); | |
| 438 } | |
| 439 } | |
| OLD | NEW |