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

Side by Side Diff: Source/core/loader/BeaconLoader.cpp

Issue 232053005: Implement navigator.sendBeacon() (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Restructured ping loader code + added size restriction Created 6 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
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 "config.h"
6 #include "core/loader/BeaconLoader.h"
7
8 #include "FetchInitiatorTypeNames.h"
9 #include "core/dom/Document.h"
10 #include "core/fetch/CrossOriginAccessControl.h"
11 #include "core/fetch/FetchContext.h"
12 #include "core/fileapi/File.h"
13 #include "core/frame/LocalFrame.h"
14 #include "core/html/DOMFormData.h"
15 #include "platform/network/FormData.h"
16 #include "platform/network/ParsedContentType.h"
17 #include "platform/network/ResourceRequest.h"
18 #include "wtf/ArrayBufferView.h"
19
20 namespace WebCore {
21
22 void BeaconLoader::prepareRequest(LocalFrame* frame, ResourceRequest& request)
23 {
24 // NOTE: do not distinguish Beacon by target type.
25 request.setTargetType(ResourceRequest::TargetIsPing);
26 request.setHTTPMethod("POST");
27 request.setHTTPHeaderField("Cache-Control", "max-age=0");
28 request.setAllowStoredCredentials(true);
29 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document() , request, FetchSubresource);
30
31 // Origin: is unconditionally added for POSTs by addAdditionalRequestHeaders ();
32 // limit ourselves to cross-origin requests.
33 SecurityOrigin* securityOrigin = frame->document()->securityOrigin();
34 if (securityOrigin->canRequest(request.url()))
35 request.clearHTTPOrigin();
abarth-chromium 2014/05/08 13:15:25 Why? I'd just remove this logic. We include Orig
sof 2014/05/08 20:47:07 ok, no strong reason for filtering it out here. Re
36 }
37
38 void BeaconLoader::issueRequest(LocalFrame* frame, ResourceRequest& request)
39 {
40 FetchInitiatorInfo initiatorInfo;
41 initiatorInfo.name = FetchInitiatorTypeNames::beacon;
42
43 PingLoader::start(frame, request, initiatorInfo);
44 }
45
46 bool BeaconLoader::sendBeacon(LocalFrame* frame, int allowance, const KURL& beac onURL, const String& data, int& payloadLength)
47 {
48 ResourceRequest request(beaconURL);
49 prepareRequest(frame, request);
50
51 RefPtr<FormData> entityBody = FormData::create(data.utf8());
52 unsigned long long entitySize = entityBody->sizeInBytes();
53 if (allowance > 0 && static_cast<unsigned>(allowance) < entitySize)
54 return false;
55
56 request.setHTTPBody(entityBody);
57 request.setHTTPContentType("text/plain;charset=UTF-8");
58
59 issueRequest(frame, request);
60 payloadLength = entitySize;
61 return true;
62 }
63
64 bool BeaconLoader::sendBeacon(LocalFrame* frame, int allowance, const KURL& beac onURL, PassRefPtr<ArrayBufferView>& data, int& payloadLength)
65 {
66 ASSERT(data);
67 unsigned long long entitySize = data->byteLength();
68 if (allowance > 0 && static_cast<unsigned long long>(allowance) < entitySize )
69 return false;
70
71 ResourceRequest request(beaconURL);
72 prepareRequest(frame, request);
73
74 RefPtr<FormData> entityBody = FormData::create(data->baseAddress(), data->by teLength());
75 request.setHTTPBody(entityBody.release());
76 // Q: not in the spec; should there be some default?
77 AtomicString contentType = AtomicString("application/octet-stream");
78
79 request.setHTTPContentType(contentType);
80
81 issueRequest(frame, request);
82 payloadLength = entitySize;
83 return true;
84 }
85
86 bool BeaconLoader::sendBeacon(LocalFrame* frame, int allowance, const KURL& beac onURL, PassRefPtrWillBeRawPtr<Blob>& data, int& payloadLength)
87 {
88 ASSERT(data);
89 unsigned long long entitySize = data->size();
90 if (allowance > 0 && static_cast<unsigned long long>(allowance) < entitySize )
91 return false;
92
93 ResourceRequest request(beaconURL);
94 prepareRequest(frame, request);
95
96 RefPtr<FormData> entityBody = FormData::create();
97 if (data->hasBackingFile())
98 entityBody->appendFile(toFile(data.get())->path());
99 else
100 entityBody->appendBlob(data->uuid(), data->blobDataHandle());
101
102 request.setHTTPBody(entityBody.release());
103
104 AtomicString contentType;
105 const String& blobType = data->type();
106 if (!blobType.isEmpty() && isValidContentType(blobType))
107 request.setHTTPContentType(AtomicString(contentType));
108
109 issueRequest(frame, request);
110 payloadLength = entitySize;
111 return true;
112 }
113
114 bool BeaconLoader::sendBeacon(LocalFrame* frame, int allowance, const KURL& beac onURL, PassRefPtrWillBeRawPtr<DOMFormData>& data, int& payloadLength)
115 {
116 ASSERT(data);
117 ResourceRequest request(beaconURL);
118 prepareRequest(frame, request);
119
120 RefPtr<FormData> entityBody = data->createMultiPartFormData(data->encoding() );
121
122 unsigned long long entitySize = entityBody->sizeInBytes();
123 if (allowance > 0 && static_cast<unsigned long long>(allowance) < entitySize )
124 return false;
125
126 AtomicString contentType = AtomicString("multipart/form-data; boundary=", At omicString::ConstructFromLiteral) + entityBody->boundary().data();
127 request.setHTTPBody(entityBody.release());
128 request.setHTTPContentType(contentType);
129
130 issueRequest(frame, request);
131 payloadLength = entitySize;
132 return true;
133 }
134
135 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698