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/browser/safe_browsing/client_side_detection_service.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
13 #include "base/ref_counted_memory.h" | |
14 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
15 #include "base/stl_util-inl.h" | 14 #include "base/stl_util-inl.h" |
16 #include "base/task.h" | 15 #include "base/task.h" |
17 #include "chrome/browser/browser_thread.h" | 16 #include "chrome/browser/browser_thread.h" |
18 #include "chrome/browser/safe_browsing/csd.pb.h" | 17 #include "chrome/browser/safe_browsing/csd.pb.h" |
19 #include "chrome/common/net/http_return.h" | 18 #include "chrome/common/net/http_return.h" |
20 #include "chrome/common/net/url_fetcher.h" | 19 #include "chrome/common/net/url_fetcher.h" |
21 #include "chrome/common/net/url_request_context_getter.h" | 20 #include "chrome/common/net/url_request_context_getter.h" |
22 #include "gfx/codec/png_codec.h" | |
23 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
24 #include "net/base/load_flags.h" | 22 #include "net/base/load_flags.h" |
25 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
26 #include "third_party/skia/include/core/SkBitmap.h" | |
27 | 24 |
28 namespace safe_browsing { | 25 namespace safe_browsing { |
29 | 26 |
30 const char ClientSideDetectionService::kClientReportPhishingUrl[] = | 27 const char ClientSideDetectionService::kClientReportPhishingUrl[] = |
31 "https://sb-ssl.google.com/safebrowsing/clientreport/phishing"; | 28 "https://sb-ssl.google.com/safebrowsing/clientreport/phishing"; |
32 const char ClientSideDetectionService::kClientModelUrl[] = | 29 const char ClientSideDetectionService::kClientModelUrl[] = |
33 "https://ssl.gstatic.com/safebrowsing/csd/client_model_v0.pb"; | 30 "https://ssl.gstatic.com/safebrowsing/csd/client_model_v0.pb"; |
34 | 31 |
35 struct ClientSideDetectionService::ClientReportInfo { | 32 struct ClientSideDetectionService::ClientReportInfo { |
36 scoped_ptr<ClientReportPhishingRequestCallback> callback; | 33 scoped_ptr<ClientReportPhishingRequestCallback> callback; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
86 MessageLoop::current()->PostTask( | 83 MessageLoop::current()->PostTask( |
87 FROM_HERE, | 84 FROM_HERE, |
88 method_factory_.NewRunnableMethod( | 85 method_factory_.NewRunnableMethod( |
89 &ClientSideDetectionService::StartGetModelFile, callback)); | 86 &ClientSideDetectionService::StartGetModelFile, callback)); |
90 } | 87 } |
91 | 88 |
92 void ClientSideDetectionService::SendClientReportPhishingRequest( | 89 void ClientSideDetectionService::SendClientReportPhishingRequest( |
93 const GURL& phishing_url, | 90 const GURL& phishing_url, |
94 double score, | 91 double score, |
95 SkBitmap thumbnail, | |
96 ClientReportPhishingRequestCallback* callback) { | 92 ClientReportPhishingRequestCallback* callback) { |
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
98 MessageLoop::current()->PostTask( | 94 MessageLoop::current()->PostTask( |
99 FROM_HERE, | 95 FROM_HERE, |
100 method_factory_.NewRunnableMethod( | 96 method_factory_.NewRunnableMethod( |
101 &ClientSideDetectionService::StartClientReportPhishingRequest, | 97 &ClientSideDetectionService::StartClientReportPhishingRequest, |
102 phishing_url, score, thumbnail, callback)); | 98 phishing_url, score, callback)); |
103 } | 99 } |
104 | 100 |
105 void ClientSideDetectionService::OnURLFetchComplete( | 101 void ClientSideDetectionService::OnURLFetchComplete( |
106 const URLFetcher* source, | 102 const URLFetcher* source, |
107 const GURL& url, | 103 const GURL& url, |
108 const net::URLRequestStatus& status, | 104 const net::URLRequestStatus& status, |
109 int response_code, | 105 int response_code, |
110 const ResponseCookies& cookies, | 106 const ResponseCookies& cookies, |
111 const std::string& data) { | 107 const std::string& data) { |
112 if (source == model_fetcher_) { | 108 if (source == model_fetcher_) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // The model is either in READY or ERROR state which means we can | 215 // The model is either in READY or ERROR state which means we can |
220 // call the callback right away. | 216 // call the callback right away. |
221 callback->Run(model_file_); | 217 callback->Run(model_file_); |
222 delete callback; | 218 delete callback; |
223 } | 219 } |
224 } | 220 } |
225 | 221 |
226 void ClientSideDetectionService::StartClientReportPhishingRequest( | 222 void ClientSideDetectionService::StartClientReportPhishingRequest( |
227 const GURL& phishing_url, | 223 const GURL& phishing_url, |
228 double score, | 224 double score, |
229 SkBitmap thumbnail, | |
230 ClientReportPhishingRequestCallback* callback) { | 225 ClientReportPhishingRequestCallback* callback) { |
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
232 scoped_ptr<ClientReportPhishingRequestCallback> cb(callback); | 227 scoped_ptr<ClientReportPhishingRequestCallback> cb(callback); |
233 // The server expects an encoded PNG image. | |
234 scoped_refptr<RefCountedBytes> thumbnail_data(new RefCountedBytes); | |
235 SkAutoLockPixels lock(thumbnail); | |
236 if (!thumbnail.readyToDraw() || | |
237 !gfx::PNGCodec::EncodeBGRASkBitmap(thumbnail, | |
238 true /* discard_transparency */, | |
239 &thumbnail_data->data)) { | |
240 cb->Run(phishing_url, false); | |
241 return; | |
242 } | |
243 | 228 |
244 ClientPhishingRequest request; | 229 ClientPhishingRequest request; |
245 request.set_url(phishing_url.spec()); | 230 request.set_url(phishing_url.spec()); |
246 request.set_client_score(static_cast<float>(score)); | 231 request.set_client_score(static_cast<float>(score)); |
247 request.set_snapshot(reinterpret_cast<const char*>(thumbnail_data->front()), | |
248 thumbnail_data->size()); | |
249 std::string request_data; | 232 std::string request_data; |
250 if (!request.SerializeToString(&request_data)) { | 233 if (!request.SerializeToString(&request_data)) { |
251 // For consistency, we always call the callback asynchronously, rather than | 234 // For consistency, we always call the callback asynchronously, rather than |
252 // directly from this method. | 235 // directly from this method. |
253 LOG(ERROR) << "Unable to serialize the CSD request. Proto file changed?"; | 236 LOG(ERROR) << "Unable to serialize the CSD request. Proto file changed?"; |
254 cb->Run(phishing_url, false); | 237 cb->Run(phishing_url, false); |
255 return; | 238 return; |
256 } | 239 } |
257 | 240 |
258 URLFetcher* fetcher = URLFetcher::Create(0 /* ID is not used */, | 241 URLFetcher* fetcher = URLFetcher::Create(0 /* ID is not used */, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 info->callback->Run(info->phishing_url, response.phishy()); | 300 info->callback->Run(info->phishing_url, response.phishy()); |
318 } else { | 301 } else { |
319 DLOG(ERROR) << "Unable to get the server verdict for URL: " | 302 DLOG(ERROR) << "Unable to get the server verdict for URL: " |
320 << info->phishing_url; | 303 << info->phishing_url; |
321 info->callback->Run(info->phishing_url, false); | 304 info->callback->Run(info->phishing_url, false); |
322 } | 305 } |
323 client_phishing_reports_.erase(source); | 306 client_phishing_reports_.erase(source); |
324 } | 307 } |
325 | 308 |
326 } // namespace safe_browsing | 309 } // namespace safe_browsing |
OLD | NEW |