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