Chromium Code Reviews| 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 protected: | |
| 39 // Starts transmission of the next log. | |
| 40 void StartScheduledUpload(); | |
| 41 | |
| 42 // Schedules a future call to StartScheduledUpload if one isn't already | |
| 43 // pending. | |
| 44 void ScheduleNextUpload(); | |
| 45 | |
| 46 private: | |
| 47 // Increases the upload interval each time it's called, to handle the case | |
| 48 // where the server is having issues. | |
| 49 void BackOffUploadInterval(); | |
| 50 | |
| 51 // Implementation of net::URLFetcherDelegate. Called after transmission | |
| 52 // completes (either successfully or with failure). | |
| 53 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 54 | |
| 55 // Called when the upload is completed. | |
| 56 void UploadFinished(bool server_is_healthy, bool more_logs_remaining); | |
|
Alexei Svitkine (slow)
2014/02/06 17:47:20
Nit: I'd name this OnUploadFinished().
| |
| 57 | |
| 58 // The server URL to upload logs to. | |
| 59 const GURL server_url_; | |
| 60 | |
| 61 // The mime type to specify on uploaded logs. | |
| 62 const std::string mime_type_; | |
| 63 | |
| 64 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
| 65 | |
| 66 // The outstanding transmission appears as a URL Fetch operation. | |
| 67 scoped_ptr<net::URLFetcher> current_fetch_; | |
| 68 | |
| 69 // The logs that still need to be uploaded. | |
| 70 std::queue<std::string> queued_logs_; | |
| 71 | |
| 72 base::OneShotTimer<LogUploader> upload_timer_; | |
| 73 | |
| 74 // Indicates that the last triggered upload hasn't resolved yet. | |
| 75 bool has_callback_pending_; | |
| 76 | |
| 77 // The interval to wait after an upload's URLFetcher completion before | |
| 78 // starting the next upload attempt. | |
| 79 base::TimeDelta upload_interval_; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(LogUploader); | |
| 82 }; | |
| 83 | |
| 84 } // namespace rappor | |
| 85 | |
| 86 #endif // COMPONENTS_RAPPOR_LOG_UPLOADER_H_ | |
| OLD | NEW |