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

Side by Side Diff: Source/core/loader/PingLoader.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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 18 matching lines...) Expand all
29 * 29 *
30 */ 30 */
31 31
32 #include "config.h" 32 #include "config.h"
33 #include "core/loader/PingLoader.h" 33 #include "core/loader/PingLoader.h"
34 34
35 #include "FetchInitiatorTypeNames.h" 35 #include "FetchInitiatorTypeNames.h"
36 #include "core/dom/Document.h" 36 #include "core/dom/Document.h"
37 #include "core/fetch/FetchContext.h" 37 #include "core/fetch/FetchContext.h"
38 #include "core/frame/LocalFrame.h" 38 #include "core/frame/LocalFrame.h"
39 #include "core/inspector/InspectorInstrumentation.h"
40 #include "core/loader/FrameLoader.h"
41 #include "core/loader/FrameLoaderClient.h"
42 #include "core/loader/UniqueIdentifier.h"
43 #include "core/page/Page.h"
44 #include "platform/exported/WrappedResourceRequest.h"
45 #include "platform/network/FormData.h" 39 #include "platform/network/FormData.h"
46 #include "platform/network/ResourceError.h"
47 #include "platform/network/ResourceRequest.h" 40 #include "platform/network/ResourceRequest.h"
48 #include "platform/network/ResourceResponse.h"
49 #include "platform/weborigin/SecurityOrigin.h" 41 #include "platform/weborigin/SecurityOrigin.h"
50 #include "platform/weborigin/SecurityPolicy.h" 42 #include "platform/weborigin/SecurityPolicy.h"
51 #include "public/platform/Platform.h"
52 #include "public/platform/WebURLLoader.h"
53 #include "public/platform/WebURLResponse.h"
54 #include "wtf/OwnPtr.h"
55 43
56 namespace WebCore { 44 namespace WebCore {
57 45
58 void PingLoader::loadImage(LocalFrame* frame, const KURL& url) 46 void PingLoader::loadImage(LocalFrame* frame, const KURL& url)
59 { 47 {
60 if (!frame->document()->securityOrigin()->canDisplay(url)) { 48 if (!frame->document()->securityOrigin()->canDisplay(url)) {
61 FrameLoader::reportLocalLoadFailed(frame, url.string()); 49 FrameLoader::reportLocalLoadFailed(frame, url.string());
62 return; 50 return;
63 } 51 }
64 52
65 ResourceRequest request(url); 53 ResourceRequest request(url);
66 request.setTargetType(ResourceRequest::TargetIsPing); 54 request.setTargetType(ResourceRequest::TargetIsPing);
67 request.setHTTPHeaderField("Cache-Control", "max-age=0"); 55 request.setHTTPHeaderField("Cache-Control", "max-age=0");
68 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document() , request, FetchSubresource); 56 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document() , request, FetchSubresource);
69 57
70 FetchInitiatorInfo initiatorInfo; 58 FetchInitiatorInfo initiatorInfo;
71 initiatorInfo.name = FetchInitiatorTypeNames::ping; 59 initiatorInfo.name = FetchInitiatorTypeNames::ping;
72 PingLoader::start(frame, request, initiatorInfo); 60 SimplexLoader::start(frame, request, initiatorInfo);
73 } 61 }
74 62
75 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperl ink-auditing 63 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperl ink-auditing
76 void PingLoader::sendPing(LocalFrame* frame, const KURL& pingURL, const KURL& de stinationURL) 64 void PingLoader::sendPing(LocalFrame* frame, const KURL& pingURL, const KURL& de stinationURL)
77 { 65 {
78 ResourceRequest request(pingURL); 66 ResourceRequest request(pingURL);
79 request.setTargetType(ResourceRequest::TargetIsPing); 67 request.setTargetType(ResourceRequest::TargetIsPing);
80 request.setHTTPMethod("POST"); 68 request.setHTTPMethod("POST");
81 request.setHTTPContentType("text/ping"); 69 request.setHTTPContentType("text/ping");
82 request.setHTTPBody(FormData::create("PING")); 70 request.setHTTPBody(FormData::create("PING"));
83 request.setHTTPHeaderField("Cache-Control", "max-age=0"); 71 request.setHTTPHeaderField("Cache-Control", "max-age=0");
84 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document() , request, FetchSubresource); 72 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document() , request, FetchSubresource);
85 73
86 RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL); 74 RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL);
87 // addAdditionalRequestHeaders() will have added a referrer for same origin requests, 75 // addAdditionalRequestHeaders() will have added a referrer for same origin requests,
88 // but the spec omits the referrer for same origin. 76 // but the spec omits the referrer for same origin.
89 if (frame->document()->securityOrigin()->isSameSchemeHostPort(pingOrigin.get ())) 77 if (frame->document()->securityOrigin()->isSameSchemeHostPort(pingOrigin.get ()))
90 request.clearHTTPReferrer(); 78 request.clearHTTPReferrer();
91 79
92 request.setHTTPHeaderField("Ping-To", AtomicString(destinationURL.string())) ; 80 request.setHTTPHeaderField("Ping-To", AtomicString(destinationURL.string())) ;
93 81
94 // Ping-From follows the same rules as the default referrer beahavior for su bresource requests. 82 // Ping-From follows the same rules as the default referrer beahavior for su bresource requests.
95 // FIXME: Should Ping-From obey ReferrerPolicy? 83 // FIXME: Should Ping-From obey ReferrerPolicy?
96 if (!SecurityPolicy::shouldHideReferrer(pingURL, frame->document()->url().st ring())) 84 if (!SecurityPolicy::shouldHideReferrer(pingURL, frame->document()->url().st ring()))
97 request.setHTTPHeaderField("Ping-From", AtomicString(frame->document()-> url().string())); 85 request.setHTTPHeaderField("Ping-From", AtomicString(frame->document()-> url().string()));
98 86
99 FetchInitiatorInfo initiatorInfo; 87 FetchInitiatorInfo initiatorInfo;
100 initiatorInfo.name = FetchInitiatorTypeNames::ping; 88 initiatorInfo.name = FetchInitiatorTypeNames::ping;
101 PingLoader::start(frame, request, initiatorInfo); 89 SimplexLoader::start(frame, request, initiatorInfo);
102 } 90 }
103 91
104 void PingLoader::sendViolationReport(LocalFrame* frame, const KURL& reportURL, P assRefPtr<FormData> report, ViolationReportType type) 92 void PingLoader::sendViolationReport(LocalFrame* frame, const KURL& reportURL, P assRefPtr<FormData> report, ViolationReportType type)
105 { 93 {
106 ResourceRequest request(reportURL); 94 ResourceRequest request(reportURL);
107 request.setTargetType(ResourceRequest::TargetIsSubresource); 95 request.setTargetType(ResourceRequest::TargetIsSubresource);
108 request.setHTTPMethod("POST"); 96 request.setHTTPMethod("POST");
109 request.setHTTPContentType(type == ContentSecurityPolicyViolationReport ? "a pplication/csp-report" : "application/json"); 97 request.setHTTPContentType(type == ContentSecurityPolicyViolationReport ? "a pplication/csp-report" : "application/json");
110 request.setHTTPBody(report); 98 request.setHTTPBody(report);
111 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document() , request, FetchSubresource); 99 frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document() , request, FetchSubresource);
112 100
113 FetchInitiatorInfo initiatorInfo; 101 FetchInitiatorInfo initiatorInfo;
114 initiatorInfo.name = FetchInitiatorTypeNames::violationreport; 102 initiatorInfo.name = FetchInitiatorTypeNames::violationreport;
115 PingLoader::start(frame, request, initiatorInfo, SecurityOrigin::create(repo rtURL)->isSameSchemeHostPort(frame->document()->securityOrigin()) ? AllowStoredC redentials : DoNotAllowStoredCredentials); 103 SimplexLoader::start(frame, request, initiatorInfo, SecurityOrigin::create(r eportURL)->isSameSchemeHostPort(frame->document()->securityOrigin()) ? AllowStor edCredentials : DoNotAllowStoredCredentials);
116 }
117
118 void PingLoader::start(LocalFrame* frame, ResourceRequest& request, const FetchI nitiatorInfo& initiatorInfo, StoredCredentials credentialsAllowed)
119 {
120 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request, init iatorInfo, credentialsAllowed));
121
122 // Leak the ping loader, since it will kill itself as soon as it receives a response.
123 PingLoader* ALLOW_UNUSED leakedPingLoader = pingLoader.leakPtr();
124 }
125
126 PingLoader::PingLoader(LocalFrame* frame, ResourceRequest& request, const FetchI nitiatorInfo& initiatorInfo, StoredCredentials credentialsAllowed)
127 : PageLifecycleObserver(frame->page())
128 , m_timeout(this, &PingLoader::timeout)
129 , m_url(request.url())
130 , m_identifier(createUniqueIdentifier())
131 {
132 frame->loader().client()->didDispatchPingLoader(request.url());
133
134 m_loader = adoptPtr(blink::Platform::current()->createURLLoader());
135 ASSERT(m_loader);
136 blink::WrappedResourceRequest wrappedRequest(request);
137 wrappedRequest.setAllowStoredCredentials(credentialsAllowed == AllowStoredCr edentials);
138 m_loader->loadAsynchronously(wrappedRequest, this);
139
140 InspectorInstrumentation::willSendRequest(frame, m_identifier, frame->loader ().documentLoader(), request, ResourceResponse(), initiatorInfo);
141
142 // If the server never responds, FrameLoader won't be able to cancel this lo ad and
143 // we'll sit here waiting forever. Set a very generous timeout, just in case .
144 m_timeout.startOneShot(60000, FROM_HERE);
145 }
146
147 PingLoader::~PingLoader()
148 {
149 if (m_loader)
150 m_loader->cancel();
151 }
152
153 void PingLoader::didReceiveResponse(blink::WebURLLoader*, const blink::WebURLRes ponse&)
154 {
155 if (Page* page = this->page())
156 InspectorInstrumentation::didFailLoading(page->mainFrame(), m_identifier , ResourceError::cancelledError(m_url));
157 delete this;
158 }
159
160 void PingLoader::didReceiveData(blink::WebURLLoader*, const char* data, int data Length, int encodedDataLength)
161 {
162 if (Page* page = this->page())
163 InspectorInstrumentation::didFailLoading(page->mainFrame(), m_identifier , ResourceError::cancelledError(m_url));
164 delete this;
165 }
166
167 void PingLoader::didFinishLoading(blink::WebURLLoader*, double, int64_t)
168 {
169 if (Page* page = this->page())
170 InspectorInstrumentation::didFailLoading(page->mainFrame(), m_identifier , ResourceError::cancelledError(m_url));
171 delete this;
172 }
173
174 void PingLoader::didFail(blink::WebURLLoader*, const blink::WebURLError& resourc eError)
175 {
176 if (Page* page = this->page())
177 InspectorInstrumentation::didFailLoading(page->mainFrame(), m_identifier , ResourceError(resourceError));
178 delete this;
179 }
180
181 void PingLoader::timeout(Timer<PingLoader>*)
182 {
183 if (Page* page = this->page())
184 InspectorInstrumentation::didFailLoading(page->mainFrame(), m_identifier , ResourceError::cancelledError(m_url));
185 delete this;
186 } 104 }
187 105
188 } 106 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698