// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Defines the public interface of the disk cache. For more details see // http://dev.chromium.org/developers/design-documents/network-stack/disk-cache #ifndef NET_DISK_CACHE_DISK_CACHE_H_ #define NET_DISK_CACHE_DISK_CACHE_H_ #include <stdint.h> #include <memory> #include <optional> #include <string> #include <vector> #include "base/files/file.h" #include "base/memory/ref_counted.h" #include "base/strings/string_split.h" #include "base/task/sequenced_task_runner.h" #include "base/time/time.h" #include "build/build_config.h" #include "net/base/cache_type.h" #include "net/base/completion_once_callback.h" #include "net/base/net_errors.h" #include "net/base/net_export.h" #include "net/base/request_priority.h" namespace base { class FilePath; namespace android { class ApplicationStatusListener; } // namespace android } // namespace base namespace net { class IOBuffer; class NetLog; } namespace disk_cache { class Entry; class Backend; class EntryResult; class BackendFileOperationsFactory; struct RangeResult; EntryResultCallback; RangeResultCallback; // How to handle resetting the back-end cache from the previous session. // See CreateCacheBackend() for its usage. enum class ResetHandling { … }; struct NET_EXPORT BackendResult { … }; BackendResultCallback; // Returns an instance of a Backend of the given `type`. `file_operations` // (nullable) is used to broker file operations in sandboxed environments. // Currently `file_operations` is only used for the simple backend. // `path` points to a folder where the cached data will be stored (if // appropriate). This cache instance must be the only object that will be // reading or writing files to that folder (if another one exists, and `type` is // not net::DISK_CACHE this operation will not complete until the previous // duplicate gets destroyed and finishes all I/O). The returned object should be // deleted when not needed anymore. // // If `reset_handling` is set to kResetOnError and there is a problem with the // cache initialization, the files will be deleted and a new set will be // created. If it's set to kReset, this will happen even if there isn't a // problem with cache initialization. Finally, if it's set to kNeverReset, the // cache creation will fail if there is a problem with cache initialization. // // `max_bytes` is the maximum size the cache can grow to. If zero is passed in // as `max_bytes`, the cache will determine the value to use. // // `net_error` in return value of the function is a net error code. If it is // ERR_IO_PENDING, the `callback` will be invoked when a backend is available or // a fatal error condition is reached. `backend` in return value or parameter // to callback can be nullptr if a fatal error is found. NET_EXPORT BackendResult CreateCacheBackend(net::CacheType type, net::BackendType backend_type, scoped_refptr<BackendFileOperationsFactory> file_operations, const base::FilePath& path, int64_t max_bytes, ResetHandling reset_handling, net::NetLog* net_log, BackendResultCallback callback); // Note: this is permitted to return nullptr when things are in process of // shutting down. ApplicationStatusListenerGetter; #if BUILDFLAG(IS_ANDROID) // Similar to the function above, but takes an |app_status_listener_getter| // which is used to listen for when the Android application status changes, so // we can flush the cache to disk when the app goes to the background. NET_EXPORT BackendResult CreateCacheBackend(net::CacheType type, net::BackendType backend_type, scoped_refptr<BackendFileOperationsFactory> file_operations, const base::FilePath& path, int64_t max_bytes, ResetHandling reset_handling, net::NetLog* net_log, BackendResultCallback callback, ApplicationStatusListenerGetter app_status_listener_getter); #endif // Variant of the above that calls |post_cleanup_callback| once all the I/O // that was in flight has completed post-destruction. |post_cleanup_callback| // will get invoked even if the creation fails. The invocation will always be // via the event loop, and never direct. // // This is currently unsupported for |type| == net::DISK_CACHE. // // Note that this will not wait for |post_cleanup_callback| of a previous // instance for |path| to run. NET_EXPORT BackendResult CreateCacheBackend(net::CacheType type, net::BackendType backend_type, scoped_refptr<BackendFileOperationsFactory> file_operations, const base::FilePath& path, int64_t max_bytes, ResetHandling reset_handling, net::NetLog* net_log, base::OnceClosure post_cleanup_callback, BackendResultCallback callback); // This will flush any internal threads used by backends created w/o an // externally injected thread specified, so tests can be sure that all I/O // has finished before inspecting the world. NET_EXPORT void FlushCacheThreadForTesting(); // Async version of FlushCacheThreadForTesting. `callback` will be called on // the calling sequence. NET_EXPORT void FlushCacheThreadAsynchronouslyForTesting( base::OnceClosure cllback); // The root interface for a disk cache instance. class NET_EXPORT Backend { … }; // This interface represents an entry in the disk cache. class NET_EXPORT Entry { … }; struct EntryDeleter { … }; // Automatically closes an entry when it goes out of scope. // Warning: Be careful. Automatically closing may not be the desired behavior // when writing to an entry. You may wish to doom first (e.g., in case writing // hasn't yet completed but the browser is shutting down). ScopedEntryPtr; // Represents the result of an entry open or create operation. // This is a move-only, owning type, which will close the entry it owns unless // it's released from it via ReleaseEntry (or it's moved away from). class NET_EXPORT EntryResult { … }; // Represents a result of GetAvailableRange. struct NET_EXPORT RangeResult { … }; // The maximum size of cache that can be created for type // GENERATED_WEBUI_BYTE_CODE_CACHE. There are only a handful of commonly // accessed WebUI pages, which can each cache 0.5 - 1.5 MB of code. There is no // point in having a very large WebUI code cache, even if lots of disk space is // available. constexpr int kMaxWebUICodeCacheSize = …; class UnboundBackendFileOperations; // An interface to provide file operations so that the HTTP cache works on // a sandboxed process. // All the paths must be absolute paths. // A BackendFileOperations object is bound to a sequence. class BackendFileOperations { … }; // BackendFileOperations which is not yet bound to a sequence. class UnboundBackendFileOperations { … }; // A factory interface that creates BackendFileOperations. class BackendFileOperationsFactory : public base::RefCounted<BackendFileOperationsFactory> { … }; // A trivial BackendFileOperations implementation which uses corresponding // base functions. class NET_EXPORT TrivialFileOperations final : public BackendFileOperations { … }; class NET_EXPORT TrivialFileOperationsFactory : public BackendFileOperationsFactory { … }; } // namespace disk_cache #endif // NET_DISK_CACHE_DISK_CACHE_H_