#ifndef CONTENT_BROWSER_RENDERER_HOST_CODE_CACHE_HOST_IMPL_H_
#define CONTENT_BROWSER_RENDERER_HOST_CODE_CACHE_HOST_IMPL_H_
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "build/build_config.h"
#include "components/services/storage/public/mojom/cache_storage_control.mojom-forward.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/unique_receiver_set.h"
#include "net/base/network_isolation_key.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/loader/code_cache.mojom.h"
class GURL;
namespace content {
class GeneratedCodeCache;
class GeneratedCodeCacheContext;
class CONTENT_EXPORT CodeCacheHostImpl : public blink::mojom::CodeCacheHost {
public:
class ReceiverSet {
public:
explicit ReceiverSet(
scoped_refptr<GeneratedCodeCacheContext> generated_code_cache_context);
~ReceiverSet();
using CodeCacheHostReceiverHandler = base::OnceCallback<void(
CodeCacheHostImpl*,
mojo::ReceiverId,
mojo::UniqueReceiverSet<blink::mojom::CodeCacheHost>&)>;
void Add(int render_process_id,
const net::NetworkIsolationKey& nik,
const blink::StorageKey& storage_key,
mojo::PendingReceiver<blink::mojom::CodeCacheHost> receiver,
CodeCacheHostReceiverHandler handler);
void Add(int render_process_id,
const net::NetworkIsolationKey& nik,
const blink::StorageKey& storage_key,
mojo::PendingReceiver<blink::mojom::CodeCacheHost> receiver);
void Clear();
private:
scoped_refptr<GeneratedCodeCacheContext> generated_code_cache_context_;
std::unique_ptr<mojo::UniqueReceiverSet<blink::mojom::CodeCacheHost>,
base::OnTaskRunnerDeleter>
receiver_set_;
};
static std::unique_ptr<CodeCacheHostImpl> Create(
int render_process_id,
scoped_refptr<GeneratedCodeCacheContext> generated_code_cache_context,
const net::NetworkIsolationKey& nik,
const blink::StorageKey& storage_key);
CodeCacheHostImpl(const CodeCacheHostImpl&) = delete;
CodeCacheHostImpl& operator=(const CodeCacheHostImpl&) = delete;
~CodeCacheHostImpl() override;
void SetCacheStorageControlForTesting(
storage::mojom::CacheStorageControl* cache_storage_control);
static void SetUseEmptySecondaryKeyForTesting();
protected:
CodeCacheHostImpl(
int render_process_id,
scoped_refptr<GeneratedCodeCacheContext> generated_code_cache_context,
const net::NetworkIsolationKey& nik,
const blink::StorageKey& storage_key);
int render_process_id() const { return render_process_id_; }
GeneratedCodeCacheContext* generated_code_cache_context() {
return generated_code_cache_context_.get();
}
const net::NetworkIsolationKey& network_isolation_key() const {
return network_isolation_key_;
}
const blink::StorageKey& storage_key() const { return storage_key_; }
SEQUENCE_CHECKER(sequence_checker_);
private:
FRIEND_TEST_ALL_PREFIXES(CodeCacheHostImplTest,
PersistentCacheWriteAndReadFullIsolationSetup);
FRIEND_TEST_ALL_PREFIXES(CodeCacheHostImplTest,
PersistentCacheNoCachingWhenNoProperIsolation);
FRIEND_TEST_ALL_PREFIXES(
CodeCacheHostImplTest,
PersistentCacheLockedAndUnlockedProcessesShareNoData);
void DidGenerateCacheableMetadataInCacheStorage(
const GURL& url,
base::Time expected_response_time,
mojo_base::BigBuffer data,
const std::string& cache_storage_cache_name) override;
const int render_process_id_;
const scoped_refptr<GeneratedCodeCacheContext> generated_code_cache_context_;
const net::NetworkIsolationKey network_isolation_key_;
const blink::StorageKey storage_key_;
raw_ptr<storage::mojom::CacheStorageControl>
cache_storage_control_for_testing_ = nullptr;
};
}
#endif