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 | |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 5 | #ifndef COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_ |
| 6 | #define COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_ |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 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" |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 21 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 22 | #include "url/gurl.h" |
| 23 | |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 24 | namespace network { |
| 25 | class SimpleURLLoader; |
| 26 | } // namespace network |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 27 | |
| 28 | namespace safe_browsing { |
| 29 | |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 30 | class PingManager { |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 31 | public: |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 32 | virtual ~PingManager(); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 33 | |
| 34 | // Create an instance of the safe browsing ping manager. |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 35 | static std::unique_ptr<PingManager> Create( |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 36 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 37 | const SafeBrowsingProtocolConfig& config); |
| 38 | |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 39 | void OnURLLoaderComplete(network::SimpleURLLoader* source, |
| 40 | std::unique_ptr<std::string> response_body); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 41 | |
| 42 | // Report to Google when a SafeBrowsing warning is shown to the user. |
| 43 | // |hit_report.threat_type| should be one of the types known by |
| 44 | // SafeBrowsingtHitUrl. |
| 45 | void ReportSafeBrowsingHit(const safe_browsing::HitReport& hit_report); |
| 46 | |
| 47 | // Users can opt-in on the SafeBrowsing interstitial to send detailed |
| 48 | // threat reports. |report| is the serialized report. |
| 49 | void ReportThreatDetails(const std::string& report); |
| 50 | |
| 51 | protected: |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 52 | friend class PingManagerTest; |
| 53 | // Constructs a PingManager that issues network requests |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 54 | // using |url_loader_factory|. |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 55 | PingManager(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, |
| 56 | const SafeBrowsingProtocolConfig& config); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 57 | |
| 58 | private: |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 59 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestSafeBrowsingHitUrl); |
| 60 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestThreatDetailsUrl); |
| 61 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestReportThreatDetails); |
| 62 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestReportSafeBrowsingHit); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 63 | |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 64 | typedef std::set<std::unique_ptr<network::SimpleURLLoader>> Reports; |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 65 | |
| 66 | // Generates URL for reporting safe browsing hits. |
| 67 | GURL SafeBrowsingHitUrl(const safe_browsing::HitReport& hit_report) const; |
| 68 | |
| 69 | // Generates URL for reporting threat details for users who opt-in. |
| 70 | GURL ThreatDetailsUrl() const; |
| 71 | |
| 72 | // Current product version sent in each request. |
| 73 | std::string version_; |
| 74 | |
| 75 | // The safe browsing client name sent in each request. |
| 76 | std::string client_name_; |
| 77 | |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 78 | // The URLLoaderFactory we use to issue network requests. |
| 79 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 80 | |
| 81 | // URL prefix where browser reports hits to the safebrowsing list and |
| 82 | // sends detaild threat reports for UMA users. |
| 83 | std::string url_prefix_; |
| 84 | |
| 85 | // Track outstanding SafeBrowsing report fetchers for clean up. |
| 86 | // We add both "hit" and "detail" fetchers in this set. |
| 87 | Reports safebrowsing_reports_; |
| 88 | |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 89 | DISALLOW_COPY_AND_ASSIGN(PingManager); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace safe_browsing |
| 93 | |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame^] | 94 | #endif // COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_ |