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_CORE_PING_MANAGER_H_ 6 #define COMPONENTS_SAFE_BROWSING_CORE_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/containers/unique_ptr_adapters.h" 16 #include "base/gtest_prod_util.h" 17 #include "base/macros.h" 18 #include "base/memory/ref_counted.h" 19 #include "components/safe_browsing/core/db/hit_report.h" 20 #include "components/safe_browsing/core/db/util.h" 21 #include "services/network/public/cpp/shared_url_loader_factory.h" 22 #include "url/gurl.h" 23 24 namespace network { 25 class SimpleURLLoader; 26 } // namespace network 27 28 namespace safe_browsing { 29 30 class PingManager { 31 public: 32 virtual ~PingManager(); 33 34 // Create an instance of the safe browsing ping manager. 35 static std::unique_ptr<PingManager> Create( 36 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 37 const V4ProtocolConfig& config); 38 39 void OnURLLoaderComplete(network::SimpleURLLoader* source, 40 std::unique_ptr<std::string> response_body); 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: 52 friend class PingManagerTest; 53 // Constructs a PingManager that issues network requests 54 // using |url_loader_factory|. 55 PingManager(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 56 const V4ProtocolConfig& config); 57 58 private: 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); 63 64 const V4ProtocolConfig config_; 65 66 using Reports = std::set<std::unique_ptr<network::SimpleURLLoader>, 67 base::UniquePtrComparator>; 68 69 // Generates URL for reporting safe browsing hits. 70 GURL SafeBrowsingHitUrl(const safe_browsing::HitReport& hit_report) const; 71 72 // Generates URL for reporting threat details for users who opt-in. 73 GURL ThreatDetailsUrl() const; 74 75 // The URLLoaderFactory we use to issue network requests. 76 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; 77 78 // Track outstanding SafeBrowsing report fetchers for clean up. 79 // We add both "hit" and "detail" fetchers in this set. 80 Reports safebrowsing_reports_; 81 82 DISALLOW_COPY_AND_ASSIGN(PingManager); 83 }; 84 85 } // namespace safe_browsing 86 87 #endif // COMPONENTS_SAFE_BROWSING_CORE_PING_MANAGER_H_ 88