OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // Implementation of the SafeBrowsingMalwareReport class. |
| 6 |
| 7 #include "chrome/browser/safe_browsing/malware_report.h" |
| 8 |
| 9 #include "chrome/browser/browser_thread.h" |
| 10 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 11 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #include "chrome/browser/safe_browsing/report.pb.h" |
| 14 |
| 15 // Create a SafeBrowsingMalwareReport for the given tab. |
| 16 // Runs in the UI thread. |
| 17 SafeBrowsingMalwareReport::SafeBrowsingMalwareReport( |
| 18 TabContents* tab_contents, |
| 19 const SafeBrowsingService::UnsafeResource resource) |
| 20 : tab_contents_(tab_contents), |
| 21 resource_(resource) { |
| 22 StartCollection(); |
| 23 } |
| 24 |
| 25 SafeBrowsingMalwareReport::~SafeBrowsingMalwareReport() {} |
| 26 |
| 27 bool SafeBrowsingMalwareReport::IsPublicUrl(const GURL& url) const { |
| 28 return url.SchemeIs("http"); // TODO(panayiotis): also skip internal urls. |
| 29 } |
| 30 |
| 31 void SafeBrowsingMalwareReport::AddNode( |
| 32 const std::string& url, |
| 33 const std::string& parent) { |
| 34 if (!IsPublicUrl(GURL(url))) |
| 35 return; |
| 36 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> resource( |
| 37 new safe_browsing::ClientMalwareReportRequest::Resource()); |
| 38 resource->set_url(url); |
| 39 if (!parent.empty() && IsPublicUrl(GURL(parent))) |
| 40 resource->set_parent(parent); |
| 41 urls_[url] = resource; |
| 42 } |
| 43 |
| 44 void SafeBrowsingMalwareReport::StartCollection() { |
| 45 DVLOG(1) << "Starting to compute a new malware report."; |
| 46 report_.reset(new safe_browsing::ClientMalwareReportRequest()); |
| 47 |
| 48 if (IsPublicUrl(resource_.url)) { |
| 49 report_->set_malware_url(resource_.url.spec()); |
| 50 } |
| 51 |
| 52 GURL page_url = tab_contents_->GetURL(); |
| 53 if (IsPublicUrl(page_url)) { |
| 54 report_->set_page_url(page_url.spec()); |
| 55 } |
| 56 |
| 57 GURL referrer_url; |
| 58 NavigationEntry* nav_entry = tab_contents_->controller().GetActiveEntry(); |
| 59 if (nav_entry) { |
| 60 referrer_url = nav_entry->referrer(); |
| 61 if (IsPublicUrl(referrer_url)) { |
| 62 report_->set_referrer_url(referrer_url.spec()); |
| 63 } |
| 64 } |
| 65 |
| 66 // Add the nodes, starting from the page url. |
| 67 AddNode(page_url.spec(), ""); |
| 68 |
| 69 // Add the resource_url and its original url, if non-empty and different. |
| 70 if (!resource_.original_url.spec().empty() && |
| 71 resource_.url != resource_.original_url) { |
| 72 // Add original_url, as the parent of resource_url. |
| 73 AddNode(resource_.original_url.spec(), ""); |
| 74 AddNode(resource_.url.spec(), resource_.original_url.spec()); |
| 75 } else { |
| 76 AddNode(resource_.url.spec(), ""); |
| 77 } |
| 78 |
| 79 // Add the referrer url. |
| 80 if (nav_entry && !referrer_url.spec().empty()) { |
| 81 AddNode(referrer_url.spec(), ""); |
| 82 } |
| 83 |
| 84 // Add all the urls in our |urls_| map to the |report_| protobuf. |
| 85 for (ResourceMap::const_iterator it = urls_.begin(); |
| 86 it != urls_.end(); it++) { |
| 87 safe_browsing::ClientMalwareReportRequest::Resource* pb_resource = |
| 88 report_->add_nodes(); |
| 89 pb_resource->CopyFrom(*(it->second)); |
| 90 } |
| 91 } |
| 92 |
| 93 // Called from the SB Service on the IO thread. |
| 94 const std::string* SafeBrowsingMalwareReport::GetSerializedReport() { |
| 95 scoped_ptr<std::string> request_data(new std::string()); |
| 96 if (!report_->SerializeToString(request_data.get())) { |
| 97 DLOG(ERROR) << "Unable to serialize the malware report."; |
| 98 } |
| 99 |
| 100 return request_data.release(); |
| 101 } |
OLD | NEW |