1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Brought to you by number 42.
6
7 #include "net/cookies/cookie_options.h"
8
9 #include <tuple>
10
11 #include "base/metrics/histogram_functions.h"
12 #include "net/cookies/cookie_util.h"
13 #include "net/first_party_sets/same_party_context.h"
14
15 namespace net {
16
17 CookieOptions::SameSiteCookieContext
MakeInclusive()18 CookieOptions::SameSiteCookieContext::MakeInclusive() {
19 return SameSiteCookieContext(ContextType::SAME_SITE_STRICT,
20 ContextType::SAME_SITE_STRICT);
21 }
22
23 CookieOptions::SameSiteCookieContext
MakeInclusiveForSet()24 CookieOptions::SameSiteCookieContext::MakeInclusiveForSet() {
25 return SameSiteCookieContext(ContextType::SAME_SITE_LAX,
26 ContextType::SAME_SITE_LAX);
27 }
28
29 CookieOptions::SameSiteCookieContext::ContextType
GetContextForCookieInclusion() const30 CookieOptions::SameSiteCookieContext::GetContextForCookieInclusion() const {
31 DCHECK_LE(schemeful_context_, context_);
32
33 if (cookie_util::IsSchemefulSameSiteEnabled())
34 return schemeful_context_;
35
36 return context_;
37 }
38
39 const CookieOptions::SameSiteCookieContext::ContextMetadata&
GetMetadataForCurrentSchemefulMode() const40 CookieOptions::SameSiteCookieContext::GetMetadataForCurrentSchemefulMode()
41 const {
42 return cookie_util::IsSchemefulSameSiteEnabled() ? schemeful_metadata()
43 : metadata();
44 }
45
SetContextTypesForTesting(ContextType context_type,ContextType schemeful_context_type)46 void CookieOptions::SameSiteCookieContext::SetContextTypesForTesting(
47 ContextType context_type,
48 ContextType schemeful_context_type) {
49 context_ = context_type;
50 schemeful_context_ = schemeful_context_type;
51 }
52
CompleteEquivalenceForTesting(const SameSiteCookieContext & other) const53 bool CookieOptions::SameSiteCookieContext::CompleteEquivalenceForTesting(
54 const SameSiteCookieContext& other) const {
55 return (*this == other) && (metadata() == other.metadata()) &&
56 (schemeful_metadata() == other.schemeful_metadata());
57 }
58
operator ==(const CookieOptions::SameSiteCookieContext & lhs,const CookieOptions::SameSiteCookieContext & rhs)59 bool operator==(const CookieOptions::SameSiteCookieContext& lhs,
60 const CookieOptions::SameSiteCookieContext& rhs) {
61 return std::tie(lhs.context_, lhs.schemeful_context_) ==
62 std::tie(rhs.context_, rhs.schemeful_context_);
63 }
64
operator !=(const CookieOptions::SameSiteCookieContext & lhs,const CookieOptions::SameSiteCookieContext & rhs)65 bool operator!=(const CookieOptions::SameSiteCookieContext& lhs,
66 const CookieOptions::SameSiteCookieContext& rhs) {
67 return !(lhs == rhs);
68 }
69
operator ==(const CookieOptions::SameSiteCookieContext::ContextMetadata & lhs,const CookieOptions::SameSiteCookieContext::ContextMetadata & rhs)70 bool operator==(
71 const CookieOptions::SameSiteCookieContext::ContextMetadata& lhs,
72 const CookieOptions::SameSiteCookieContext::ContextMetadata& rhs) {
73 return std::tie(lhs.cross_site_redirect_downgrade,
74 lhs.redirect_type_bug_1221316) ==
75 std::tie(rhs.cross_site_redirect_downgrade,
76 rhs.redirect_type_bug_1221316);
77 }
78
operator !=(const CookieOptions::SameSiteCookieContext::ContextMetadata & lhs,const CookieOptions::SameSiteCookieContext::ContextMetadata & rhs)79 bool operator!=(
80 const CookieOptions::SameSiteCookieContext::ContextMetadata& lhs,
81 const CookieOptions::SameSiteCookieContext::ContextMetadata& rhs) {
82 return !(lhs == rhs);
83 }
84
85 // Keep default values in sync with
86 // services/network/public/mojom/cookie_manager.mojom.
CookieOptions()87 CookieOptions::CookieOptions()
88 : same_site_cookie_context_(SameSiteCookieContext(
89 SameSiteCookieContext::ContextType::CROSS_SITE)) {}
90
91 CookieOptions::CookieOptions(const CookieOptions& other) = default;
92 CookieOptions::CookieOptions(CookieOptions&& other) = default;
93 CookieOptions::~CookieOptions() = default;
94
95 CookieOptions& CookieOptions::operator=(const CookieOptions&) = default;
96 CookieOptions& CookieOptions::operator=(CookieOptions&&) = default;
97
98 // static
MakeAllInclusive()99 CookieOptions CookieOptions::MakeAllInclusive() {
100 CookieOptions options;
101 options.set_include_httponly();
102 options.set_same_site_cookie_context(SameSiteCookieContext::MakeInclusive());
103 options.set_do_not_update_access_time();
104 options.set_same_party_context(SamePartyContext::MakeInclusive());
105 options.set_is_in_nontrivial_first_party_set(true);
106 return options;
107 }
108
109 } // namespace net
110