blob: b3c87a471bdfebd4346fd243e8dd7159db61ea62 [file] [log] [blame]
jialiuld06419a2017-02-27 19:40:111// 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 Khanejad38b38422018-07-18 19:53:495#ifndef COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_
6#define COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_
jialiuld06419a2017-02-27 19:40:117
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 Benjamin8048aac2019-04-08 01:33:4615#include "base/containers/unique_ptr_adapters.h"
jialiuld06419a2017-02-27 19:40:1116#include "base/gtest_prod_util.h"
17#include "base/macros.h"
18#include "base/memory/ref_counted.h"
Tim Volodinee45938472017-09-21 10:08:2219#include "components/safe_browsing/db/hit_report.h"
20#include "components/safe_browsing/db/util.h"
jialiuld06419a2017-02-27 19:40:1121#include "content/public/browser/permission_type.h"
John Abd-El-Malek7870ba0c2018-04-10 04:49:1722#include "services/network/public/cpp/shared_url_loader_factory.h"
jialiuld06419a2017-02-27 19:40:1123#include "url/gurl.h"
24
John Abd-El-Malek7870ba0c2018-04-10 04:49:1725namespace network {
26class SimpleURLLoader;
27} // namespace network
jialiuld06419a2017-02-27 19:40:1128
29namespace safe_browsing {
30
Varun Khanejad38b38422018-07-18 19:53:4931class PingManager {
jialiuld06419a2017-02-27 19:40:1132 public:
Varun Khanejad38b38422018-07-18 19:53:4933 virtual ~PingManager();
jialiuld06419a2017-02-27 19:40:1134
35 // Create an instance of the safe browsing ping manager.
Varun Khanejad38b38422018-07-18 19:53:4936 static std::unique_ptr<PingManager> Create(
John Abd-El-Malek7870ba0c2018-04-10 04:49:1737 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
Varun Khaneja03484492018-08-14 19:19:3938 const V4ProtocolConfig& config);
jialiuld06419a2017-02-27 19:40:1139
John Abd-El-Malek7870ba0c2018-04-10 04:49:1740 void OnURLLoaderComplete(network::SimpleURLLoader* source,
41 std::unique_ptr<std::string> response_body);
jialiuld06419a2017-02-27 19:40:1142
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 Khanejad38b38422018-07-18 19:53:4953 friend class PingManagerTest;
54 // Constructs a PingManager that issues network requests
John Abd-El-Malek7870ba0c2018-04-10 04:49:1755 // using |url_loader_factory|.
Varun Khanejad38b38422018-07-18 19:53:4956 PingManager(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
Varun Khaneja03484492018-08-14 19:19:3957 const V4ProtocolConfig& config);
jialiuld06419a2017-02-27 19:40:1158
59 private:
Varun Khanejad38b38422018-07-18 19:53:4960 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);
jialiuld06419a2017-02-27 19:40:1164
Varun Khaneja03484492018-08-14 19:19:3965 const V4ProtocolConfig config_;
66
David Benjamin8048aac2019-04-08 01:33:4667 using Reports = std::set<std::unique_ptr<network::SimpleURLLoader>,
68 base::UniquePtrComparator>;
jialiuld06419a2017-02-27 19:40:1169
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-Malek7870ba0c2018-04-10 04:49:1776 // The URLLoaderFactory we use to issue network requests.
77 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
jialiuld06419a2017-02-27 19:40:1178
jialiuld06419a2017-02-27 19:40:1179 // 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 Khanejad38b38422018-07-18 19:53:4983 DISALLOW_COPY_AND_ASSIGN(PingManager);
jialiuld06419a2017-02-27 19:40:1184};
85
86} // namespace safe_browsing
87
Varun Khanejad38b38422018-07-18 19:53:4988#endif // COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_