uploader.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2014 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. #ifndef COMPONENTS_DOMAIN_RELIABILITY_UPLOADER_H_
  5. #define COMPONENTS_DOMAIN_RELIABILITY_UPLOADER_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/time/time.h"
  11. #include "components/domain_reliability/domain_reliability_export.h"
  12. #include "url/gurl.h"
  13. namespace net {
  14. class NetworkIsolationKey;
  15. class URLRequest;
  16. class URLRequestContext;
  17. } // namespace net
  18. namespace domain_reliability {
  19. class MockableTime;
  20. // Uploads Domain Reliability reports to collectors.
  21. class DOMAIN_RELIABILITY_EXPORT DomainReliabilityUploader {
  22. public:
  23. struct UploadResult {
  24. enum UploadStatus {
  25. FAILURE,
  26. SUCCESS,
  27. RETRY_AFTER,
  28. };
  29. bool is_success() const { return status == SUCCESS; }
  30. bool is_failure() const { return status == FAILURE; }
  31. bool is_retry_after() const { return status == RETRY_AFTER; }
  32. UploadStatus status;
  33. base::TimeDelta retry_after;
  34. };
  35. using UploadCallback = base::OnceCallback<void(const UploadResult& result)>;
  36. DomainReliabilityUploader();
  37. virtual ~DomainReliabilityUploader();
  38. // Creates an uploader that uses the given |url_request_context| for uploads.
  39. // (See test_util.h for a mock version.)
  40. static std::unique_ptr<DomainReliabilityUploader> Create(
  41. MockableTime* time,
  42. net::URLRequestContext* url_request_context);
  43. // Uploads |report_json| to |upload_url| and calls |callback| when the upload
  44. // has either completed or failed.
  45. virtual void UploadReport(
  46. const std::string& report_json,
  47. int max_beacon_depth,
  48. const GURL& upload_url,
  49. const net::NetworkIsolationKey& network_isolation_key,
  50. UploadCallback callback) = 0;
  51. // Shuts down the uploader prior to destruction. Currently, terminates pending
  52. // uploads and prevents the uploader from starting new ones to avoid hairy
  53. // lifetime issues at destruction.
  54. virtual void Shutdown() = 0;
  55. // Sets whether the uploader will discard uploads but pretend they succeeded.
  56. // In Chrome, this is used when the user has not opted in to metrics
  57. // collection; in unittests, this is used in combination with
  58. // GetDiscardedUploadCount to simplify checking whether a test scenario
  59. // generates an upload or not.
  60. virtual void SetDiscardUploads(bool discard_uploads) = 0;
  61. // Gets the number of uploads that have been discarded after SetDiscardUploads
  62. // was called with true.
  63. virtual int GetDiscardedUploadCount() const = 0;
  64. static int GetURLRequestUploadDepth(const net::URLRequest& request);
  65. };
  66. } // namespace domain_reliability
  67. #endif // COMPONENTS_DOMAIN_RELIABILITY_UPLOADER_H_