Chromium Code Reviews| 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 and Node protocol messages for the given url |
| 32 // in resources_ and nodes_. If found, it updates |resource| and | |
| 33 // |node|. Otherwise, it creates new messages, adds them to resources_ | |
| 34 // and nodes_, and updates |resource| and |node| to point to them. | |
| 35 // Note: We create exactly one Node for each Resource. | |
| 36 void MalwareDetails::FindOrCreateResource( | |
| 37 const std::string& url, | |
| 38 ClientMalwareReportRequest::Resource** resource, | |
| 39 ClientMalwareReportRequest::Node** node) { | |
| 40 ResourceMap::iterator it = resources_.find(url); | |
| 41 if (it != resources_.end()) { | |
| 42 // We have the resource, also find the corresponding node. | |
| 43 *resource = it->second.get(); | |
| 44 NodeMap::iterator node_it = nodes_.find((*resource)->id()); | |
| 45 DCHECK(node_it != nodes_.end()); | |
| 46 *node = node_it->second.get(); | |
|
noelutz
2011/01/11 22:28:27
The release binary will crash here if the DCHECK c
| |
| 47 } else { | |
| 48 // Create the resource and node for |url| | |
| 49 int id = resources_.size(); | |
| 50 linked_ptr<ClientMalwareReportRequest::Resource> new_resource( | |
| 51 new ClientMalwareReportRequest::Resource()); | |
| 52 new_resource->set_url(url); | |
| 53 new_resource->set_id(id); | |
| 54 resources_[url] = new_resource; | |
| 55 *resource = new_resource.get(); | |
| 56 | |
| 57 linked_ptr<ClientMalwareReportRequest::Node> new_node( | |
| 58 new ClientMalwareReportRequest::Node()); | |
| 59 new_node->set_id(id); | |
| 60 nodes_[id] = new_node; | |
| 61 *node = new_node.get(); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void MalwareDetails::AddUrl(const std::string& url, | |
| 30 const std::string& parent) { | 66 const std::string& parent) { |
| 31 if (!IsPublicUrl(GURL(url))) | 67 if (!IsPublicUrl(GURL(url))) |
| 32 return; | 68 return; |
| 33 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> resource( | 69 |
| 34 new safe_browsing::ClientMalwareReportRequest::Resource()); | 70 // Find (or create) the resource for the url. |
| 35 resource->set_url(url); | 71 ClientMalwareReportRequest::Resource* url_resource = NULL; |
| 36 if (!parent.empty() && IsPublicUrl(GURL(parent))) | 72 ClientMalwareReportRequest::Node* url_node = NULL; |
| 37 resource->set_parent(parent); | 73 FindOrCreateResource(url, &url_resource, &url_node); |
| 38 urls_[url] = resource; | 74 DCHECK(url_resource != NULL && url_node != NULL); |
| 75 | |
| 76 if (!parent.empty() && IsPublicUrl(GURL(parent))) { | |
| 77 // Add the resource and node for the parent node. | |
| 78 ClientMalwareReportRequest::Resource* parent_resource = NULL; | |
| 79 ClientMalwareReportRequest::Node* parent_node = NULL; | |
| 80 FindOrCreateResource(parent, &parent_resource, &parent_node); | |
| 81 DCHECK(parent_resource != NULL && parent_node != NULL); | |
| 82 | |
| 83 // Update the parent-child relation | |
| 84 url_node->set_parent_id(parent_node->id()); | |
|
noelutz
2011/01/11 22:28:27
same as above.
| |
| 85 } | |
| 39 } | 86 } |
| 40 | 87 |
| 41 void MalwareDetails::StartCollection() { | 88 void MalwareDetails::StartCollection() { |
| 42 DVLOG(1) << "Starting to compute malware details."; | 89 DVLOG(1) << "Starting to compute malware details."; |
| 43 report_.reset(new safe_browsing::ClientMalwareReportRequest()); | 90 report_.reset(new ClientMalwareReportRequest()); |
| 44 | 91 |
| 45 if (IsPublicUrl(resource_.url)) { | 92 if (IsPublicUrl(resource_.url)) { |
| 46 report_->set_malware_url(resource_.url.spec()); | 93 report_->set_malware_url(resource_.url.spec()); |
| 47 } | 94 } |
| 48 | 95 |
| 49 GURL page_url = tab_contents_->GetURL(); | 96 GURL page_url = tab_contents_->GetURL(); |
| 50 if (IsPublicUrl(page_url)) { | 97 if (IsPublicUrl(page_url)) { |
| 51 report_->set_page_url(page_url.spec()); | 98 report_->set_page_url(page_url.spec()); |
| 52 } | 99 } |
| 53 | 100 |
| 54 GURL referrer_url; | 101 GURL referrer_url; |
| 55 NavigationEntry* nav_entry = tab_contents_->controller().GetActiveEntry(); | 102 NavigationEntry* nav_entry = tab_contents_->controller().GetActiveEntry(); |
| 56 if (nav_entry) { | 103 if (nav_entry) { |
| 57 referrer_url = nav_entry->referrer(); | 104 referrer_url = nav_entry->referrer(); |
| 58 if (IsPublicUrl(referrer_url)) { | 105 if (IsPublicUrl(referrer_url)) { |
| 59 report_->set_referrer_url(referrer_url.spec()); | 106 report_->set_referrer_url(referrer_url.spec()); |
| 60 } | 107 } |
| 61 } | 108 } |
| 62 | 109 |
| 63 // Add the nodes, starting from the page url. | 110 // Add the nodes, starting from the page url. |
| 64 AddNode(page_url.spec(), ""); | 111 AddUrl(page_url.spec(), ""); |
| 65 | 112 |
| 66 // Add the resource_url and its original url, if non-empty and different. | 113 // Add the resource_url and its original url, if non-empty and different. |
| 67 if (!resource_.original_url.spec().empty() && | 114 if (!resource_.original_url.spec().empty() && |
| 68 resource_.url != resource_.original_url) { | 115 resource_.url != resource_.original_url) { |
| 69 // Add original_url, as the parent of resource_url. | 116 // Add original_url, as the parent of resource_url. |
| 70 AddNode(resource_.original_url.spec(), ""); | 117 AddUrl(resource_.original_url.spec(), ""); |
| 71 AddNode(resource_.url.spec(), resource_.original_url.spec()); | 118 AddUrl(resource_.url.spec(), resource_.original_url.spec()); |
| 72 } else { | 119 } else { |
| 73 AddNode(resource_.url.spec(), ""); | 120 AddUrl(resource_.url.spec(), ""); |
| 74 } | 121 } |
| 75 | 122 |
| 76 // Add the referrer url. | 123 // Add the referrer url. |
| 77 if (nav_entry && !referrer_url.spec().empty()) { | 124 if (nav_entry && !referrer_url.spec().empty()) { |
| 78 AddNode(referrer_url.spec(), ""); | 125 AddUrl(referrer_url.spec(), ""); |
| 79 } | 126 } |
| 80 | 127 |
| 81 // Add all the urls in our |urls_| map to the |report_| protobuf. | 128 // The |report_| protocol buffer is now generated: We add all the |
| 82 for (ResourceMap::const_iterator it = urls_.begin(); | 129 // urls in our |resources_| and |nodes_| maps. |
| 83 it != urls_.end(); it++) { | 130 for (ResourceMap::const_iterator it = resources_.begin(); |
| 84 safe_browsing::ClientMalwareReportRequest::Resource* pb_resource = | 131 it != resources_.end(); it++) { |
| 85 report_->add_nodes(); | 132 ClientMalwareReportRequest::Resource* pb_resource = |
| 133 report_->add_resources(); | |
| 86 pb_resource->CopyFrom(*(it->second)); | 134 pb_resource->CopyFrom(*(it->second)); |
| 87 } | 135 } |
| 136 for (NodeMap::const_iterator it = nodes_.begin(); it != nodes_.end(); it++) { | |
| 137 ClientMalwareReportRequest::Node* pb_node = report_->add_nodes(); | |
| 138 pb_node->CopyFrom(*(it->second)); | |
| 139 } | |
| 88 } | 140 } |
| 89 | 141 |
| 90 // Called from the SB Service on the IO thread. | 142 // Called from the SB Service on the IO thread. |
| 91 const std::string* MalwareDetails::GetSerializedReport() { | 143 const std::string* MalwareDetails::GetSerializedReport() { |
| 92 scoped_ptr<std::string> request_data(new std::string()); | 144 scoped_ptr<std::string> request_data(new std::string()); |
| 93 if (!report_->SerializeToString(request_data.get())) { | 145 if (!report_->SerializeToString(request_data.get())) { |
| 94 DLOG(ERROR) << "Unable to serialize the malware report."; | 146 DLOG(ERROR) << "Unable to serialize the malware report."; |
| 95 } | 147 } |
| 96 | 148 |
| 97 return request_data.release(); | 149 return request_data.release(); |
| 98 } | 150 } |
| OLD | NEW |