File: cookie_jar.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (132 lines) | stat: -rw-r--r-- 5,786 bytes parent folder | download | duplicates (5)
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
// 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 "mojo/public/cpp/base/shared_memory_version.h"
#include "services/network/public/mojom/restricted_cookie_manager.mojom-blink.h"
#include "third_party/blink/renderer/core/core_export.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;
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange(FirstCookieRequest)
enum class FirstCookieRequest {
  kFirstOperationWasSet = 0,
  kFirstOperationWasGet = 1,
  kFirstOperationWasCookiesEnabled = 2,
  kMaxValue = kFirstOperationWasCookiesEnabled,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/blink/enums.xml:FirstCookieRequest)

class CORE_EXPORT 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:
  using CookiesResponsePtr = network::mojom::blink::CookiesResponsePtr;

  void OnSetCookieResponse(const KURL& cookie_url,
                           const bool apply_devtools_overrides,
                           CookiesResponsePtr response);

  void RequestRestrictedCookieManagerIfNeeded();
  void OnBackendDisconnect();

  // 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(bool should_apply_devtools_overrides);

  // 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);

  // This mechanism is designed to capture and isolate only the very first
  // request to cookie. We specifically focus on whether this initial action is
  // a GET or a SET operation or check to CookiesEnabled.

  // We want to evaluate the possible performance gain of returning the local
  // cache version and/or cookie string on SET. Especially, if SET is the first
  // request.
  void LogFirstCookieRequest(FirstCookieRequest first_cookie_request);

  // Checks with probe function if devtools is active. If so, devtools overrides
  // are applied to the cookie operation.
  bool ShouldApplyDevtoolsOverrides() const;

  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};

  std::optional<mojo::SharedMemoryVersionClient> shared_memory_version_client_;
  uint64_t last_version_ = mojo::shared_memory_version::kInvalidVersion;

  // Last decision of if devtools overrides needed to be applied. If the
  // decision changes, IPC is needed to get cookie with new devtools overrides
  bool last_devtools_overrides_were_applied = false;

  // 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_;
  bool is_first_operation_ = true;

  // The number of required writes (via SetCookieFomString) that should be
  // observed by the network service in order to skip an IPC in Cookies().
  // This number increments when calling SetCookieFromString asynchronously
  // and it is compared with the committed_writes_count in shared memory.
  mojo::CountType required_committed_writes_ = 0;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_COOKIE_JAR_H_