Chromium Code Reviews| Index: chrome/browser/safe_browsing/malware_details.cc |
| =================================================================== |
| --- chrome/browser/safe_browsing/malware_details.cc (revision 79632) |
| +++ chrome/browser/safe_browsing/malware_details.cc (working copy) |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| // |
| @@ -6,20 +6,35 @@ |
| #include "chrome/browser/safe_browsing/malware_details.h" |
| +#include "base/callback.h" |
| #include "base/lazy_instance.h" |
| +#include "base/md5.h" |
| +#include "chrome/browser/net/chrome_url_request_context.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/safe_browsing/malware_details_cache.h" |
| +#include "chrome/browser/safe_browsing/report.pb.h" |
| #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| -#include "chrome/browser/safe_browsing/report.pb.h" |
| +#include "chrome/common/net/url_request_context_getter.h" |
| #include "chrome/common/safebrowsing_messages.h" |
| #include "content/browser/browser_thread.h" |
| #include "content/browser/renderer_host/render_view_host.h" |
| #include "content/browser/tab_contents/navigation_entry.h" |
| #include "content/browser/tab_contents/tab_contents.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/disk_cache/disk_cache.h" |
| +#include "net/http/http_cache.h" |
| +#include "net/http/http_response_headers.h" |
| +#include "net/http/http_response_info.h" |
| using safe_browsing::ClientMalwareReportRequest; |
| // Keep in sync with KMaxNodes in renderer/safe_browsing/malware_dom_details |
| static const uint32 kMaxDomNodes = 500; |
| +// Only send tiny files for now, a better strategy would use the size |
| +// of the whole report and the user's bandwidth. |
| +static const int kMaxBodySize = 1024; |
| + |
| // static |
| MalwareDetailsFactory* MalwareDetails::factory_ = NULL; |
| @@ -29,9 +44,10 @@ |
| : public MalwareDetailsFactory { |
| public: |
| MalwareDetails* CreateMalwareDetails( |
| + SafeBrowsingService* sb_service, |
| TabContents* tab_contents, |
| const SafeBrowsingService::UnsafeResource& unsafe_resource) { |
| - return new MalwareDetails(tab_contents, unsafe_resource); |
| + return new MalwareDetails(sb_service, tab_contents, unsafe_resource); |
| } |
| private: |
| @@ -49,25 +65,31 @@ |
| // Create a MalwareDetails for the given tab. |
| /* static */ |
| MalwareDetails* MalwareDetails::NewMalwareDetails( |
| + SafeBrowsingService* sb_service, |
| TabContents* tab_contents, |
| const SafeBrowsingService::UnsafeResource& resource) { |
| // Set up the factory if this has not been done already (tests do that |
| // before this method is called). |
| if (!factory_) |
| factory_ = g_malware_details_factory_impl.Pointer(); |
| - return factory_->CreateMalwareDetails(tab_contents, resource); |
| + return factory_->CreateMalwareDetails(sb_service, tab_contents, resource); |
| } |
| // Create a MalwareDetails for the given tab. Runs in the UI thread. |
| MalwareDetails::MalwareDetails( |
| + SafeBrowsingService* sb_service, |
| TabContents* tab_contents, |
| const SafeBrowsingService::UnsafeResource& resource) |
| : TabContentsObserver(tab_contents), |
| - resource_(resource) { |
| + request_context_getter_(tab_contents->profile()->GetRequestContext()), |
| + sb_service_(sb_service), |
| + resource_(resource), |
| + cache_collector_(new MalwareDetailsCacheCollector) { |
| StartCollection(); |
| } |
| -MalwareDetails::~MalwareDetails() {} |
| +MalwareDetails::~MalwareDetails() { |
| +} |
| bool MalwareDetails::OnMessageReceived(const IPC::Message& message) { |
| bool handled = true; |
| @@ -88,7 +110,7 @@ |
| // resources_ and updates |resource| to point to it. |
| ClientMalwareReportRequest::Resource* MalwareDetails::FindOrCreateResource( |
| const GURL& url) { |
| - ResourceMap::iterator it = resources_.find(url.spec()); |
| + safe_browsing::ResourceMap::iterator it = resources_.find(url.spec()); |
| if (it != resources_.end()) { |
| return it->second.get(); |
| } |
| @@ -207,9 +229,15 @@ |
| void MalwareDetails::AddDOMDetails( |
| const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DVLOG(1) << "Nodes from the DOM: " << params.size(); |
| + |
| + // If we have already started collecting data from the HTTP cache, don't |
| + // modify our state. |
| + if (cache_collector_->InProgress()) |
| + return; |
| + |
| // Add the urls from the DOM to |resources_|. The renderer could be |
| // sending bogus messages, so limit the number of nodes we accept. |
| - DVLOG(1) << "Nodes from the DOM: " << params.size(); |
| for (uint32 i = 0; i < params.size() && i < kMaxDomNodes; ++i) { |
| SafeBrowsingHostMsg_MalwareDOMDetails_Node node = params[i]; |
| DVLOG(1) << node.url << ", " << node.tag_name << ", " << node.parent; |
| @@ -222,21 +250,33 @@ |
| // to take an action, we expect this to be called after |
| // OnReceivedMalwareDOMDetails in most cases. If not, we don't include |
| // the DOM data in our report. |
| -const std::string* MalwareDetails::GetSerializedReport() { |
| +void MalwareDetails::FinishCollection() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - // The |report_| protocol buffer is now generated: We add all the |
| - // urls in our |resources_| maps. |
| - for (ResourceMap::const_iterator it = resources_.begin(); |
| + |
| + AddRef(); // Ensures we remain alive during cache collection. |
|
panayiotis
2011/03/29 21:18:17
I was able to get rid of this AddRef. Now the only
|
| + cache_collector_->StartCacheCollection( |
| + request_context_getter_, |
| + &resources_, |
| + NewCallback(this, &MalwareDetails::OnCacheCollectionReady)); |
| +} |
| + |
| +void MalwareDetails::OnCacheCollectionReady(int result) { |
| + DVLOG(1) << "OnCacheCollectionReady."; |
| + // Add all the urls in our |resources_| maps to the |report_| protocol buffer. |
| + for (safe_browsing::ResourceMap::const_iterator it = resources_.begin(); |
| it != resources_.end(); it++) { |
| ClientMalwareReportRequest::Resource* pb_resource = |
| report_->add_resources(); |
| pb_resource->CopyFrom(*(it->second)); |
| } |
| + report_->set_complete(result == net::OK); |
| - scoped_ptr<std::string> request_data(new std::string()); |
| - if (!report_->SerializeToString(request_data.get())) { |
| + // Send the report, using the SafeBrowsingService. |
| + std::string serialized; |
| + if (!report_->SerializeToString(&serialized)) { |
| DLOG(ERROR) << "Unable to serialize the malware report."; |
| } |
| - return request_data.release(); |
| + sb_service_->SendSerializedMalwareDetails(serialized); |
| + Release(); |
| } |