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 // Implementation of the ThreatDetails class. | 5 // Implementation of the ThreatDetails class. |
6 | 6 |
7 #include "chrome/browser/safe_browsing/threat_details.h" | 7 #include "chrome/browser/safe_browsing/threat_details.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/safe_browsing/threat_details_cache.h" | 12 #include "chrome/browser/safe_browsing/threat_details_cache.h" |
13 #include "chrome/browser/safe_browsing/threat_details_history.h" | 13 #include "chrome/browser/safe_browsing/threat_details_history.h" |
14 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 14 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/navigation_entry.h" | 17 #include "content/public/browser/navigation_entry.h" |
18 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
20 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
21 | 21 |
22 using content::BrowserThread; | 22 using content::BrowserThread; |
23 using content::NavigationEntry; | 23 using content::NavigationEntry; |
24 using content::WebContents; | 24 using content::WebContents; |
25 using safe_browsing::ClientSafeBrowsingReportRequest; | |
26 | 25 |
27 // Keep in sync with KMaxNodes in renderer/safe_browsing/threat_dom_details | 26 // Keep in sync with KMaxNodes in renderer/safe_browsing/threat_dom_details |
28 static const uint32 kMaxDomNodes = 500; | 27 static const uint32 kMaxDomNodes = 500; |
29 | 28 |
| 29 namespace safe_browsing { |
| 30 |
30 // static | 31 // static |
31 ThreatDetailsFactory* ThreatDetails::factory_ = NULL; | 32 ThreatDetailsFactory* ThreatDetails::factory_ = NULL; |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 // Helper function that converts SBThreatType to | 36 // Helper function that converts SBThreatType to |
36 // ClientSafeBrowsingReportRequest::ReportType. | 37 // ClientSafeBrowsingReportRequest::ReportType. |
37 ClientSafeBrowsingReportRequest::ReportType GetReportTypeFromSBThreatType( | 38 ClientSafeBrowsingReportRequest::ReportType GetReportTypeFromSBThreatType( |
38 SBThreatType threat_type) { | 39 SBThreatType threat_type) { |
39 switch (threat_type) { | 40 switch (threat_type) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // TODO(panayiotis): also skip internal urls. | 123 // TODO(panayiotis): also skip internal urls. |
123 return url.SchemeIs("http") || url.SchemeIs("https"); | 124 return url.SchemeIs("http") || url.SchemeIs("https"); |
124 } | 125 } |
125 | 126 |
126 // Looks for a Resource for the given url in resources_. If found, it | 127 // Looks for a Resource for the given url in resources_. If found, it |
127 // updates |resource|. Otherwise, it creates a new message, adds it to | 128 // updates |resource|. Otherwise, it creates a new message, adds it to |
128 // resources_ and updates |resource| to point to it. | 129 // resources_ and updates |resource| to point to it. |
129 // | 130 // |
130 ClientSafeBrowsingReportRequest::Resource* ThreatDetails::FindOrCreateResource( | 131 ClientSafeBrowsingReportRequest::Resource* ThreatDetails::FindOrCreateResource( |
131 const GURL& url) { | 132 const GURL& url) { |
132 safe_browsing::ResourceMap::iterator it = resources_.find(url.spec()); | 133 ResourceMap::iterator it = resources_.find(url.spec()); |
133 if (it != resources_.end()) | 134 if (it != resources_.end()) |
134 return it->second.get(); | 135 return it->second.get(); |
135 | 136 |
136 // Create the resource for |url|. | 137 // Create the resource for |url|. |
137 int id = resources_.size(); | 138 int id = resources_.size(); |
138 linked_ptr<ClientSafeBrowsingReportRequest::Resource> new_resource( | 139 linked_ptr<ClientSafeBrowsingReportRequest::Resource> new_resource( |
139 new ClientSafeBrowsingReportRequest::Resource()); | 140 new ClientSafeBrowsingReportRequest::Resource()); |
140 new_resource->set_url(url.spec()); | 141 new_resource->set_url(url.spec()); |
141 new_resource->set_id(id); | 142 new_resource->set_id(id); |
142 resources_[url.spec()] = new_resource; | 143 resources_[url.spec()] = new_resource; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 // closed the tab, or clicked proceed or goback. Since the user needs | 271 // closed the tab, or clicked proceed or goback. Since the user needs |
271 // to take an action, we expect this to be called after | 272 // to take an action, we expect this to be called after |
272 // OnReceivedThreatDOMDetails in most cases. If not, we don't include | 273 // OnReceivedThreatDOMDetails in most cases. If not, we don't include |
273 // the DOM data in our report. | 274 // the DOM data in our report. |
274 void ThreatDetails::FinishCollection(bool did_proceed, int num_visit) { | 275 void ThreatDetails::FinishCollection(bool did_proceed, int num_visit) { |
275 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 276 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
276 | 277 |
277 did_proceed_ = did_proceed; | 278 did_proceed_ = did_proceed; |
278 num_visits_ = num_visit; | 279 num_visits_ = num_visit; |
279 std::vector<GURL> urls; | 280 std::vector<GURL> urls; |
280 for (safe_browsing::ResourceMap::const_iterator it = resources_.begin(); | 281 for (ResourceMap::const_iterator it = resources_.begin(); |
281 it != resources_.end(); ++it) { | 282 it != resources_.end(); ++it) { |
282 urls.push_back(GURL(it->first)); | 283 urls.push_back(GURL(it->first)); |
283 } | 284 } |
284 redirects_collector_->StartHistoryCollection( | 285 redirects_collector_->StartHistoryCollection( |
285 urls, base::Bind(&ThreatDetails::OnRedirectionCollectionReady, this)); | 286 urls, base::Bind(&ThreatDetails::OnRedirectionCollectionReady, this)); |
286 } | 287 } |
287 | 288 |
288 void ThreatDetails::OnRedirectionCollectionReady() { | 289 void ThreatDetails::OnRedirectionCollectionReady() { |
289 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 290 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
290 const std::vector<safe_browsing::RedirectChain>& redirects = | 291 const std::vector<RedirectChain>& redirects = |
291 redirects_collector_->GetCollectedUrls(); | 292 redirects_collector_->GetCollectedUrls(); |
292 | 293 |
293 for (size_t i = 0; i < redirects.size(); ++i) | 294 for (size_t i = 0; i < redirects.size(); ++i) |
294 AddRedirectUrlList(redirects[i]); | 295 AddRedirectUrlList(redirects[i]); |
295 | 296 |
296 // Call the cache collector | 297 // Call the cache collector |
297 cache_collector_->StartCacheCollection( | 298 cache_collector_->StartCacheCollection( |
298 request_context_getter_.get(), &resources_, &cache_result_, | 299 request_context_getter_.get(), &resources_, &cache_result_, |
299 base::Bind(&ThreatDetails::OnCacheCollectionReady, this)); | 300 base::Bind(&ThreatDetails::OnCacheCollectionReady, this)); |
300 } | 301 } |
301 | 302 |
302 void ThreatDetails::AddRedirectUrlList(const std::vector<GURL>& urls) { | 303 void ThreatDetails::AddRedirectUrlList(const std::vector<GURL>& urls) { |
303 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 304 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
304 for (size_t i = 0; i < urls.size() - 1; ++i) { | 305 for (size_t i = 0; i < urls.size() - 1; ++i) { |
305 AddUrl(urls[i], urls[i + 1], std::string(), NULL); | 306 AddUrl(urls[i], urls[i + 1], std::string(), NULL); |
306 } | 307 } |
307 } | 308 } |
308 | 309 |
309 void ThreatDetails::OnCacheCollectionReady() { | 310 void ThreatDetails::OnCacheCollectionReady() { |
310 DVLOG(1) << "OnCacheCollectionReady."; | 311 DVLOG(1) << "OnCacheCollectionReady."; |
311 // Add all the urls in our |resources_| maps to the |report_| protocol buffer. | 312 // Add all the urls in our |resources_| maps to the |report_| protocol buffer. |
312 for (safe_browsing::ResourceMap::const_iterator it = resources_.begin(); | 313 for (ResourceMap::const_iterator it = resources_.begin(); |
313 it != resources_.end(); ++it) { | 314 it != resources_.end(); ++it) { |
314 ClientSafeBrowsingReportRequest::Resource* pb_resource = | 315 ClientSafeBrowsingReportRequest::Resource* pb_resource = |
315 report_->add_resources(); | 316 report_->add_resources(); |
316 pb_resource->CopyFrom(*(it->second)); | 317 pb_resource->CopyFrom(*(it->second)); |
317 const GURL url(pb_resource->url()); | 318 const GURL url(pb_resource->url()); |
318 if (url.SchemeIs("https")) { | 319 if (url.SchemeIs("https")) { |
319 // Don't report headers of HTTPS requests since they may contain private | 320 // Don't report headers of HTTPS requests since they may contain private |
320 // cookies. We still retain the full URL. | 321 // cookies. We still retain the full URL. |
321 DVLOG(1) << "Clearing out HTTPS resource: " << pb_resource->url(); | 322 DVLOG(1) << "Clearing out HTTPS resource: " << pb_resource->url(); |
322 pb_resource->clear_request(); | 323 pb_resource->clear_request(); |
323 pb_resource->clear_response(); | 324 pb_resource->clear_response(); |
324 // Keep id, parent_id, child_ids, and tag_name. | 325 // Keep id, parent_id, child_ids, and tag_name. |
325 } | 326 } |
326 } | 327 } |
327 report_->set_did_proceed(did_proceed_); | 328 report_->set_did_proceed(did_proceed_); |
328 // Only sets repeat_visit if num_visits_ >= 0. | 329 // Only sets repeat_visit if num_visits_ >= 0. |
329 if (num_visits_ >= 0) { | 330 if (num_visits_ >= 0) { |
330 report_->set_repeat_visit(num_visits_ > 0); | 331 report_->set_repeat_visit(num_visits_ > 0); |
331 } | 332 } |
332 report_->set_complete(cache_result_); | 333 report_->set_complete(cache_result_); |
333 | 334 |
334 // Send the report, using the SafeBrowsingService. | 335 // Send the report, using the SafeBrowsingService. |
335 std::string serialized; | 336 std::string serialized; |
336 if (!report_->SerializeToString(&serialized)) { | 337 if (!report_->SerializeToString(&serialized)) { |
337 DLOG(ERROR) << "Unable to serialize the threat report."; | 338 DLOG(ERROR) << "Unable to serialize the threat report."; |
338 return; | 339 return; |
339 } | 340 } |
340 ui_manager_->SendSerializedThreatDetails(serialized); | 341 ui_manager_->SendSerializedThreatDetails(serialized); |
341 } | 342 } |
| 343 |
| 344 } // namespace safe_browsing |
OLD | NEW |