• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "components/feedback/feedback_uploader_delegate.h"
6 
7 #include <sstream>
8 
9 #include "base/logging.h"
10 #include "net/url_request/url_fetcher.h"
11 
12 namespace feedback {
13 namespace {
14 
15 const int kHttpPostSuccessNoContent = 204;
16 const int kHttpPostFailNoConnection = -1;
17 const int kHttpPostFailClientError = 400;
18 const int kHttpPostFailServerError = 500;
19 
20 }  // namespace
21 
FeedbackUploaderDelegate(const std::string & post_body,const base::Closure & success_callback,const ReportDataCallback & error_callback)22 FeedbackUploaderDelegate::FeedbackUploaderDelegate(
23     const std::string& post_body,
24     const base::Closure& success_callback,
25     const ReportDataCallback& error_callback)
26         : post_body_(post_body),
27           success_callback_(success_callback),
28           error_callback_(error_callback) {
29 }
30 
~FeedbackUploaderDelegate()31 FeedbackUploaderDelegate::~FeedbackUploaderDelegate() {}
32 
OnURLFetchComplete(const net::URLFetcher * source)33 void FeedbackUploaderDelegate::OnURLFetchComplete(
34     const net::URLFetcher* source) {
35   scoped_ptr<const net::URLFetcher> source_scoper(source);
36 
37   std::stringstream error_stream;
38   int response_code = source->GetResponseCode();
39   if (response_code == kHttpPostSuccessNoContent) {
40     error_stream << "Success";
41     success_callback_.Run();
42   } else {
43     // Process the error for debug output
44     if (response_code == kHttpPostFailNoConnection) {
45       error_stream << "No connection to server.";
46     } else if ((response_code > kHttpPostFailClientError) &&
47                (response_code < kHttpPostFailServerError)) {
48       error_stream << "Client error: HTTP response code " << response_code;
49     } else if (response_code > kHttpPostFailServerError) {
50       error_stream << "Server error: HTTP response code " << response_code;
51     } else {
52       error_stream << "Unknown error: HTTP response code " << response_code;
53     }
54     error_callback_.Run(post_body_);
55   }
56 
57   LOG(WARNING) << "FEEDBACK: Submission to feedback server ("
58                << source->GetURL() << ") status: " << error_stream.str();
59 
60   // This instance won't be used for anything else, delete us.
61   delete this;
62 }
63 
64 }  // namespace feedback
65