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