protected override void OnDownloadProgress(CefUrlRequest request, long current, long total) { try { ThrowIfMoreThan5MB(request, total); base.OnDownloadProgress(request, current, total); } catch (Exception e) { SetException(e); request.Cancel(); } }
protected override void OnDownloadData(CefUrlRequest request, IntPtr data, long dataLength) { _total += dataLength; try { ThrowIfMoreThan5MB(request, _total); base.OnDownloadData(request, data, dataLength); } catch (Exception e) { SetException(e); request.Cancel(); } }
/// <summary> /// Called when some part of the response is read. This function will not be called if the /// <see cref="CefUrlRequestFlags.NoDownloadData"/> flag is set on the request. /// </summary> /// <param name="request">The associated <see cref="CefUrlRequest"/>.</param> /// <param name="data">The pointer to the buffer that contains the current bytes received since the last call.</param> /// <param name="dataLength">The size of the data buffer in bytes.</param> protected internal override void OnDownloadData(CefUrlRequest request, IntPtr data, long dataLength) { try { if (_stream is null) { _stream = CreateResourceStream((int)dataLength); if (_stream is null) { request.Cancel(); return; } } if (_stream is MemoryStream mem) { long startPos = _stream.Position; long endPos = startPos + dataLength; if (endPos < mem.Capacity) { Marshal.Copy(data, mem.GetBuffer(), (int)startPos, (int)dataLength); mem.SetLength(endPos); mem.Position = endPos; return; } } var buffer = new byte[dataLength]; Marshal.Copy(data, buffer, 0, buffer.Length); _stream.Write(buffer, 0, buffer.Length); } catch (Exception e) { SetException(e); request.Cancel(); } }
/// <summary> /// Notifies about a download progress. /// </summary> /// <param name="request">The associated <see cref="CefUrlRequest"/>.</param> /// <param name="current">The number of bytes received up to the call.</param> /// <param name="total">The expected total size of the response (or -1 if not determined).</param> protected internal override void OnDownloadProgress(CefUrlRequest request, long current, long total) { try { if (_stream is MemoryStream mem && mem.Capacity < total) { mem.Capacity = (int)total; } } catch (Exception e) { SetException(e); request.Cancel(); } }