1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_SAFE_BROWSING_PING_MANAGER_H_
6#define CHROME_BROWSER_SAFE_BROWSING_PING_MANAGER_H_
7
8// A class that reports safebrowsing statistics to Google's SafeBrowsing
9// servers.
10#include <set>
11#include <string>
12#include <vector>
13
14#include "base/gtest_prod_util.h"
15#include "base/memory/scoped_ptr.h"
16#include "chrome/browser/safe_browsing/protocol_manager_helper.h"
17#include "chrome/browser/safe_browsing/safe_browsing_util.h"
18#include "net/url_request/url_fetcher_delegate.h"
19#include "url/gurl.h"
20
21namespace net {
22class URLRequestContextGetter;
23}  // namespace net
24
25
26class SafeBrowsingPingManager : public net::URLFetcherDelegate {
27 public:
28  virtual ~SafeBrowsingPingManager();
29
30  // Create an instance of the safe browsing ping manager.
31  static SafeBrowsingPingManager* Create(
32      net::URLRequestContextGetter* request_context_getter,
33      const SafeBrowsingProtocolConfig& config);
34
35  // net::URLFetcherDelegate interface.
36  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
37
38  // For UMA users we report to Google when a SafeBrowsing interstitial is shown
39  // to the user.  |threat_type| should be one of the types known by
40  // SafeBrowsingHitUrl.
41  void ReportSafeBrowsingHit(const GURL& malicious_url,
42                             const GURL& page_url,
43                             const GURL& referrer_url,
44                             bool is_subresource,
45                             SBThreatType threat_type,
46                             const std::string& post_data);
47
48  // Users can opt-in on the SafeBrowsing interstitial to send detailed
49  // malware reports. |report| is the serialized report.
50  void ReportMalwareDetails(const std::string& report);
51
52 private:
53  FRIEND_TEST_ALL_PREFIXES(SafeBrowsingPingManagerTest,
54                           TestSafeBrowsingHitUrl);
55  FRIEND_TEST_ALL_PREFIXES(SafeBrowsingPingManagerTest,
56                           TestMalwareDetailsUrl);
57
58  typedef std::set<const net::URLFetcher*> Reports;
59
60  // Constructs a SafeBrowsingPingManager that issues network requests
61  // using |request_context_getter|.
62  SafeBrowsingPingManager(
63      net::URLRequestContextGetter* request_context_getter,
64      const SafeBrowsingProtocolConfig& config);
65
66  // Generates URL for reporting safe browsing hits for UMA users.
67  GURL SafeBrowsingHitUrl(
68      const GURL& malicious_url, const GURL& page_url, const GURL& referrer_url,
69      bool is_subresource,
70      SBThreatType threat_type) const;
71  // Generates URL for reporting malware details for users who opt-in.
72  GURL MalwareDetailsUrl() const;
73
74  // Current product version sent in each request.
75  std::string version_;
76
77  // The safe browsing client name sent in each request.
78  std::string client_name_;
79
80  // The context we use to issue network requests.
81  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
82
83  // URL prefix where browser reports hits to the safebrowsing list and
84  // sends detaild malware reports for UMA users.
85  std::string url_prefix_;
86
87  // Track outstanding SafeBrowsing report fetchers for clean up.
88  // We add both "hit" and "detail" fetchers in this set.
89  Reports safebrowsing_reports_;
90
91  DISALLOW_COPY_AND_ASSIGN(SafeBrowsingPingManager);
92};
93
94#endif  // CHROME_BROWSER_SAFE_BROWSING_PING_MANAGER_H_
95