// Copyright © 2014 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.IO;
using CefSharp.Callback;
namespace CefSharp
{
///
/// Class used to implement a custom resource handler. The methods of this class will always be called on the CEF IO thread.
/// Blocking the CEF IO thread will adversely affect browser performance. We suggest you execute your code in a Task (or similar).
/// To implement async handling, spawn a new Task (or similar), keep a reference to the callback. When you have a
/// fully populated stream, execute the callback. Once the callback Executes, GetResponseHeaders will be called where you
/// can modify the response including headers, or even redirect to a new Url. Set your responseLength and headers
/// Populate the dataOut stream in ReadResponse. For those looking for a sample implementation or upgrading from
/// a previous version . For those upgrading, inherit from ResourceHandler instead of IResourceHandler
/// add the override keywoard to existing methods e.g. ProcessRequestAsync.
///
public interface IResourceHandler : IDisposable
{
///
/// Open the response stream.
/// - To handle the request immediately set to true and return true.
/// - To decide at a later time set to false, return true, and execute
/// to continue or cancel the request.
/// - To cancel the request immediately set to true and return false.
/// This method will be called in sequence but not from a dedicated thread.
/// For backwards compatibility set to false and return false and the method
/// will be called.
///
/// request
/// see main summary
/// callback
/// see main summary
bool Open(IRequest request, out bool handleRequest, ICallback callback);
///
/// Begin processing the request.
///
/// The request object.
/// The callback used to Continue or Cancel the request (async).
/// To handle the request return true and call
/// once the response header information is available
/// can also be called from inside this method if
/// header information is available immediately).
/// To cancel the request return false.
[Obsolete("This method is deprecated and will be removed in the next version. Use Open instead.")]
bool ProcessRequest(IRequest request, ICallback callback);
///
/// Retrieve response header information. If the response length is not known
/// set to -1 and ReadResponse() will be called until it
/// returns false. If the response length is known set
/// to a positive value and ReadResponse() will be called until it returns
/// false or the specified number of bytes have been read.
///
/// It is also possible to set to a redirect http status code
/// and pass the new URL via a Location header. Likewise with it
/// is valid to set a relative or fully qualified URL as the Location header
/// value. If an error occured while setting up the request you can call
/// on to indicate the error condition.
///
/// Use the response object to set the mime type, http status code and other optional header values.
/// If the response length is not known set responseLength to -1
/// To redirect the request to a new URL set this to the new URL. Can be either a relative or fully qualified URL.
void GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl);
///
/// Skip response data when requested by a Range header.
/// Skip over and discard bytesToSkip bytes of response data.
/// - If data is available immediately set bytesSkipped to the number of of bytes skipped and return true.
/// - To read the data at a later time set bytesSkipped to 0, return true and execute callback when the data is available.
/// - To indicate failure set bytesSkipped to < 0 (e.g. -2 for ERR_FAILED) and return false.
/// This method will be called in sequence but not from a dedicated thread.
///
/// number of bytes to be skipped
///
/// If data is available immediately set bytesSkipped to the number of of bytes skipped and return true.
/// To read the data at a later time set bytesSkipped to 0, return true and execute callback when the data is available.
///
/// To read the data at a later time set bytesSkipped to 0,
/// return true and execute callback when the data is available.
/// See summary
bool Skip(Int64 bytesToSkip, out Int64 bytesSkipped, IResourceSkipCallback callback);
///
/// Read response data. If data is available immediately copy up to
/// dataOut.Length bytes into dataOut, set bytesRead to the number of
/// bytes copied, and return true. To read the data at a later time keep a
/// pointer to dataOut, set bytesRead to 0, return true and execute
/// callback when the data is available (dataOut will remain valid until
/// the callback is executed). To indicate response completion set bytesRead
/// to 0 and return false. To indicate failure set bytesRead to < 0 (e.g. -2
/// for ERR_FAILED) and return false. This method will be called in sequence
/// but not from a dedicated thread.
///
/// For backwards compatibility set bytesRead to -1 and return false and the ReadResponse method will be called.
///
/// If data is available immediately copy up to bytes into dataOut.
/// To indicate response completion set bytesRead to 0 and return false.
/// set to 0, return true and execute callback when the data is available
/// (dataOut will remain valid until the callback is executed). If you have no need
/// of the callback then Dispose of it immeduately.
/// return true or false depending on the criteria, see summary.
bool Read(Stream dataOut, out int bytesRead, IResourceReadCallback callback);
///
/// Read response data. If data is available immediately copy to
/// dataOut, set bytesRead to the number of bytes copied, and return true.
/// To read the data at a later time set bytesRead to 0, return true and call ICallback.Continue() when the
/// data is available. To indicate response completion return false.
///
/// Stream to write to
/// Number of bytes copied to the stream
/// The callback used to Continue or Cancel the request (async).
/// If data is available immediately copy to dataOut, set bytesRead to the number of bytes copied,
/// and return true.To indicate response completion return false.
/// Depending on this size of your response this method may be called multiple times
[Obsolete("This method is deprecated and will be removed in the next version. Use Skip and Read instead.")]
bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback);
///
/// Request processing has been canceled.
///
void Cancel();
}
}