blob: f9c520260a793f3ff5d8248b1b91eb69ef21bede [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
15#include "base/gtest_prod_util.h"
16#include "base/macros.h"
17#include "base/memory/ref_counted.h"
Tim Volodinee45938472017-09-21 10:08:2218#include "components/safe_browsing/db/hit_report.h"
19#include "components/safe_browsing/db/util.h"
jialiuld06419a2017-02-27 19:40:1120#include "content/public/browser/permission_type.h"
John Abd-El-Malek7870ba0c2018-04-10 04:49:1721#include "services/network/public/cpp/shared_url_loader_factory.h"
jialiuld06419a2017-02-27 19:40:1122#include "url/gurl.h"
23
John Abd-El-Malek7870ba0c2018-04-10 04:49:1724namespace network {
25class SimpleURLLoader;
26} // namespace network
jialiuld06419a2017-02-27 19:40:1127
28namespace safe_browsing {
29
Varun Khanejad38b38422018-07-18 19:53:4930class PingManager {
jialiuld06419a2017-02-27 19:40:1131 public:
Varun Khanejad38b38422018-07-18 19:53:4932 virtual ~PingManager();
jialiuld06419a2017-02-27 19:40:1133
34 // Create an instance of the safe browsing ping manager.
Varun Khanejad38b38422018-07-18 19:53:4935 static std::unique_ptr<PingManager> Create(
John Abd-El-Malek7870ba0c2018-04-10 04:49:1736 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
Varun Khaneja03484492018-08-14 19:19:3937 const V4ProtocolConfig& config);
jialiuld06419a2017-02-27 19:40:1138
John Abd-El-Malek7870ba0c2018-04-10 04:49:1739 void OnURLLoaderComplete(network::SimpleURLLoader* source,
40 std::unique_ptr<std::string> response_body);
jialiuld06419a2017-02-27 19:40:1141
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 Khanejad38b38422018-07-18 19:53:4952 friend class PingManagerTest;
53 // Constructs a PingManager that issues network requests
John Abd-El-Malek7870ba0c2018-04-10 04:49:1754 // using |url_loader_factory|.
Varun Khanejad38b38422018-07-18 19:53:4955 PingManager(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
Varun Khaneja03484492018-08-14 19:19:3956 const V4ProtocolConfig& config);
jialiuld06419a2017-02-27 19:40:1157
58 private:
Varun Khanejad38b38422018-07-18 19:53:4959 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);
jialiuld06419a2017-02-27 19:40:1163
Varun Khaneja03484492018-08-14 19:19:3964 const V4ProtocolConfig config_;
65
John Abd-El-Malek7870ba0c2018-04-10 04:49:1766 typedef std::set<std::unique_ptr<network::SimpleURLLoader>> Reports;
jialiuld06419a2017-02-27 19:40:1167
68 // Generates URL for reporting safe browsing hits.
69 GURL SafeBrowsingHitUrl(const safe_browsing::HitReport& hit_report) const;
70
71 // Generates URL for reporting threat details for users who opt-in.
72 GURL ThreatDetailsUrl() const;
73
John Abd-El-Malek7870ba0c2018-04-10 04:49:1774 // The URLLoaderFactory we use to issue network requests.
75 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
jialiuld06419a2017-02-27 19:40:1176
jialiuld06419a2017-02-27 19:40:1177 // Track outstanding SafeBrowsing report fetchers for clean up.
78 // We add both "hit" and "detail" fetchers in this set.
79 Reports safebrowsing_reports_;
80
Varun Khanejad38b38422018-07-18 19:53:4981 DISALLOW_COPY_AND_ASSIGN(PingManager);
jialiuld06419a2017-02-27 19:40:1182};
83
84} // namespace safe_browsing
85
Varun Khanejad38b38422018-07-18 19:53:4986#endif // COMPONENTS_SAFE_BROWSING_PING_MANAGER_H_