1 : //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Url Classifier code
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Dave Camp <dcamp@mozilla.com>
24 : * Gian-Carlo Pascutto <gpascutto@mozilla.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef Classifier_h__
41 : #define Classifier_h__
42 :
43 : #include "Entries.h"
44 : #include "HashStore.h"
45 : #include "ProtocolParser.h"
46 : #include "LookupCache.h"
47 : #include "nsCOMPtr.h"
48 : #include "nsString.h"
49 : #include "nsIFile.h"
50 : #include "nsICryptoHash.h"
51 : #include "nsDataHashtable.h"
52 :
53 : namespace mozilla {
54 : namespace safebrowsing {
55 :
56 : /**
57 : * Maintains the stores and LookupCaches for the url classifier.
58 : */
59 : class Classifier {
60 : public:
61 : Classifier();
62 : ~Classifier();
63 :
64 : nsresult Open(nsIFile& aCacheDirectory);
65 : nsresult Close();
66 : nsresult Reset();
67 :
68 : /**
69 : * Get the list of active tables and their chunks in a format
70 : * suitable for an update request.
71 : */
72 : void TableRequest(nsACString& aResult);
73 :
74 : /*
75 : * Get all tables that we know about.
76 : */
77 : nsresult ActiveTables(nsTArray<nsCString>& aTables);
78 :
79 : /**
80 : * Check a URL against the database.
81 : */
82 : nsresult Check(const nsACString& aSpec, LookupResultArray& aResults);
83 :
84 : /**
85 : * Apply the table updates in the array. Takes ownership of
86 : * the updates in the array and clears it. Wacky!
87 : */
88 : nsresult ApplyUpdates(nsTArray<TableUpdate*>* aUpdates);
89 : /**
90 : * Failed update. Spoil the entries so we don't block hosts
91 : * unnecessarily
92 : */
93 : nsresult MarkSpoiled(nsTArray<nsCString>& aTables);
94 : nsresult CacheCompletions(const CacheResultArray& aResults);
95 50 : PRUint32 GetHashKey(void) { return mHashKey; };
96 190 : void SetFreshTime(PRUint32 aTime) { mFreshTime = aTime; };
97 : /*
98 : * Get a bunch of extra prefixes to query for completion
99 : * and mask the real entry being requested
100 : */
101 : nsresult ReadNoiseEntries(const Prefix& aPrefix,
102 : const nsACString& aTableName,
103 : PRInt32 aCount,
104 : PrefixArray* aNoiseEntries);
105 : private:
106 : void DropStores();
107 : nsresult RegenActiveTables();
108 : nsresult ScanStoreDir(nsTArray<nsCString>& aTables);
109 :
110 : nsresult ApplyTableUpdates(nsTArray<TableUpdate*>* aUpdates,
111 : const nsACString& aTable);
112 :
113 : LookupCache *GetLookupCache(const nsACString& aTable);
114 : nsresult InitKey();
115 :
116 : nsCOMPtr<nsICryptoHash> mCryptoHash;
117 : nsCOMPtr<nsIFile> mStoreDirectory;
118 : nsTArray<HashStore*> mHashStores;
119 : nsTArray<LookupCache*> mLookupCaches;
120 : nsTArray<nsCString> mActiveTablesCache;
121 : PRUint32 mHashKey;
122 : // Stores the last time a given table was updated (seconds).
123 : nsDataHashtable<nsCStringHashKey, PRInt64> mTableFreshness;
124 : PRUint32 mFreshTime;
125 : };
126 :
127 : }
128 : }
129 :
130 : #endif
|