summaryrefslogtreecommitdiff
path: root/chromium/components/rappor/log_uploader.h
blob: a44ce544ec08b1633689d6787787cea25bc09b75 (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
// Copyright 2014 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_RAPPOR_LOG_UPLOADER_H_
#define COMPONENTS_RAPPOR_LOG_UPLOADER_H_

#include <memory>
#include <string>

#include "base/compiler_specific.h"
#include "base/containers/queue.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/rappor/log_uploader_interface.h"
#include "url/gurl.h"

namespace network {
class SimpleURLLoader;
class SharedURLLoaderFactory;
}  // namespace network

namespace rappor {

// Uploads logs from RapporServiceImpl.  Logs are passed in via QueueLog(),
// stored internally, and uploaded one at a time.  A queued log will be uploaded
// at a fixed interval after the successful upload of the previous logs.  If an
// upload fails, the uploader will keep retrying the upload with an exponential
// backoff interval.
class LogUploader : public LogUploaderInterface {
 public:
  // Constructor takes the |server_url| that logs should be uploaded to, the
  // |mime_type| of the uploaded data, and |request_context| to create uploads
  // with.
  LogUploader(
      const GURL& server_url,
      const std::string& mime_type,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);

  // If the object is destroyed (or the program terminates) while logs are
  // queued, the logs are lost.
  ~LogUploader() override;

  // LogUploaderInterface:
  void Start() override;
  void Stop() override;
  void QueueLog(const std::string& log) override;

 protected:
  // Checks if an upload has been scheduled.
  virtual bool IsUploadScheduled() const;

  // Schedules a future call to StartScheduledUpload if one isn't already
  // pending.  Can be overridden for testing.
  virtual void ScheduleNextUpload(base::TimeDelta interval);

  // Starts transmission of the next log. Exposed for tests.
  void StartScheduledUpload();

  // Increases the upload interval each time it's called, to handle the case
  // where the server is having issues. Exposed for tests.
  static base::TimeDelta BackOffUploadInterval(base::TimeDelta);

 private:
  // Returns true if the uploader is allowed to start another upload.
  bool CanStartUpload() const;

  // Drops excess logs until we are under the size limit.
  void DropExcessLogs();

  // Called after transmission completes (whether successful or not).
  void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);

  // Called when the upload is completed.
  void OnUploadFinished(bool server_is_healthy);

  // The server URL to upload logs to.
  const GURL server_url_;

  // The mime type to specify on uploaded logs.
  const std::string mime_type_;

  // The URL loader factory used to send uploads.
  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;

  // True if the uploader is currently running.
  bool is_running_;

  // The outstanding transmission.
  std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;

  // The logs that still need to be uploaded.
  base::queue<std::string> queued_logs_;

  // A timer used to delay before attempting another upload.
  base::OneShotTimer upload_timer_;

  // Indicates that the last triggered upload hasn't resolved yet.
  bool has_callback_pending_;

  // The interval to wait after an upload's URLFetcher completion before
  // starting the next upload attempt.
  base::TimeDelta upload_interval_;

  DISALLOW_COPY_AND_ASSIGN(LogUploader);
};

}  // namespace rappor

#endif  // COMPONENTS_RAPPOR_LOG_UPLOADER_H_