2015-05-23 03:57:31 +02:00
|
|
|
// Copyright (c) 2015 Marshall A. Greenblatt. All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the name Chromium Embedded
|
|
|
|
// Framework nor the names of its contributors may be used to endorse
|
|
|
|
// or promote products derived from this software without specific prior
|
|
|
|
// written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// The contents of this file are only available to applications that link
|
|
|
|
// against the libcef_dll_wrapper target.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef CEF_INCLUDE_WRAPPER_CEF_RESOURCE_MANAGER_H_
|
|
|
|
#define CEF_INCLUDE_WRAPPER_CEF_RESOURCE_MANAGER_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <list>
|
2021-06-17 22:08:01 +02:00
|
|
|
#include <memory>
|
2015-05-23 03:57:31 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2015-05-23 03:57:31 +02:00
|
|
|
#include "include/base/cef_ref_counted.h"
|
|
|
|
#include "include/base/cef_weak_ptr.h"
|
|
|
|
#include "include/cef_request_handler.h"
|
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
|
|
|
#include "include/wrapper/cef_helpers.h"
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Class for managing multiple resource providers. For each resource request
|
|
|
|
/// providers will be called in order and have the option to (a) handle the
|
|
|
|
/// request by returning a CefResourceHandler, (b) pass the request to the next
|
|
|
|
/// provider in order, or (c) stop handling the request. See comments on the
|
|
|
|
/// Request object for additional usage information. The methods of this class
|
|
|
|
/// may be called on any browser process thread unless otherwise indicated.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2017-05-17 11:29:28 +02:00
|
|
|
class CefResourceManager
|
|
|
|
: public base::RefCountedThreadSafe<CefResourceManager,
|
|
|
|
CefDeleteOnIOThread> {
|
2015-05-23 03:57:31 +02:00
|
|
|
public:
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Provides an opportunity to modify |url| before it is passed to a provider.
|
|
|
|
/// For example, the implementation could rewrite |url| to include a default
|
|
|
|
/// file extension. |url| will be fully qualified and may contain query or
|
|
|
|
/// fragment components.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2021-06-19 21:35:12 +02:00
|
|
|
using UrlFilter =
|
|
|
|
base::RepeatingCallback<std::string(const std::string& /*url*/)>;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Used to resolve mime types for URLs, usually based on the file extension.
|
|
|
|
/// |url| will be fully qualified and may contain query or fragment
|
|
|
|
/// components.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2021-06-19 21:35:12 +02:00
|
|
|
using MimeTypeResolver =
|
|
|
|
base::RepeatingCallback<std::string(const std::string& /*url*/)>;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Values that stay with a request as it moves between providers.
|
|
|
|
struct RequestParams {
|
|
|
|
std::string url_;
|
|
|
|
CefRefPtr<CefBrowser> browser_;
|
|
|
|
CefRefPtr<CefFrame> frame_;
|
|
|
|
CefRefPtr<CefRequest> request_;
|
|
|
|
UrlFilter url_filter_;
|
|
|
|
MimeTypeResolver mime_type_resolver_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Values that are associated with the pending request only.
|
|
|
|
struct RequestState;
|
|
|
|
|
|
|
|
public:
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Object representing a request. Each request object is used for a single
|
|
|
|
/// call to Provider::OnRequest and will become detached (meaning the
|
|
|
|
/// callbacks will no longer trigger) after Request::Continue or Request::Stop
|
|
|
|
/// is called. A request passed to Provider::OnRequestCanceled will already
|
|
|
|
/// have been detached. The methods of this class may be called on any browser
|
|
|
|
/// process thread.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
class Request : public base::RefCountedThreadSafe<Request> {
|
|
|
|
public:
|
2021-12-06 22:59:14 +01:00
|
|
|
Request(const Request&) = delete;
|
|
|
|
Request& operator=(const Request&) = delete;
|
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Returns the URL associated with this request. The returned value will be
|
|
|
|
/// fully qualified but will not contain query or fragment components. It
|
|
|
|
/// will already have been passed through the URL filter.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
std::string url() const { return params_.url_; }
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Returns the CefBrowser associated with this request.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
CefRefPtr<CefBrowser> browser() const { return params_.browser_; }
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Returns the CefFrame associated with this request.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
CefRefPtr<CefFrame> frame() const { return params_.frame_; }
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Returns the CefRequest associated with this request.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
CefRefPtr<CefRequest> request() const { return params_.request_; }
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Returns the current URL filter.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
const CefResourceManager::UrlFilter& url_filter() const {
|
|
|
|
return params_.url_filter_;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Returns the current mime type resolver.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
const CefResourceManager::MimeTypeResolver& mime_type_resolver() const {
|
|
|
|
return params_.mime_type_resolver_;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Continue handling the request. If |handler| is non-NULL then no
|
|
|
|
/// additional providers will be called and the |handler| value will be
|
|
|
|
/// returned via CefResourceManager::GetResourceHandler. If |handler| is
|
|
|
|
/// NULL then the next provider in order, if any, will be called. If there
|
|
|
|
/// are no additional providers then NULL will be returned via
|
|
|
|
/// CefResourceManager:: GetResourceHandler.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void Continue(CefRefPtr<CefResourceHandler> handler);
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Stop handling the request. No additional providers will be called and
|
|
|
|
/// NULL will be returned via CefResourceManager::GetResourceHandler.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void Stop();
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
private:
|
2015-05-23 03:57:31 +02:00
|
|
|
// Only allow deletion via scoped_refptr.
|
|
|
|
friend class base::RefCountedThreadSafe<Request>;
|
|
|
|
|
|
|
|
friend class CefResourceManager;
|
|
|
|
|
|
|
|
// The below methods are called on the browser process IO thread.
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
explicit Request(std::unique_ptr<RequestState> state);
|
2015-05-23 03:57:31 +02:00
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
std::unique_ptr<RequestState> SendRequest();
|
2015-05-23 03:57:31 +02:00
|
|
|
bool HasState();
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
static void ContinueOnIOThread(std::unique_ptr<RequestState> state,
|
2015-05-23 03:57:31 +02:00
|
|
|
CefRefPtr<CefResourceHandler> handler);
|
2021-06-17 22:08:01 +02:00
|
|
|
static void StopOnIOThread(std::unique_ptr<RequestState> state);
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
// Will be non-NULL while the request is pending. Only accessed on the
|
|
|
|
// browser process IO thread.
|
2021-06-17 22:08:01 +02:00
|
|
|
std::unique_ptr<RequestState> state_;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
// Params that stay with this request object. Safe to access on any thread.
|
|
|
|
RequestParams params_;
|
|
|
|
};
|
|
|
|
|
2021-12-06 22:59:14 +01:00
|
|
|
using RequestList = std::list<scoped_refptr<Request>>;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Interface implemented by resource providers. A provider may be created on
|
|
|
|
/// any thread but the methods will be called on, and the object will be
|
|
|
|
/// destroyed on, the browser process IO thread.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
class Provider {
|
|
|
|
public:
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Called to handle a request. If the provider knows immediately that it
|
|
|
|
/// will not handle the request return false. Otherwise, return true and
|
|
|
|
/// call Request::Continue or Request::Stop either in this method or
|
|
|
|
/// asynchronously to indicate completion. See comments on Request for
|
|
|
|
/// additional usage information.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual bool OnRequest(scoped_refptr<Request> request) = 0;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Called when a request has been canceled. It is still safe to dereference
|
|
|
|
/// |request| but any calls to Request::Continue or Request::Stop will be
|
|
|
|
/// ignored.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
virtual void OnRequestCanceled(scoped_refptr<Request> request) {}
|
|
|
|
|
2024-01-20 18:34:13 +01:00
|
|
|
virtual ~Provider() = default;
|
2015-05-23 03:57:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
CefResourceManager();
|
|
|
|
|
2021-12-06 22:59:14 +01:00
|
|
|
CefResourceManager(const CefResourceManager&) = delete;
|
|
|
|
CefResourceManager& operator=(const CefResourceManager&) = delete;
|
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Add a provider that maps requests for |url| to |content|. |url| should be
|
|
|
|
/// fully qualified but not include a query or fragment component. If
|
|
|
|
/// |mime_type| is empty the MimeTypeResolver will be used. See comments on
|
|
|
|
/// AddProvider for usage of the |order| and |identifier| parameters.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void AddContentProvider(const std::string& url,
|
|
|
|
const std::string& content,
|
|
|
|
const std::string& mime_type,
|
|
|
|
int order,
|
|
|
|
const std::string& identifier);
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Add a provider that maps requests that start with |url_path| to files
|
|
|
|
/// under |directory_path|. |url_path| should include an origin and optional
|
|
|
|
/// path component only. Files will be loaded when a matching URL is
|
|
|
|
/// requested. See comments on AddProvider for usage of the |order| and
|
|
|
|
/// |identifier| parameters.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void AddDirectoryProvider(const std::string& url_path,
|
|
|
|
const std::string& directory_path,
|
|
|
|
int order,
|
|
|
|
const std::string& identifier);
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Add a provider that maps requests that start with |url_path| to files
|
|
|
|
/// stored in the archive file at |archive_path|. |url_path| should include an
|
|
|
|
/// origin and optional path component only. The archive file will be loaded
|
|
|
|
/// when a matching URL is requested for the first time. See comments on
|
|
|
|
/// AddProvider for usage of the |order| and |identifier| parameters.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void AddArchiveProvider(const std::string& url_path,
|
|
|
|
const std::string& archive_path,
|
|
|
|
const std::string& password,
|
|
|
|
int order,
|
|
|
|
const std::string& identifier);
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Add a provider. This object takes ownership of |provider|. Providers will
|
|
|
|
/// be called in ascending order based on the |order| value. Multiple
|
|
|
|
/// providers sharing the same |order| value will be called in the order that
|
|
|
|
/// they were added. The |identifier| value, which does not need to be unique,
|
|
|
|
/// can be used to remove the provider at a later time.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void AddProvider(Provider* provider,
|
|
|
|
int order,
|
|
|
|
const std::string& identifier);
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Remove all providers with the specified |identifier| value. If any removed
|
|
|
|
/// providers have pending requests the Provider::OnRequestCancel method will
|
|
|
|
/// be called. The removed providers may be deleted immediately or at a later
|
|
|
|
/// time.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void RemoveProviders(const std::string& identifier);
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Remove all providers. If any removed providers have pending requests the
|
|
|
|
/// Provider::OnRequestCancel method will be called. The removed providers may
|
|
|
|
/// be deleted immediately or at a later time.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void RemoveAllProviders();
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Set the url filter. If not set the default no-op filter will be used.
|
|
|
|
/// Changes to this value will not affect currently pending requests.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void SetUrlFilter(const UrlFilter& filter);
|
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Set the mime type resolver. If not set the default resolver will be used.
|
|
|
|
/// Changes to this value will not affect currently pending requests.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
void SetMimeTypeResolver(const MimeTypeResolver& resolver);
|
|
|
|
|
2022-09-01 04:03:04 +02:00
|
|
|
/// The below methods should be called from other CEF handlers. They must be
|
|
|
|
/// called exactly as documented for the manager to function correctly.
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Called from CefRequestHandler::OnBeforeResourceLoad on the browser process
|
|
|
|
/// IO thread.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
2021-09-27 11:09:45 +02:00
|
|
|
cef_return_value_t OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback);
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
///
|
2022-09-01 04:03:04 +02:00
|
|
|
/// Called from CefRequestHandler::GetResourceHandler on the browser process
|
|
|
|
/// IO thread.
|
2015-05-23 03:57:31 +02:00
|
|
|
///
|
|
|
|
CefRefPtr<CefResourceHandler> GetResourceHandler(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Only allow deletion via scoped_refptr.
|
|
|
|
friend struct CefDeleteOnThread<TID_IO>;
|
|
|
|
friend class base::RefCountedThreadSafe<CefResourceManager,
|
|
|
|
CefDeleteOnIOThread>;
|
|
|
|
|
|
|
|
~CefResourceManager();
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
// Provider and associated information.
|
|
|
|
struct ProviderEntry;
|
2021-12-06 22:59:14 +01:00
|
|
|
using ProviderEntryList = std::list<ProviderEntry*>;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
// Values associated with the pending request only. Ownership will be passed
|
|
|
|
// between requests and the resource manager as request handling proceeds.
|
|
|
|
struct RequestState {
|
|
|
|
~RequestState();
|
|
|
|
|
|
|
|
base::WeakPtr<CefResourceManager> manager_;
|
|
|
|
|
|
|
|
// Callback to execute once request handling is complete.
|
2021-09-27 11:09:45 +02:00
|
|
|
CefRefPtr<CefCallback> callback_;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
// Position of the currently associated ProviderEntry in the |providers_|
|
|
|
|
// list.
|
|
|
|
ProviderEntryList::iterator current_entry_pos_;
|
|
|
|
|
|
|
|
// Position of this request object in the currently associated
|
|
|
|
// ProviderEntry's |pending_requests_| list.
|
|
|
|
RequestList::iterator current_request_pos_;
|
|
|
|
|
|
|
|
// Params that will be copied to each request object.
|
|
|
|
RequestParams params_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Methods that manage request state between requests. Called on the browser
|
|
|
|
// process IO thread.
|
2021-06-17 22:08:01 +02:00
|
|
|
bool SendRequest(std::unique_ptr<RequestState> state);
|
|
|
|
void ContinueRequest(std::unique_ptr<RequestState> state,
|
2015-05-23 03:57:31 +02:00
|
|
|
CefRefPtr<CefResourceHandler> handler);
|
2021-06-17 22:08:01 +02:00
|
|
|
void StopRequest(std::unique_ptr<RequestState> state);
|
2015-05-23 03:57:31 +02:00
|
|
|
bool IncrementProvider(RequestState* state);
|
|
|
|
void DetachRequestFromProvider(RequestState* state);
|
|
|
|
void GetNextValidProvider(ProviderEntryList::iterator& iterator);
|
|
|
|
void DeleteProvider(ProviderEntryList::iterator& iterator, bool stop);
|
|
|
|
|
|
|
|
// The below members are only accessed on the browser process IO thread.
|
|
|
|
|
|
|
|
// List of providers including additional associated information.
|
|
|
|
ProviderEntryList providers_;
|
|
|
|
|
|
|
|
// Map of response ID to pending CefResourceHandler object.
|
2023-06-01 16:06:15 +02:00
|
|
|
using PendingHandlersMap = std::map<uint64_t, CefRefPtr<CefResourceHandler>>;
|
2015-05-23 03:57:31 +02:00
|
|
|
PendingHandlersMap pending_handlers_;
|
|
|
|
|
|
|
|
UrlFilter url_filter_;
|
|
|
|
MimeTypeResolver mime_type_resolver_;
|
|
|
|
|
2015-10-14 18:40:56 +02:00
|
|
|
// Must be the last member. Created and accessed on the IO thread.
|
2021-06-17 22:08:01 +02:00
|
|
|
std::unique_ptr<base::WeakPtrFactory<CefResourceManager>> weak_ptr_factory_;
|
2015-05-23 03:57:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CEF_INCLUDE_WRAPPER_CEF_RESOURCE_MANAGER_H_
|