1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_CODE_CACHE_GENERATED_CODE_CACHE_H_
#define CONTENT_BROWSER_CODE_CACHE_GENERATED_CODE_CACHE_H_
#include <map>
#include "base/containers/queue.h"
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "content/browser/code_cache/simple_lru_cache.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "net/base/io_buffer.h"
#include "net/base/network_isolation_key.h"
#include "net/disk_cache/disk_cache.h"
#include "url/origin.h"
namespace content {
// Cache for storing generated code from the renderer on the disk. This cache
// uses |resource_url| + |origin_lock| as a key for storing the generated code.
// |resource_url| is the url corresponding to the requested resource.
// |origin_lock| is the origin that the renderer which requested this resource
// is locked to. This is used to enforce site isolation policy on cached code.
// For example, if SitePerProcess is enabled and http://script.com/script1.js is
// requested by http://example.com, then http://script.com/script.js is the
// resource_url and http://example.com is the origin_lock.
//
// The key is generated by concatenating the serialized url and origin lock
// with a separator in between. The separator is non-valid URL characters, to
// prevent any attacks by crafting the URLs. |origin_lock| could be empty when
// renderer is not locked to an origin (ex:SitePerProcess is disabled) and it
// is safe to use only |resource_url| as the key in such cases.
//
// This uses a simple disk_cache backend. It just stores one data stream and
// stores response_time + generated code as one data blob.
//
// There exists one cache per storage partition and is owned by the storage
// partition. This cache is created, accessed and destroyed on the I/O
// thread.
class CONTENT_EXPORT GeneratedCodeCache {
public:
using ReadDataCallback =
base::OnceCallback<void(const base::Time&, mojo_base::BigBuffer data)>;
using GetBackendCallback = base::OnceCallback<void(disk_cache::Backend*)>;
// Cache type. Used for collecting statistics for JS and Wasm in separate
// buckets.
enum CodeCacheType {
// JavaScript from http(s) pages.
kJavaScript,
// WebAssembly from http(s) pages. This cache allows more total size and
// more size per item than the JavaScript cache, since some
// WebAssembly programs are very large.
kWebAssembly,
// JavaScript from chrome and chrome-untrusted pages. The resource URLs are
// limited to only those fetched via chrome and chrome-untrusted schemes.
// The cache size is limited to disk_cache::kMaxWebUICodeCacheSize.
// Deduplication of very large items is disabled in this cache.
kWebUIJavaScript,
};
// Used for collecting statistics about cache behaviour.
// Since it's uploaded to UMA, its values must never change.
enum CacheEntryStatus : uint8_t {
kHit,
kMiss,
kClear,
kUpdate,
kCreate,
kError,
kIncompleteEntry,
kWriteFailed,
kMaxValue = kWriteFailed
};
// Returns the resource URL from the key. The key has the format prefix +
// resource URL + separator + requesting origin. This function extracts and
// returns resource URL from the key, or the empty string if key is invalid.
static std::string GetResourceURLFromKey(const std::string& key);
// Creates a GeneratedCodeCache with the specified path and the maximum size.
// If |max_size_bytes| is 0, then disk_cache picks a default size based on
// some heuristics.
GeneratedCodeCache(const base::FilePath& path,
int max_size_bytes,
CodeCacheType cache_type);
GeneratedCodeCache(const GeneratedCodeCache&) = delete;
GeneratedCodeCache& operator=(const GeneratedCodeCache&) = delete;
~GeneratedCodeCache();
// Runs the callback with a raw pointer to the backend. If we could not create
// the backend then it will return a null. This runs the callback
// synchronously if the backend is already open or asynchronously on the
// completion of a pending backend creation.
void GetBackend(GetBackendCallback callback);
// Writes data to the cache. If there is an entry corresponding to
// <|resource_url|, |origin_lock|> this overwrites the existing data. If
// there is no entry it creates a new one.
void WriteEntry(const GURL& resource_url,
const GURL& origin_lock,
const net::NetworkIsolationKey& nik,
const base::Time& response_time,
mojo_base::BigBuffer data);
// Fetch entry corresponding to <resource_url, origin_lock> from the cache
// and return it using the ReadDataCallback.
void FetchEntry(const GURL& resource_url,
const GURL& origin_lock,
const net::NetworkIsolationKey& nik,
ReadDataCallback);
// Delete the entry corresponding to <resource_url, origin_lock>
void DeleteEntry(const GURL& resource_url,
const GURL& origin_lock,
const net::NetworkIsolationKey& nik);
// Should be only used for tests. Sets the last accessed timestamp of an
// entry.
void SetLastUsedTimeForTest(const GURL& resource_url,
const GURL& origin_lock,
const net::NetworkIsolationKey& nik,
base::Time time,
base::OnceClosure callback);
// Clears the in-memory cache.
void ClearInMemoryCache();
void CollectStatisticsForTest(const GURL& resource_url,
const GURL& origin_lock,
GeneratedCodeCache::CacheEntryStatus status);
const base::FilePath& path() const { return path_; }
private:
class PendingOperation;
// State of the backend.
enum BackendState { kInitializing, kInitialized, kFailed };
// The operation requested.
enum Operation {
kFetch,
kFetchWithSHAKey,
kWrite,
kWriteWithSHAKey,
kDelete,
kGetBackend
};
// Data streams corresponding to each entry.
enum { kSmallDataStream = 0, kLargeDataStream = 1 };
// Creates a simple_disk_cache backend.
void CreateBackend();
void DidCreateBackend(disk_cache::BackendResult result);
// Adds operation to the appropriate queue.
void EnqueueOperation(std::unique_ptr<PendingOperation> op);
// Issues ops that were received while the backend was being initialized.
void IssuePendingOperations();
void IssueOperation(PendingOperation* op);
// Writes entry to cache.
void WriteEntryImpl(PendingOperation* op);
void OpenCompleteForWrite(PendingOperation* op,
disk_cache::EntryResult result);
void WriteSmallBufferComplete(PendingOperation* op, int rv);
void WriteLargeBufferComplete(PendingOperation* op, int rv);
void WriteComplete(PendingOperation* op);
// Fetches entry from cache.
void FetchEntryImpl(PendingOperation* op);
void OpenCompleteForRead(PendingOperation* op,
disk_cache::EntryResult result);
void ReadSmallBufferComplete(PendingOperation* op, int rv);
void ReadLargeBufferComplete(PendingOperation* op, int rv);
void ReadComplete(PendingOperation* op);
// Deletes entry from cache.
void DeleteEntryImpl(PendingOperation* op);
void DoomEntry(PendingOperation* op);
// Issues the next operation on the queue for |key|.
void IssueNextOperation(const std::string& key);
// Removes |op| and issues the next operation on its queue.
void CloseOperationAndIssueNext(PendingOperation* op);
// Enqueues the operation issues it if there are no pending operations for
// its key.
void EnqueueOperationAndIssueIfNext(std::unique_ptr<PendingOperation> op);
// Dequeues the operation and transfers ownership to caller.
std::unique_ptr<PendingOperation> DequeueOperation(PendingOperation* op);
void DoPendingGetBackend(PendingOperation* op);
void OpenCompleteForSetLastUsedForTest(base::Time time,
base::OnceClosure callback,
disk_cache::EntryResult result);
void CollectStatistics(const GURL& resource_url,
const GURL& origin_lock,
GeneratedCodeCache::CacheEntryStatus status);
// Whether very large cache entries are deduplicated in this cache.
// Deduplication is disabled in the WebUI code cache, as an additional defense
// against privilege escalation in case there is a bug in the deduplication
// logic.
bool IsDeduplicationEnabled() const;
bool ShouldDeduplicateEntry(uint32_t data_size) const;
// Checks that the header data in the small buffer is valid. We may read cache
// entries that were written by a previous version of Chrome which uses
// obsolete formats. These reads should fail and be doomed as soon as
// possible.
bool IsValidHeader(scoped_refptr<net::IOBufferWithSize> small_buffer) const;
std::unique_ptr<disk_cache::Backend> backend_;
BackendState backend_state_;
// Queue for operations received while initializing the backend.
using PendingOperationQueue = base::queue<std::unique_ptr<PendingOperation>>;
PendingOperationQueue pending_ops_;
// Map from key to queue of pending operations.
std::map<std::string, PendingOperationQueue> active_entries_map_;
base::FilePath path_;
int max_size_bytes_;
CodeCacheType cache_type_;
// A hypothetical memory-backed code cache. Used to collect UMAs.
SimpleLruCache lru_cache_;
static constexpr int64_t kLruCacheCapacity = 50 * 1024 * 1024;
base::WeakPtrFactory<GeneratedCodeCache> weak_ptr_factory_{this};
};
} // namespace content
#endif // CONTENT_BROWSER_CODE_CACHE_GENERATED_CODE_CACHE_H_
|