| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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). |
| 14 // SafeBrowsing service when the warning goes away. | |
| 15 | 14 |
| 16 #include <string> | 15 #include <string> |
| 17 #include <vector> | 16 #include <vector> |
| 18 | 17 |
| 19 #include "base/hash_tables.h" | 18 #include "base/hash_tables.h" |
| 20 #include "base/memory/linked_ptr.h" | 19 #include "base/memory/linked_ptr.h" |
| 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/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 SafeBrowsingHostMsg_MalwareDOMDetails_Node; | 28 struct SafeBrowsingHostMsg_MalwareDOMDetails_Node; |
| 28 | 29 |
| 30 class MalwareDetailsCacheCollector; |
| 29 class MalwareDetailsFactory; | 31 class MalwareDetailsFactory; |
| 30 | 32 |
| 33 namespace safe_browsing { |
| 34 // Maps a URL to its Resource. |
| 35 typedef base::hash_map< |
| 36 std::string, |
| 37 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > ResourceMap; |
| 38 } |
| 39 |
| 31 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>, | 40 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>, |
| 32 public TabContentsObserver { | 41 public TabContentsObserver { |
| 33 public: | 42 public: |
| 34 // Constructs a new MalwareDetails instance, using the factory. | 43 // Constructs a new MalwareDetails instance, using the factory. |
| 35 static MalwareDetails* NewMalwareDetails( | 44 static MalwareDetails* NewMalwareDetails( |
| 45 SafeBrowsingService* sb_service, |
| 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 |
| 45 // The SafeBrowsingService calls this from the IO thread, to get the | 55 // The SafeBrowsingBlockingPage calls this from the IO thread when |
| 46 // serialized report as a string and send it over. | 56 // the user is leaving the blocking page and has opted-in to sending |
| 47 const std::string* GetSerializedReport(); | 57 // the report. We start the cache collection, and when we are done, |
| 58 // we send the report. |
| 59 void FinishCollection(); |
| 60 |
| 61 void OnCacheCollectionReady(); |
| 48 | 62 |
| 49 // TabContentsObserver implementation. | 63 // TabContentsObserver implementation. |
| 50 virtual bool OnMessageReceived(const IPC::Message& message); | 64 virtual bool OnMessageReceived(const IPC::Message& message); |
| 51 | 65 |
| 52 protected: | 66 protected: |
| 53 friend class MalwareDetailsFactoryImpl; | 67 friend class MalwareDetailsFactoryImpl; |
| 54 | 68 |
| 55 MalwareDetails(TabContents* tab_contents, | 69 MalwareDetails(SafeBrowsingService* sb_service, |
| 70 TabContents* tab_contents, |
| 56 const SafeBrowsingService::UnsafeResource& resource); | 71 const SafeBrowsingService::UnsafeResource& resource); |
| 57 | 72 |
| 73 virtual ~MalwareDetails(); |
| 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 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); | 77 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); |
| 61 | 78 |
| 62 virtual ~MalwareDetails(); | 79 // The report protocol buffer. |
| 80 scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_; |
| 81 |
| 82 // Used to get a pointer to the HTTP cache. |
| 83 net::URLRequestContextGetter* request_context_getter_; |
| 63 | 84 |
| 64 private: | 85 private: |
| 65 friend class base::RefCountedThreadSafe<MalwareDetails>; | 86 friend class base::RefCountedThreadSafe<MalwareDetails>; |
| 66 | 87 |
| 67 // Maps a URL to its Resource. | |
| 68 typedef base::hash_map< | |
| 69 std::string, | |
| 70 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > | |
| 71 ResourceMap; | |
| 72 | |
| 73 // Starts the collection of the report. | 88 // Starts the collection of the report. |
| 74 void StartCollection(); | 89 void StartCollection(); |
| 75 | 90 |
| 76 // Whether the url is "public" so we can add it to the report. | 91 // Whether the url is "public" so we can add it to the report. |
| 77 bool IsPublicUrl(const GURL& url) const; | 92 bool IsPublicUrl(const GURL& url) const; |
| 78 | 93 |
| 79 // Finds an existing Resource for the given url, or creates a new | 94 // Finds an existing Resource for the given url, or creates a new |
| 80 // one if not found, and adds it to |resources_|. Returns the | 95 // one if not found, and adds it to |resources_|. Returns the |
| 81 // found/created resource. | 96 // found/created resource. |
| 82 safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource( | 97 safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource( |
| 83 const GURL& url); | 98 const GURL& url); |
| 84 | 99 |
| 85 // Adds a Resource to resources_ with the given parent-child | 100 // Adds a Resource to resources_ with the given parent-child |
| 86 // relationship. |parent| and |tagname| can be empty, |children| can be NULL. | 101 // relationship. |parent| and |tagname| can be empty, |children| can be NULL. |
| 87 void AddUrl(const GURL& url, | 102 void AddUrl(const GURL& url, |
| 88 const GURL& parent, | 103 const GURL& parent, |
| 89 const std::string& tagname, | 104 const std::string& tagname, |
| 90 const std::vector<GURL>* children); | 105 const std::vector<GURL>* children); |
| 91 | 106 |
| 92 // Message handler. | 107 // Message handler. |
| 93 void OnReceivedMalwareDOMDetails( | 108 void OnReceivedMalwareDOMDetails( |
| 94 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); | 109 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); |
| 95 | 110 |
| 111 scoped_refptr<SafeBrowsingService> sb_service_; |
| 112 |
| 96 const SafeBrowsingService::UnsafeResource resource_; | 113 const SafeBrowsingService::UnsafeResource resource_; |
| 97 | 114 |
| 98 // For every Url we collect we create a Resource message. We keep | 115 // For every Url we collect we create a Resource message. We keep |
| 99 // them in a map so we can avoid duplicates. | 116 // them in a map so we can avoid duplicates. |
| 100 ResourceMap resources_; | 117 safe_browsing::ResourceMap resources_; |
| 101 | 118 |
| 102 // The report protocol buffer. | 119 // Result from the cache extractor. |
| 103 scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_; | 120 int cache_result_; |
| 104 | 121 |
| 105 // The factory used to instanciate SafeBrowsingBlockingPage objects. | 122 // The factory used to instanciate SafeBrowsingBlockingPage objects. |
| 106 // Usefull for tests, so they can provide their own implementation of | 123 // Usefull for tests, so they can provide their own implementation of |
| 107 // SafeBrowsingBlockingPage. | 124 // SafeBrowsingBlockingPage. |
| 108 static MalwareDetailsFactory* factory_; | 125 static MalwareDetailsFactory* factory_; |
| 109 | 126 |
| 127 // Used to collect details from the HTTP Cache. |
| 128 scoped_refptr<MalwareDetailsCacheCollector> cache_collector_; |
| 129 |
| 110 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, MalwareDOMDetails); | 130 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, MalwareDOMDetails); |
| 131 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCache); |
| 132 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCacheNoEntries); |
| 111 | 133 |
| 112 DISALLOW_COPY_AND_ASSIGN(MalwareDetails); | 134 DISALLOW_COPY_AND_ASSIGN(MalwareDetails); |
| 113 }; | 135 }; |
| 114 | 136 |
| 115 // Factory for creating MalwareDetails. Useful for tests. | 137 // Factory for creating MalwareDetails. Useful for tests. |
| 116 class MalwareDetailsFactory { | 138 class MalwareDetailsFactory { |
| 117 public: | 139 public: |
| 118 virtual ~MalwareDetailsFactory() { } | 140 virtual ~MalwareDetailsFactory() { } |
| 119 | 141 |
| 120 virtual MalwareDetails* CreateMalwareDetails( | 142 virtual MalwareDetails* CreateMalwareDetails( |
| 143 SafeBrowsingService* sb_service, |
| 121 TabContents* tab_contents, | 144 TabContents* tab_contents, |
| 122 const SafeBrowsingService::UnsafeResource& unsafe_resource) = 0; | 145 const SafeBrowsingService::UnsafeResource& unsafe_resource) = 0; |
| 123 }; | 146 }; |
| 124 | 147 |
| 125 #endif // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ | 148 #endif // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_ |
| OLD | NEW |