Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: chrome/renderer/safe_browsing/phishing_classifier.cc

Issue 8573018: Convert to base::Callback in safe_browsing client-side-detection code. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Don't call Run() on null callbacks. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_classifier.h" 5 #include "chrome/renderer/safe_browsing/phishing_classifier.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 url_extractor_.reset(); 70 url_extractor_.reset();
71 dom_extractor_.reset(); 71 dom_extractor_.reset();
72 term_extractor_.reset(); 72 term_extractor_.reset();
73 } 73 }
74 } 74 }
75 75
76 bool PhishingClassifier::is_ready() const { 76 bool PhishingClassifier::is_ready() const {
77 return scorer_ != NULL; 77 return scorer_ != NULL;
78 } 78 }
79 79
80 void PhishingClassifier::BeginClassification(const string16* page_text, 80 void PhishingClassifier::BeginClassification(
81 DoneCallback* done_callback) { 81 const string16* page_text,
82 const DoneCallback& done_callback) {
82 DCHECK(is_ready()); 83 DCHECK(is_ready());
83 84
84 // The RenderView should have called CancelPendingClassification() before 85 // The RenderView should have called CancelPendingClassification() before
85 // starting a new classification, so DCHECK this. 86 // starting a new classification, so DCHECK this.
86 CheckNoPendingClassification(); 87 CheckNoPendingClassification();
87 // However, in an opt build, we will go ahead and clean up the pending 88 // However, in an opt build, we will go ahead and clean up the pending
88 // classification so that we can start in a known state. 89 // classification so that we can start in a known state.
89 CancelPendingClassification(); 90 CancelPendingClassification();
90 91
91 page_text_ = page_text; 92 page_text_ = page_text;
92 done_callback_.reset(done_callback); 93 done_callback_ = done_callback;
93 94
94 // For consistency, we always want to invoke the DoneCallback 95 // For consistency, we always want to invoke the DoneCallback
95 // asynchronously, rather than directly from this method. To ensure that 96 // asynchronously, rather than directly from this method. To ensure that
96 // this is the case, post a task to begin feature extraction on the next 97 // this is the case, post a task to begin feature extraction on the next
97 // iteration of the message loop. 98 // iteration of the message loop.
98 MessageLoop::current()->PostTask( 99 MessageLoop::current()->PostTask(
99 FROM_HERE, 100 FROM_HERE,
100 base::Bind(&PhishingClassifier::BeginFeatureExtraction, 101 base::Bind(&PhishingClassifier::BeginFeatureExtraction,
101 weak_factory_.GetWeakPtr())); 102 weak_factory_.GetWeakPtr()));
102 } 103 }
(...skipping 28 matching lines...) Expand all
131 features_.reset(new FeatureMap); 132 features_.reset(new FeatureMap);
132 if (!url_extractor_->ExtractFeatures(url, features_.get())) { 133 if (!url_extractor_->ExtractFeatures(url, features_.get())) {
133 RunFailureCallback(); 134 RunFailureCallback();
134 return; 135 return;
135 } 136 }
136 137
137 // DOM feature extraction can take awhile, so it runs asynchronously 138 // DOM feature extraction can take awhile, so it runs asynchronously
138 // in several chunks of work and invokes the callback when finished. 139 // in several chunks of work and invokes the callback when finished.
139 dom_extractor_->ExtractFeatures( 140 dom_extractor_->ExtractFeatures(
140 features_.get(), 141 features_.get(),
141 NewCallback(this, &PhishingClassifier::DOMExtractionFinished)); 142 base::Bind(&PhishingClassifier::DOMExtractionFinished,
143 base::Unretained(this)));
142 } 144 }
143 145
144 void PhishingClassifier::CancelPendingClassification() { 146 void PhishingClassifier::CancelPendingClassification() {
145 // Note that cancelling the feature extractors is simply a no-op if they 147 // Note that cancelling the feature extractors is simply a no-op if they
146 // were not running. 148 // were not running.
147 DCHECK(is_ready()); 149 DCHECK(is_ready());
148 dom_extractor_->CancelPendingExtraction(); 150 dom_extractor_->CancelPendingExtraction();
149 term_extractor_->CancelPendingExtraction(); 151 term_extractor_->CancelPendingExtraction();
150 weak_factory_.InvalidateWeakPtrs(); 152 weak_factory_.InvalidateWeakPtrs();
151 Clear(); 153 Clear();
152 } 154 }
153 155
154 void PhishingClassifier::DOMExtractionFinished(bool success) { 156 void PhishingClassifier::DOMExtractionFinished(bool success) {
155 if (success) { 157 if (success) {
156 // Term feature extraction can take awhile, so it runs asynchronously 158 // Term feature extraction can take awhile, so it runs asynchronously
157 // in several chunks of work and invokes the callback when finished. 159 // in several chunks of work and invokes the callback when finished.
158 term_extractor_->ExtractFeatures( 160 term_extractor_->ExtractFeatures(
159 page_text_, 161 page_text_,
160 features_.get(), 162 features_.get(),
161 NewCallback(this, &PhishingClassifier::TermExtractionFinished)); 163 base::Bind(&PhishingClassifier::TermExtractionFinished,
164 base::Unretained(this)));
162 } else { 165 } else {
163 RunFailureCallback(); 166 RunFailureCallback();
164 } 167 }
165 } 168 }
166 169
167 void PhishingClassifier::TermExtractionFinished(bool success) { 170 void PhishingClassifier::TermExtractionFinished(bool success) {
168 if (success) { 171 if (success) {
169 WebKit::WebView* web_view = render_view_->GetWebView(); 172 WebKit::WebView* web_view = render_view_->GetWebView();
170 if (!web_view) { 173 if (!web_view) {
171 RunFailureCallback(); 174 RunFailureCallback();
(...skipping 25 matching lines...) Expand all
197 float score = static_cast<float>(scorer_->ComputeScore(hashed_features)); 200 float score = static_cast<float>(scorer_->ComputeScore(hashed_features));
198 verdict.set_client_score(score); 201 verdict.set_client_score(score);
199 verdict.set_is_phishing(score >= kPhishyThreshold); 202 verdict.set_is_phishing(score >= kPhishyThreshold);
200 RunCallback(verdict); 203 RunCallback(verdict);
201 } else { 204 } else {
202 RunFailureCallback(); 205 RunFailureCallback();
203 } 206 }
204 } 207 }
205 208
206 void PhishingClassifier::CheckNoPendingClassification() { 209 void PhishingClassifier::CheckNoPendingClassification() {
207 DCHECK(!done_callback_.get()); 210 DCHECK(done_callback_.is_null());
208 DCHECK(!page_text_); 211 DCHECK(!page_text_);
209 if (done_callback_.get() || page_text_) { 212 if (done_callback_.is_null() || page_text_) {
210 LOG(ERROR) << "Classification in progress, missing call to " 213 LOG(ERROR) << "Classification in progress, missing call to "
211 << "CancelPendingClassification"; 214 << "CancelPendingClassification";
212 UMA_HISTOGRAM_COUNTS("SBClientPhishing.CheckNoPendingClassificationFailed", 215 UMA_HISTOGRAM_COUNTS("SBClientPhishing.CheckNoPendingClassificationFailed",
213 1); 216 1);
214 } 217 }
215 } 218 }
216 219
217 void PhishingClassifier::RunCallback(const ClientPhishingRequest& verdict) { 220 void PhishingClassifier::RunCallback(const ClientPhishingRequest& verdict) {
218 done_callback_->Run(verdict); 221 done_callback_.Run(verdict);
219 Clear(); 222 Clear();
220 } 223 }
221 224
222 void PhishingClassifier::RunFailureCallback() { 225 void PhishingClassifier::RunFailureCallback() {
223 ClientPhishingRequest verdict; 226 ClientPhishingRequest verdict;
224 // In this case we're not guaranteed to have a valid URL. Just set it 227 // In this case we're not guaranteed to have a valid URL. Just set it
225 // to the empty string to make sure we have a valid protocol buffer. 228 // to the empty string to make sure we have a valid protocol buffer.
226 verdict.set_url(""); 229 verdict.set_url("");
227 verdict.set_client_score(kInvalidScore); 230 verdict.set_client_score(kInvalidScore);
228 verdict.set_is_phishing(false); 231 verdict.set_is_phishing(false);
229 RunCallback(verdict); 232 RunCallback(verdict);
230 } 233 }
231 234
232 void PhishingClassifier::Clear() { 235 void PhishingClassifier::Clear() {
233 page_text_ = NULL; 236 page_text_ = NULL;
234 done_callback_.reset(NULL); 237 done_callback_.Reset();
235 features_.reset(NULL); 238 features_.reset(NULL);
236 } 239 }
237 240
238 } // namespace safe_browsing 241 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/renderer/safe_browsing/phishing_classifier.h ('k') | chrome/renderer/safe_browsing/phishing_classifier_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698