Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Change for readability | |
|
ktl
2014/03/21 13:40:02
Don't forget to remove these.
Steven Holte
2014/03/21 19:52:25
Done.
| |
| 6 | |
| 5 #ifndef COMPONENTS_RAPPOR_LOG_UPLOADER_H_ | 7 #ifndef COMPONENTS_RAPPOR_LOG_UPLOADER_H_ |
| 6 #define COMPONENTS_RAPPOR_LOG_UPLOADER_H_ | 8 #define COMPONENTS_RAPPOR_LOG_UPLOADER_H_ |
| 7 | 9 |
| 8 #include <queue> | 10 #include <queue> |
| 9 #include <string> | 11 #include <string> |
| 10 | 12 |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/macros.h" | |
| 11 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 14 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
| 15 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 16 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 17 | 21 |
| 18 namespace net { | 22 namespace net { |
| 19 class URLFetcher; | 23 class URLFetcher; |
| 20 } | 24 } |
| 21 | 25 |
| 22 namespace rappor { | 26 namespace rappor { |
| 23 | 27 |
| 24 // Handles uploading logs to an external server. | 28 // Uploads logs from RapporService. Logs are passed in via QueueLog(), stored |
| 29 // internally, and uploaded one at a time. A queued log will be uploaded at a | |
| 30 // fixed interval after the successful upload of the previous logs. If an | |
| 31 // upload fails, the uploader will keep retrying the upload with an exponential | |
| 32 // backoff interval. | |
| 25 class LogUploader : public net::URLFetcherDelegate { | 33 class LogUploader : public net::URLFetcherDelegate { |
| 26 public: | 34 public: |
| 27 // Constructor takes the server_url that logs should be uploaded to, the | 35 // Constructor takes the |server_url| that logs should be uploaded to, the |
| 28 // mime_type of the uploaded data, and request_context to create uploads | 36 // |mime_type| of the uploaded data, and |request_context| to create uploads |
| 29 // with. | 37 // with. |
| 30 LogUploader(const GURL& server_url, | 38 LogUploader(const GURL& server_url, |
| 31 const std::string& mime_type, | 39 const std::string& mime_type, |
| 32 net::URLRequestContextGetter* request_context); | 40 net::URLRequestContextGetter* request_context); |
| 33 | 41 |
| 42 // If the object is destroyed (or the program terminates) while logs are | |
| 43 // queued, the logs are lost. | |
| 34 virtual ~LogUploader(); | 44 virtual ~LogUploader(); |
| 35 | 45 |
| 36 // Adds an entry to the queue of logs to be uploaded to the server. The | 46 // Adds an entry to the queue of logs to be uploaded to the server. The |
| 37 // uploader makes no assumptions about the format of |log| and simply sends | 47 // uploader makes no assumptions about the format of |log| and simply sends |
| 38 // it verbatim to the server. | 48 // it verbatim to the server. |
| 39 void QueueLog(const std::string& log); | 49 void QueueLog(const std::string& log); |
| 40 | 50 |
| 41 protected: | 51 protected: |
| 42 // Check if an upload has been scheduled. | 52 // Checks if an upload has been scheduled. |
| 43 virtual bool IsUploadScheduled() const; | 53 virtual bool IsUploadScheduled() const; |
| 44 | 54 |
| 45 // Schedules a future call to StartScheduledUpload if one isn't already | 55 // Schedules a future call to StartScheduledUpload if one isn't already |
| 46 // pending. Can be overridden for testing. | 56 // pending. Can be overridden for testing. |
| 47 virtual void ScheduleNextUpload(base::TimeDelta interval); | 57 virtual void ScheduleNextUpload(base::TimeDelta interval); |
| 48 | 58 |
| 49 // Starts transmission of the next log. Exposed for tests. | 59 // Starts transmission of the next log. Exposed for tests. |
| 50 void StartScheduledUpload(); | 60 void StartScheduledUpload(); |
| 51 | 61 |
| 52 // Increases the upload interval each time it's called, to handle the case | 62 // Increases the upload interval each time it's called, to handle the case |
| 53 // where the server is having issues. Exposed for tests. | 63 // where the server is having issues. Exposed for tests. |
| 54 static base::TimeDelta BackOffUploadInterval(base::TimeDelta); | 64 static base::TimeDelta BackOffUploadInterval(base::TimeDelta); |
| 55 | 65 |
| 56 private: | 66 private: |
| 57 // Implementation of net::URLFetcherDelegate. Called after transmission | 67 // Implements net::URLFetcherDelegate. Called after transmission completes |
| 58 // completes (either successfully or with failure). | 68 // (whether successful or not). |
| 59 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 69 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 60 | 70 |
| 61 // Called when the upload is completed. | 71 // Called when the upload is completed. |
| 62 void OnUploadFinished(bool server_is_healthy, bool more_logs_remaining); | 72 void OnUploadFinished(bool server_is_healthy, bool more_logs_remaining); |
| 63 | 73 |
| 64 // The server URL to upload logs to. | 74 // The server URL to upload logs to. |
| 65 const GURL server_url_; | 75 const GURL server_url_; |
| 66 | 76 |
| 67 // The mime type to specify on uploaded logs. | 77 // The mime type to specify on uploaded logs. |
| 68 const std::string mime_type_; | 78 const std::string mime_type_; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 85 // The interval to wait after an upload's URLFetcher completion before | 95 // The interval to wait after an upload's URLFetcher completion before |
| 86 // starting the next upload attempt. | 96 // starting the next upload attempt. |
| 87 base::TimeDelta upload_interval_; | 97 base::TimeDelta upload_interval_; |
| 88 | 98 |
| 89 DISALLOW_COPY_AND_ASSIGN(LogUploader); | 99 DISALLOW_COPY_AND_ASSIGN(LogUploader); |
| 90 }; | 100 }; |
| 91 | 101 |
| 92 } // namespace rappor | 102 } // namespace rappor |
| 93 | 103 |
| 94 #endif // COMPONENTS_RAPPOR_LOG_UPLOADER_H_ | 104 #endif // COMPONENTS_RAPPOR_LOG_UPLOADER_H_ |
| OLD | NEW |