1 // Copyright 2013 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 CHROME_BROWSER_FEEDBACK_FEEDBACK_UPLOADER_H_
6 #define CHROME_BROWSER_FEEDBACK_FEEDBACK_UPLOADER_H_
11 #include "base/basictypes.h"
12 #include "base/file_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
20 typedef base::Callback<void(const std::string&)> ReportDataCallback;
24 // FeedbackUploader is used to add a feedback report to the queue of reports
25 // being uploaded. In case uploading a report fails, it is written to disk and
26 // tried again when it's turn comes up next in the queue.
27 class FeedbackUploader : public base::SupportsWeakPtr<FeedbackUploader> {
29 explicit FeedbackUploader(const base::FilePath& path,
30 base::SequencedWorkerPool* pool);
31 virtual ~FeedbackUploader();
33 // Queues a report for uploading.
34 void QueueReport(const std::string& data);
36 base::FilePath GetFeedbackReportsPath() { return report_path_; }
39 friend class FeedbackUploaderTest;
40 struct ReportsUploadTimeComparator {
41 bool operator()(FeedbackReport* a, FeedbackReport* b) const;
44 // Dispatches the report to be uploaded.
45 virtual void DispatchReport(const std::string& data) = 0;
47 // Update our timer for uploading the next report.
48 void UpdateUploadTimer();
50 // Requeue this report with a delay.
51 void RetryReport(const std::string& data);
53 void QueueReportWithDelay(const std::string& data, base::TimeDelta delay);
55 void setup_for_test(const ReportDataCallback& dispatch_callback,
56 const base::TimeDelta& retry_delay);
58 base::FilePath report_path_;
59 // Timer to upload the next report at.
60 base::OneShotTimer<FeedbackUploader> upload_timer_;
61 // Priority queue of reports prioritized by the time the report is supposed
63 std::priority_queue<scoped_refptr<FeedbackReport>,
64 std::vector<scoped_refptr<FeedbackReport> >,
65 ReportsUploadTimeComparator> reports_queue_;
67 ReportDataCallback dispatch_callback_;
68 base::TimeDelta retry_delay_;
70 base::SequencedWorkerPool* pool_;
72 DISALLOW_COPY_AND_ASSIGN(FeedbackUploader);
75 } // namespace feedback
77 #endif // CHROME_BROWSER_FEEDBACK_FEEDBACK_UPLOADER_H_