| 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" |
| 21 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 22 #include "chrome/browser/safe_browsing/report.pb.h" | 21 #include "chrome/browser/safe_browsing/report.pb.h" |
| 23 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 22 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 24 #include "content/browser/tab_contents/tab_contents_observer.h" | 23 #include "content/browser/tab_contents/tab_contents_observer.h" |
| 24 #include "net/base/completion_callback.h" |
| 25 | 25 |
| 26 class TabContents; | 26 class TabContents; |
| 27 struct SafeBrowsingHostMsg_MalwareDOMDetails_Node; | 27 struct SafeBrowsingHostMsg_MalwareDOMDetails_Node; |
| 28 | 28 |
| 29 class MalwareDetailsCacheCollector; |
| 29 class MalwareDetailsFactory; | 30 class MalwareDetailsFactory; |
| 30 | 31 |
| 32 namespace safe_browsing { |
| 33 // Maps a URL to its Resource. |
| 34 typedef base::hash_map< |
| 35 std::string, |
| 36 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > ResourceMap; |
| 37 } |
| 38 |
| 31 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>, | 39 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>, |
| 32 public TabContentsObserver { | 40 public TabContentsObserver { |
| 33 public: | 41 public: |
| 34 // Constructs a new MalwareDetails instance, using the factory. | 42 // Constructs a new MalwareDetails instance, using the factory. |
| 35 static MalwareDetails* NewMalwareDetails( | 43 static MalwareDetails* NewMalwareDetails( |
| 44 SafeBrowsingService* sb_service, |
| 36 TabContents* tab_contents, | 45 TabContents* tab_contents, |
| 37 const SafeBrowsingService::UnsafeResource& resource); | 46 const SafeBrowsingService::UnsafeResource& resource); |
| 38 | 47 |
| 39 // Makes the passed |factory| the factory used to instanciate | 48 // Makes the passed |factory| the factory used to instanciate |
| 40 // SafeBrowsingBlockingPage objects. Useful for tests. | 49 // SafeBrowsingBlockingPage objects. Useful for tests. |
| 41 static void RegisterFactory(MalwareDetailsFactory* factory) { | 50 static void RegisterFactory(MalwareDetailsFactory* factory) { |
| 42 factory_ = factory; | 51 factory_ = factory; |
| 43 } | 52 } |
| 44 | 53 |
| 45 // The SafeBrowsingService calls this from the IO thread, to get the | 54 // Callback with the malware details, serialized. See GetSerializedReport. |
| 46 // serialized report as a string and send it over. | 55 typedef Callback1<scoped_refptr<MalwareDetails> >::Type |
| 47 const std::string* GetSerializedReport(); | 56 MalwareDetailsCallback; |
| 57 |
| 58 // The SafeBrowsingBlockingPage calls this from the IO thread when |
| 59 // the user is leaving the blocking page and has opted-in to sending |
| 60 // the report. We start the cache collection, and when we are done, |
| 61 // we send the report. |
| 62 void FinishCollection(); |
| 63 |
| 64 void OnCacheCollectionReady(int result); |
| 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(SafeBrowsingService* sb_service, |
| 73 TabContents* tab_contents, |
| 56 const SafeBrowsingService::UnsafeResource& resource); | 74 const SafeBrowsingService::UnsafeResource& resource); |
| 57 | 75 |
| 76 virtual ~MalwareDetails(); |
| 77 |
| 58 // Called on the IO thread with the DOM details. | 78 // Called on the IO thread with the DOM details. |
| 59 virtual void AddDOMDetails( | 79 virtual void AddDOMDetails( |
| 60 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); | 80 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); |
| 61 | 81 |
| 62 virtual ~MalwareDetails(); | 82 // The report protocol buffer. |
| 83 scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_; |
| 84 |
| 85 // Used to get a pointer to the HTTP cache. |
| 86 URLRequestContextGetter* request_context_getter_; |
| 63 | 87 |
| 64 private: | 88 private: |
| 65 friend class base::RefCountedThreadSafe<MalwareDetails>; | 89 friend class base::RefCountedThreadSafe<MalwareDetails>; |
| 66 | 90 |
| 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. | 91 // Starts the collection of the report. |
| 74 void StartCollection(); | 92 void StartCollection(); |
| 75 | 93 |
| 76 // Whether the url is "public" so we can add it to the report. | 94 // Whether the url is "public" so we can add it to the report. |
| 77 bool IsPublicUrl(const GURL& url) const; | 95 bool IsPublicUrl(const GURL& url) const; |
| 78 | 96 |
| 79 // Finds an existing Resource for the given url, or creates a new | 97 // Finds an existing Resource for the given url, or creates a new |
| 80 // one if not found, and adds it to |resources_|. Returns the | 98 // one if not found, and adds it to |resources_|. Returns the |
| 81 // found/created resource. | 99 // found/created resource. |
| 82 safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource( | 100 safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource( |
| 83 const GURL& url); | 101 const GURL& url); |
| 84 | 102 |
| 85 // Adds a Resource to resources_ with the given parent-child | 103 // Adds a Resource to resources_ with the given parent-child |
| 86 // relationship. |parent| and |tagname| can be empty, |children| can be NULL. | 104 // relationship. |parent| and |tagname| can be empty, |children| can be NULL. |
| 87 void AddUrl(const GURL& url, | 105 void AddUrl(const GURL& url, |
| 88 const GURL& parent, | 106 const GURL& parent, |
| 89 const std::string& tagname, | 107 const std::string& tagname, |
| 90 const std::vector<GURL>* children); | 108 const std::vector<GURL>* children); |
| 91 | 109 |
| 92 // Message handler. | 110 // Message handler. |
| 93 void OnReceivedMalwareDOMDetails( | 111 void OnReceivedMalwareDOMDetails( |
| 94 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); | 112 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params); |
| 95 | 113 |
| 114 SafeBrowsingService* sb_service_; |
| 115 |
| 96 const SafeBrowsingService::UnsafeResource resource_; | 116 const SafeBrowsingService::UnsafeResource resource_; |
| 97 | 117 |
| 98 // For every Url we collect we create a Resource message. We keep | 118 // For every Url we collect we create a Resource message. We keep |
| 99 // them in a map so we can avoid duplicates. | 119 // them in a map so we can avoid duplicates. |
| 100 ResourceMap resources_; | 120 safe_browsing::ResourceMap resources_; |
| 101 | |
| 102 // The report protocol buffer. | |
| 103 scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_; | |
| 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 |