Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / safe_browsing / malware_details.h
blob4da96b644e01cbab02090ec841c0269be02eeeb3
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.
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_
8 // A class that encapsulates the detailed malware reports sent when
9 // users opt-in to do so from the malware warning page.
11 // An instance of this class is generated when a malware warning page
12 // is shown (SafeBrowsingBlockingPage).
14 #include <string>
15 #include <vector>
17 #include "base/containers/hash_tables.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/memory/linked_ptr.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "chrome/browser/safe_browsing/report.pb.h"
23 #include "chrome/browser/safe_browsing/ui_manager.h"
24 #include "content/public/browser/web_contents_observer.h"
25 #include "net/base/completion_callback.h"
27 namespace net {
28 class URLRequestContextGetter;
31 class MalwareDetailsCacheCollector;
32 class MalwareDetailsRedirectsCollector;
33 class MalwareDetailsFactory;
34 class Profile;
35 struct SafeBrowsingHostMsg_MalwareDOMDetails_Node;
37 namespace safe_browsing {
38 // Maps a URL to its Resource.
39 typedef base::hash_map<
40 std::string,
41 linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > ResourceMap;
44 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>,
45 public content::WebContentsObserver {
46 public:
47 typedef SafeBrowsingUIManager::UnsafeResource UnsafeResource;
49 // Constructs a new MalwareDetails instance, using the factory.
50 static MalwareDetails* NewMalwareDetails(
51 SafeBrowsingUIManager* ui_manager,
52 content::WebContents* web_contents,
53 const UnsafeResource& resource);
55 // Makes the passed |factory| the factory used to instanciate
56 // SafeBrowsingBlockingPage objects. Useful for tests.
57 static void RegisterFactory(MalwareDetailsFactory* factory) {
58 factory_ = factory;
61 // The SafeBrowsingBlockingPage calls this from the IO thread when
62 // the user is leaving the blocking page and has opted-in to sending
63 // the report. We start the redirection urls collection from history service
64 // in UI thread; then do cache collection back in IO thread. We also record
65 // if the user did proceed with the warning page, and how many times user
66 // visited this page before. When we are done, we send the report.
67 void FinishCollection(bool did_proceed, int num_visits);
69 void OnCacheCollectionReady();
71 void OnRedirectionCollectionReady();
73 // content::WebContentsObserver implementation.
74 bool OnMessageReceived(const IPC::Message& message) override;
76 protected:
77 friend class MalwareDetailsFactoryImpl;
79 MalwareDetails(SafeBrowsingUIManager* ui_manager,
80 content::WebContents* web_contents,
81 const UnsafeResource& resource);
83 ~MalwareDetails() override;
85 // Called on the IO thread with the DOM details.
86 virtual void AddDOMDetails(
87 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params);
89 Profile* profile_;
91 // The report protocol buffer.
92 scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_;
94 // Used to get a pointer to the HTTP cache.
95 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
97 private:
98 friend class base::RefCountedThreadSafe<MalwareDetails>;
100 // Starts the collection of the report.
101 void StartCollection();
103 // Whether the url is "public" so we can add it to the report.
104 bool IsReportableUrl(const GURL& url) const;
106 // Finds an existing Resource for the given url, or creates a new
107 // one if not found, and adds it to |resources_|. Returns the
108 // found/created resource.
109 safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource(
110 const GURL& url);
112 // Adds a Resource to resources_ with the given parent-child
113 // relationship. |parent| and |tagname| can be empty, |children| can be NULL.
114 void AddUrl(const GURL& url,
115 const GURL& parent,
116 const std::string& tagname,
117 const std::vector<GURL>* children);
119 // Message handler.
120 void OnReceivedMalwareDOMDetails(
121 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params);
123 void AddRedirectUrlList(const std::vector<GURL>& urls);
125 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
127 const UnsafeResource resource_;
129 // For every Url we collect we create a Resource message. We keep
130 // them in a map so we can avoid duplicates.
131 safe_browsing::ResourceMap resources_;
133 // Result from the cache extractor.
134 bool cache_result_;
136 // Whether user did proceed with the safe browsing blocking page or
137 // not.
138 bool did_proceed_;
140 // How many times this user has visited this page before.
141 int num_visits_;
143 // The factory used to instanciate SafeBrowsingBlockingPage objects.
144 // Usefull for tests, so they can provide their own implementation of
145 // SafeBrowsingBlockingPage.
146 static MalwareDetailsFactory* factory_;
148 // Used to collect details from the HTTP Cache.
149 scoped_refptr<MalwareDetailsCacheCollector> cache_collector_;
151 // Used to collect redirect urls from the history service
152 scoped_refptr<MalwareDetailsRedirectsCollector> redirects_collector_;
154 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, MalwareDOMDetails);
155 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCache);
156 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCacheNoEntries);
157 FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HistoryServiceUrls);
159 DISALLOW_COPY_AND_ASSIGN(MalwareDetails);
162 // Factory for creating MalwareDetails. Useful for tests.
163 class MalwareDetailsFactory {
164 public:
165 virtual ~MalwareDetailsFactory() { }
167 virtual MalwareDetails* CreateMalwareDetails(
168 SafeBrowsingUIManager* ui_manager,
169 content::WebContents* web_contents,
170 const SafeBrowsingUIManager::UnsafeResource& unsafe_resource) = 0;
173 #endif // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_