Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 // A class that encapsulates the detailed malware reports sent when | 9 // A class that encapsulates the detailed malware reports sent when |
| 10 // users opt-in to do so from the malware warning page. | 10 // users opt-in to do so from the malware warning page. |
| 11 | 11 |
| 12 // An instance of this class is generated when a malware warning page | 12 // An instance of this class is generated when a malware warning page |
| 13 // is shown (SafeBrowsingBlockingPage). It is passed on to the | 13 // is shown (SafeBrowsingBlockingPage). It is passed on to the |
| 14 // SafeBrowsing service when the warning goes away. | 14 // SafeBrowsing service when the warning goes away. |
| 15 | 15 |
| 16 #include <string> | 16 #include <string> |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "base/hash_tables.h" | 19 #include "base/hash_tables.h" |
| 20 #include "base/linked_ptr.h" | 20 #include "base/linked_ptr.h" |
| 21 #include "base/scoped_ptr.h" | 21 #include "base/scoped_ptr.h" |
| 22 #include "chrome/browser/safe_browsing/report.pb.h" | 22 #include "chrome/browser/safe_browsing/report.pb.h" |
| 23 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 23 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 24 #include "content/browser/tab_contents/tab_contents_observer.h" | 24 #include "content/browser/tab_contents/tab_contents_observer.h" |
| 25 #include "net/base/completion_callback.h" | |
| 25 | 26 |
| 26 class TabContents; | 27 class TabContents; |
| 27 struct ViewHostMsg_MalwareDOMDetails_Params; | 28 struct ViewHostMsg_MalwareDOMDetails_Params; |
| 28 | 29 |
| 30 namespace disk_cache { | |
| 31 class Backend; | |
| 32 class Entry; | |
| 33 } | |
| 34 namespace net { | |
| 35 class IOBuffer; | |
| 36 class URLRequestContext; | |
| 37 } | |
| 38 | |
| 29 class MalwareDetailsFactory; | 39 class MalwareDetailsFactory; |
| 30 | 40 |
| 31 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>, | 41 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>, |
| 32 public TabContentsObserver { | 42 public TabContentsObserver { |
| 33 public: | 43 public: |
| 34 // Constructs a new MalwareDetails instance, using the factory. | 44 // Constructs a new MalwareDetails instance, using the factory. |
| 35 static MalwareDetails* NewMalwareDetails( | 45 static MalwareDetails* NewMalwareDetails( |
| 36 TabContents* tab_contents, | 46 TabContents* tab_contents, |
| 37 const SafeBrowsingService::UnsafeResource& resource); | 47 const SafeBrowsingService::UnsafeResource& resource); |
| 38 | 48 |
| 39 // Makes the passed |factory| the factory used to instanciate | 49 // Makes the passed |factory| the factory used to instanciate |
| 40 // SafeBrowsingBlockingPage objects. Useful for tests. | 50 // SafeBrowsingBlockingPage objects. Useful for tests. |
| 41 static void RegisterFactory(MalwareDetailsFactory* factory) { | 51 static void RegisterFactory(MalwareDetailsFactory* factory) { |
| 42 factory_ = factory; | 52 factory_ = factory; |
| 43 } | 53 } |
| 44 | 54 |
| 55 // Callback with the malware details, serialized. See GetSerializedReport. | |
| 56 typedef Callback1<scoped_refptr<MalwareDetails> >::Type | |
| 57 MalwareDetailsCallback; | |
| 58 | |
| 45 // The SafeBrowsingService calls this from the IO thread, to get the | 59 // The SafeBrowsingService calls this from the IO thread, to get the |
| 46 // serialized report as a string and send it over. | 60 // serialized report as a string and send it over. |
| 61 // |callback| will be notified when the operation completes. | |
| 62 void StartCacheCollection(MalwareDetailsCallback* callback); | |
| 63 | |
| 47 const std::string* GetSerializedReport(); | 64 const std::string* GetSerializedReport(); |
| 48 | 65 |
| 49 // TabContentsObserver implementation. | 66 // TabContentsObserver implementation. |
| 50 virtual bool OnMessageReceived(const IPC::Message& message); | 67 virtual bool OnMessageReceived(const IPC::Message& message); |
| 51 | 68 |
| 52 protected: | 69 protected: |
| 53 friend class MalwareDetailsFactoryImpl; | 70 friend class MalwareDetailsFactoryImpl; |
| 54 | 71 |
| 55 MalwareDetails(TabContents* tab_contents, | 72 MalwareDetails(TabContents* tab_contents, |
| 56 const SafeBrowsingService::UnsafeResource resource); | 73 const SafeBrowsingService::UnsafeResource resource); |
| 57 | 74 |
| 58 // Called on the IO thread with the DOM details. | 75 // Called on the IO thread with the DOM details. |
| 59 virtual void AddDOMDetails( | 76 virtual void AddDOMDetails( |
| 60 const ViewHostMsg_MalwareDOMDetails_Params& params); | 77 const ViewHostMsg_MalwareDOMDetails_Params& params); |
| 61 | 78 |
| 79 virtual net::URLRequestContext* GetURLRequestContext(); | |
| 80 | |
| 62 virtual ~MalwareDetails(); | 81 virtual ~MalwareDetails(); |
|
lzheng
2011/03/05 00:37:50
I think ~MalwareDetails() should be right after Ma
panayiotis
2011/03/29 18:14:55
Done.
| |
| 63 | 82 |
| 83 // The report protocol buffer. | |
| 84 scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_; | |
| 85 | |
| 64 private: | 86 private: |
| 65 friend class base::RefCountedThreadSafe<MalwareDetails>; | 87 friend class base::RefCountedThreadSafe<MalwareDetails>; |
| 66 | 88 |
| 67 // Maps a URL to its Resource. | 89 // Maps a URL to its Resource. |
| 68 typedef base::hash_map< | 90 typedef base::hash_map< |
| 69 std::string, | 91 std::string, |
| 70 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > | 92 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > |
| 71 ResourceMap; | 93 ResourceMap; |
|
lzheng
2011/03/05 00:37:50
I would suggest to move the cache-mining code to i
panayiotis
2011/03/29 18:14:55
Done.
| |
| 72 | 94 |
| 95 enum CacheState { | |
| 96 STATE_NONE, | |
| 97 STATE_OPEN_CACHE, | |
| 98 STATE_OPEN_CACHE_COMPLETE, | |
| 99 STATE_OPEN_ENTRY, | |
| 100 STATE_OPEN_ENTRY_COMPLETE, | |
| 101 STATE_READ_RESPONSE, | |
| 102 STATE_READ_RESPONSE_COMPLETE, | |
| 103 STATE_READ_DATA, | |
| 104 STATE_READ_DATA_COMPLETE, | |
| 105 STATE_CACHE_DONE, | |
| 106 }; | |
| 107 | |
| 73 // Starts the collection of the report. | 108 // Starts the collection of the report. |
| 74 void StartCollection(); | 109 void StartCollection(); |
| 75 | 110 |
| 76 // Whether the url is "public" so we can add it to the report. | 111 // Whether the url is "public" so we can add it to the report. |
| 77 bool IsPublicUrl(const GURL& url) const; | 112 bool IsPublicUrl(const GURL& url) const; |
| 78 | 113 |
| 79 // Finds an existing Resource for the given url, or creates a new | 114 // Finds an existing Resource for the given url, or creates a new |
| 80 // one if not found, and adds it to |resources_|. Returns the | 115 // one if not found, and adds it to |resources_|. Returns the |
| 81 // found/created resource. | 116 // found/created resource. |
| 82 safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource( | 117 safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource( |
| 83 const GURL& url); | 118 const GURL& url); |
| 84 | 119 |
| 85 // Adds a Resource to resources_ with the given parent-child | 120 // Adds a Resource to resources_ with the given parent-child |
| 86 // relationship. |parent| and |tagname| can be empty, |children| can be NULL. | 121 // relationship. |parent| and |tagname| can be empty, |children| can be NULL. |
| 87 void AddUrl(const GURL& url, | 122 void AddUrl(const GURL& url, |
| 88 const GURL& parent, | 123 const GURL& parent, |
| 89 const std::string& tagname, | 124 const std::string& tagname, |
| 90 const std::vector<GURL>* children); | 125 const std::vector<GURL>* children); |
| 91 | 126 |
| 92 // Message handler. | 127 // Message handler. |
| 93 void OnReceivedMalwareDOMDetails( | 128 void OnReceivedMalwareDOMDetails( |
| 94 const ViewHostMsg_MalwareDOMDetails_Params& params); | 129 const ViewHostMsg_MalwareDOMDetails_Params& params); |
| 95 | 130 |
| 131 // Methods for getting the HTML body and HTTP headers from the HTTP cache. | |
| 132 // They run on the IO thread. | |
| 133 void CacheLoop(int result); | |
| 134 int DoOpenCache(); | |
| 135 int DoOpenCacheComplete(int result); | |
| 136 int DoOpenEntry(); | |
| 137 int DoOpenEntryComplete(int result); | |
| 138 int DoReadResponse(); | |
| 139 int DoReadResponseComplete(int result); | |
| 140 int DoReadData(); | |
| 141 int DoReadDataComplete(int result); | |
| 142 void DoCacheDone(int result); | |
| 143 | |
| 96 const SafeBrowsingService::UnsafeResource resource_; | 144 const SafeBrowsingService::UnsafeResource resource_; |
| 97 | 145 |
| 98 // For every Url we collect we create a Resource message. We keep | 146 // For every Url we collect we create a Resource message. We keep |
| 99 // them in a map so we can avoid duplicates. | 147 // them in a map so we can avoid duplicates. |
| 100 ResourceMap resources_; | 148 ResourceMap resources_; |
| 101 | 149 |
| 102 // The report protocol buffer. | |
| 103 scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_; | |
| 104 | |
| 105 // The factory used to instanciate SafeBrowsingBlockingPage objects. | 150 // The factory used to instanciate SafeBrowsingBlockingPage objects. |
| 106 // Usefull for tests, so they can provide their own implementation of | 151 // Usefull for tests, so they can provide their own implementation of |
| 107 // SafeBrowsingBlockingPage. | 152 // SafeBrowsingBlockingPage. |
| 108 static MalwareDetailsFactory* factory_; | 153 static MalwareDetailsFactory* factory_; |
| 109 | 154 |
| 155 // The next cache loop state. | |
| 156 CacheState cache_state_; | |
| 157 | |
| 158 // Used to get a pointer to the HTTP cache. | |
| 159 URLRequestContextGetter* request_context_getter_; | |
| 160 | |
| 161 // The disk cache. | |
| 162 disk_cache::Backend* cache_; | |
| 163 | |
| 164 // Callback for when the cache entry is ready. | |
| 165 net::CompletionCallbackImpl<MalwareDetails> cache_callback_; | |
| 166 | |
| 167 // Callback for when the cache entry is opened. | |
| 168 scoped_refptr< | |
| 169 net::CancelableCompletionCallback<MalwareDetails> > entry_callback_; | |
| 170 | |
| 171 // SafeBrowsingService provides us a method to call when we are done | |
| 172 // with the cache. The service outlives the MalwareDetails (in fact, it | |
| 173 // takes ownership of MalwareDetails). | |
| 174 MalwareDetailsCallback* callback_; | |
| 175 | |
| 176 // Points to the url for which we are fetching the HTTP cache entry or | |
| 177 // redirect chain. | |
| 178 ResourceMap::iterator resources_it_; | |
| 179 | |
| 180 // The current disk entry. | |
| 181 disk_cache::Entry* entry_; | |
| 182 | |
| 183 // The current buffer for reading the contents of the disk entry. | |
| 184 scoped_refptr<net::IOBuffer> buf_; | |
| 185 int buf_len_; | |
| 186 | |
| 110 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, MalwareDOMDetails); | 187 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, MalwareDOMDetails); |
| 188 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCache); | |
| 189 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCacheNoEntries); | |
| 111 | 190 |
| 112 DISALLOW_COPY_AND_ASSIGN(MalwareDetails); | 191 DISALLOW_COPY_AND_ASSIGN(MalwareDetails); |
| 113 }; | 192 }; |
| 114 | 193 |
| 115 // Factory for creating MalwareDetails. Useful for tests. | 194 // Factory for creating MalwareDetails. Useful for tests. |
| 116 class MalwareDetailsFactory { | 195 class MalwareDetailsFactory { |
| 117 public: | 196 public: |
| 118 virtual ~MalwareDetailsFactory() { } | 197 virtual ~MalwareDetailsFactory() { } |
| 119 | 198 |
| 120 virtual MalwareDetails* CreateMalwareDetails( | 199 virtual MalwareDetails* CreateMalwareDetails( |
| 121 TabContents* tab_contents, | 200 TabContents* tab_contents, |
| 122 const SafeBrowsingService::UnsafeResource& unsafe_resource) = 0; | 201 const SafeBrowsingService::UnsafeResource& unsafe_resource) = 0; |
| 123 }; | 202 }; |
| 124 | 203 |
| 125 #endif // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ | 204 #endif // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ |
| OLD | NEW |