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.
5 #include "components/feedback/feedback_uploader.h"
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"
18 const char kFeedbackPostUrl
[] =
19 "https://www.google.com/tools/feedback/chrome/__submit";
21 const int64 kRetryDelayMinutes
= 60;
23 const base::FilePath::CharType kFeedbackReportPath
[] =
24 FILE_PATH_LITERAL("Feedback Reports");
28 bool FeedbackUploader::ReportsUploadTimeComparator::operator()(
29 const scoped_refptr
<FeedbackReport
>& a
,
30 const scoped_refptr
<FeedbackReport
>& b
) const {
31 return a
->upload_at() > b
->upload_at();
34 FeedbackUploader::FeedbackUploader(const base::FilePath
& path
,
35 base::SequencedWorkerPool
* pool
)
36 : report_path_(path
.Append(kFeedbackReportPath
)),
37 retry_delay_(base::TimeDelta::FromMinutes(kRetryDelayMinutes
)),
38 url_(kFeedbackPostUrl
),
43 FeedbackUploader::FeedbackUploader(const base::FilePath
& path
,
44 base::SequencedWorkerPool
* pool
,
45 const std::string
& url
)
46 : report_path_(path
.Append(kFeedbackReportPath
)),
47 retry_delay_(base::TimeDelta::FromMinutes(kRetryDelayMinutes
)),
53 FeedbackUploader::~FeedbackUploader() {}
55 void FeedbackUploader::Init() {
56 dispatch_callback_
= base::Bind(&FeedbackUploader::DispatchReport
,
60 void FeedbackUploader::QueueReport(const std::string
& data
) {
61 QueueReportWithDelay(data
, base::TimeDelta());
64 void FeedbackUploader::UpdateUploadTimer() {
65 if (reports_queue_
.empty())
68 scoped_refptr
<FeedbackReport
> report
= reports_queue_
.top();
69 base::Time now
= base::Time::Now();
70 if (report
->upload_at() <= now
) {
72 dispatch_callback_
.Run(report
->data());
73 report
->DeleteReportOnDisk();
75 // Stop the old timer and start an updated one.
76 if (upload_timer_
.IsRunning())
79 FROM_HERE
, report
->upload_at() - now
, this,
80 &FeedbackUploader::UpdateUploadTimer
);
84 void FeedbackUploader::RetryReport(const std::string
& data
) {
85 QueueReportWithDelay(data
, retry_delay_
);
88 void FeedbackUploader::QueueReportWithDelay(const std::string
& data
,
89 base::TimeDelta delay
) {
90 // Uses a BLOCK_SHUTDOWN file task runner because we really don't want to
92 scoped_refptr
<base::SequencedTaskRunner
> task_runner
=
93 pool_
->GetSequencedTaskRunnerWithShutdownBehavior(
94 pool_
->GetSequenceToken(),
95 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
97 reports_queue_
.push(new FeedbackReport(report_path_
,
98 base::Time::Now() + delay
,
104 void FeedbackUploader::setup_for_test(
105 const ReportDataCallback
& dispatch_callback
,
106 const base::TimeDelta
& retry_delay
) {
107 dispatch_callback_
= dispatch_callback
;
108 retry_delay_
= retry_delay
;
111 } // namespace feedback