OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef COMPONENTS_RAPPOR_LOG_UPLOADER_H_ |
| 6 #define COMPONENTS_RAPPOR_LOG_UPLOADER_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <string> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" |
| 14 #include "net/url_request/url_fetcher.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace rappor { |
| 20 |
| 21 // Handles uploading logs to an external server. |
| 22 class LogUploader : public net::URLFetcherDelegate { |
| 23 public: |
| 24 // Constructor takes the server_url that logs should be uploaded to, the |
| 25 // mime_type of the uploaded data, and request_context to create uploads |
| 26 // with. |
| 27 LogUploader(const GURL& server_url, |
| 28 const std::string& mime_type, |
| 29 net::URLRequestContextGetter* request_context); |
| 30 |
| 31 ~LogUploader(); |
| 32 |
| 33 // Adds an entry to the queue of logs to be uploaded to the server. The |
| 34 // uploader makes no assumptions about the format of |log| and simply sends |
| 35 // it verbatim to the server. |
| 36 void QueueLog(const std::string& log); |
| 37 |
| 38 // Increases the upload interval each time it's called, to handle the case |
| 39 // where the server is having issues. |
| 40 static base::TimeDelta BackOffUploadInterval(base::TimeDelta); |
| 41 |
| 42 protected: |
| 43 // Schedules a future call to StartScheduledUpload if one isn't already |
| 44 // pending. Can be overridden for testing. |
| 45 virtual void ScheduleNextUpload(base::TimeDelta interval); |
| 46 |
| 47 // Check if an upload has been scheduled. |
| 48 virtual bool IsUploadScheduled() const; |
| 49 |
| 50 // Starts transmission of the next log. Exposed for tests. |
| 51 void StartScheduledUpload(); |
| 52 |
| 53 private: |
| 54 // Implementation of net::URLFetcherDelegate. Called after transmission |
| 55 // completes (either successfully or with failure). |
| 56 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 57 |
| 58 // Called when the upload is completed. |
| 59 void OnUploadFinished(bool server_is_healthy, bool more_logs_remaining); |
| 60 |
| 61 // The server URL to upload logs to. |
| 62 const GURL server_url_; |
| 63 |
| 64 // The mime type to specify on uploaded logs. |
| 65 const std::string mime_type_; |
| 66 |
| 67 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 68 |
| 69 // The outstanding transmission appears as a URL Fetch operation. |
| 70 scoped_ptr<net::URLFetcher> current_fetch_; |
| 71 |
| 72 // The logs that still need to be uploaded. |
| 73 std::queue<std::string> queued_logs_; |
| 74 |
| 75 base::OneShotTimer<LogUploader> upload_timer_; |
| 76 |
| 77 // Indicates that the last triggered upload hasn't resolved yet. |
| 78 bool has_callback_pending_; |
| 79 |
| 80 // The interval to wait after an upload's URLFetcher completion before |
| 81 // starting the next upload attempt. |
| 82 base::TimeDelta upload_interval_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(LogUploader); |
| 85 }; |
| 86 |
| 87 } // namespace rappor |
| 88 |
| 89 #endif // COMPONENTS_RAPPOR_LOG_UPLOADER_H_ |
OLD | NEW |