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 | |
David Benjamin | 8048aac | 2019-04-08 01:33:46 | [diff] [blame] | 15 | #include "base/containers/unique_ptr_adapters.h" |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 16 | #include "base/gtest_prod_util.h" |
| 17 | #include "base/macros.h" |
| 18 | #include "base/memory/ref_counted.h" |
Tim Volodine | e4593847 | 2017-09-21 10:08:22 | [diff] [blame] | 19 | #include "components/safe_browsing/db/hit_report.h" |
| 20 | #include "components/safe_browsing/db/util.h" |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 21 | #include "content/public/browser/permission_type.h" |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 22 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 23 | #include "url/gurl.h" |
| 24 | |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 25 | namespace network { |
| 26 | class SimpleURLLoader; |
| 27 | } // namespace network |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 28 | |
| 29 | namespace safe_browsing { |
| 30 | |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 31 | class PingManager { |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 32 | public: |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 33 | virtual ~PingManager(); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 34 | |
| 35 | // Create an instance of the safe browsing ping manager. |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 36 | static std::unique_ptr<PingManager> Create( |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 37 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, |
Varun Khaneja | 0348449 | 2018-08-14 19:19:39 | [diff] [blame] | 38 | const V4ProtocolConfig& config); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 39 | |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 40 | void OnURLLoaderComplete(network::SimpleURLLoader* source, |
| 41 | std::unique_ptr<std::string> response_body); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 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: |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 53 | friend class PingManagerTest; |
| 54 | // Constructs a PingManager that issues network requests |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 55 | // using |url_loader_factory|. |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 56 | PingManager(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, |
Varun Khaneja | 0348449 | 2018-08-14 19:19:39 | [diff] [blame] | 57 | const V4ProtocolConfig& config); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 58 | |
| 59 | private: |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 60 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestSafeBrowsingHitUrl); |
| 61 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestThreatDetailsUrl); |
| 62 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestReportThreatDetails); |
| 63 | FRIEND_TEST_ALL_PREFIXES(PingManagerTest, TestReportSafeBrowsingHit); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 64 | |
Varun Khaneja | 0348449 | 2018-08-14 19:19:39 | [diff] [blame] | 65 | const V4ProtocolConfig config_; |
| 66 | |
David Benjamin | 8048aac | 2019-04-08 01:33:46 | [diff] [blame] | 67 | using Reports = std::set<std::unique_ptr<network::SimpleURLLoader>, |
| 68 | base::UniquePtrComparator>; |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 69 | |
| 70 | // Generates URL for reporting safe browsing hits. |
| 71 | GURL SafeBrowsingHitUrl(const safe_browsing::HitReport& hit_report) const; |
| 72 | |
| 73 | // Generates URL for reporting threat details for users who opt-in. |
| 74 | GURL ThreatDetailsUrl() const; |
| 75 | |
John Abd-El-Malek | 7870ba0c | 2018-04-10 04:49:17 | [diff] [blame] | 76 | // The URLLoaderFactory we use to issue network requests. |
| 77 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 78 | |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 79 | // Track outstanding SafeBrowsing report fetchers for clean up. |
| 80 | // We add both "hit" and "detail" fetchers in this set. |
| 81 | Reports safebrowsing_reports_; |
| 82 | |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 83 | DISALLOW_COPY_AND_ASSIGN(PingManager); |
jialiul | d06419a | 2017-02-27 19:40:11 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace safe_browsing |
| 87 | |
Varun Khaneja | d38b3842 | 2018-07-18 19:53:49 | [diff] [blame] | 88 | #endif // COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_ |