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.
5 #ifndef COMPONENTS_RAPPOR_LOG_UPLOADER_H_
6 #define COMPONENTS_RAPPOR_LOG_UPLOADER_H_
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "components/rappor/log_uploader_interface.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "net/url_request/url_request_context_getter.h"
27 // Uploads logs from RapporService. Logs are passed in via QueueLog(), stored
28 // internally, and uploaded one at a time. A queued log will be uploaded at a
29 // fixed interval after the successful upload of the previous logs. If an
30 // upload fails, the uploader will keep retrying the upload with an exponential
32 class LogUploader
: public net::URLFetcherDelegate
,
33 public LogUploaderInterface
{
35 // Constructor takes the |server_url| that logs should be uploaded to, the
36 // |mime_type| of the uploaded data, and |request_context| to create uploads
38 LogUploader(const GURL
& server_url
,
39 const std::string
& mime_type
,
40 net::URLRequestContextGetter
* request_context
);
42 // If the object is destroyed (or the program terminates) while logs are
43 // queued, the logs are lost.
44 ~LogUploader() override
;
46 // LogUploaderInterface:
47 void Start() override
;
49 void QueueLog(const std::string
& log
) override
;
52 // Checks if an upload has been scheduled.
53 virtual bool IsUploadScheduled() const;
55 // Schedules a future call to StartScheduledUpload if one isn't already
56 // pending. Can be overridden for testing.
57 virtual void ScheduleNextUpload(base::TimeDelta interval
);
59 // Starts transmission of the next log. Exposed for tests.
60 void StartScheduledUpload();
62 // Increases the upload interval each time it's called, to handle the case
63 // where the server is having issues. Exposed for tests.
64 static base::TimeDelta
BackOffUploadInterval(base::TimeDelta
);
67 // Returns true if the uploader is allowed to start another upload.
68 bool CanStartUpload() const;
70 // Drops excess logs until we are under the size limit.
71 void DropExcessLogs();
73 // Implements net::URLFetcherDelegate. Called after transmission completes
74 // (whether successful or not).
75 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
77 // Called when the upload is completed.
78 void OnUploadFinished(bool server_is_healthy
);
80 // The server URL to upload logs to.
81 const GURL server_url_
;
83 // The mime type to specify on uploaded logs.
84 const std::string mime_type_
;
86 // The request context used to send uploads.
87 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
89 // True if the uploader is currently running.
92 // The outstanding transmission that appears as a URL Fetch operation.
93 scoped_ptr
<net::URLFetcher
> current_fetch_
;
95 // The logs that still need to be uploaded.
96 std::queue
<std::string
> queued_logs_
;
98 // A timer used to delay before attempting another upload.
99 base::OneShotTimer
<LogUploader
> upload_timer_
;
101 // Indicates that the last triggered upload hasn't resolved yet.
102 bool has_callback_pending_
;
104 // The interval to wait after an upload's URLFetcher completion before
105 // starting the next upload attempt.
106 base::TimeDelta upload_interval_
;
108 DISALLOW_COPY_AND_ASSIGN(LogUploader
);
111 } // namespace rappor
113 #endif // COMPONENTS_RAPPOR_LOG_UPLOADER_H_