• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "components/feedback/feedback_uploader.h"
6 
7 #include "base/callback.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/task_runner_util.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "components/feedback/feedback_report.h"
14 
15 namespace feedback {
16 namespace {
17 
18 const char kFeedbackPostUrl[] =
19     "https://www.google.com/tools/feedback/chrome/__submit";
20 
21 const int64 kRetryDelayMinutes = 60;
22 
23 const base::FilePath::CharType kFeedbackReportPath[] =
24     FILE_PATH_LITERAL("Feedback Reports");
25 
26 }  // namespace
27 
operator ()(FeedbackReport * a,FeedbackReport * b) const28 bool FeedbackUploader::ReportsUploadTimeComparator::operator()(
29     FeedbackReport* a, FeedbackReport* b) const {
30   return a->upload_at() > b->upload_at();
31 }
32 
FeedbackUploader(const base::FilePath & path,base::SequencedWorkerPool * pool)33 FeedbackUploader::FeedbackUploader(const base::FilePath& path,
34                                    base::SequencedWorkerPool* pool)
35     : report_path_(path.Append(kFeedbackReportPath)),
36       retry_delay_(base::TimeDelta::FromMinutes(kRetryDelayMinutes)),
37       url_(kFeedbackPostUrl),
38       pool_(pool) {
39   Init();
40 }
41 
FeedbackUploader(const base::FilePath & path,base::SequencedWorkerPool * pool,const std::string & url)42 FeedbackUploader::FeedbackUploader(const base::FilePath& path,
43                                    base::SequencedWorkerPool* pool,
44                                    const std::string& url)
45     : report_path_(path.Append(kFeedbackReportPath)),
46       retry_delay_(base::TimeDelta::FromMinutes(kRetryDelayMinutes)),
47       url_(url),
48       pool_(pool) {
49   Init();
50 }
51 
~FeedbackUploader()52 FeedbackUploader::~FeedbackUploader() {}
53 
Init()54 void FeedbackUploader::Init() {
55   dispatch_callback_ = base::Bind(&FeedbackUploader::DispatchReport,
56                                   AsWeakPtr());
57 }
58 
QueueReport(const std::string & data)59 void FeedbackUploader::QueueReport(const std::string& data) {
60   QueueReportWithDelay(data, base::TimeDelta());
61 }
62 
UpdateUploadTimer()63 void FeedbackUploader::UpdateUploadTimer() {
64   if (reports_queue_.empty())
65     return;
66 
67   scoped_refptr<FeedbackReport> report = reports_queue_.top();
68   base::Time now = base::Time::Now();
69   if (report->upload_at() <= now) {
70     reports_queue_.pop();
71     dispatch_callback_.Run(report->data());
72     report->DeleteReportOnDisk();
73   } else {
74     // Stop the old timer and start an updated one.
75     if (upload_timer_.IsRunning())
76       upload_timer_.Stop();
77     upload_timer_.Start(
78         FROM_HERE, report->upload_at() - now, this,
79         &FeedbackUploader::UpdateUploadTimer);
80   }
81 }
82 
RetryReport(const std::string & data)83 void FeedbackUploader::RetryReport(const std::string& data) {
84   QueueReportWithDelay(data, retry_delay_);
85 }
86 
QueueReportWithDelay(const std::string & data,base::TimeDelta delay)87 void FeedbackUploader::QueueReportWithDelay(const std::string& data,
88                                             base::TimeDelta delay) {
89   // Uses a BLOCK_SHUTDOWN file task runner because we really don't want to
90   // lose reports.
91   scoped_refptr<base::SequencedTaskRunner> task_runner =
92       pool_->GetSequencedTaskRunnerWithShutdownBehavior(
93           pool_->GetSequenceToken(),
94           base::SequencedWorkerPool::BLOCK_SHUTDOWN);
95 
96   reports_queue_.push(new FeedbackReport(report_path_,
97                                          base::Time::Now() + delay,
98                                          data,
99                                          task_runner));
100   UpdateUploadTimer();
101 }
102 
setup_for_test(const ReportDataCallback & dispatch_callback,const base::TimeDelta & retry_delay)103 void FeedbackUploader::setup_for_test(
104     const ReportDataCallback& dispatch_callback,
105     const base::TimeDelta& retry_delay) {
106   dispatch_callback_ = dispatch_callback;
107   retry_delay_ = retry_delay;
108 }
109 
110 }  // namespace feedback
111