Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(676)

Unified Diff: net/reporting/reporting_uploader.cc

Issue 2249213002: [OBSOLETE] Reporting: Initial implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/reporting/reporting_uploader.cc
diff --git a/net/reporting/reporting_uploader.cc b/net/reporting/reporting_uploader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..371534eca075fa3f79ded5be946d6ffa15b5586d
--- /dev/null
+++ b/net/reporting/reporting_uploader.cc
@@ -0,0 +1,125 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/reporting/reporting_uploader.h"
+
+#include "base/callback_helpers.h"
+#include "base/memory/ptr_util.h"
+#include "net/base/elements_upload_data_stream.h"
+#include "net/base/upload_bytes_element_reader.h"
+#include "net/reporting/reporting_metrics.h"
+#include "net/url_request/url_request_context.h"
+
+namespace {
+const char kUploadContentType[] = "application/report";
+} // namespace
+
+namespace net {
+
+namespace {
+
+ReportingUploader::Outcome ResponseCodeToOutcome(int response_code) {
+ if (response_code >= 200 && response_code <= 299)
+ return ReportingUploader::SUCCESS;
+ if (response_code == 410)
+ return ReportingUploader::REMOVE_ENDPOINT;
+ return ReportingUploader::FAILURE;
+}
+
+DeliveryFate OutcomeToFate(ReportingUploader::Outcome outcome) {
+ return (outcome == ReportingUploader::SUCCESS) ? DELIVERY_FATE_SUCCESS
+ : DELIVERY_FATE_FAILURE;
+}
+
+class ReportingUploaderImpl : public ReportingUploader, URLRequest::Delegate {
+ public:
+ ReportingUploaderImpl(const URLRequestContext* context) : context_(context) {
+ DCHECK(context_);
+ }
+
+ ~ReportingUploaderImpl() override {
+ for (auto& it : uploads_) {
+ base::ResetAndReturn(&it.second->second).Run(FAILURE);
+ it.second->first->Cancel();
+ HistogramDeliveryOutcome(DELIVERY_FATE_SHUTDOWN, 0, 0);
+ }
+ uploads_.clear();
+ }
+
+ void AttemptDelivery(const GURL& url,
+ const std::string& json,
+ const Callback& callback) override {
+ std::unique_ptr<URLRequest> request =
+ context_->CreateRequest(url, IDLE, this);
+
+ request->set_method("POST");
+
+ request->SetExtraRequestHeaderByName(HttpRequestHeaders::kContentType,
+ kUploadContentType, true);
+
+ std::vector<char> json_data(json.begin(), json.end());
+ std::unique_ptr<UploadElementReader> reader(
+ new UploadOwnedBytesElementReader(&json_data));
+ request->set_upload(
+ ElementsUploadDataStream::CreateWithReader(std::move(reader), 0));
+
+ // This inherently sets mode = "no-cors", but that doesn't matter, because
+ // the origins that are included in the upload don't actually get to see
+ // the response.
+ //
+ // This inherently skips Service Worker, too.
+ request->Start();
+
+ // Have to grab this as a reference to ensure request.get() happens before
+ // std::move(request).
+ std::unique_ptr<Upload>& upload = uploads_[request.get()];
+ upload = base::MakeUnique<Upload>(std::move(request), callback);
+ }
+
+ // URLRequest::Delegate implementation:
+ void OnResponseStarted(URLRequest* request, int net_error) override {
+ UploadMap::iterator it = uploads_.find(request);
+ std::unique_ptr<Upload> upload = std::move(it->second);
+ uploads_.erase(it);
+
+ int response_code = request->GetResponseCode();
+
+ Outcome outcome = ResponseCodeToOutcome(response_code);
+
+ HistogramDeliveryOutcome(OutcomeToFate(outcome),
+ request->status().ToNetError(), response_code);
+
+ upload->second.Run(outcome);
+
+ request->Cancel();
+
+ // Deletes request (upload->first) when upload goes out of scope and is
+ // deleted.
+ }
+
+ void OnReadCompleted(URLRequest* request, int bytes_read) override {
+ // Reporting doesn't need anything in the body of the response, so it
+ // doesn't read it, so it should never get OnReadCompleted calls.
+ NOTREACHED();
+ }
+
+ private:
+ using Upload = std::pair<std::unique_ptr<URLRequest>, Callback>;
+ using UploadMap = std::map<const URLRequest*, std::unique_ptr<Upload>>;
+
+ const URLRequestContext* context_;
+ UploadMap uploads_;
+};
+
+} // namespace
+
+ReportingUploader::~ReportingUploader() {}
+
+// static
+std::unique_ptr<ReportingUploader> ReportingUploader::Create(
+ const URLRequestContext* context) {
+ return base::MakeUnique<ReportingUploaderImpl>(context);
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698