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