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