1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef nsChannelClassifier_h__
6 #define nsChannelClassifier_h__
7 
8 #include "nsIObserver.h"
9 #include "nsIURIClassifier.h"
10 #include "nsCOMPtr.h"
11 #include "mozilla/Attributes.h"
12 
13 #include <functional>
14 
15 class nsIChannel;
16 
17 namespace mozilla {
18 namespace net {
19 
20 class nsChannelClassifier final : public nsIURIClassifierCallback,
21                                   public nsIObserver {
22  public:
23   explicit nsChannelClassifier(nsIChannel* aChannel);
24 
25   NS_DECL_ISUPPORTS
26   NS_DECL_NSIURICLASSIFIERCALLBACK
27   NS_DECL_NSIOBSERVER
28 
29   // Calls nsIURIClassifier.Classify with the principal of the given channel,
30   // and cancels the channel on a bad verdict.
31   void Start();
32 
33  private:
34   // True if the channel is on the allow list.
35   bool mIsAllowListed;
36   // True if the channel has been suspended.
37   bool mSuspendedChannel;
38   nsCOMPtr<nsIChannel> mChannel;
39 
40   ~nsChannelClassifier();
41   // Caches good classifications for the channel principal.
42   void MarkEntryClassified(nsresult status);
43   bool HasBeenClassified(nsIChannel* aChannel);
44   // Helper function so that we ensure we call ContinueBeginConnect once
45   // Start is called. Returns NS_OK if and only if we will get a callback
46   // from the classifier service.
47   nsresult StartInternal();
48   // Helper function to check a URI against the hostname entitylist
49   bool IsHostnameEntitylisted(nsIURI* aUri, const nsACString& aEntitylisted);
50 
51   void AddShutdownObserver();
52   void RemoveShutdownObserver();
53   static nsresult SendThreatHitReport(nsIChannel* aChannel,
54                                       const nsACString& aProvider,
55                                       const nsACString& aList,
56                                       const nsACString& aFullHash);
57 
58  public:
59   // If we are blocking content, update the corresponding flag in the respective
60   // docshell and call nsDocLoader::OnSecurityChange.
61   static nsresult SetBlockedContent(nsIChannel* channel, nsresult aErrorCode,
62                                     const nsACString& aList,
63                                     const nsACString& aProvider,
64                                     const nsACString& aFullHash);
65 };
66 
67 }  // namespace net
68 }  // namespace mozilla
69 
70 #endif
71