Home | History | Annotate | Download | only in feedback
      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_FEEDBACK_FEEDBACK_UPLOADER_H_
      6 #define COMPONENTS_FEEDBACK_FEEDBACK_UPLOADER_H_
      7 
      8 #include <queue>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/files/file_util.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/threading/sequenced_worker_pool.h"
     16 #include "base/time/time.h"
     17 #include "base/timer/timer.h"
     18 
     19 namespace feedback {
     20 
     21 typedef base::Callback<void(const std::string&)> ReportDataCallback;
     22 
     23 class FeedbackReport;
     24 
     25 // FeedbackUploader is used to add a feedback report to the queue of reports
     26 // being uploaded. In case uploading a report fails, it is written to disk and
     27 // tried again when it's turn comes up next in the queue.
     28 class FeedbackUploader : public base::SupportsWeakPtr<FeedbackUploader> {
     29  public:
     30   FeedbackUploader(const base::FilePath& path,
     31                    base::SequencedWorkerPool* pool);
     32   FeedbackUploader(const base::FilePath& path,
     33                    base::SequencedWorkerPool* pool,
     34                    const std::string& url);
     35   virtual ~FeedbackUploader();
     36 
     37   // Queues a report for uploading.
     38   virtual void QueueReport(const std::string& data);
     39 
     40   base::FilePath GetFeedbackReportsPath() { return report_path_; }
     41 
     42   bool QueueEmpty() const { return reports_queue_.empty(); }
     43 
     44  protected:
     45   friend class FeedbackUploaderTest;
     46 
     47   struct ReportsUploadTimeComparator {
     48     bool operator()(const scoped_refptr<FeedbackReport>& a,
     49                     const scoped_refptr<FeedbackReport>& b) const;
     50   };
     51 
     52   void Init();
     53 
     54   // Dispatches the report to be uploaded.
     55   virtual void DispatchReport(const std::string& data) = 0;
     56 
     57   // Update our timer for uploading the next report.
     58   void UpdateUploadTimer();
     59 
     60   // Requeue this report with a delay.
     61   void RetryReport(const std::string& data);
     62 
     63   void QueueReportWithDelay(const std::string& data, base::TimeDelta delay);
     64 
     65   void setup_for_test(const ReportDataCallback& dispatch_callback,
     66                       const base::TimeDelta& retry_delay);
     67 
     68   base::FilePath report_path_;
     69   // Timer to upload the next report at.
     70   base::OneShotTimer<FeedbackUploader> upload_timer_;
     71   // Priority queue of reports prioritized by the time the report is supposed
     72   // to be uploaded at.
     73   std::priority_queue<scoped_refptr<FeedbackReport>,
     74                       std::vector<scoped_refptr<FeedbackReport> >,
     75                       ReportsUploadTimeComparator> reports_queue_;
     76 
     77   ReportDataCallback dispatch_callback_;
     78   base::TimeDelta retry_delay_;
     79   std::string url_;
     80   base::SequencedWorkerPool* pool_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(FeedbackUploader);
     83 };
     84 
     85 }  // namespace feedback
     86 
     87 #endif  // COMPONENTS_FEEDBACK_FEEDBACK_UPLOADER_H_
     88