| 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 #include "chrome/renderer/safe_browsing/phishing_term_feature_extractor.h" | 5 #include "chrome/renderer/safe_browsing/phishing_term_feature_extractor.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 108 |
| 109 PhishingTermFeatureExtractor::~PhishingTermFeatureExtractor() { | 109 PhishingTermFeatureExtractor::~PhishingTermFeatureExtractor() { |
| 110 // The RenderView should have called CancelPendingExtraction() before | 110 // The RenderView should have called CancelPendingExtraction() before |
| 111 // we are destroyed. | 111 // we are destroyed. |
| 112 CheckNoPendingExtraction(); | 112 CheckNoPendingExtraction(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void PhishingTermFeatureExtractor::ExtractFeatures( | 115 void PhishingTermFeatureExtractor::ExtractFeatures( |
| 116 const string16* page_text, | 116 const string16* page_text, |
| 117 FeatureMap* features, | 117 FeatureMap* features, |
| 118 DoneCallback* done_callback) { | 118 const DoneCallback& done_callback) { |
| 119 // The RenderView should have called CancelPendingExtraction() before | 119 // The RenderView should have called CancelPendingExtraction() before |
| 120 // starting a new extraction, so DCHECK this. | 120 // starting a new extraction, so DCHECK this. |
| 121 CheckNoPendingExtraction(); | 121 CheckNoPendingExtraction(); |
| 122 // However, in an opt build, we will go ahead and clean up the pending | 122 // However, in an opt build, we will go ahead and clean up the pending |
| 123 // extraction so that we can start in a known state. | 123 // extraction so that we can start in a known state. |
| 124 CancelPendingExtraction(); | 124 CancelPendingExtraction(); |
| 125 | 125 |
| 126 page_text_ = page_text; | 126 page_text_ = page_text; |
| 127 features_ = features; | 127 features_ = features; |
| 128 done_callback_.reset(done_callback); | 128 done_callback_ = done_callback; |
| 129 | 129 |
| 130 state_.reset(new ExtractionState(*page_text_, clock_->Now())); | 130 state_.reset(new ExtractionState(*page_text_, clock_->Now())); |
| 131 MessageLoop::current()->PostTask( | 131 MessageLoop::current()->PostTask( |
| 132 FROM_HERE, | 132 FROM_HERE, |
| 133 base::Bind(&PhishingTermFeatureExtractor::ExtractFeaturesWithTimeout, | 133 base::Bind(&PhishingTermFeatureExtractor::ExtractFeaturesWithTimeout, |
| 134 weak_factory_.GetWeakPtr())); | 134 weak_factory_.GetWeakPtr())); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void PhishingTermFeatureExtractor::CancelPendingExtraction() { | 137 void PhishingTermFeatureExtractor::CancelPendingExtraction() { |
| 138 // Cancel any pending callbacks, and clear our state. | 138 // Cancel any pending callbacks, and clear our state. |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 state_->previous_word_sizes.push_back(word_lower.size() + 1); | 270 state_->previous_word_sizes.push_back(word_lower.size() + 1); |
| 271 | 271 |
| 272 // Cap the number of previous words. | 272 // Cap the number of previous words. |
| 273 if (state_->previous_word_sizes.size() >= max_words_per_term_) { | 273 if (state_->previous_word_sizes.size() >= max_words_per_term_) { |
| 274 state_->previous_words.erase(0, state_->previous_word_sizes.front()); | 274 state_->previous_words.erase(0, state_->previous_word_sizes.front()); |
| 275 state_->previous_word_sizes.pop_front(); | 275 state_->previous_word_sizes.pop_front(); |
| 276 } | 276 } |
| 277 } | 277 } |
| 278 | 278 |
| 279 void PhishingTermFeatureExtractor::CheckNoPendingExtraction() { | 279 void PhishingTermFeatureExtractor::CheckNoPendingExtraction() { |
| 280 DCHECK(!done_callback_.get()); | 280 DCHECK(done_callback_.is_null()); |
| 281 DCHECK(!state_.get()); | 281 DCHECK(!state_.get()); |
| 282 if (done_callback_.get() || state_.get()) { | 282 if (!done_callback_.is_null() || state_.get()) { |
| 283 LOG(ERROR) << "Extraction in progress, missing call to " | 283 LOG(ERROR) << "Extraction in progress, missing call to " |
| 284 << "CancelPendingExtraction"; | 284 << "CancelPendingExtraction"; |
| 285 } | 285 } |
| 286 } | 286 } |
| 287 | 287 |
| 288 void PhishingTermFeatureExtractor::RunCallback(bool success) { | 288 void PhishingTermFeatureExtractor::RunCallback(bool success) { |
| 289 // Record some timing stats that we can use to evaluate feature extraction | 289 // Record some timing stats that we can use to evaluate feature extraction |
| 290 // performance. These include both successful and failed extractions. | 290 // performance. These include both successful and failed extractions. |
| 291 DCHECK(state_.get()); | 291 DCHECK(state_.get()); |
| 292 UMA_HISTOGRAM_COUNTS("SBClientPhishing.TermFeatureIterations", | 292 UMA_HISTOGRAM_COUNTS("SBClientPhishing.TermFeatureIterations", |
| 293 state_->num_iterations); | 293 state_->num_iterations); |
| 294 UMA_HISTOGRAM_TIMES("SBClientPhishing.TermFeatureTotalTime", | 294 UMA_HISTOGRAM_TIMES("SBClientPhishing.TermFeatureTotalTime", |
| 295 clock_->Now() - state_->start_time); | 295 clock_->Now() - state_->start_time); |
| 296 | 296 |
| 297 DCHECK(done_callback_.get()); | 297 DCHECK(!done_callback_.is_null()); |
| 298 done_callback_->Run(success); | 298 done_callback_.Run(success); |
| 299 Clear(); | 299 Clear(); |
| 300 } | 300 } |
| 301 | 301 |
| 302 void PhishingTermFeatureExtractor::Clear() { | 302 void PhishingTermFeatureExtractor::Clear() { |
| 303 page_text_ = NULL; | 303 page_text_ = NULL; |
| 304 features_ = NULL; | 304 features_ = NULL; |
| 305 done_callback_.reset(NULL); | 305 done_callback_.Reset(); |
| 306 state_.reset(NULL); | 306 state_.reset(NULL); |
| 307 negative_word_cache_.Clear(); | 307 negative_word_cache_.Clear(); |
| 308 } | 308 } |
| 309 | 309 |
| 310 } // namespace safe_browsing | 310 } // namespace safe_browsing |
| OLD | NEW |