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 FeedbackReport
* a
, FeedbackReport
* b
) const {
30 return a
->upload_at() > b
->upload_at();
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
),
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
)),
52 FeedbackUploader::~FeedbackUploader() {}
54 void FeedbackUploader::Init() {
55 dispatch_callback_
= base::Bind(&FeedbackUploader::DispatchReport
,
59 void FeedbackUploader::QueueReport(const std::string
& data
) {
60 QueueReportWithDelay(data
, base::TimeDelta());
63 void FeedbackUploader::UpdateUploadTimer() {
64 if (reports_queue_
.empty())
67 scoped_refptr
<FeedbackReport
> report
= reports_queue_
.top();
68 base::Time now
= base::Time::Now();
69 if (report
->upload_at() <= now
) {
71 dispatch_callback_
.Run(report
->data());
72 report
->DeleteReportOnDisk();
74 // Stop the old timer and start an updated one.
75 if (upload_timer_
.IsRunning())
78 FROM_HERE
, report
->upload_at() - now
, this,
79 &FeedbackUploader::UpdateUploadTimer
);
83 void FeedbackUploader::RetryReport(const std::string
& data
) {
84 QueueReportWithDelay(data
, retry_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
91 scoped_refptr
<base::SequencedTaskRunner
> task_runner
=
92 pool_
->GetSequencedTaskRunnerWithShutdownBehavior(
93 pool_
->GetSequenceToken(),
94 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
96 reports_queue_
.push(new FeedbackReport(report_path_
,
97 base::Time::Now() + 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
;
110 } // namespace feedback