|
| 1 | +// Copyright © 2021 The CefSharp Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +using System.IO; |
| 6 | + |
| 7 | +namespace CefSharp.Fluent |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Called on the CEF IO thread when the browser needs credentials from the user. |
| 11 | + /// This method will only be called for requests initiated from the browser process. |
| 12 | + /// </summary> |
| 13 | + /// <param name="isProxy">indicates whether the host is a proxy server.</param> |
| 14 | + /// <param name="host">the hostname.</param> |
| 15 | + /// <param name="port">the port number.</param> |
| 16 | + /// <param name="realm">realm</param> |
| 17 | + /// <param name="scheme">scheme</param> |
| 18 | + /// <param name="callback">is a callback for authentication information</param> |
| 19 | + /// <returns> |
| 20 | + /// Return true to continue the request and call <see cref="IAuthCallback.Continue(string, string)"/> when the authentication information is available. |
| 21 | + /// If the request has an associated browser/frame then returning false will result in a call to <see cref="IRequestHandler.GetAuthCredentials"/> |
| 22 | + /// on the <see cref="IRequestHandler"/> associated with that browser, if any. |
| 23 | + /// Otherwise, returning false will cancel the request immediately. |
| 24 | + /// </returns> |
| 25 | + public delegate bool GetAuthCredentialsDelegate(bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Called when some part of the response is read. This method will not be called if the <see cref="UrlRequestFlags.NoDownloadData"/> flag is set on the request. |
| 29 | + /// </summary> |
| 30 | + /// <param name="request">request</param> |
| 31 | + /// <param name="data">A stream containing the bytes received since the last call. Cannot be used outside the scope of this method. </param> |
| 32 | + public delegate void OnDownloadDataDelegate(IUrlRequest request, Stream data); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Notifies the client of download progress. |
| 36 | + /// </summary> |
| 37 | + /// <param name="request">request</param> |
| 38 | + /// <param name="current">denotes the number of bytes received up to the call </param> |
| 39 | + /// <param name="total">is the expected total size of the response (or -1 if not determined).</param> |
| 40 | + public delegate void OnDownloadProgressDelegate(IUrlRequest request, long current, long total); |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Notifies the client that the request has completed. |
| 44 | + /// Use the <see cref="IUrlRequest.RequestStatus"/> property to determine if the |
| 45 | + /// request was successful or not. |
| 46 | + /// </summary> |
| 47 | + /// <param name="request">request</param> |
| 48 | + public delegate void OnRequestCompleteDelegate(IUrlRequest request); |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Notifies the client of upload progress. |
| 52 | + /// This method will only be called if the UR_FLAG_REPORT_UPLOAD_PROGRESS flag is set on the request. |
| 53 | + /// </summary> |
| 54 | + /// <param name="request">request</param> |
| 55 | + /// <param name="current">denotes the number of bytes sent so far.</param> |
| 56 | + /// <param name="total">is the total size of uploading data (or -1 if chunked upload is enabled).</param> |
| 57 | + public delegate void OnUploadProgressDelegate(IUrlRequest request, long current, long total); |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Fluent UrlRequestClient |
| 61 | + /// </summary> |
| 62 | + public class UrlRequestClient : CefSharp.UrlRequestClient |
| 63 | + { |
| 64 | + private GetAuthCredentialsDelegate getAuthCredentials; |
| 65 | + private OnDownloadDataDelegate onDownloadData; |
| 66 | + private OnDownloadProgressDelegate onDownloadProgress; |
| 67 | + private OnRequestCompleteDelegate onRequestComplete; |
| 68 | + private OnUploadProgressDelegate onUploadProgress; |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Create a new UrlRequestClient Builder |
| 72 | + /// </summary> |
| 73 | + /// <returns>Fluent UrlRequestClient Builder</returns> |
| 74 | + public static UrlRequestClientBuilder Create() |
| 75 | + { |
| 76 | + return new UrlRequestClientBuilder(); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Use <see cref="Create"/> to create a new instance of the fluent builder |
| 81 | + /// </summary> |
| 82 | + internal UrlRequestClient() |
| 83 | + { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + internal void SetGetAuthCredentials(GetAuthCredentialsDelegate func) |
| 88 | + { |
| 89 | + getAuthCredentials = func; |
| 90 | + } |
| 91 | + |
| 92 | + internal void SetOnDownloadData(OnDownloadDataDelegate action) |
| 93 | + { |
| 94 | + onDownloadData = action; |
| 95 | + } |
| 96 | + |
| 97 | + internal void SetOnDownloadProgress(OnDownloadProgressDelegate action) |
| 98 | + { |
| 99 | + onDownloadProgress = action; |
| 100 | + } |
| 101 | + |
| 102 | + internal void SetOnRequestComplete(OnRequestCompleteDelegate action) |
| 103 | + { |
| 104 | + onRequestComplete = action; |
| 105 | + } |
| 106 | + |
| 107 | + internal void SetOnUploadProgress(OnUploadProgressDelegate action) |
| 108 | + { |
| 109 | + onUploadProgress = action; |
| 110 | + } |
| 111 | + |
| 112 | + /// <inheritdoc/> |
| 113 | + protected override bool GetAuthCredentials(bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) |
| 114 | + { |
| 115 | + return getAuthCredentials?.Invoke(isProxy, host, port, realm, scheme, callback) ?? false; |
| 116 | + } |
| 117 | + |
| 118 | + /// <inheritdoc/> |
| 119 | + protected override void OnDownloadData(IUrlRequest request, Stream data) |
| 120 | + { |
| 121 | + onDownloadData?.Invoke(request, data); |
| 122 | + } |
| 123 | + |
| 124 | + /// <inheritdoc/> |
| 125 | + protected override void OnDownloadProgress(IUrlRequest request, long current, long total) |
| 126 | + { |
| 127 | + onDownloadProgress?.Invoke(request, current, total); |
| 128 | + } |
| 129 | + |
| 130 | + /// <inheritdoc/> |
| 131 | + protected override void OnRequestComplete(IUrlRequest request) |
| 132 | + { |
| 133 | + onRequestComplete?.Invoke(request); |
| 134 | + } |
| 135 | + |
| 136 | + /// <inheritdoc/> |
| 137 | + protected override void OnUploadProgress(IUrlRequest request, long current, long total) |
| 138 | + { |
| 139 | + onUploadProgress?.Invoke(request, current, total); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments