| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 MalwareDetails class. | 5 // Implementation of the MalwareDetails class. |
| 6 | 6 |
| 7 #include "chrome/browser/safe_browsing/malware_details.h" | 7 #include "chrome/browser/safe_browsing/malware_details.h" |
| 8 | 8 |
| 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 10 #include "chrome/browser/tab_contents/navigation_entry.h" | 10 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 11 #include "chrome/browser/tab_contents/tab_contents.h" | 11 #include "chrome/browser/tab_contents/tab_contents.h" |
| 12 #include "chrome/browser/safe_browsing/report.pb.h" | 12 #include "chrome/browser/safe_browsing/report.pb.h" |
| 13 | 13 |
| 14 using safe_browsing::ClientMalwareReportRequest; |
| 15 |
| 14 // Create a MalwareDetails for the given tab. Runs in the UI thread. | 16 // Create a MalwareDetails for the given tab. Runs in the UI thread. |
| 15 MalwareDetails::MalwareDetails( | 17 MalwareDetails::MalwareDetails( |
| 16 TabContents* tab_contents, | 18 TabContents* tab_contents, |
| 17 const SafeBrowsingService::UnsafeResource resource) | 19 const SafeBrowsingService::UnsafeResource resource) |
| 18 : tab_contents_(tab_contents), | 20 : tab_contents_(tab_contents), |
| 19 resource_(resource) { | 21 resource_(resource) { |
| 20 StartCollection(); | 22 StartCollection(); |
| 21 } | 23 } |
| 22 | 24 |
| 23 MalwareDetails::~MalwareDetails() {} | 25 MalwareDetails::~MalwareDetails() {} |
| 24 | 26 |
| 25 bool MalwareDetails::IsPublicUrl(const GURL& url) const { | 27 bool MalwareDetails::IsPublicUrl(const GURL& url) const { |
| 26 return url.SchemeIs("http"); // TODO(panayiotis): also skip internal urls. | 28 return url.SchemeIs("http"); // TODO(panayiotis): also skip internal urls. |
| 27 } | 29 } |
| 28 | 30 |
| 29 void MalwareDetails::AddNode(const std::string& url, | 31 // Looks for a Resource for the given url in resources_. If found, it |
| 32 // updates |resource|. Otherwise, it creates a new message, adds it to |
| 33 // resources_ and updates |resource| to point to it. |
| 34 ClientMalwareReportRequest::Resource* MalwareDetails::FindOrCreateResource( |
| 35 const std::string& url) { |
| 36 ResourceMap::iterator it = resources_.find(url); |
| 37 if (it != resources_.end()) { |
| 38 return it->second.get(); |
| 39 } |
| 40 |
| 41 // Create the resource for |url|. |
| 42 int id = resources_.size(); |
| 43 linked_ptr<ClientMalwareReportRequest::Resource> new_resource( |
| 44 new ClientMalwareReportRequest::Resource()); |
| 45 new_resource->set_url(url); |
| 46 new_resource->set_id(id); |
| 47 resources_[url] = new_resource; |
| 48 return new_resource.get(); |
| 49 } |
| 50 |
| 51 void MalwareDetails::AddUrl(const std::string& url, |
| 30 const std::string& parent) { | 52 const std::string& parent) { |
| 31 if (!IsPublicUrl(GURL(url))) | 53 if (!IsPublicUrl(GURL(url))) |
| 32 return; | 54 return; |
| 33 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> resource( | 55 |
| 34 new safe_browsing::ClientMalwareReportRequest::Resource()); | 56 // Find (or create) the resource for the url. |
| 35 resource->set_url(url); | 57 ClientMalwareReportRequest::Resource* url_resource = |
| 36 if (!parent.empty() && IsPublicUrl(GURL(parent))) | 58 FindOrCreateResource(url); |
| 37 resource->set_parent(parent); | 59 if (!parent.empty() && IsPublicUrl(GURL(parent))) { |
| 38 urls_[url] = resource; | 60 // Add the resource for the parent. |
| 61 ClientMalwareReportRequest::Resource* parent_resource = |
| 62 FindOrCreateResource(parent); |
| 63 // Update the parent-child relation |
| 64 url_resource->set_parent_id(parent_resource->id()); |
| 65 } |
| 39 } | 66 } |
| 40 | 67 |
| 41 void MalwareDetails::StartCollection() { | 68 void MalwareDetails::StartCollection() { |
| 42 DVLOG(1) << "Starting to compute malware details."; | 69 DVLOG(1) << "Starting to compute malware details."; |
| 43 report_.reset(new safe_browsing::ClientMalwareReportRequest()); | 70 report_.reset(new ClientMalwareReportRequest()); |
| 44 | 71 |
| 45 if (IsPublicUrl(resource_.url)) { | 72 if (IsPublicUrl(resource_.url)) { |
| 46 report_->set_malware_url(resource_.url.spec()); | 73 report_->set_malware_url(resource_.url.spec()); |
| 47 } | 74 } |
| 48 | 75 |
| 49 GURL page_url = tab_contents_->GetURL(); | 76 GURL page_url = tab_contents_->GetURL(); |
| 50 if (IsPublicUrl(page_url)) { | 77 if (IsPublicUrl(page_url)) { |
| 51 report_->set_page_url(page_url.spec()); | 78 report_->set_page_url(page_url.spec()); |
| 52 } | 79 } |
| 53 | 80 |
| 54 GURL referrer_url; | 81 GURL referrer_url; |
| 55 NavigationEntry* nav_entry = tab_contents_->controller().GetActiveEntry(); | 82 NavigationEntry* nav_entry = tab_contents_->controller().GetActiveEntry(); |
| 56 if (nav_entry) { | 83 if (nav_entry) { |
| 57 referrer_url = nav_entry->referrer(); | 84 referrer_url = nav_entry->referrer(); |
| 58 if (IsPublicUrl(referrer_url)) { | 85 if (IsPublicUrl(referrer_url)) { |
| 59 report_->set_referrer_url(referrer_url.spec()); | 86 report_->set_referrer_url(referrer_url.spec()); |
| 60 } | 87 } |
| 61 } | 88 } |
| 62 | 89 |
| 63 // Add the nodes, starting from the page url. | 90 // Add the nodes, starting from the page url. |
| 64 AddNode(page_url.spec(), ""); | 91 AddUrl(page_url.spec(), ""); |
| 65 | 92 |
| 66 // Add the resource_url and its original url, if non-empty and different. | 93 // Add the resource_url and its original url, if non-empty and different. |
| 67 if (!resource_.original_url.spec().empty() && | 94 if (!resource_.original_url.spec().empty() && |
| 68 resource_.url != resource_.original_url) { | 95 resource_.url != resource_.original_url) { |
| 69 // Add original_url, as the parent of resource_url. | 96 // Add original_url, as the parent of resource_url. |
| 70 AddNode(resource_.original_url.spec(), ""); | 97 AddUrl(resource_.original_url.spec(), ""); |
| 71 AddNode(resource_.url.spec(), resource_.original_url.spec()); | 98 AddUrl(resource_.url.spec(), resource_.original_url.spec()); |
| 72 } else { | 99 } else { |
| 73 AddNode(resource_.url.spec(), ""); | 100 AddUrl(resource_.url.spec(), ""); |
| 74 } | 101 } |
| 75 | 102 |
| 76 // Add the referrer url. | 103 // Add the referrer url. |
| 77 if (nav_entry && !referrer_url.spec().empty()) { | 104 if (nav_entry && !referrer_url.spec().empty()) { |
| 78 AddNode(referrer_url.spec(), ""); | 105 AddUrl(referrer_url.spec(), ""); |
| 79 } | 106 } |
| 80 | 107 |
| 81 // Add all the urls in our |urls_| map to the |report_| protobuf. | 108 // The |report_| protocol buffer is now generated: We add all the |
| 82 for (ResourceMap::const_iterator it = urls_.begin(); | 109 // urls in our |resources_| maps. |
| 83 it != urls_.end(); it++) { | 110 for (ResourceMap::const_iterator it = resources_.begin(); |
| 84 safe_browsing::ClientMalwareReportRequest::Resource* pb_resource = | 111 it != resources_.end(); it++) { |
| 85 report_->add_nodes(); | 112 ClientMalwareReportRequest::Resource* pb_resource = |
| 113 report_->add_resources(); |
| 86 pb_resource->CopyFrom(*(it->second)); | 114 pb_resource->CopyFrom(*(it->second)); |
| 87 } | 115 } |
| 88 } | 116 } |
| 89 | 117 |
| 90 // Called from the SB Service on the IO thread. | 118 // Called from the SB Service on the IO thread. |
| 91 const std::string* MalwareDetails::GetSerializedReport() { | 119 const std::string* MalwareDetails::GetSerializedReport() { |
| 92 scoped_ptr<std::string> request_data(new std::string()); | 120 scoped_ptr<std::string> request_data(new std::string()); |
| 93 if (!report_->SerializeToString(request_data.get())) { | 121 if (!report_->SerializeToString(request_data.get())) { |
| 94 DLOG(ERROR) << "Unable to serialize the malware report."; | 122 DLOG(ERROR) << "Unable to serialize the malware report."; |
| 95 } | 123 } |
| 96 | 124 |
| 97 return request_data.release(); | 125 return request_data.release(); |
| 98 } | 126 } |
| OLD | NEW |