OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/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/task_runner_util.h" | |
11 #include "base/threading/sequenced_worker_pool.h" | |
12 #include "chrome/common/chrome_switches.h" | |
13 #include "content/public/browser/browser_context.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "net/base/load_flags.h" | |
16 #include "net/url_request/url_fetcher.h" | |
17 #include "url/gurl.h" | |
18 | |
19 using content::BrowserThread; | |
20 | |
21 namespace feedback { | |
22 namespace { | |
23 | |
24 const char kFeedbackPostUrl[] = | |
25 "https://www.google.com/tools/feedback/chrome/__submit"; | |
26 const char kProtBufMimeType[] = "application/x-protobuf"; | |
27 | |
28 const base::TimeDelta kRetryDelay = base::TimeDelta::FromMinutes(60); | |
Zachary Kuznia
2013/12/17 01:12:57
Is there a reason you don't use a shorter delay an
rkc
2013/12/17 23:26:00
Most of the reason behind the exponential backoff
| |
29 | |
30 } // namespace | |
31 | |
32 FeedbackUploader::FeedbackUploader(content::BrowserContext* context) | |
33 : context_(context), | |
34 retry_delay_(kRetryDelay) { | |
35 CHECK(context_); | |
36 dispatch_callback_ = base::Bind(&FeedbackUploader::DispatchReport, | |
37 AsWeakPtr()); | |
38 } | |
39 | |
40 FeedbackUploader::~FeedbackUploader() { | |
41 } | |
42 | |
43 void FeedbackUploader::QueueReport(scoped_ptr<std::string> data) { | |
44 reports_queue_.push(FeedbackReport(base::Time::Now(), data.Pass())); | |
45 UpdateUploadTimer(); | |
46 } | |
47 | |
48 void FeedbackUploader::DispatchReport(scoped_ptr<std::string> data) { | |
49 GURL post_url; | |
50 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kFeedbackServer)) | |
51 post_url = GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
52 switches::kFeedbackServer)); | |
53 else | |
54 post_url = GURL(kFeedbackPostUrl); | |
55 | |
56 // Save the report data pointer since the report.Pass() in the next statement | |
57 // will invalidate the scoper. | |
58 std::string* data_ptr = data.get(); | |
59 net::URLFetcher* fetcher = net::URLFetcher::Create( | |
60 post_url, net::URLFetcher::POST, | |
61 new FeedbackUploaderDelegate( | |
62 data.Pass(), | |
63 base::Bind(&FeedbackUploader::UpdateUploadTimer, AsWeakPtr()), | |
64 base::Bind(&FeedbackUploader::RetryReport, AsWeakPtr()))); | |
65 | |
66 fetcher->SetUploadData(std::string(kProtBufMimeType), *data_ptr); | |
67 fetcher->SetRequestContext(context_->GetRequestContext()); | |
68 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | |
69 net::LOAD_DO_NOT_SEND_COOKIES); | |
70 fetcher->Start(); | |
71 } | |
72 | |
73 void FeedbackUploader::UpdateUploadTimer() { | |
74 if (reports_queue_.empty()) | |
75 return; | |
76 | |
77 const FeedbackReport& report = reports_queue_.top(); | |
78 base::Time now = base::Time::Now(); | |
79 if (report.upload_at <= now) { | |
80 scoped_ptr<std::string> data = report.data.Pass(); | |
81 reports_queue_.pop(); | |
82 dispatch_callback_.Run(data.Pass()); | |
83 } else { | |
84 // Stop the old timer and start an updated one. | |
85 if (upload_timer_.IsRunning()) | |
86 upload_timer_.Stop(); | |
87 upload_timer_.Start( | |
88 FROM_HERE, report.upload_at - now, this, | |
89 &FeedbackUploader::UpdateUploadTimer); | |
90 } | |
91 } | |
92 | |
93 void FeedbackUploader::RetryReport(scoped_ptr<std::string> data) { | |
94 reports_queue_.push( | |
95 FeedbackReport(base::Time::Now() + retry_delay_, data.Pass())); | |
96 UpdateUploadTimer(); | |
97 } | |
98 | |
99 void FeedbackUploader::setup_for_test( | |
100 const ReportDataCallback& dispatch_callback, | |
101 const base::TimeDelta& retry_delay) { | |
102 dispatch_callback_ = dispatch_callback; | |
103 retry_delay_ = retry_delay; | |
104 } | |
105 | |
106 } // namespace feedback | |
OLD | NEW |