1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_CACHE_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_CACHE_H_
8 // A class that gets malware details from the HTTP Cache.
9 // An instance of this class is generated by MalwareDetails.
14 #include "base/callback.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/memory/linked_ptr.h"
17 #include "base/memory/ref_counted.h"
18 #include "chrome/browser/safe_browsing/report.pb.h"
19 #include "net/base/completion_callback.h"
20 #include "net/url_request/url_fetcher_delegate.h"
24 class URLRequestContext
;
27 namespace safe_browsing
{
29 // Maps a URL to its Resource.
30 typedef base::hash_map
<
32 linked_ptr
<safe_browsing::ClientMalwareReportRequest::Resource
> > ResourceMap
;
35 class MalwareDetailsCacheCollector
36 : public base::RefCountedThreadSafe
<MalwareDetailsCacheCollector
>,
37 public net::URLFetcherDelegate
{
40 MalwareDetailsCacheCollector();
42 // We use |request_context_getter|, we modify |resources| and
43 // |result|, and we call |callback|, so they must all remain alive
44 // for the lifetime of this object.
45 void StartCacheCollection(
46 net::URLRequestContextGetter
* request_context_getter
,
47 safe_browsing::ResourceMap
* resources
,
49 const base::Closure
& callback
);
51 // Returns whether or not StartCacheCollection has been called.
55 // Implementation of URLFetcher::Delegate. Called after the request
56 // completes (either successfully or with failure).
57 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
60 friend class base::RefCountedThreadSafe
<MalwareDetailsCacheCollector
>;
62 virtual ~MalwareDetailsCacheCollector();
64 // Points to the url for which we are fetching the HTTP cache entry or
66 safe_browsing::ResourceMap::iterator resources_it_
;
68 // Points to the resources_ map in the MalwareDetails.
69 safe_browsing::ResourceMap
* resources_
;
71 // Points to the cache_result_ in the MalwareDetails.
74 // Method we call when we are done. The caller must be alive for the
75 // whole time, we are modifying its state (see above).
76 base::Closure callback_
;
78 // Set to true as soon as StartCacheCollection is called.
81 // Used to get a pointer to the current request context.
82 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
84 // The current URLFetcher.
85 scoped_ptr
<net::URLFetcher
> current_fetch_
;
87 // Returns the resource from resources_ that corresponds to |url|
88 safe_browsing::ClientMalwareReportRequest::Resource
* GetResource(
91 // Creates a new URLFetcher and starts it.
94 // Read the HTTP response from |source| and add it to |pb_resource|.
96 safe_browsing::ClientMalwareReportRequest::Resource
* pb_resource
,
97 const net::URLFetcher
* source
);
99 // Read the body |data| and add it to |pb_resource|.
101 safe_browsing::ClientMalwareReportRequest::Resource
* pb_resource
,
102 const std::string
& data
);
104 // Called when we are done.
105 void AllDone(bool success
);
107 // Advances to the next entry in resources_it_.
111 #endif // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_CACHE_H_