Home | History | Annotate | Download | only in safe_browsing
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_CACHE_H_
      6 #define CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_CACHE_H_
      7 #pragma once
      8 
      9 // A class that gets malware details from the HTTP Cache.
     10 // An instance of this class is generated by MalwareDetails.
     11 
     12 #include <string>
     13 #include <vector>
     14 
     15 #include "base/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 "chrome/common/net/url_fetcher.h"
     20 #include "net/base/completion_callback.h"
     21 
     22 namespace net {
     23 class URLRequestContext;
     24 }
     25 
     26 class MalwareDetailsFactory;
     27 
     28 namespace safe_browsing {
     29 
     30 // Maps a URL to its Resource.
     31 typedef base::hash_map<
     32   std::string,
     33   linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > ResourceMap;
     34 }
     35 
     36 class MalwareDetailsCacheCollector
     37     : public base::RefCountedThreadSafe<MalwareDetailsCacheCollector>,
     38       public URLFetcher::Delegate {
     39 
     40  public:
     41   MalwareDetailsCacheCollector();
     42   virtual ~MalwareDetailsCacheCollector();
     43 
     44   // We use |request_context_getter|, we modify |resources| and
     45   // |result|, and we call |callback|, so they must all remain alive
     46   // for the lifetime of this object.
     47   void StartCacheCollection(
     48       net::URLRequestContextGetter* request_context_getter,
     49       safe_browsing::ResourceMap* resources,
     50       bool* result,
     51       Task* callback);
     52 
     53   // Returns whether or not StartCacheCollection has been called.
     54   bool HasStarted();
     55 
     56  protected:
     57   // Implementation of URLFetcher::Delegate. Called after the request
     58   // completes (either successfully or with failure).
     59   virtual void OnURLFetchComplete(const URLFetcher* source,
     60                                   const GURL& url,
     61                                   const net::URLRequestStatus& status,
     62                                   int response_code,
     63                                   const ResponseCookies& cookies,
     64                                   const std::string& data);
     65 
     66  private:
     67   // Points to the url for which we are fetching the HTTP cache entry or
     68   // redirect chain.
     69   safe_browsing::ResourceMap::iterator resources_it_;
     70 
     71   // Points to the resources_ map in the MalwareDetails.
     72   safe_browsing::ResourceMap* resources_;
     73 
     74   // Points to the cache_result_ in the MalwareDetails.
     75   bool* result_;
     76 
     77   // Method we call when we are done. The caller must be alive for the
     78   // whole time, we are modifying its state (see above).
     79   Task* callback_;
     80 
     81   // Set to true as soon as StartCacheCollection is called.
     82   bool has_started_;
     83 
     84   // Used to get a pointer to the current request context.
     85   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
     86 
     87   // The current URLFetcher.
     88   scoped_ptr<URLFetcher> current_fetch_;
     89 
     90   // Returns the resource from resources_ that corresponds to |url|
     91   safe_browsing::ClientMalwareReportRequest::Resource* GetResource(
     92       const GURL& url);
     93 
     94   // Creates a new URLFetcher and starts it.
     95   void OpenEntry();
     96 
     97   // Read the HTTP response from |source| and add it to |pb_resource|.
     98   void ReadResponse(
     99       safe_browsing::ClientMalwareReportRequest::Resource* pb_resource,
    100       const URLFetcher* source);
    101 
    102   // Read the body |data| and add it to |pb_resource|.
    103   void ReadData(
    104       safe_browsing::ClientMalwareReportRequest::Resource* pb_resource,
    105       const std::string& data);
    106 
    107   // Called when we are done.
    108   void AllDone(bool success);
    109 
    110   // Advances to the next entry in resources_it_.
    111   void AdvanceEntry();
    112 };
    113 
    114 #endif  // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_CACHE_H_
    115