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

Unified Diff: Source/core/loader/BeaconLoader.cpp

Issue 232053005: Implement navigator.sendBeacon() (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Adjust unrelated test expectation Created 6 years, 8 months 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: Source/core/loader/BeaconLoader.cpp
diff --git a/Source/core/loader/BeaconLoader.cpp b/Source/core/loader/BeaconLoader.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..8bde6f1bef9cd5a3a5755254ca05bee7f41cb7a6
--- /dev/null
+++ b/Source/core/loader/BeaconLoader.cpp
@@ -0,0 +1,106 @@
+// Copyright 2014 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 "config.h"
+#include "core/loader/BeaconLoader.h"
+
+#include "FetchInitiatorTypeNames.h"
+#include "core/dom/Document.h"
+#include "core/fetch/CrossOriginAccessControl.h"
+#include "core/fetch/FetchContext.h"
+#include "core/fileapi/File.h"
+#include "core/frame/LocalFrame.h"
+#include "core/html/DOMFormData.h"
+#include "platform/network/FormData.h"
+#include "platform/network/ParsedContentType.h"
+#include "platform/network/ResourceRequest.h"
+#include "wtf/ArrayBufferView.h"
+
+namespace WebCore {
+
+void BeaconLoader::prepareRequest(LocalFrame* frame, ResourceRequest& request)
+{
+ request.setTargetType(ResourceRequest::TargetIsBeacon);
+ request.setHTTPMethod("POST");
+ request.setHTTPHeaderField("Cache-Control", "max-age=0");
+ request.setAllowStoredCredentials(true);
+ frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document(), request, FetchSubresource);
+
+ // Origin: is unconditionally added for POSTs by addAdditionalRequestHeaders();
+ // limit ourselves to cross-origin requests.
+ SecurityOrigin* securityOrigin = frame->document()->securityOrigin();
+ if (securityOrigin->canRequest(request.url()))
+ request.clearHTTPOrigin();
+}
+
+void BeaconLoader::issueRequest(LocalFrame* frame, ResourceRequest& request)
+{
+ FetchInitiatorInfo initiatorInfo;
+ initiatorInfo.name = FetchInitiatorTypeNames::beacon;
+
+ SimplexLoader::start(frame, request, initiatorInfo);
+}
+
+void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, const String& data)
+{
+ ResourceRequest request(beaconURL);
+ prepareRequest(frame, request);
+
+ request.setHTTPContentType("text/plain;charset=UTF-8");
+ request.setHTTPBody(FormData::create(data.utf8()));
+
+ issueRequest(frame, request);
+}
+
+void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, PassRefPtr<ArrayBufferView>& data)
+{
+ ResourceRequest request(beaconURL);
+ prepareRequest(frame, request);
+
+ RefPtr<FormData> entityBody = FormData::create(data->baseAddress(), data->byteLength());
+ // Q: not in the spec; should there be some default?
+ AtomicString contentType = AtomicString("application/octet-stream");
+
+ request.setHTTPContentType(contentType);
+ request.setHTTPBody(entityBody.release());
+
+ issueRequest(frame, request);
+}
+
+void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, PassRefPtrWillBeRawPtr<Blob>& data)
+{
+ ResourceRequest request(beaconURL);
+ prepareRequest(frame, request);
+
+ RefPtr<FormData> entityBody = FormData::create();
+ if (data->hasBackingFile())
+ entityBody->appendFile(toFile(data.get())->path());
+ else
+ entityBody->appendBlob(data->uuid(), data->blobDataHandle());
+
+ AtomicString contentType;
+ const String& blobType = data->type();
+ if (!blobType.isEmpty() && isValidContentType(blobType))
+ request.setHTTPContentType(AtomicString(contentType));
+
+ request.setHTTPBody(entityBody.release());
+
+ issueRequest(frame, request);
+}
+
+void BeaconLoader::sendBeacon(LocalFrame* frame, const KURL& beaconURL, PassRefPtrWillBeRawPtr<DOMFormData>& data)
+{
+ ResourceRequest request(beaconURL);
+ prepareRequest(frame, request);
+
+ RefPtr<FormData> entityBody = data->createMultiPartFormData(data->encoding());
+ AtomicString contentType = AtomicString("multipart/form-data; boundary=", AtomicString::ConstructFromLiteral) + entityBody->boundary().data();
+
+ request.setHTTPContentType(contentType);
+ request.setHTTPBody(entityBody.release());
+
+ issueRequest(frame, request);
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698