xref: /aosp_15_r20/external/cronet/components/metrics/net/net_metrics_log_uploader.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
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_METRICS_NET_NET_METRICS_LOG_UPLOADER_H_
6 #define COMPONENTS_METRICS_NET_NET_METRICS_LOG_UPLOADER_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/strings/string_piece.h"
12 #include "components/metrics/metrics_log.h"
13 #include "components/metrics/metrics_log_uploader.h"
14 #include "third_party/metrics_proto/reporting_info.pb.h"
15 #include "url/gurl.h"
16 
17 namespace network {
18 class SharedURLLoaderFactory;
19 class SimpleURLLoader;
20 }  // namespace network
21 
22 namespace metrics {
23 
24 // Implementation of MetricsLogUploader using the Chrome network stack.
25 class NetMetricsLogUploader : public MetricsLogUploader {
26  public:
27   // Constructs a NetMetricsLogUploader which uploads data to |server_url| with
28   // the specified |mime_type|. The |service_type| marks which service the
29   // data usage should be attributed to. The |on_upload_complete| callback will
30   // be called with the HTTP response code of the upload or with -1 on an error.
31   NetMetricsLogUploader(
32       scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
33       const GURL& server_url,
34       base::StringPiece mime_type,
35       MetricsLogUploader::MetricServiceType service_type,
36       const MetricsLogUploader::UploadCallback& on_upload_complete);
37 
38   // This constructor allows a secondary non-HTTPS URL to be passed in as
39   // |insecure_server_url|. That URL is used as a fallback if a connection
40   // to |server_url| fails, requests are encrypted when sent to an HTTP URL.
41   NetMetricsLogUploader(
42       scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
43       const GURL& server_url,
44       const GURL& insecure_server_url,
45       base::StringPiece mime_type,
46       MetricsLogUploader::MetricServiceType service_type,
47       const MetricsLogUploader::UploadCallback& on_upload_complete);
48 
49   NetMetricsLogUploader(const NetMetricsLogUploader&) = delete;
50   NetMetricsLogUploader& operator=(const NetMetricsLogUploader&) = delete;
51 
52   ~NetMetricsLogUploader() override;
53 
54   // MetricsLogUploader:
55   // Uploads a log to the server_url specified in the constructor.
56   void UploadLog(const std::string& compressed_log_data,
57                  const LogMetadata& log_metadata,
58                  const std::string& log_hash,
59                  const std::string& log_signature,
60                  const ReportingInfo& reporting_info) override;
61 
62  private:
63   // Uploads a log to a URL passed as a parameter.
64   void UploadLogToURL(const std::string& compressed_log_data,
65                       const LogMetadata& log_metadata,
66                       const std::string& log_hash,
67                       const std::string& log_signature,
68                       const ReportingInfo& reporting_info,
69                       const GURL& url);
70 
71   // Calls |on_upload_complete_| with failure codes. Used when there's a local
72   // reason that prevented an upload over HTTP, such as an error encrpyting
73   // the payload.
74   void HTTPFallbackAborted();
75 
76   void OnURLLoadComplete(std::unique_ptr<std::string> response_body);
77 
78   // The URLLoader factory for loads done using the network stack.
79   scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
80 
81   const GURL server_url_;
82   const GURL insecure_server_url_;
83   const std::string mime_type_;
84   const MetricsLogUploader ::MetricServiceType service_type_;
85   const MetricsLogUploader::UploadCallback on_upload_complete_;
86   // The outstanding transmission appears as a URL Fetch operation.
87   std::unique_ptr<network::SimpleURLLoader> url_loader_;
88 };
89 
90 }  // namespace metrics
91 
92 #endif  // COMPONENTS_METRICS_NET_NET_METRICS_LOG_UPLOADER_H_
93