summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cookiebanners/nsCookieBannerRule.cpp
blob: 47f3fe7ac2650265c83dfaea2b2909a4d5b43582 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsCookieBannerRule.h"

#include "mozilla/Logging.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsClickRule.h"
#include "nsCookieRule.h"

namespace mozilla {

NS_IMPL_ISUPPORTS(nsCookieBannerRule, nsICookieBannerRule)

LazyLogModule gCookieRuleLog("nsCookieBannerRule");

NS_IMETHODIMP
nsCookieBannerRule::ClearCookies() {
  mCookiesOptOut.Clear();
  mCookiesOptIn.Clear();

  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::AddCookie(bool aIsOptOut, const nsACString& aName,
                              const nsACString& aValue, const nsACString& aHost,
                              const nsACString& aPath, int64_t aExpiryRelative,
                              const nsACString& aUnsetValue, bool aIsSecure,
                              bool aIsHttpOnly, bool aIsSession,
                              int32_t aSameSite,
                              nsICookie::schemeType aSchemeMap) {
  LogRule(gCookieRuleLog, "AddCookie:", this, LogLevel::Debug);
  MOZ_LOG(
      gCookieRuleLog, LogLevel::Debug,
      ("%s: aIsOptOut: %d, aHost: %s, aName: %s", __FUNCTION__, aIsOptOut,
       nsPromiseFlatCString(aHost).get(), nsPromiseFlatCString(aName).get()));

  // Create and insert cookie rule.
  nsCOMPtr<nsICookieRule> cookieRule = new nsCookieRule(
      aIsOptOut, aName, aValue, aHost, aPath, aExpiryRelative, aUnsetValue,
      aIsSecure, aIsHttpOnly, aIsSession, aSameSite, aSchemeMap);
  Cookies(aIsOptOut).AppendElement(cookieRule);

  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::GetId(nsACString& aId) {
  aId.Assign(mId);
  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::SetId(const nsACString& aId) {
  mId.Assign(aId);
  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::GetDomains(nsTArray<nsCString>& aDomains) {
  aDomains.Clear();

  AppendToArray(aDomains, mDomains);

  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::SetDomains(const nsTArray<nsCString>& aDomains) {
  AppendToArray(mDomains, aDomains);

  return NS_OK;
}

nsTArray<nsCOMPtr<nsICookieRule>>& nsCookieBannerRule::Cookies(bool isOptOut) {
  if (isOptOut) {
    return mCookiesOptOut;
  }
  return mCookiesOptIn;
}

NS_IMETHODIMP
nsCookieBannerRule::GetCookies(bool aIsOptOut, const nsACString& aDomain,
                               nsTArray<RefPtr<nsICookieRule>>& aCookies) {
  nsTArray<nsCOMPtr<nsICookieRule>>& cookies = Cookies(aIsOptOut);
  for (nsICookieRule* cookie : cookies) {
    // If we don't need to set a domain we can simply return the existing rule.
    if (aDomain.IsEmpty()) {
      aCookies.AppendElement(cookie);
      continue;
    }
    // Otherwise get a copy.
    nsCOMPtr<nsICookieRule> ruleForDomain;
    nsresult rv = cookie->CopyForDomain(aDomain, getter_AddRefs(ruleForDomain));
    NS_ENSURE_SUCCESS(rv, rv);
    aCookies.AppendElement(ruleForDomain.forget());
  }
  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::GetCookiesOptOut(
    nsTArray<RefPtr<nsICookieRule>>& aCookies) {
  return GetCookies(true, ""_ns, aCookies);
}

NS_IMETHODIMP
nsCookieBannerRule::GetCookiesOptIn(nsTArray<RefPtr<nsICookieRule>>& aCookies) {
  return GetCookies(false, ""_ns, aCookies);
}

NS_IMETHODIMP
nsCookieBannerRule::GetClickRule(nsIClickRule** aClickRule) {
  NS_IF_ADDREF(*aClickRule = mClickRule);
  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::AddClickRule(const nsACString& aPresence,
                                 const bool aSkipPresenceVisibilityCheck,
                                 const nsIClickRule::RunContext aRunContext,
                                 const nsACString& aHide,
                                 const nsACString& aOptOut,
                                 const nsACString& aOptIn) {
  mClickRule = MakeRefPtr<nsClickRule>(aPresence, aSkipPresenceVisibilityCheck,
                                       aRunContext, aHide, aOptOut, aOptIn);
  return NS_OK;
}

NS_IMETHODIMP
nsCookieBannerRule::ClearClickRule() {
  mClickRule = nullptr;

  return NS_OK;
}

// Static
void nsCookieBannerRule::LogRule(LazyLogModule& aLogger, const char* aMessage,
                                 nsICookieBannerRule* aRule,
                                 LogLevel aLogLevel) {
  NS_ENSURE_TRUE_VOID(aMessage);
  NS_ENSURE_TRUE_VOID(aRule);

  // Exit early if logging is disabled for the given log-level.
  if (!MOZ_LOG_TEST(aLogger, aLogLevel)) {
    return;
  }

  nsAutoCString id;
  nsresult rv = aRule->GetId(id);
  NS_ENSURE_SUCCESS_VOID(rv);

  nsTArray<nsCString> domains;
  rv = aRule->GetDomains(domains);
  NS_ENSURE_SUCCESS_VOID(rv);

  // Create a comma delimited string of domains this rule supports.
  nsAutoCString domainsStr("*");
  for (const nsCString& domain : domains) {
    if (domainsStr.EqualsLiteral("*")) {
      domainsStr.Truncate();
    } else {
      domainsStr.AppendLiteral(",");
    }
    domainsStr.Append(domain);
  }

  MOZ_LOG(aLogger, aLogLevel,
          ("%s Rule: id=%s; domains=[%s]; isGlobal: %d", aMessage, id.get(),
           PromiseFlatCString(domainsStr).get(), domains.IsEmpty()));
}

}  // namespace mozilla