jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 1 | // Copyright (c) 2017 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 COMPONENTS_SAFE_BROWSING_BASE_PING_MANAGER_H_ |
| 6 | #define COMPONENTS_SAFE_BROWSING_BASE_PING_MANAGER_H_ |
| 7 | |
| 8 | // A class that reports basic safebrowsing statistics to Google's SafeBrowsing |
| 9 | // servers. |
| 10 | #include <memory> |
| 11 | #include <set> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "base/gtest_prod_util.h" |
| 16 | #include "base/macros.h" |
| 17 | #include "base/memory/ref_counted.h" |
Tim Volodine | e4593847 | 2017-09-21 10:08:22 | [diff] [blame^] | 18 | #include "components/safe_browsing/db/hit_report.h" |
| 19 | #include "components/safe_browsing/db/util.h" |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 20 | #include "content/public/browser/permission_type.h" |
| 21 | #include "net/log/net_log_with_source.h" |
| 22 | #include "net/url_request/url_fetcher_delegate.h" |
| 23 | #include "url/gurl.h" |
| 24 | |
| 25 | namespace net { |
| 26 | class URLRequestContextGetter; |
| 27 | } // namespace net |
| 28 | |
| 29 | namespace safe_browsing { |
| 30 | |
| 31 | class BasePingManager : public net::URLFetcherDelegate { |
| 32 | public: |
| 33 | ~BasePingManager() override; |
| 34 | |
| 35 | // Create an instance of the safe browsing ping manager. |
| 36 | static std::unique_ptr<BasePingManager> Create( |
| 37 | net::URLRequestContextGetter* request_context_getter, |
| 38 | const SafeBrowsingProtocolConfig& config); |
| 39 | |
| 40 | // net::URLFetcherDelegate interface. |
| 41 | void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 42 | |
| 43 | // Report to Google when a SafeBrowsing warning is shown to the user. |
| 44 | // |hit_report.threat_type| should be one of the types known by |
| 45 | // SafeBrowsingtHitUrl. |
| 46 | void ReportSafeBrowsingHit(const safe_browsing::HitReport& hit_report); |
| 47 | |
| 48 | // Users can opt-in on the SafeBrowsing interstitial to send detailed |
| 49 | // threat reports. |report| is the serialized report. |
| 50 | void ReportThreatDetails(const std::string& report); |
| 51 | |
| 52 | protected: |
| 53 | friend class BasePingManagerTest; |
| 54 | // Constructs a BasePingManager that issues network requests |
| 55 | // using |request_context_getter|. |
| 56 | BasePingManager(net::URLRequestContextGetter* request_context_getter, |
| 57 | const SafeBrowsingProtocolConfig& config); |
| 58 | |
| 59 | private: |
| 60 | FRIEND_TEST_ALL_PREFIXES(BasePingManagerTest, TestSafeBrowsingHitUrl); |
| 61 | FRIEND_TEST_ALL_PREFIXES(BasePingManagerTest, TestThreatDetailsUrl); |
| 62 | FRIEND_TEST_ALL_PREFIXES(BasePingManagerTest, TestReportThreatDetails); |
| 63 | FRIEND_TEST_ALL_PREFIXES(BasePingManagerTest, TestReportSafeBrowsingHit); |
| 64 | |
| 65 | typedef std::set<std::unique_ptr<net::URLFetcher>> Reports; |
| 66 | |
| 67 | // Generates URL for reporting safe browsing hits. |
| 68 | GURL SafeBrowsingHitUrl(const safe_browsing::HitReport& hit_report) const; |
| 69 | |
| 70 | // Generates URL for reporting threat details for users who opt-in. |
| 71 | GURL ThreatDetailsUrl() const; |
| 72 | |
| 73 | // Current product version sent in each request. |
| 74 | std::string version_; |
| 75 | |
| 76 | // The safe browsing client name sent in each request. |
| 77 | std::string client_name_; |
| 78 | |
| 79 | // The context we use to issue network requests. |
| 80 | scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 81 | |
| 82 | // URL prefix where browser reports hits to the safebrowsing list and |
| 83 | // sends detaild threat reports for UMA users. |
| 84 | std::string url_prefix_; |
| 85 | |
| 86 | // Track outstanding SafeBrowsing report fetchers for clean up. |
| 87 | // We add both "hit" and "detail" fetchers in this set. |
| 88 | Reports safebrowsing_reports_; |
| 89 | |
| 90 | net::NetLogWithSource net_log_; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(BasePingManager); |
| 93 | }; |
| 94 | |
| 95 | } // namespace safe_browsing |
| 96 | |
| 97 | #endif // COMPONENTS_SAFE_BROWSING_BASE_PING_MANAGER_H_ |