Bug 1869043 assert that graph set access is main thread only r=padenot
[gecko.git] / netwerk / cookie / CookieLogging.cpp
blob9f5262b2bbb737378d5599c6f911c8fe5fa80f6e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "CookieLogging.h"
7 #include "Cookie.h"
8 #include "nsIConsoleReportCollector.h"
10 constexpr auto TIME_STRING_LENGTH = 40;
12 namespace mozilla {
13 namespace net {
15 LazyLogModule gCookieLog("cookie");
17 static const char* SameSiteToString(uint32_t aSameSite) {
18 switch (aSameSite) {
19 case nsICookie::SAMESITE_NONE:
20 return "none";
21 case nsICookie::SAMESITE_LAX:
22 return "lax";
23 case nsICookie::SAMESITE_STRICT:
24 return "strict";
25 default:
26 MOZ_CRASH("Invalid nsICookie sameSite value");
27 return "";
31 // static
32 void CookieLogging::LogSuccess(bool aSetCookie, nsIURI* aHostURI,
33 const nsACString& aCookieString, Cookie* aCookie,
34 bool aReplacing) {
35 // if logging isn't enabled, return now to save cycles
36 if (!MOZ_LOG_TEST(gCookieLog, LogLevel::Debug)) {
37 return;
40 nsAutoCString spec;
41 if (aHostURI) {
42 aHostURI->GetAsciiSpec(spec);
45 MOZ_LOG(gCookieLog, LogLevel::Debug,
46 ("===== %s =====\n", aSetCookie ? "COOKIE ACCEPTED" : "COOKIE SENT"));
47 MOZ_LOG(gCookieLog, LogLevel::Debug, ("request URL: %s\n", spec.get()));
48 MOZ_LOG(gCookieLog, LogLevel::Debug,
49 ("cookie string: %s\n", aCookieString.BeginReading()));
50 if (aSetCookie) {
51 MOZ_LOG(gCookieLog, LogLevel::Debug,
52 ("replaces existing cookie: %s\n", aReplacing ? "true" : "false"));
55 LogCookie(aCookie);
57 MOZ_LOG(gCookieLog, LogLevel::Debug, ("\n"));
60 // static
61 void CookieLogging::LogFailure(bool aSetCookie, nsIURI* aHostURI,
62 const nsACString& aCookieString,
63 const char* aReason) {
64 // if logging isn't enabled, return now to save cycles
65 if (!MOZ_LOG_TEST(gCookieLog, LogLevel::Warning)) {
66 return;
69 nsAutoCString spec;
70 if (aHostURI) {
71 aHostURI->GetAsciiSpec(spec);
74 MOZ_LOG(gCookieLog, LogLevel::Warning,
75 ("===== %s =====\n",
76 aSetCookie ? "COOKIE NOT ACCEPTED" : "COOKIE NOT SENT"));
77 MOZ_LOG(gCookieLog, LogLevel::Warning, ("request URL: %s\n", spec.get()));
78 if (aSetCookie) {
79 MOZ_LOG(gCookieLog, LogLevel::Warning,
80 ("cookie string: %s\n", aCookieString.BeginReading()));
83 PRExplodedTime explodedTime;
84 PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime);
85 char timeString[TIME_STRING_LENGTH];
86 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
87 &explodedTime);
89 MOZ_LOG(gCookieLog, LogLevel::Warning, ("current time: %s", timeString));
90 MOZ_LOG(gCookieLog, LogLevel::Warning, ("rejected because %s\n", aReason));
91 MOZ_LOG(gCookieLog, LogLevel::Warning, ("\n"));
94 // static
95 void CookieLogging::LogCookie(Cookie* aCookie) {
96 PRExplodedTime explodedTime;
97 PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime);
98 char timeString[TIME_STRING_LENGTH];
99 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
100 &explodedTime);
102 MOZ_LOG(gCookieLog, LogLevel::Debug, ("current time: %s", timeString));
104 if (aCookie) {
105 MOZ_LOG(gCookieLog, LogLevel::Debug, ("----------------\n"));
106 MOZ_LOG(gCookieLog, LogLevel::Debug, ("name: %s\n", aCookie->Name().get()));
107 MOZ_LOG(gCookieLog, LogLevel::Debug,
108 ("value: %s\n", aCookie->Value().get()));
109 MOZ_LOG(gCookieLog, LogLevel::Debug,
110 ("%s: %s\n", aCookie->IsDomain() ? "domain" : "host",
111 aCookie->Host().get()));
112 MOZ_LOG(gCookieLog, LogLevel::Debug, ("path: %s\n", aCookie->Path().get()));
114 PR_ExplodeTime(aCookie->Expiry() * int64_t(PR_USEC_PER_SEC),
115 PR_GMTParameters, &explodedTime);
116 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
117 &explodedTime);
118 MOZ_LOG(gCookieLog, LogLevel::Debug,
119 ("expires: %s%s", timeString,
120 aCookie->IsSession() ? " (at end of session)" : ""));
122 PR_ExplodeTime(aCookie->CreationTime(), PR_GMTParameters, &explodedTime);
123 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
124 &explodedTime);
125 MOZ_LOG(gCookieLog, LogLevel::Debug, ("created: %s", timeString));
127 MOZ_LOG(gCookieLog, LogLevel::Debug,
128 ("is secure: %s\n", aCookie->IsSecure() ? "true" : "false"));
129 MOZ_LOG(gCookieLog, LogLevel::Debug,
130 ("is httpOnly: %s\n", aCookie->IsHttpOnly() ? "true" : "false"));
131 MOZ_LOG(gCookieLog, LogLevel::Debug,
132 ("sameSite: %s - rawSameSite: %s\n",
133 SameSiteToString(aCookie->SameSite()),
134 SameSiteToString(aCookie->RawSameSite())));
135 MOZ_LOG(
136 gCookieLog, LogLevel::Debug,
137 ("schemeMap %d (http: %s | https: %s | file: %s)\n",
138 aCookie->SchemeMap(),
139 (aCookie->SchemeMap() & nsICookie::SCHEME_HTTP ? "true" : "false"),
140 (aCookie->SchemeMap() & nsICookie::SCHEME_HTTPS ? "true" : "false"),
141 (aCookie->SchemeMap() & nsICookie::SCHEME_FILE ? "true" : "false")));
143 nsAutoCString suffix;
144 aCookie->OriginAttributesRef().CreateSuffix(suffix);
145 MOZ_LOG(gCookieLog, LogLevel::Debug,
146 ("origin attributes: %s\n",
147 suffix.IsEmpty() ? "{empty}" : suffix.get()));
151 // static
152 void CookieLogging::LogEvicted(Cookie* aCookie, const char* details) {
153 MOZ_LOG(gCookieLog, LogLevel::Debug, ("===== COOKIE EVICTED =====\n"));
154 MOZ_LOG(gCookieLog, LogLevel::Debug, ("%s\n", details));
156 LogCookie(aCookie);
158 MOZ_LOG(gCookieLog, LogLevel::Debug, ("\n"));
161 // static
162 void CookieLogging::LogMessageToConsole(nsIConsoleReportCollector* aCRC,
163 nsIURI* aURI, uint32_t aErrorFlags,
164 const nsACString& aCategory,
165 const nsACString& aMsg,
166 const nsTArray<nsString>& aParams) {
167 if (!aCRC) {
168 return;
171 nsAutoCString uri;
172 if (aURI) {
173 nsresult rv = aURI->GetSpec(uri);
174 if (NS_WARN_IF(NS_FAILED(rv))) {
175 return;
179 aCRC->AddConsoleReport(aErrorFlags, aCategory,
180 nsContentUtils::eNECKO_PROPERTIES, uri, 0, 0, aMsg,
181 aParams);
184 } // namespace net
185 } // namespace mozilla