Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / reporting / ReportingHeader.h
blob3c0497454bad0dd07c45c0c9d1178d41e1cf814d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_ReportingHeader_h
8 #define mozilla_dom_ReportingHeader_h
10 #include "mozilla/TimeStamp.h"
11 #include "nsClassHashtable.h"
12 #include "nsIObserver.h"
13 #include "nsITimer.h"
14 #include "nsTObserverArray.h"
16 class nsIHttpChannel;
17 class nsIPrincipal;
18 class nsIURI;
20 namespace mozilla {
22 class OriginAttributesPattern;
24 namespace ipc {
25 class PrincipalInfo;
28 namespace dom {
30 class ReportingHeader final : public nsIObserver,
31 public nsITimerCallback,
32 public nsINamed {
33 public:
34 NS_DECL_ISUPPORTS
35 NS_DECL_NSIOBSERVER
36 NS_DECL_NSITIMERCALLBACK
37 NS_DECL_NSINAMED
39 static void Initialize();
41 // Exposed structs for gtests
43 struct Endpoint {
44 nsCOMPtr<nsIURI> mUrl;
45 nsCString mEndpointName;
46 uint32_t mPriority;
47 uint32_t mWeight;
48 uint32_t mFailures;
51 struct Group {
52 nsString mName;
53 bool mIncludeSubdomains;
54 int32_t mTTL;
55 TimeStamp mCreationTime;
56 nsTObserverArray<Endpoint> mEndpoints;
59 struct Client {
60 nsTObserverArray<Group> mGroups;
63 // Parses the Reporting-Endpoints of a given header according to the algorithm
64 // in https://www.w3.org/TR/reporting-1/#header
65 static UniquePtr<Client> ParseReportingEndpointsHeader(
66 const nsACString& aHeaderValue, nsIURI* aURI);
68 // [Deprecated] Parses the contents of a given header according to the
69 // algorithm in https://www.w3.org/TR/2018/WD-reporting-1-20180925/#header
70 static UniquePtr<Client> ParseReportToHeader(nsIHttpChannel* aChannel,
71 nsIURI* aURI,
72 const nsACString& aHeaderValue);
74 static void GetEndpointForReport(
75 const nsAString& aGroupName,
76 const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
77 nsACString& aEndpointURI);
79 static void GetEndpointForReport(const nsAString& aGroupName,
80 nsIPrincipal* aPrincipal,
81 nsACString& aEndpointURI);
83 static void RemoveEndpoint(const nsAString& aGroupName,
84 const nsACString& aEndpointURL,
85 const mozilla::ipc::PrincipalInfo& aPrincipalInfo);
87 // ChromeOnly-WebIDL methods
89 static bool HasReportingHeaderForOrigin(const nsACString& aOrigin);
91 private:
92 ReportingHeader();
93 ~ReportingHeader();
95 static void Shutdown();
97 // Checks if a channel contains a Report-To header and parses its value.
98 void ReportingFromChannel(nsIHttpChannel* aChannel);
100 // This method checks if the protocol handler of the URI has the
101 // URI_IS_POTENTIALLY_TRUSTWORTHY flag.
102 static bool IsSecureURI(nsIURI* aURI);
104 void RemoveOriginsFromHost(const nsAString& aHost);
106 void RemoveOriginsFromOriginAttributesPattern(
107 const OriginAttributesPattern& aPattern);
109 void RemoveOrigins();
111 void RemoveOriginsForTTL();
113 void MaybeCreateCleanupTimer();
115 void MaybeCancelCleanupTimer();
117 static void LogToConsoleInvalidJSON(nsIHttpChannel* aChannel, nsIURI* aURI);
119 static void LogToConsoleDuplicateGroup(nsIHttpChannel* aChannel, nsIURI* aURI,
120 const nsAString& aName);
122 static void LogToConsoleInvalidNameItem(nsIHttpChannel* aChannel,
123 nsIURI* aURI);
125 static void LogToConsoleIncompleteItem(nsIHttpChannel* aChannel, nsIURI* aURI,
126 const nsAString& aName);
128 static void LogToConsoleIncompleteEndpoint(nsIHttpChannel* aChannel,
129 nsIURI* aURI,
130 const nsAString& aName);
132 static void LogToConsoleInvalidURLEndpoint(nsIHttpChannel* aChannel,
133 nsIURI* aURI,
134 const nsAString& aName,
135 const nsAString& aURL);
137 static void LogToConsoleInternal(nsIHttpChannel* aChannel, nsIURI* aURI,
138 const char* aMsg,
139 const nsTArray<nsString>& aParams);
141 static void GetEndpointForReportInternal(const ReportingHeader::Group& aGrup,
142 nsACString& aEndpointURI);
144 nsClassHashtable<nsCStringHashKey, Client> mOrigins;
146 nsCOMPtr<nsITimer> mCleanupTimer;
149 } // namespace dom
150 } // namespace mozilla
152 #endif // mozilla_dom_ReportingHeader_h