|
1 //* -*- Mode: C++; tab-width: 8; 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/. */ |
|
5 |
|
6 #ifndef nsUrlClassifierDBService_h_ |
|
7 #define nsUrlClassifierDBService_h_ |
|
8 |
|
9 #include <nsISupportsUtils.h> |
|
10 |
|
11 #include "nsID.h" |
|
12 #include "nsInterfaceHashtable.h" |
|
13 #include "nsIObserver.h" |
|
14 #include "nsUrlClassifierPrefixSet.h" |
|
15 #include "nsIUrlClassifierHashCompleter.h" |
|
16 #include "nsIUrlClassifierDBService.h" |
|
17 #include "nsIURIClassifier.h" |
|
18 #include "nsToolkitCompsCID.h" |
|
19 #include "nsICryptoHash.h" |
|
20 #include "nsICryptoHMAC.h" |
|
21 #include "mozilla/Attributes.h" |
|
22 |
|
23 #include "LookupCache.h" |
|
24 |
|
25 // The hash length for a domain key. |
|
26 #define DOMAIN_LENGTH 4 |
|
27 |
|
28 // The hash length of a partial hash entry. |
|
29 #define PARTIAL_LENGTH 4 |
|
30 |
|
31 // The hash length of a complete hash entry. |
|
32 #define COMPLETE_LENGTH 32 |
|
33 |
|
34 class nsUrlClassifierDBServiceWorker; |
|
35 class nsIThread; |
|
36 class nsIURI; |
|
37 |
|
38 // This is a proxy class that just creates a background thread and delagates |
|
39 // calls to the background thread. |
|
40 class nsUrlClassifierDBService MOZ_FINAL : public nsIUrlClassifierDBService, |
|
41 public nsIURIClassifier, |
|
42 public nsIObserver |
|
43 { |
|
44 public: |
|
45 // This is thread safe. It throws an exception if the thread is busy. |
|
46 nsUrlClassifierDBService(); |
|
47 |
|
48 nsresult Init(); |
|
49 |
|
50 static nsUrlClassifierDBService* GetInstance(nsresult *result); |
|
51 |
|
52 NS_DECLARE_STATIC_IID_ACCESSOR(NS_URLCLASSIFIERDBSERVICE_CID) |
|
53 |
|
54 NS_DECL_THREADSAFE_ISUPPORTS |
|
55 NS_DECL_NSIURLCLASSIFIERDBSERVICE |
|
56 NS_DECL_NSIURICLASSIFIER |
|
57 NS_DECL_NSIOBSERVER |
|
58 |
|
59 bool GetCompleter(const nsACString& tableName, |
|
60 nsIUrlClassifierHashCompleter** completer); |
|
61 nsresult CacheCompletions(mozilla::safebrowsing::CacheResultArray *results); |
|
62 nsresult CacheMisses(mozilla::safebrowsing::PrefixArray *results); |
|
63 |
|
64 static nsIThread* BackgroundThread(); |
|
65 |
|
66 private: |
|
67 // No subclassing |
|
68 ~nsUrlClassifierDBService(); |
|
69 |
|
70 // Disallow copy constructor |
|
71 nsUrlClassifierDBService(nsUrlClassifierDBService&); |
|
72 |
|
73 nsresult LookupURI(nsIPrincipal* aPrincipal, |
|
74 const nsACString& tables, |
|
75 nsIUrlClassifierCallback* c, |
|
76 bool forceCheck, bool *didCheck); |
|
77 |
|
78 // Close db connection and join the background thread if it exists. |
|
79 nsresult Shutdown(); |
|
80 |
|
81 // Check if the key is on a known-clean host. |
|
82 nsresult CheckClean(const nsACString &lookupKey, |
|
83 bool *clean); |
|
84 |
|
85 // Read everything into mGethashTables and mDisallowCompletionTables |
|
86 nsresult ReadTablesFromPrefs(); |
|
87 |
|
88 nsRefPtr<nsUrlClassifierDBServiceWorker> mWorker; |
|
89 nsCOMPtr<nsIUrlClassifierDBServiceWorker> mWorkerProxy; |
|
90 |
|
91 nsInterfaceHashtable<nsCStringHashKey, nsIUrlClassifierHashCompleter> mCompleters; |
|
92 |
|
93 // TRUE if the nsURIClassifier implementation should check for malware |
|
94 // uris on document loads. |
|
95 bool mCheckMalware; |
|
96 |
|
97 // TRUE if the nsURIClassifier implementation should check for phishing |
|
98 // uris on document loads. |
|
99 bool mCheckPhishing; |
|
100 |
|
101 // TRUE if a BeginUpdate() has been called without an accompanying |
|
102 // CancelUpdate()/FinishUpdate(). This is used to prevent competing |
|
103 // updates, not to determine whether an update is still being |
|
104 // processed. |
|
105 bool mInUpdate; |
|
106 |
|
107 // The list of tables that can use the default hash completer object. |
|
108 nsTArray<nsCString> mGethashTables; |
|
109 |
|
110 // The list of tables that should never be hash completed. |
|
111 nsTArray<nsCString> mDisallowCompletionsTables; |
|
112 |
|
113 // Thread that we do the updates on. |
|
114 static nsIThread* gDbBackgroundThread; |
|
115 }; |
|
116 |
|
117 NS_DEFINE_STATIC_IID_ACCESSOR(nsUrlClassifierDBService, NS_URLCLASSIFIERDBSERVICE_CID) |
|
118 |
|
119 #endif // nsUrlClassifierDBService_h_ |