2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "PingLoader.h"
38 #include "FrameLoader.h"
39 #include "FrameLoaderClient.h"
40 #include "InspectorInstrumentation.h"
42 #include "ProgressTracker.h"
43 #include "ResourceHandle.h"
44 #include "ResourceRequest.h"
45 #include "ResourceResponse.h"
46 #include "SecurityOrigin.h"
47 #include "SecurityPolicy.h"
48 #include <wtf/OwnPtr.h>
49 #include <wtf/text/CString.h>
53 void PingLoader::loadImage(Frame* frame, const URL& url)
55 if (!frame->document()->securityOrigin()->canDisplay(url)) {
56 FrameLoader::reportLocalLoadFailed(frame, url);
60 ResourceRequest request(url);
61 #if PLATFORM(BLACKBERRY)
62 request.setTargetType(ResourceRequest::TargetIsImage);
64 request.setHTTPHeaderField("Cache-Control", "max-age=0");
65 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), request.url(), frame->loader().outgoingReferrer());
66 if (!referrer.isEmpty())
67 request.setHTTPReferrer(referrer);
68 frame->loader().addExtraFieldsToSubresourceRequest(request);
69 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request));
71 // Leak the ping loader, since it will kill itself as soon as it receives a response.
72 PingLoader* leakedPingLoader = pingLoader.leakPtr();
73 UNUSED_PARAM(leakedPingLoader);
76 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing
77 void PingLoader::sendPing(Frame* frame, const URL& pingURL, const URL& destinationURL)
79 ResourceRequest request(pingURL);
80 #if PLATFORM(BLACKBERRY)
81 request.setTargetType(ResourceRequest::TargetIsSubresource);
83 request.setHTTPMethod("POST");
84 request.setHTTPContentType("text/ping");
85 request.setHTTPBody(FormData::create("PING"));
86 request.setHTTPHeaderField("Cache-Control", "max-age=0");
87 frame->loader().addExtraFieldsToSubresourceRequest(request);
89 SecurityOrigin* sourceOrigin = frame->document()->securityOrigin();
90 RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL);
91 FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin->toString());
92 request.setHTTPHeaderField("Ping-To", destinationURL);
93 if (!SecurityPolicy::shouldHideReferrer(pingURL, frame->loader().outgoingReferrer())) {
94 request.setHTTPHeaderField("Ping-From", frame->document()->url());
95 if (!sourceOrigin->isSameSchemeHostPort(pingOrigin.get())) {
96 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), pingURL, frame->loader().outgoingReferrer());
97 if (!referrer.isEmpty())
98 request.setHTTPReferrer(referrer);
101 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request));
103 // Leak the ping loader, since it will kill itself as soon as it receives a response.
104 PingLoader* leakedPingLoader = pingLoader.leakPtr();
105 UNUSED_PARAM(leakedPingLoader);
108 void PingLoader::sendViolationReport(Frame* frame, const URL& reportURL, PassRefPtr<FormData> report)
110 ResourceRequest request(reportURL);
111 #if PLATFORM(BLACKBERRY)
112 request.setTargetType(ResourceRequest::TargetIsSubresource);
114 request.setHTTPMethod("POST");
115 request.setHTTPContentType("application/json");
116 request.setHTTPBody(report);
117 frame->loader().addExtraFieldsToSubresourceRequest(request);
119 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), reportURL, frame->loader().outgoingReferrer());
120 if (!referrer.isEmpty())
121 request.setHTTPReferrer(referrer);
122 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request));
124 // Leak the ping loader, since it will kill itself as soon as it receives a response.
125 PingLoader* leakedPingLoader = pingLoader.leakPtr();
126 UNUSED_PARAM(leakedPingLoader);
129 PingLoader::PingLoader(Frame* frame, ResourceRequest& request)
130 : m_timeout(this, &PingLoader::timeout)
132 unsigned long identifier = frame->page()->progress().createUniqueIdentifier();
133 // FIXME: Why activeDocumentLoader? I would have expected documentLoader().
134 // Itseems like the PingLoader should be associated with the current
135 // Document in the Frame, but the activeDocumentLoader will be associated
136 // with the provisional DocumentLoader if there is a provisional
138 m_shouldUseCredentialStorage = frame->loader().client().shouldUseCredentialStorage(frame->loader().activeDocumentLoader(), identifier);
139 m_handle = ResourceHandle::create(frame->loader().networkingContext(), request, this, false, false);
141 InspectorInstrumentation::continueAfterPingLoader(frame, identifier, frame->loader().activeDocumentLoader(), request, ResourceResponse());
143 // If the server never responds, FrameLoader won't be able to cancel this load and
144 // we'll sit here waiting forever. Set a very generous timeout, just in case.
145 m_timeout.startOneShot(60000);
148 PingLoader::~PingLoader()