| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/feedback/feedback_uploader.h" | 5 #include "chrome/browser/feedback/feedback_uploader.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "chrome/browser/feedback/feedback_report.h" |
| 12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 13 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 16 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
| 17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 18 | 19 |
| 19 using content::BrowserThread; | 20 using content::BrowserThread; |
| 20 | 21 |
| 21 namespace feedback { | 22 namespace feedback { |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const char kFeedbackPostUrl[] = | 25 const char kFeedbackPostUrl[] = |
| 25 "https://www.google.com/tools/feedback/chrome/__submit"; | 26 "https://www.google.com/tools/feedback/chrome/__submit"; |
| 26 const char kProtBufMimeType[] = "application/x-protobuf"; | 27 const char kProtBufMimeType[] = "application/x-protobuf"; |
| 27 | 28 |
| 28 const int64 kRetryDelayMinutes = 60; | 29 const int64 kRetryDelayMinutes = 60; |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 struct FeedbackReport { | |
| 33 FeedbackReport(const base::Time& upload_at, scoped_ptr<std::string> data) | |
| 34 : upload_at(upload_at), data(data.Pass()) {} | |
| 35 | |
| 36 FeedbackReport(const FeedbackReport& report) { | |
| 37 upload_at = report.upload_at; | |
| 38 data = report.data.Pass(); | |
| 39 } | |
| 40 | |
| 41 FeedbackReport& operator=(const FeedbackReport& report) { | |
| 42 upload_at = report.upload_at; | |
| 43 data = report.data.Pass(); | |
| 44 return *this; | |
| 45 } | |
| 46 | |
| 47 base::Time upload_at; // Upload this report at or after this time. | |
| 48 mutable scoped_ptr<std::string> data; | |
| 49 }; | |
| 50 | |
| 51 bool FeedbackUploader::ReportsUploadTimeComparator::operator()( | 33 bool FeedbackUploader::ReportsUploadTimeComparator::operator()( |
| 52 const FeedbackReport& a, const FeedbackReport& b) const { | 34 FeedbackReport* a, FeedbackReport* b) const { |
| 53 return a.upload_at > b.upload_at; | 35 return a->upload_at() > b->upload_at(); |
| 54 } | 36 } |
| 55 | 37 |
| 56 FeedbackUploader::FeedbackUploader(content::BrowserContext* context) | 38 FeedbackUploader::FeedbackUploader(content::BrowserContext* context) |
| 57 : context_(context), | 39 : context_(context), |
| 58 retry_delay_(base::TimeDelta::FromMinutes(kRetryDelayMinutes)) { | 40 retry_delay_(base::TimeDelta::FromMinutes(kRetryDelayMinutes)) { |
| 59 CHECK(context_); | 41 CHECK(context_); |
| 60 dispatch_callback_ = base::Bind(&FeedbackUploader::DispatchReport, | 42 dispatch_callback_ = base::Bind(&FeedbackUploader::DispatchReport, |
| 61 AsWeakPtr()); | 43 AsWeakPtr()); |
| 62 } | 44 } |
| 63 | 45 |
| 64 FeedbackUploader::~FeedbackUploader() { | 46 FeedbackUploader::~FeedbackUploader() {} |
| 65 } | |
| 66 | 47 |
| 67 void FeedbackUploader::QueueReport(scoped_ptr<std::string> data) { | 48 void FeedbackUploader::QueueReport(const std::string& data) { |
| 68 reports_queue_.push(FeedbackReport(base::Time::Now(), data.Pass())); | 49 reports_queue_.push( |
| 50 new FeedbackReport(context_, base::Time::Now(), data)); |
| 69 UpdateUploadTimer(); | 51 UpdateUploadTimer(); |
| 70 } | 52 } |
| 71 | 53 |
| 72 void FeedbackUploader::DispatchReport(scoped_ptr<std::string> data) { | 54 void FeedbackUploader::DispatchReport(const std::string& data) { |
| 73 GURL post_url; | 55 GURL post_url; |
| 74 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kFeedbackServer)) | 56 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kFeedbackServer)) |
| 75 post_url = GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 57 post_url = GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 76 switches::kFeedbackServer)); | 58 switches::kFeedbackServer)); |
| 77 else | 59 else |
| 78 post_url = GURL(kFeedbackPostUrl); | 60 post_url = GURL(kFeedbackPostUrl); |
| 79 | 61 |
| 80 // Save the report data pointer since the report.Pass() in the next statement | |
| 81 // will invalidate the scoper. | |
| 82 std::string* data_ptr = data.get(); | |
| 83 net::URLFetcher* fetcher = net::URLFetcher::Create( | 62 net::URLFetcher* fetcher = net::URLFetcher::Create( |
| 84 post_url, net::URLFetcher::POST, | 63 post_url, net::URLFetcher::POST, |
| 85 new FeedbackUploaderDelegate( | 64 new FeedbackUploaderDelegate( |
| 86 data.Pass(), | 65 data, |
| 87 base::Bind(&FeedbackUploader::UpdateUploadTimer, AsWeakPtr()), | 66 base::Bind(&FeedbackUploader::UpdateUploadTimer, AsWeakPtr()), |
| 88 base::Bind(&FeedbackUploader::RetryReport, AsWeakPtr()))); | 67 base::Bind(&FeedbackUploader::RetryReport, AsWeakPtr()))); |
| 89 | 68 |
| 90 fetcher->SetUploadData(std::string(kProtBufMimeType), *data_ptr); | 69 fetcher->SetUploadData(std::string(kProtBufMimeType), data); |
| 91 fetcher->SetRequestContext(context_->GetRequestContext()); | 70 fetcher->SetRequestContext(context_->GetRequestContext()); |
| 92 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 71 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| 93 net::LOAD_DO_NOT_SEND_COOKIES); | 72 net::LOAD_DO_NOT_SEND_COOKIES); |
| 94 fetcher->Start(); | 73 fetcher->Start(); |
| 95 } | 74 } |
| 96 | 75 |
| 97 void FeedbackUploader::UpdateUploadTimer() { | 76 void FeedbackUploader::UpdateUploadTimer() { |
| 98 if (reports_queue_.empty()) | 77 if (reports_queue_.empty()) |
| 99 return; | 78 return; |
| 100 | 79 |
| 101 const FeedbackReport& report = reports_queue_.top(); | 80 scoped_refptr<FeedbackReport> report = reports_queue_.top(); |
| 102 base::Time now = base::Time::Now(); | 81 base::Time now = base::Time::Now(); |
| 103 if (report.upload_at <= now) { | 82 if (report->upload_at() <= now) { |
| 104 scoped_ptr<std::string> data = report.data.Pass(); | |
| 105 reports_queue_.pop(); | 83 reports_queue_.pop(); |
| 106 dispatch_callback_.Run(data.Pass()); | 84 dispatch_callback_.Run(report->data()); |
| 85 report->DeleteReportOnDisk(); |
| 107 } else { | 86 } else { |
| 108 // Stop the old timer and start an updated one. | 87 // Stop the old timer and start an updated one. |
| 109 if (upload_timer_.IsRunning()) | 88 if (upload_timer_.IsRunning()) |
| 110 upload_timer_.Stop(); | 89 upload_timer_.Stop(); |
| 111 upload_timer_.Start( | 90 upload_timer_.Start( |
| 112 FROM_HERE, report.upload_at - now, this, | 91 FROM_HERE, report->upload_at() - now, this, |
| 113 &FeedbackUploader::UpdateUploadTimer); | 92 &FeedbackUploader::UpdateUploadTimer); |
| 114 } | 93 } |
| 115 } | 94 } |
| 116 | 95 |
| 117 void FeedbackUploader::RetryReport(scoped_ptr<std::string> data) { | 96 void FeedbackUploader::RetryReport(const std::string& data) { |
| 118 reports_queue_.push( | 97 reports_queue_.push(new FeedbackReport(context_, |
| 119 FeedbackReport(base::Time::Now() + retry_delay_, data.Pass())); | 98 base::Time::Now() + retry_delay_, |
| 99 data)); |
| 120 UpdateUploadTimer(); | 100 UpdateUploadTimer(); |
| 121 } | 101 } |
| 122 | 102 |
| 123 void FeedbackUploader::setup_for_test( | 103 void FeedbackUploader::setup_for_test( |
| 124 const ReportDataCallback& dispatch_callback, | 104 const ReportDataCallback& dispatch_callback, |
| 125 const base::TimeDelta& retry_delay) { | 105 const base::TimeDelta& retry_delay) { |
| 126 dispatch_callback_ = dispatch_callback; | 106 dispatch_callback_ = dispatch_callback; |
| 127 retry_delay_ = retry_delay; | 107 retry_delay_ = retry_delay; |
| 128 } | 108 } |
| 129 | 109 |
| 130 } // namespace feedback | 110 } // namespace feedback |
| OLD | NEW |