NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / safe_browsing / browser_feature_extractor.h
blob4821c65e00d00634b26e67f8d3f5781b3e18a428
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.
4 //
5 // BrowserFeatureExtractor computes various browser features for client-side
6 // phishing detection. For now it does a bunch of lookups in the history
7 // service to see whether a particular URL has been visited before by the
8 // user.
10 #ifndef CHROME_BROWSER_SAFE_BROWSING_BROWSER_FEATURE_EXTRACTOR_H_
11 #define CHROME_BROWSER_SAFE_BROWSING_BROWSER_FEATURE_EXTRACTOR_H_
13 #include <map>
14 #include <set>
15 #include <string>
16 #include <utility>
17 #include <vector>
19 #include "base/basictypes.h"
20 #include "base/callback.h"
21 #include "base/containers/hash_tables.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/task/cancelable_task_tracker.h"
24 #include "base/time/time.h"
25 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
26 #include "chrome/browser/safe_browsing/ui_manager.h"
27 #include "components/history/core/browser/history_types.h"
28 #include "content/public/common/resource_type.h"
29 #include "url/gurl.h"
32 class HistoryService;
34 namespace content {
35 class WebContents;
38 namespace safe_browsing {
39 class ClientMalwareRequest;
40 class ClientPhishingRequest;
41 class ClientSideDetectionHost;
43 struct IPUrlInfo {
44 // The url on the bad IP address.
45 std::string url;
46 std::string method;
47 std::string referrer;
48 content::ResourceType resource_type;
50 IPUrlInfo(const std::string& url,
51 const std::string& method,
52 const std::string& referrer,
53 const content::ResourceType& resource_type);
54 ~IPUrlInfo();
57 typedef std::map<std::string, std::vector<IPUrlInfo> > IPUrlMap;
59 struct BrowseInfo {
60 // The URL we're currently browsing.
61 GURL url;
63 // List of IPv4 and IPv6 addresses from which content was requested
64 // together with the hosts on it, while browsing to the |url|.
65 IPUrlMap ips;
67 // If a SafeBrowsing interstitial was shown for the current URL
68 // this will contain the UnsafeResource struct for that URL.
69 scoped_ptr<SafeBrowsingUIManager::UnsafeResource> unsafe_resource;
71 // List of redirects that lead to the first page on the current host and
72 // the current url respectively. These may be the same if the current url
73 // is the first page on its host.
74 std::vector<GURL> host_redirects;
75 std::vector<GURL> url_redirects;
77 // URL of the referrer of this URL load.
78 GURL referrer;
80 // The HTTP status code from this navigation.
81 int http_status_code;
83 BrowseInfo();
84 ~BrowseInfo();
87 // All methods of this class must be called on the UI thread (including
88 // the constructor).
89 class BrowserFeatureExtractor {
90 public:
91 // Called when feature extraction is done. The first argument will be
92 // true iff feature extraction succeeded. The second argument is the
93 // phishing request which was modified by the feature extractor. The
94 // DoneCallback takes ownership of the request object.
95 typedef base::Callback<void(bool, scoped_ptr<ClientPhishingRequest>)>
96 DoneCallback;
97 typedef base::Callback<void(bool, scoped_ptr<ClientMalwareRequest>)>
98 MalwareDoneCallback;
100 // The caller keeps ownership of the tab and host objects and is
101 // responsible for ensuring that they stay valid for the entire
102 // lifetime of this object.
103 BrowserFeatureExtractor(content::WebContents* tab,
104 ClientSideDetectionHost* host);
106 // The destructor will cancel any pending requests.
107 virtual ~BrowserFeatureExtractor();
109 // Begins extraction of the browser features. We take ownership
110 // of the request object until |callback| is called (see DoneCallback above)
111 // and will write the extracted features to the feature map. Once the
112 // feature extraction is complete, |callback| is run on the UI thread. We
113 // take ownership of the |callback| object. |info| may not be valid after
114 // ExtractFeatures returns. This method must run on the UI thread.
115 virtual void ExtractFeatures(const BrowseInfo* info,
116 ClientPhishingRequest* request,
117 const DoneCallback& callback);
119 // Begins extraction of the malware related features. We take ownership
120 // of the request object until |callback| is called. Once feature extraction
121 // is complete, |callback| will run on the UI thread. |info| is not expected
122 // to stay valid after ExtractMalwareFeatures returns. All IPs stored in
123 // |info| will be cleared by calling this function.
124 virtual void ExtractMalwareFeatures(BrowseInfo* info,
125 ClientMalwareRequest* request,
126 const MalwareDoneCallback& callback);
128 private:
129 // Synchronous browser feature extraction.
130 void ExtractBrowseInfoFeatures(const BrowseInfo& info,
131 ClientPhishingRequest* request);
133 // Actually starts feature extraction (does the real work).
134 void StartExtractFeatures(scoped_ptr<ClientPhishingRequest> request,
135 const DoneCallback& callback);
137 // HistoryService callback which is called when we're done querying URL visits
138 // in the history.
139 void QueryUrlHistoryDone(scoped_ptr<ClientPhishingRequest> request,
140 const DoneCallback& callback,
141 bool success,
142 const history::URLRow& row,
143 const history::VisitVector& visits);
145 // HistoryService callback which is called when we're done querying HTTP host
146 // visits in the history.
147 void QueryHttpHostVisitsDone(scoped_ptr<ClientPhishingRequest> request,
148 const DoneCallback& callback,
149 bool success,
150 int num_visits,
151 base::Time first_visit);
153 // HistoryService callback which is called when we're done querying HTTPS host
154 // visits in the history.
155 void QueryHttpsHostVisitsDone(scoped_ptr<ClientPhishingRequest> request,
156 const DoneCallback& callback,
157 bool success,
158 int num_visits,
159 base::Time first_visit);
161 // Helper function which sets the host history features given the
162 // number of host visits and the time of the fist host visit. Set
163 // |is_http_query| to true if the URL scheme is HTTP and to false if
164 // the scheme is HTTPS.
165 void SetHostVisitsFeatures(int num_visits,
166 base::Time first_visit,
167 bool is_http_query,
168 ClientPhishingRequest* request);
170 // Helper function which gets the history server if possible. If the pointer
171 // is set it will return true and false otherwise.
172 bool GetHistoryService(HistoryService** history);
174 // Helper function which is called when we're done filtering out benign IPs
175 // on the IO thread. This function is called on the UI thread.
176 void FinishExtractMalwareFeatures(scoped_ptr<IPUrlMap> bad_ips,
177 MalwareDoneCallback callback,
178 scoped_ptr<ClientMalwareRequest> request);
180 content::WebContents* tab_;
181 ClientSideDetectionHost* host_;
182 base::CancelableTaskTracker cancelable_task_tracker_;
183 base::WeakPtrFactory<BrowserFeatureExtractor> weak_factory_;
185 DISALLOW_COPY_AND_ASSIGN(BrowserFeatureExtractor);
188 } // namespace safe_browsing
189 #endif // CHROME_BROWSER_SAFE_BROWSING_BROWSER_FEATURE_EXTRACTOR_H_