OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // PhishingTermFeatureExtractor handles computing term features from the text | 5 // PhishingTermFeatureExtractor handles computing term features from the text |
6 // of a web page for the client-side phishing detection model. To do this, it | 6 // of a web page for the client-side phishing detection model. To do this, it |
7 // takes a list of terms that appear in the model, and scans through the page | 7 // takes a list of terms that appear in the model, and scans through the page |
8 // text looking for them. Any terms that appear will cause a corresponding | 8 // text looking for them. Any terms that appear will cause a corresponding |
9 // features::kPageTerm feature to be added to the FeatureMap. | 9 // features::kPageTerm feature to be added to the FeatureMap. |
10 // | 10 // |
11 // To make it harder for a phisher to enumerate all of the relevant terms in | 11 // To make it harder for a phisher to enumerate all of the relevant terms in |
12 // the model, the terms are provided as SHA-256 hashes, rather than plain text. | 12 // the model, the terms are provided as SHA-256 hashes, rather than plain text. |
13 // | 13 // |
14 // There is one PhishingTermFeatureExtractor per RenderView. | 14 // There is one PhishingTermFeatureExtractor per RenderView. |
15 | 15 |
16 #ifndef CHROME_RENDERER_SAFE_BROWSING_PHISHING_TERM_FEATURE_EXTRACTOR_H_ | 16 #ifndef CHROME_RENDERER_SAFE_BROWSING_PHISHING_TERM_FEATURE_EXTRACTOR_H_ |
17 #define CHROME_RENDERER_SAFE_BROWSING_PHISHING_TERM_FEATURE_EXTRACTOR_H_ | 17 #define CHROME_RENDERER_SAFE_BROWSING_PHISHING_TERM_FEATURE_EXTRACTOR_H_ |
18 | 18 |
19 #include <set> | |
19 #include <string> | 20 #include <string> |
20 | 21 |
21 #include "base/basictypes.h" | 22 #include "base/basictypes.h" |
22 #include "base/callback.h" | 23 #include "base/callback.h" |
23 #include "base/containers/hash_tables.h" | 24 #include "base/containers/hash_tables.h" |
24 #include "base/containers/mru_cache.h" | 25 #include "base/containers/mru_cache.h" |
25 #include "base/memory/scoped_ptr.h" | 26 #include "base/memory/scoped_ptr.h" |
26 #include "base/memory/weak_ptr.h" | 27 #include "base/memory/weak_ptr.h" |
27 #include "base/strings/string16.h" | 28 #include "base/strings/string16.h" |
28 #include "base/strings/string_piece.h" | 29 #include "base/strings/string_piece.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
40 // Creates a PhishingTermFeatureExtractor which will extract features for | 41 // Creates a PhishingTermFeatureExtractor which will extract features for |
41 // all of the terms whose SHA-256 hashes are in |page_term_hashes|. These | 42 // all of the terms whose SHA-256 hashes are in |page_term_hashes|. These |
42 // terms may be multi-word n-grams, with at most |max_words_per_term| words. | 43 // terms may be multi-word n-grams, with at most |max_words_per_term| words. |
43 // | 44 // |
44 // |page_word_hashes| contains the murmur3 hashes for all of the individual | 45 // |page_word_hashes| contains the murmur3 hashes for all of the individual |
45 // words that make up the terms. Both sets of strings are UTF-8 encoded and | 46 // words that make up the terms. Both sets of strings are UTF-8 encoded and |
46 // lowercased prior to hashing. The caller owns both sets of strings, and | 47 // lowercased prior to hashing. The caller owns both sets of strings, and |
47 // must ensure that they are valid until the PhishingTermFeatureExtractor is | 48 // must ensure that they are valid until the PhishingTermFeatureExtractor is |
48 // destroyed. | 49 // destroyed. |
49 // | 50 // |
51 // In addition to extracting page terms, we will also extract text shingling | |
52 // sketch, which consists of hashes of N-gram-words (referred to as shingles) | |
53 // in the page. |shingle_size| defines N, and |max_shingles_per_page| defines | |
54 // the maximum number of unique shingle hashes we extracted per page. | |
55 // | |
50 // |clock| is used for timing feature extractor operations, and may be mocked | 56 // |clock| is used for timing feature extractor operations, and may be mocked |
51 // for testing. The caller keeps ownership of the clock. | 57 // for testing. The caller keeps ownership of the clock. |
52 PhishingTermFeatureExtractor( | 58 PhishingTermFeatureExtractor( |
53 const base::hash_set<std::string>* page_term_hashes, | 59 const base::hash_set<std::string>* page_term_hashes, |
54 const base::hash_set<uint32>* page_word_hashes, | 60 const base::hash_set<uint32>* page_word_hashes, |
55 size_t max_words_per_term, | 61 size_t max_words_per_term, |
56 uint32 murmurhash3_seed, | 62 uint32 murmurhash3_seed, |
63 size_t max_shingles_per_page, | |
64 size_t shingle_size, | |
57 FeatureExtractorClock* clock); | 65 FeatureExtractorClock* clock); |
58 ~PhishingTermFeatureExtractor(); | 66 ~PhishingTermFeatureExtractor(); |
59 | 67 |
60 // Begins extracting features from |page_text| into the given FeatureMap. | 68 // Begins extracting features from |page_text| into the given FeatureMap. |
61 // |page_text| should contain the plain text of a web page, including any | 69 // |page_text| should contain the plain text of a web page, including any |
62 // subframes, as returned by RenderView::CaptureText(). | 70 // subframes, as returned by RenderView::CaptureText(). |
63 // | 71 // |
64 // To avoid blocking the render thread for too long, the feature extractor | 72 // To avoid blocking the render thread for too long, the feature extractor |
65 // may run in several chunks of work, posting a task to the current | 73 // may run in several chunks of work, posting a task to the current |
66 // MessageLoop to continue processing. Once feature extraction is complete, | 74 // MessageLoop to continue processing. Once feature extraction is complete, |
67 // |done_callback| is run on the current thread. | 75 // |done_callback| is run on the current thread. |
68 // PhishingTermFeatureExtractor takes ownership of the callback. | 76 // PhishingTermFeatureExtractor takes ownership of the callback. |
69 // | 77 // |
70 // |page_text| and |features| are owned by the caller, and must not be | 78 // |page_text| and |features| are owned by the caller, and must not be |
71 // destroyed until either |done_callback| is run or | 79 // destroyed until either |done_callback| is run or |
72 // CancelPendingExtraction() is called. | 80 // CancelPendingExtraction() is called. |
noelutz
2014/05/06 21:40:18
nit: mention ownership of shingle_hashes as well?
zysxqn
2014/05/07 19:29:19
Done.
| |
73 void ExtractFeatures(const base::string16* page_text, | 81 void ExtractFeatures(const base::string16* page_text, |
74 FeatureMap* features, | 82 FeatureMap* features, |
83 std::set<uint32>* shingle_hashes, | |
75 const DoneCallback& done_callback); | 84 const DoneCallback& done_callback); |
76 | 85 |
77 // Cancels any pending feature extraction. The DoneCallback will not be run. | 86 // Cancels any pending feature extraction. The DoneCallback will not be run. |
78 // Must be called if there is a feature extraction in progress when the page | 87 // Must be called if there is a feature extraction in progress when the page |
79 // is unloaded or the PhishingTermFeatureExtractor is destroyed. | 88 // is unloaded or the PhishingTermFeatureExtractor is destroyed. |
80 void CancelPendingExtraction(); | 89 void CancelPendingExtraction(); |
81 | 90 |
82 private: | 91 private: |
83 struct ExtractionState; | 92 struct ExtractionState; |
84 | 93 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 // quick out in the common case that the current word we are processing | 137 // quick out in the common case that the current word we are processing |
129 // doesn't contain any part of one of our terms. | 138 // doesn't contain any part of one of our terms. |
130 const base::hash_set<uint32>* page_word_hashes_; | 139 const base::hash_set<uint32>* page_word_hashes_; |
131 | 140 |
132 // The maximum number of words in an n-gram. | 141 // The maximum number of words in an n-gram. |
133 const size_t max_words_per_term_; | 142 const size_t max_words_per_term_; |
134 | 143 |
135 // The seed for murmurhash3. | 144 // The seed for murmurhash3. |
136 const uint32 murmurhash3_seed_; | 145 const uint32 murmurhash3_seed_; |
137 | 146 |
147 // The maximum number of unique shingle hashes we extract in a page. | |
148 const size_t max_shingles_per_page_; | |
149 | |
150 // The number of words in a shingle. | |
151 const size_t shingle_size_; | |
152 | |
138 // This cache is used to see if we need to check the word at all, as | 153 // This cache is used to see if we need to check the word at all, as |
139 // converting to UTF8, lowercasing, and hashing are all relatively expensive | 154 // converting to UTF8, lowercasing, and hashing are all relatively expensive |
140 // operations. Though this is called an MRU cache, it seems to behave like | 155 // operations. Though this is called an MRU cache, it seems to behave like |
141 // an LRU cache (i.e. it evicts the oldest accesses first). | 156 // an LRU cache (i.e. it evicts the oldest accesses first). |
142 typedef base::HashingMRUCache<base::StringPiece16, bool> WordCache; | 157 typedef base::HashingMRUCache<base::StringPiece16, bool> WordCache; |
143 WordCache negative_word_cache_; | 158 WordCache negative_word_cache_; |
144 | 159 |
145 // Non-owned pointer to our clock. | 160 // Non-owned pointer to our clock. |
146 FeatureExtractorClock* clock_; | 161 FeatureExtractorClock* clock_; |
147 | 162 |
148 // The output parameters from the most recent call to ExtractFeatures(). | 163 // The output parameters from the most recent call to ExtractFeatures(). |
149 const base::string16* page_text_; // The caller keeps ownership of this. | 164 const base::string16* page_text_; // The caller keeps ownership of this. |
150 FeatureMap* features_; // The caller keeps ownership of this. | 165 FeatureMap* features_; // The caller keeps ownership of this. |
166 std::set<uint32>* shingle_hashes_; | |
151 DoneCallback done_callback_; | 167 DoneCallback done_callback_; |
152 | 168 |
153 // Stores the current state of term extraction from |page_text_|. | 169 // Stores the current state of term extraction from |page_text_|. |
154 scoped_ptr<ExtractionState> state_; | 170 scoped_ptr<ExtractionState> state_; |
155 | 171 |
156 // Used in scheduling ExtractFeaturesWithTimeout tasks. | 172 // Used in scheduling ExtractFeaturesWithTimeout tasks. |
157 // These pointers are invalidated if extraction is cancelled. | 173 // These pointers are invalidated if extraction is cancelled. |
158 base::WeakPtrFactory<PhishingTermFeatureExtractor> weak_factory_; | 174 base::WeakPtrFactory<PhishingTermFeatureExtractor> weak_factory_; |
159 | 175 |
160 DISALLOW_COPY_AND_ASSIGN(PhishingTermFeatureExtractor); | 176 DISALLOW_COPY_AND_ASSIGN(PhishingTermFeatureExtractor); |
161 }; | 177 }; |
162 | 178 |
163 } // namespace safe_browsing | 179 } // namespace safe_browsing |
164 | 180 |
165 #endif // CHROME_RENDERER_SAFE_BROWSING_PHISHING_TERM_FEATURE_EXTRACTOR_H_ | 181 #endif // CHROME_RENDERER_SAFE_BROWSING_PHISHING_TERM_FEATURE_EXTRACTOR_H_ |
OLD | NEW |