summaryrefslogtreecommitdiff
path: root/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client.h
blob: 1f437b6a85c8e047444c734e2acc544d9fe998d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_PINGBACK_CLIENT_H_
#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_PINGBACK_CLIENT_H_

#include <stdint.h>

#include <memory>
#include <set>
#include <string>
#include <utility>

#include "base/macros.h"
#include "base/threading/thread_checker.h"
#include "components/data_reduction_proxy/proto/pageload_metrics.pb.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h"

namespace base {
class Time;
}

namespace net {
class URLFetcher;
class URLRequestContextGetter;
}

namespace data_reduction_proxy {
class DataReductionProxyData;
struct DataReductionProxyPageLoadTiming;

using NavigationID = std::pair<uint64_t, std::string>;

// Manages pingbacks about page load timing information to the data saver proxy
// server. This class is not thread safe.
class DataReductionProxyPingbackClient : public net::URLFetcherDelegate {
 public:
  // The caller must ensure that |url_request_context| remains alive for the
  // lifetime of the |DataReductionProxyPingbackClient| instance.
  explicit DataReductionProxyPingbackClient(
      net::URLRequestContextGetter* url_request_context);
  ~DataReductionProxyPingbackClient() override;

  // Sends a pingback to the data saver proxy server about various timing
  // information.
  virtual void SendPingback(const DataReductionProxyData& data,
                            const DataReductionProxyPageLoadTiming& timing);

  // Sets the probability of actually sending a pingback to the server for any
  // call to SendPingback.
  void SetPingbackReportingFraction(float pingback_reporting_fraction);

  // Adds an opt out for |tab_identifier_key| for a data saver |page_id|. An opt
  // out occurs when users dismiss the preview in favor of the full page.
  void AddOptOut(const NavigationID& navigation_id);

  // Removes any stored data associated with |tab_identifier_key| in a task that
  // runs later.
  void ClearNavigationKeyAsync(const NavigationID& navigation_id);

  // The total number of pending loads being tracked due to opt outs.
  size_t OptOutsSizeForTesting() const;

 protected:
  // Generates a float in the range [0, 1). Virtualized in testing.
  virtual float GenerateRandomFloat() const;

  // Returns the current time. Virtualized in testing.
  virtual base::Time CurrentTime() const;

 private:
  // URLFetcherDelegate implmentation:
  void OnURLFetchComplete(const net::URLFetcher* source) override;

  // Whether a pingback should be sent.
  bool ShouldSendPingback() const;

  // Creates an URLFetcher that will POST to |secure_proxy_url_| using
  // |url_request_context_|. The max retries is set to 5.
  // |data_to_send_| will be used to fill the body of the Fetcher, and will be
  // reset to an empty RecordPageloadMetricsRequest.
  void CreateFetcherForDataAndStart();

  // Removes any stored data associated with |tab_identifier_key|.
  void ClearNavigationKeySync(const NavigationID& navigation_id);

  net::URLRequestContextGetter* url_request_context_;

  // The URL for the data saver proxy's ping back service.
  const GURL pingback_url_;

  // The currently running fetcher.
  std::unique_ptr<net::URLFetcher> current_fetcher_;

  // Serialized data to send to the data saver proxy server.
  RecordPageloadMetricsRequest metrics_request_;

  // The probability of sending a pingback to the server.
  float pingback_reporting_fraction_;

  // The map of tab identifier keys to page IDs.
  std::set<NavigationID> opt_outs_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(DataReductionProxyPingbackClient);
};

}  // namespace data_reduction_proxy

#endif  // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_PINGBACK_CLIENT_H_