cookie_options.cc 3.7 KB

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