OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "chrome/browser/net/chrome_url_request_context.h" | 11 #include "chrome/browser/net/chrome_url_request_context.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/safe_browsing/malware_details_cache.h" | 13 #include "chrome/browser/safe_browsing/malware_details_cache.h" |
14 #include "chrome/browser/safe_browsing/malware_details_history.h" | |
14 #include "chrome/browser/safe_browsing/report.pb.h" | 15 #include "chrome/browser/safe_browsing/report.pb.h" |
15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
16 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 17 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
17 #include "content/browser/browser_thread.h" | 18 #include "content/browser/browser_thread.h" |
18 #include "content/browser/renderer_host/render_view_host.h" | 19 #include "content/browser/renderer_host/render_view_host.h" |
19 #include "content/browser/tab_contents/navigation_entry.h" | 20 #include "content/browser/tab_contents/navigation_entry.h" |
20 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
21 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
22 #include "net/disk_cache/disk_cache.h" | 23 #include "net/disk_cache/disk_cache.h" |
23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 | 70 |
70 // Create a MalwareDetails for the given tab. Runs in the UI thread. | 71 // Create a MalwareDetails for the given tab. Runs in the UI thread. |
71 MalwareDetails::MalwareDetails( | 72 MalwareDetails::MalwareDetails( |
72 SafeBrowsingService* sb_service, | 73 SafeBrowsingService* sb_service, |
73 TabContents* tab_contents, | 74 TabContents* tab_contents, |
74 const SafeBrowsingService::UnsafeResource& resource) | 75 const SafeBrowsingService::UnsafeResource& resource) |
75 : TabContentsObserver(tab_contents), | 76 : TabContentsObserver(tab_contents), |
76 request_context_getter_(tab_contents->profile()->GetRequestContext()), | 77 request_context_getter_(tab_contents->profile()->GetRequestContext()), |
77 sb_service_(sb_service), | 78 sb_service_(sb_service), |
78 resource_(resource), | 79 resource_(resource), |
79 cache_collector_(new MalwareDetailsCacheCollector) { | 80 cache_collector_(new MalwareDetailsCacheCollector), |
81 redirects_collector_(new MalwareDetailsRedirectsCollector) { | |
80 StartCollection(); | 82 StartCollection(); |
81 } | 83 } |
82 | 84 |
83 MalwareDetails::~MalwareDetails() { | 85 MalwareDetails::~MalwareDetails() { |
84 } | 86 } |
85 | 87 |
86 bool MalwareDetails::OnMessageReceived(const IPC::Message& message) { | 88 bool MalwareDetails::OnMessageReceived(const IPC::Message& message) { |
87 bool handled = true; | 89 bool handled = true; |
88 IPC_BEGIN_MESSAGE_MAP(MalwareDetails, message) | 90 IPC_BEGIN_MESSAGE_MAP(MalwareDetails, message) |
89 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_MalwareDOMDetails, | 91 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_MalwareDOMDetails, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 BrowserThread::IO, FROM_HERE, | 219 BrowserThread::IO, FROM_HERE, |
218 NewRunnableMethod( | 220 NewRunnableMethod( |
219 this, &MalwareDetails::AddDOMDetails, params)); | 221 this, &MalwareDetails::AddDOMDetails, params)); |
220 } | 222 } |
221 | 223 |
222 void MalwareDetails::AddDOMDetails( | 224 void MalwareDetails::AddDOMDetails( |
223 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { | 225 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { |
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
225 DVLOG(1) << "Nodes from the DOM: " << params.size(); | 227 DVLOG(1) << "Nodes from the DOM: " << params.size(); |
226 | 228 |
229 // If we have already started getting redirects from history service, | |
230 // don't modify state, otherwise will invalidate the iterators. | |
231 if (redirects_collector_->HasStarted()) | |
232 return; | |
233 | |
227 // If we have already started collecting data from the HTTP cache, don't | 234 // If we have already started collecting data from the HTTP cache, don't |
228 // modify our state. | 235 // modify our state. |
229 if (cache_collector_->HasStarted()) | 236 if (cache_collector_->HasStarted()) { |
panayiotis
2011/05/13 21:15:26
drop the { here
kewang
2011/05/16 07:11:51
Done.
| |
230 return; | 237 return; |
238 } | |
231 | 239 |
232 // Add the urls from the DOM to |resources_|. The renderer could be | 240 // Add the urls from the DOM to |resources_|. The renderer could be |
233 // sending bogus messages, so limit the number of nodes we accept. | 241 // sending bogus messages, so limit the number of nodes we accept. |
234 for (uint32 i = 0; i < params.size() && i < kMaxDomNodes; ++i) { | 242 for (uint32 i = 0; i < params.size() && i < kMaxDomNodes; ++i) { |
235 SafeBrowsingHostMsg_MalwareDOMDetails_Node node = params[i]; | 243 SafeBrowsingHostMsg_MalwareDOMDetails_Node node = params[i]; |
236 DVLOG(1) << node.url << ", " << node.tag_name << ", " << node.parent; | 244 DVLOG(1) << node.url << ", " << node.tag_name << ", " << node.parent; |
237 AddUrl(node.url, node.parent, node.tag_name, &(node.children)); | 245 AddUrl(node.url, node.parent, node.tag_name, &(node.children)); |
238 } | 246 } |
239 } | 247 } |
240 | 248 |
241 // Called from the SB Service on the IO thread, after the user has | 249 // Called from the SB Service on the IO thread, after the user has |
242 // closed the tab, or clicked proceed or goback. Since the user needs | 250 // closed the tab, or clicked proceed or goback. Since the user needs |
243 // to take an action, we expect this to be called after | 251 // to take an action, we expect this to be called after |
244 // OnReceivedMalwareDOMDetails in most cases. If not, we don't include | 252 // OnReceivedMalwareDOMDetails in most cases. If not, we don't include |
245 // the DOM data in our report. | 253 // the DOM data in our report. |
246 void MalwareDetails::FinishCollection() { | 254 void MalwareDetails::FinishCollection() { |
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
248 | 256 |
257 std::vector<GURL> urls; | |
258 for (safe_browsing::ResourceMap::const_iterator it = resources_.begin(); | |
259 it != resources_.end(); it++) { | |
260 urls.push_back(GURL(it->first)); | |
261 } | |
262 redirects_collector_->StartHistoryCollection( | |
263 urls, tab_contents(), | |
264 NewRunnableMethod(this, &MalwareDetails::OnRedirectionCollectionReady)); | |
265 } | |
266 | |
267 void MalwareDetails::OnRedirectionCollectionReady() { | |
268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
269 std::vector<std::vector<GURL> > redirects = | |
270 redirects_collector_->GetCollectedUrls(); | |
271 | |
272 for (uint i = 0; i < redirects.size(); ++i) { | |
273 AddRedirectUrlList(redirects[i]); | |
274 } | |
275 | |
276 // Call the cache collector | |
249 cache_collector_->StartCacheCollection( | 277 cache_collector_->StartCacheCollection( |
250 request_context_getter_, | 278 request_context_getter_, |
251 &resources_, | 279 &resources_, |
252 &cache_result_, | 280 &cache_result_, |
253 NewRunnableMethod(this, &MalwareDetails::OnCacheCollectionReady)); | 281 NewRunnableMethod(this, &MalwareDetails::OnCacheCollectionReady)); |
254 } | 282 } |
255 | 283 |
284 void MalwareDetails::AddRedirectUrlList(const std::vector<GURL>& urls) { | |
285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
286 LOG(INFO) << "url list size:" << urls.size(); | |
panayiotis
2011/05/13 21:15:26
let's drop the logging
kewang
2011/05/16 07:11:51
Done.
| |
287 for (uint i = 0; i < urls.size()-1; ++i) { | |
288 AddUrl(urls[i], urls[i+1], "", NULL); | |
panayiotis
2011/05/13 21:15:26
I wonder if we should specify a tagname for these
kewang
2011/05/16 07:11:51
Done.
| |
289 LOG(INFO) << "--- adding...." << urls[i]; | |
290 } | |
291 } | |
292 | |
256 void MalwareDetails::OnCacheCollectionReady() { | 293 void MalwareDetails::OnCacheCollectionReady() { |
257 DVLOG(1) << "OnCacheCollectionReady."; | 294 DVLOG(1) << "OnCacheCollectionReady."; |
258 // Add all the urls in our |resources_| maps to the |report_| protocol buffer. | 295 // Add all the urls in our |resources_| maps to the |report_| protocol buffer. |
259 for (safe_browsing::ResourceMap::const_iterator it = resources_.begin(); | 296 for (safe_browsing::ResourceMap::const_iterator it = resources_.begin(); |
260 it != resources_.end(); it++) { | 297 it != resources_.end(); it++) { |
261 ClientMalwareReportRequest::Resource* pb_resource = | 298 ClientMalwareReportRequest::Resource* pb_resource = |
262 report_->add_resources(); | 299 report_->add_resources(); |
263 pb_resource->CopyFrom(*(it->second)); | 300 pb_resource->CopyFrom(*(it->second)); |
264 } | 301 } |
265 | 302 |
266 report_->set_complete(cache_result_); | 303 report_->set_complete(cache_result_); |
267 | 304 |
268 // Send the report, using the SafeBrowsingService. | 305 // Send the report, using the SafeBrowsingService. |
269 std::string serialized; | 306 std::string serialized; |
270 if (!report_->SerializeToString(&serialized)) { | 307 if (!report_->SerializeToString(&serialized)) { |
271 DLOG(ERROR) << "Unable to serialize the malware report."; | 308 DLOG(ERROR) << "Unable to serialize the malware report."; |
272 return; | 309 return; |
273 } | 310 } |
274 | 311 |
275 sb_service_->SendSerializedMalwareDetails(serialized); | 312 sb_service_->SendSerializedMalwareDetails(serialized); |
276 } | 313 } |
OLD | NEW |