[go: nahoru, domu]

blob: e0b864d52e2f215a6633201eb2743a67f17c1ad3 [file] [log] [blame]
// 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_COOKIE_JAR_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_COOKIE_JAR_H_
#include <optional>
#include "services/network/public/mojom/restricted_cookie_manager.mojom-blink.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
class Document;
class CookieJar : public GarbageCollected<CookieJar> {
public:
explicit CookieJar(blink::Document* document);
virtual ~CookieJar();
void Trace(Visitor* visitor) const;
void SetCookie(const String& value);
String Cookies();
bool CookiesEnabled();
void SetCookieManager(
mojo::PendingRemote<network::mojom::blink::RestrictedCookieManager>
cookie_manager);
// Invalidate cached string. To be called explicitly from Document. This is
// used in cases where a Document action could change the ability for
// CookieJar to return values to JS without changing the value of the cookies
// themselves. For example changing storage access can stop the JS from being
// able to access the document's Cookie without the value ever changing. In
// that case it's faulty to treat a subsequent request as a cache hit so we
// invalidate.
void InvalidateCache();
private:
void RequestRestrictedCookieManagerIfNeeded();
void OnBackendDisconnect();
uint64_t GetSharedCookieVersion();
// Returns true if last_cookies_ is not guaranteed to be up to date and an IPC
// is needed to get the current cookie string.
bool IPCNeeded();
// Updates the fake cookie cache after a
// RestrictedCookieManager::GetCookiesString request returns.
//
// We want to evaluate the possible performance gain from having a cookie
// cache. There is no real cache right now and this class just stores a hash
// to determine if the current request could have been served from a real
// cache.
void UpdateCacheAfterGetRequest(const KURL& cookie_url,
const String& cookie_string,
uint64_t new_version);
HeapMojoRemote<network::mojom::blink::RestrictedCookieManager> backend_;
Member<blink::Document> document_;
// Hash used to determine if the value returned by a call to
// RestrictedCookieManager::GetCookiesString is the same as a previous one.
// Used to answer the question: "had we keep the last cookie_string around
// would it have been possible to return that instead of making a new IPC?".
// Combines hashes for the `cookie_string` returned by the call and the
// `cookie_url` used as a parameter to the call.
//
// ATTENTION: Just use hashes for now to keep space overhead low, but more
// importantly, because keeping cookies around is tricky from a security
// perspective.
std::optional<unsigned> last_cookies_hash_;
// Whether the last operation performed on this jar was a set or get. Used
// along with `last_cookies_hash_` when updating the histogram that tracks
// cookie access results.
bool last_operation_was_set_{false};
bool shared_memory_initialized_ = false;
base::ReadOnlySharedMemoryRegion mapped_region_;
base::ReadOnlySharedMemoryMapping mapping_;
uint64_t last_version_ = network::mojom::blink::kInvalidCookieVersion;
// Last received cookie string. Null if there is no last cached-version. Can
// be empty since that is a valid cookie string.
String last_cookies_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_COOKIE_JAR_H_