| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 client_phishing_reports_.end()); | 112 client_phishing_reports_.end()); |
| 113 client_phishing_reports_.clear(); | 113 client_phishing_reports_.clear(); |
| 114 STLDeleteContainerPairPointers(client_malware_reports_.begin(), | 114 STLDeleteContainerPairPointers(client_malware_reports_.begin(), |
| 115 client_malware_reports_.end()); | 115 client_malware_reports_.end()); |
| 116 client_malware_reports_.clear(); | 116 client_malware_reports_.clear(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 // static | 119 // static |
| 120 ClientSideDetectionService* ClientSideDetectionService::Create( | 120 ClientSideDetectionService* ClientSideDetectionService::Create( |
| 121 net::URLRequestContextGetter* request_context_getter) { | 121 net::URLRequestContextGetter* request_context_getter) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 123 return new ClientSideDetectionService(request_context_getter); | 123 return new ClientSideDetectionService(request_context_getter); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void ClientSideDetectionService::SetEnabledAndRefreshState(bool enabled) { | 126 void ClientSideDetectionService::SetEnabledAndRefreshState(bool enabled) { |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 127 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 128 SendModelToRenderers(); // always refresh the renderer state | 128 SendModelToRenderers(); // always refresh the renderer state |
| 129 if (enabled == enabled_) | 129 if (enabled == enabled_) |
| 130 return; | 130 return; |
| 131 enabled_ = enabled; | 131 enabled_ = enabled; |
| 132 if (enabled_) { | 132 if (enabled_) { |
| 133 // Refresh the model when the service is enabled. This can happen when the | 133 // Refresh the model when the service is enabled. This can happen when the |
| 134 // preference is toggled, or early during startup if the preference is | 134 // preference is toggled, or early during startup if the preference is |
| 135 // already enabled. In a lot of cases the model will be in the cache so it | 135 // already enabled. In a lot of cases the model will be in the cache so it |
| 136 // won't actually be fetched from the network. | 136 // won't actually be fetched from the network. |
| 137 // We delay the first model fetch to avoid slowing down browser startup. | 137 // We delay the first model fetch to avoid slowing down browser startup. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 160 STLDeleteContainerPairPointers(client_malware_reports_.begin(), | 160 STLDeleteContainerPairPointers(client_malware_reports_.begin(), |
| 161 client_malware_reports_.end()); | 161 client_malware_reports_.end()); |
| 162 client_malware_reports_.clear(); | 162 client_malware_reports_.clear(); |
| 163 cache_.clear(); | 163 cache_.clear(); |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 | 166 |
| 167 void ClientSideDetectionService::SendClientReportPhishingRequest( | 167 void ClientSideDetectionService::SendClientReportPhishingRequest( |
| 168 ClientPhishingRequest* verdict, | 168 ClientPhishingRequest* verdict, |
| 169 const ClientReportPhishingRequestCallback& callback) { | 169 const ClientReportPhishingRequestCallback& callback) { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 170 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 171 base::MessageLoop::current()->PostTask( | 171 base::MessageLoop::current()->PostTask( |
| 172 FROM_HERE, | 172 FROM_HERE, |
| 173 base::Bind(&ClientSideDetectionService::StartClientReportPhishingRequest, | 173 base::Bind(&ClientSideDetectionService::StartClientReportPhishingRequest, |
| 174 weak_factory_.GetWeakPtr(), verdict, callback)); | 174 weak_factory_.GetWeakPtr(), verdict, callback)); |
| 175 } | 175 } |
| 176 | 176 |
| 177 void ClientSideDetectionService::SendClientReportMalwareRequest( | 177 void ClientSideDetectionService::SendClientReportMalwareRequest( |
| 178 ClientMalwareRequest* verdict, | 178 ClientMalwareRequest* verdict, |
| 179 const ClientReportMalwareRequestCallback& callback) { | 179 const ClientReportMalwareRequestCallback& callback) { |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 180 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 181 base::MessageLoop::current()->PostTask( | 181 base::MessageLoop::current()->PostTask( |
| 182 FROM_HERE, | 182 FROM_HERE, |
| 183 base::Bind(&ClientSideDetectionService::StartClientReportMalwareRequest, | 183 base::Bind(&ClientSideDetectionService::StartClientReportMalwareRequest, |
| 184 weak_factory_.GetWeakPtr(), verdict, callback)); | 184 weak_factory_.GetWeakPtr(), verdict, callback)); |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool ClientSideDetectionService::IsPrivateIPAddress( | 187 bool ClientSideDetectionService::IsPrivateIPAddress( |
| 188 const std::string& ip_address) const { | 188 const std::string& ip_address) const { |
| 189 net::IPAddressNumber ip_number; | 189 net::IPAddressNumber ip_number; |
| 190 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) { | 190 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 216 source->GetResponseCode(), source->GetCookies(), data); | 216 source->GetResponseCode(), source->GetCookies(), data); |
| 217 } else { | 217 } else { |
| 218 NOTREACHED(); | 218 NOTREACHED(); |
| 219 } | 219 } |
| 220 } | 220 } |
| 221 | 221 |
| 222 void ClientSideDetectionService::Observe( | 222 void ClientSideDetectionService::Observe( |
| 223 int type, | 223 int type, |
| 224 const content::NotificationSource& source, | 224 const content::NotificationSource& source, |
| 225 const content::NotificationDetails& details) { | 225 const content::NotificationDetails& details) { |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 226 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 227 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CREATED); | 227 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CREATED); |
| 228 if (!model_.get()) { | 228 if (!model_.get()) { |
| 229 // Model might not be ready or maybe there was an error. | 229 // Model might not be ready or maybe there was an error. |
| 230 return; | 230 return; |
| 231 } | 231 } |
| 232 SendModelToProcess( | 232 SendModelToProcess( |
| 233 content::Source<content::RenderProcessHost>(source).ptr()); | 233 content::Source<content::RenderProcessHost>(source).ptr()); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void ClientSideDetectionService::SendModelToProcess( | 236 void ClientSideDetectionService::SendModelToProcess( |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 } | 301 } |
| 302 model_max_age_.reset(); | 302 model_max_age_.reset(); |
| 303 | 303 |
| 304 // Schedule the next model reload. | 304 // Schedule the next model reload. |
| 305 ScheduleFetchModel(delay_ms); | 305 ScheduleFetchModel(delay_ms); |
| 306 } | 306 } |
| 307 | 307 |
| 308 void ClientSideDetectionService::StartClientReportPhishingRequest( | 308 void ClientSideDetectionService::StartClientReportPhishingRequest( |
| 309 ClientPhishingRequest* verdict, | 309 ClientPhishingRequest* verdict, |
| 310 const ClientReportPhishingRequestCallback& callback) { | 310 const ClientReportPhishingRequestCallback& callback) { |
| 311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 311 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 312 scoped_ptr<ClientPhishingRequest> request(verdict); | 312 scoped_ptr<ClientPhishingRequest> request(verdict); |
| 313 | 313 |
| 314 if (!enabled_) { | 314 if (!enabled_) { |
| 315 if (!callback.is_null()) | 315 if (!callback.is_null()) |
| 316 callback.Run(GURL(request->url()), false); | 316 callback.Run(GURL(request->url()), false); |
| 317 return; | 317 return; |
| 318 } | 318 } |
| 319 | 319 |
| 320 std::string request_data; | 320 std::string request_data; |
| 321 if (!request->SerializeToString(&request_data)) { | 321 if (!request->SerializeToString(&request_data)) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 342 fetcher->SetUploadData("application/octet-stream", request_data); | 342 fetcher->SetUploadData("application/octet-stream", request_data); |
| 343 fetcher->Start(); | 343 fetcher->Start(); |
| 344 | 344 |
| 345 // Record that we made a request | 345 // Record that we made a request |
| 346 phishing_report_times_.push(base::Time::Now()); | 346 phishing_report_times_.push(base::Time::Now()); |
| 347 } | 347 } |
| 348 | 348 |
| 349 void ClientSideDetectionService::StartClientReportMalwareRequest( | 349 void ClientSideDetectionService::StartClientReportMalwareRequest( |
| 350 ClientMalwareRequest* verdict, | 350 ClientMalwareRequest* verdict, |
| 351 const ClientReportMalwareRequestCallback& callback) { | 351 const ClientReportMalwareRequestCallback& callback) { |
| 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 352 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 353 scoped_ptr<ClientMalwareRequest> request(verdict); | 353 scoped_ptr<ClientMalwareRequest> request(verdict); |
| 354 | 354 |
| 355 if (!enabled_) { | 355 if (!enabled_) { |
| 356 if (!callback.is_null()) | 356 if (!callback.is_null()) |
| 357 callback.Run(GURL(request->url()), GURL(request->url()), false); | 357 callback.Run(GURL(request->url()), GURL(request->url()), false); |
| 358 return; | 358 return; |
| 359 } | 359 } |
| 360 | 360 |
| 361 std::string request_data; | 361 std::string request_data; |
| 362 if (!request->SerializeToString(&request_data)) { | 362 if (!request->SerializeToString(&request_data)) { |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 GURL ClientSideDetectionService::GetClientReportUrl( | 635 GURL ClientSideDetectionService::GetClientReportUrl( |
| 636 const std::string& report_url) { | 636 const std::string& report_url) { |
| 637 GURL url(report_url); | 637 GURL url(report_url); |
| 638 std::string api_key = google_apis::GetAPIKey(); | 638 std::string api_key = google_apis::GetAPIKey(); |
| 639 if (!api_key.empty()) | 639 if (!api_key.empty()) |
| 640 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true)); | 640 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true)); |
| 641 | 641 |
| 642 return url; | 642 return url; |
| 643 } | 643 } |
| 644 } // namespace safe_browsing | 644 } // namespace safe_browsing |
| OLD | NEW |