Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / feedback / feedback_uploader.cc
blob1798e3bfdd976abc78708860fd7cbe0dadf24a98
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 #include "chrome/browser/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 "chrome/browser/feedback/feedback_report.h"
15 namespace feedback {
16 namespace {
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");
26 } // namespace
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),
38 pool_(pool) {
39 dispatch_callback_ = base::Bind(&FeedbackUploader::DispatchReport,
40 AsWeakPtr());
43 FeedbackUploader::~FeedbackUploader() {}
45 void FeedbackUploader::QueueReport(const std::string& data) {
46 QueueReportWithDelay(data, base::TimeDelta());
49 void FeedbackUploader::UpdateUploadTimer() {
50 if (reports_queue_.empty())
51 return;
53 scoped_refptr<FeedbackReport> report = reports_queue_.top();
54 base::Time now = base::Time::Now();
55 if (report->upload_at() <= now) {
56 reports_queue_.pop();
57 dispatch_callback_.Run(report->data());
58 report->DeleteReportOnDisk();
59 } else {
60 // Stop the old timer and start an updated one.
61 if (upload_timer_.IsRunning())
62 upload_timer_.Stop();
63 upload_timer_.Start(
64 FROM_HERE, report->upload_at() - now, this,
65 &FeedbackUploader::UpdateUploadTimer);
69 void FeedbackUploader::RetryReport(const std::string& data) {
70 QueueReportWithDelay(data, retry_delay_);
73 void FeedbackUploader::QueueReportWithDelay(const std::string& data,
74 base::TimeDelta delay) {
75 // Uses a BLOCK_SHUTDOWN file task runner because we really don't want to
76 // lose reports.
77 scoped_refptr<base::SequencedTaskRunner> task_runner =
78 pool_->GetSequencedTaskRunnerWithShutdownBehavior(
79 pool_->GetSequenceToken(),
80 base::SequencedWorkerPool::BLOCK_SHUTDOWN);
82 reports_queue_.push(new FeedbackReport(report_path_,
83 base::Time::Now() + delay,
84 data,
85 task_runner));
86 UpdateUploadTimer();
89 void FeedbackUploader::setup_for_test(
90 const ReportDataCallback& dispatch_callback,
91 const base::TimeDelta& retry_delay) {
92 dispatch_callback_ = dispatch_callback;
93 retry_delay_ = retry_delay;
96 } // namespace feedback