blob: 3f0c3533652e3dbc4749cc4b7844f0ce902402ea [file] [log] [blame]
[email protected]1eab4e92014-05-09 02:17:191// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]48372252013-12-20 12:18:012// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1eab4e92014-05-09 02:17:195#include "components/feedback/feedback_uploader_delegate.h"
[email protected]48372252013-12-20 12:18:016
dcheng84c358e2016-04-26 07:05:537#include <memory>
[email protected]48372252013-12-20 12:18:018#include <sstream>
9
10#include "base/logging.h"
Ahmed Fakhryaf8ab052017-07-21 21:39:0711#include "components/feedback/feedback_report.h"
[email protected]48372252013-12-20 12:18:0112#include "net/url_request/url_fetcher.h"
[email protected]48372252013-12-20 12:18:0113
14namespace feedback {
Ahmed Fakhryaf8ab052017-07-21 21:39:0715
[email protected]48372252013-12-20 12:18:0116namespace {
17
Ahmed Fakhryaf8ab052017-07-21 21:39:0718constexpr int kHttpPostSuccessNoContent = 204;
19constexpr int kHttpPostFailNoConnection = -1;
20constexpr int kHttpPostFailClientError = 400;
21constexpr int kHttpPostFailServerError = 500;
[email protected]48372252013-12-20 12:18:0122
23} // namespace
24
25FeedbackUploaderDelegate::FeedbackUploaderDelegate(
Ahmed Fakhryaf8ab052017-07-21 21:39:0726 scoped_refptr<FeedbackReport> pending_report,
[email protected]48372252013-12-20 12:18:0127 const base::Closure& success_callback,
Ahmed Fakhryaf8ab052017-07-21 21:39:0728 const ReportFailureCallback& error_callback)
29 : pending_report_(pending_report),
30 success_callback_(success_callback),
31 error_callback_(error_callback) {}
[email protected]48372252013-12-20 12:18:0132
33FeedbackUploaderDelegate::~FeedbackUploaderDelegate() {}
34
35void FeedbackUploaderDelegate::OnURLFetchComplete(
36 const net::URLFetcher* source) {
dcheng84c358e2016-04-26 07:05:5337 std::unique_ptr<const net::URLFetcher> source_scoper(source);
[email protected]48372252013-12-20 12:18:0138
39 std::stringstream error_stream;
40 int response_code = source->GetResponseCode();
41 if (response_code == kHttpPostSuccessNoContent) {
42 error_stream << "Success";
43 success_callback_.Run();
44 } else {
Ahmed Fakhryaf8ab052017-07-21 21:39:0745 bool should_retry = true;
[email protected]48372252013-12-20 12:18:0146 // Process the error for debug output
47 if (response_code == kHttpPostFailNoConnection) {
48 error_stream << "No connection to server.";
Ahmed Fakhryaf8ab052017-07-21 21:39:0749 } else if ((response_code >= kHttpPostFailClientError) &&
[email protected]48372252013-12-20 12:18:0150 (response_code < kHttpPostFailServerError)) {
Ahmed Fakhryaf8ab052017-07-21 21:39:0751 // Client errors mean that the server failed to parse the proto that was
52 // sent, or that some requirements weren't met by the server side
53 // validation, and hence we should NOT retry sending this corrupt report
54 // and give up.
55 should_retry = false;
56
[email protected]48372252013-12-20 12:18:0157 error_stream << "Client error: HTTP response code " << response_code;
Ahmed Fakhryaf8ab052017-07-21 21:39:0758 } else if (response_code >= kHttpPostFailServerError) {
[email protected]48372252013-12-20 12:18:0159 error_stream << "Server error: HTTP response code " << response_code;
60 } else {
61 error_stream << "Unknown error: HTTP response code " << response_code;
62 }
Ahmed Fakhryaf8ab052017-07-21 21:39:0763
64 if (should_retry)
65 error_callback_.Run(pending_report_);
[email protected]48372252013-12-20 12:18:0166 }
67
68 LOG(WARNING) << "FEEDBACK: Submission to feedback server ("
69 << source->GetURL() << ") status: " << error_stream.str();
70
71 // This instance won't be used for anything else, delete us.
72 delete this;
73}
74
75} // namespace feedback