summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/cookie_store/cookie_change_event.cc
blob: f02bb75b83f35a1c9e9e21bc5285c220c55918b3 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/cookie_store/cookie_change_event.h"

#include <utility>

#include "services/network/public/mojom/cookie_manager.mojom-blink.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_cookie_change_event_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_cookie_list_item.h"
#include "third_party/blink/renderer/core/dom/dom_time_stamp.h"
#include "third_party/blink/renderer/modules/event_modules.h"
#include "third_party/blink/renderer/platform/cookie/canonical_cookie.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

CookieChangeEvent::~CookieChangeEvent() = default;

const AtomicString& CookieChangeEvent::InterfaceName() const {
  return event_interface_names::kCookieChangeEvent;
}

void CookieChangeEvent::Trace(Visitor* visitor) {
  Event::Trace(visitor);
  visitor->Trace(changed_);
  visitor->Trace(deleted_);
}

CookieChangeEvent::CookieChangeEvent() = default;

CookieChangeEvent::CookieChangeEvent(const AtomicString& type,
                                     HeapVector<Member<CookieListItem>> changed,
                                     HeapVector<Member<CookieListItem>> deleted)
    : Event(type, Bubbles::kNo, Cancelable::kNo),
      changed_(std::move(changed)),
      deleted_(std::move(deleted)) {}

CookieChangeEvent::CookieChangeEvent(const AtomicString& type,
                                     const CookieChangeEventInit* initializer)
    : Event(type, initializer) {
  if (initializer->hasChanged())
    changed_ = initializer->changed();
  if (initializer->hasDeleted())
    deleted_ = initializer->deleted();
}

namespace {

String ToCookieListItemSameSite(network::mojom::CookieSameSite same_site) {
  switch (same_site) {
    case network::mojom::CookieSameSite::STRICT_MODE:
      return "strict";
    case network::mojom::CookieSameSite::LAX_MODE:
      return "lax";
    case network::mojom::CookieSameSite::NO_RESTRICTION:
      return "none";
    case network::mojom::CookieSameSite::UNSPECIFIED:
      return "unspecified";
  }

  NOTREACHED();
}

}  // namespace

// static
CookieListItem* CookieChangeEvent::ToCookieListItem(
    const CanonicalCookie& canonical_cookie,
    bool is_deleted) {
  CookieListItem* list_item = CookieListItem::Create();

  list_item->setName(canonical_cookie.Name());
  list_item->setPath(canonical_cookie.Path());
  list_item->setSecure(canonical_cookie.IsSecure());
  list_item->setSameSite(ToCookieListItemSameSite(canonical_cookie.SameSite()));

  // The domain of host-only cookies is the host name, without a dot (.) prefix.
  String cookie_domain = canonical_cookie.Domain();
  if (cookie_domain.StartsWith("."))
    list_item->setDomain(cookie_domain.Substring(1));

  if (!is_deleted) {
    list_item->setValue(canonical_cookie.Value());
    if (!canonical_cookie.ExpiryDate().is_null()) {
      list_item->setExpires(ConvertSecondsToDOMTimeStamp(
          canonical_cookie.ExpiryDate().ToDoubleT()));
    }
  }
  return list_item;
}

// static
void CookieChangeEvent::ToEventInfo(
    const CanonicalCookie& backend_cookie,
    ::network::mojom::CookieChangeCause change_cause,
    HeapVector<Member<CookieListItem>>& changed,
    HeapVector<Member<CookieListItem>>& deleted) {
  switch (change_cause) {
    case ::network::mojom::CookieChangeCause::INSERTED: {
      CookieListItem* cookie =
          ToCookieListItem(backend_cookie, false /* is_deleted */);
      changed.push_back(cookie);
      break;
    }
    case ::network::mojom::CookieChangeCause::EXPLICIT:
    case ::network::mojom::CookieChangeCause::UNKNOWN_DELETION:
    case ::network::mojom::CookieChangeCause::EXPIRED:
    case ::network::mojom::CookieChangeCause::EVICTED:
    case ::network::mojom::CookieChangeCause::EXPIRED_OVERWRITE: {
      CookieListItem* cookie =
          ToCookieListItem(backend_cookie, true /* is_deleted */);
      deleted.push_back(cookie);
      break;
    }

    case ::network::mojom::CookieChangeCause::OVERWRITE:
      // A cookie overwrite causes an OVERWRITE (meaning the old cookie was
      // deleted) and an INSERTED.
      break;
  }
}

}  // namespace blink