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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_net_UrlClassifierCommon_h
8 #define mozilla_net_UrlClassifierCommon_h
9 
10 #include "mozilla/Logging.h"
11 #include "nsString.h"
12 
13 #include <vector>
14 
15 class nsIChannel;
16 class nsIURI;
17 
18 #define UC_LOG(args) MOZ_LOG(UrlClassifierCommon::sLog, LogLevel::Info, args)
19 #define UC_LOG_DEBUG(args) \
20   MOZ_LOG(UrlClassifierCommon::sLog, LogLevel::Debug, args)
21 #define UC_LOG_WARN(args) \
22   MOZ_LOG(UrlClassifierCommon::sLog, LogLevel::Warning, args)
23 #define UC_LOG_LEAK(args) \
24   MOZ_LOG(UrlClassifierCommon::sLogLeak, LogLevel::Info, args)
25 
26 #define UC_LOG_ENABLED()                                     \
27   MOZ_LOG_TEST(UrlClassifierCommon::sLog, LogLevel::Info) || \
28       MOZ_LOG_TEST(UrlClassifierCommon::sLogLeak, LogLevel::Info)
29 
30 namespace mozilla {
31 namespace net {
32 
33 class UrlClassifierCommon final {
34  public:
35   static const nsCString::size_type sMaxSpecLength;
36 
37   static LazyLogModule sLog;
38   static LazyLogModule sLogLeak;
39 
40   static bool AddonMayLoad(nsIChannel* aChannel, nsIURI* aURI);
41 
42   static bool ShouldEnableProtectionForChannel(nsIChannel* aChannel);
43 
44   static nsresult SetBlockedContent(nsIChannel* channel, nsresult aErrorCode,
45                                     const nsACString& aList,
46                                     const nsACString& aProvider,
47                                     const nsACString& aFullHash);
48 
49   static nsresult SetTrackingInfo(nsIChannel* channel,
50                                   const nsTArray<nsCString>& aLists,
51                                   const nsTArray<nsCString>& aFullHashes);
52 
53   // Use this function only when you are looking for a pairwise entitylist uri
54   // with the format: http://toplevel.page/?resource=channel.uri.domain
55   static nsresult CreatePairwiseEntityListURI(nsIChannel* aChannel,
56                                               nsIURI** aURI);
57 
58   static void AnnotateChannel(nsIChannel* aChannel,
59                               uint32_t aClassificationFlags,
60                               uint32_t aLoadingState);
61 
62   static bool IsAllowListed(nsIChannel* aChannel);
63 
64   static bool IsTrackingClassificationFlag(uint32_t aFlag);
65 
66   static bool IsSocialTrackingClassificationFlag(uint32_t aFlag);
67 
68   static bool IsCryptominingClassificationFlag(uint32_t aFlag);
69 
70   // Join the table names in 1 single string.
71   static void TablesToString(const nsTArray<nsCString>& aList,
72                              nsACString& aString);
73 
74   struct ClassificationData {
75     nsCString mPrefix;
76     uint32_t mFlag;
77   };
78 
79   // Checks if the entries in aList are part of the ClassificationData vector
80   // and it returns the corresponding flags. If none of them is found, the
81   // default flag is returned.
82   static uint32_t TablesToClassificationFlags(
83       const nsTArray<nsCString>& aList,
84       const std::vector<ClassificationData>& aData, uint32_t aDefaultFlag);
85 
86   static bool IsPassiveContent(nsIChannel* aChannel);
87 
88   static void SetClassificationFlagsHelper(nsIChannel* aChannel,
89                                            uint32_t aClassificationFlags,
90                                            bool aIsThirdParty);
91 
92  private:
93   static uint32_t TableToClassificationFlag(
94       const nsACString& aTable, const std::vector<ClassificationData>& aData);
95 
96   friend class AsyncUrlChannelClassifier;
97   static nsresult GetTopWindowURI(nsIChannel* aChannel, nsIURI** aURI);
98 };
99 
100 }  // namespace net
101 }  // namespace mozilla
102 
103 #endif  // mozilla_net_UrlClassifierCommon_h
104