PingLoader objects unnecessarily pass through OwnPtr
[WebKit-https.git] / Source / WebCore / loader / PingLoader.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  *
30  */
31
32 #include "config.h"
33 #include "PingLoader.h"
34
35 #include "Document.h"
36 #include "FormData.h"
37 #include "Frame.h"
38 #include "FrameLoader.h"
39 #include "FrameLoaderClient.h"
40 #include "InspectorInstrumentation.h"
41 #include "Page.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/text/CString.h>
49
50 namespace WebCore {
51
52 void PingLoader::loadImage(Frame* frame, const URL& url)
53 {
54     if (!frame->document()->securityOrigin()->canDisplay(url)) {
55         FrameLoader::reportLocalLoadFailed(frame, url);
56         return;
57     }
58
59     ResourceRequest request(url);
60 #if PLATFORM(BLACKBERRY)
61     request.setTargetType(ResourceRequest::TargetIsImage);
62 #endif
63     request.setHTTPHeaderField("Cache-Control", "max-age=0");
64     String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), request.url(), frame->loader().outgoingReferrer());
65     if (!referrer.isEmpty())
66         request.setHTTPReferrer(referrer);
67     frame->loader().addExtraFieldsToSubresourceRequest(request);
68
69     createPingLoader(frame, request);
70 }
71
72 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing
73 void PingLoader::sendPing(Frame* frame, const URL& pingURL, const URL& destinationURL)
74 {
75     ResourceRequest request(pingURL);
76 #if PLATFORM(BLACKBERRY)
77     request.setTargetType(ResourceRequest::TargetIsSubresource);
78 #endif
79     request.setHTTPMethod("POST");
80     request.setHTTPContentType("text/ping");
81     request.setHTTPBody(FormData::create("PING"));
82     request.setHTTPHeaderField("Cache-Control", "max-age=0");
83     frame->loader().addExtraFieldsToSubresourceRequest(request);
84
85     SecurityOrigin* sourceOrigin = frame->document()->securityOrigin();
86     RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL);
87     FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin->toString());
88     request.setHTTPHeaderField("Ping-To", destinationURL);
89     if (!SecurityPolicy::shouldHideReferrer(pingURL, frame->loader().outgoingReferrer())) {
90         request.setHTTPHeaderField("Ping-From", frame->document()->url());
91         if (!sourceOrigin->isSameSchemeHostPort(pingOrigin.get())) {
92             String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), pingURL, frame->loader().outgoingReferrer());
93             if (!referrer.isEmpty())
94                 request.setHTTPReferrer(referrer);
95         }
96     }
97
98     createPingLoader(frame, request);
99 }
100
101 void PingLoader::sendViolationReport(Frame* frame, const URL& reportURL, PassRefPtr<FormData> report)
102 {
103     ResourceRequest request(reportURL);
104 #if PLATFORM(BLACKBERRY)
105     request.setTargetType(ResourceRequest::TargetIsSubresource);
106 #endif
107     request.setHTTPMethod("POST");
108     request.setHTTPContentType("application/json");
109     request.setHTTPBody(report);
110     frame->loader().addExtraFieldsToSubresourceRequest(request);
111
112     String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), reportURL, frame->loader().outgoingReferrer());
113     if (!referrer.isEmpty())
114         request.setHTTPReferrer(referrer);
115
116     createPingLoader(frame, request);
117 }
118
119 void PingLoader::createPingLoader(Frame* frame, ResourceRequest& request)
120 {
121     // No need to free the PingLoader object or manage it via a smart pointer - it will kill itself as soon as it receives a response.
122     new PingLoader(frame, request);
123 }
124
125 PingLoader::PingLoader(Frame* frame, ResourceRequest& request)
126     : m_timeout(this, &PingLoader::timeout)
127 {
128     unsigned long identifier = frame->page()->progress().createUniqueIdentifier();
129     // FIXME: Why activeDocumentLoader? I would have expected documentLoader().
130     // Itseems like the PingLoader should be associated with the current
131     // Document in the Frame, but the activeDocumentLoader will be associated
132     // with the provisional DocumentLoader if there is a provisional
133     // DocumentLoader.
134     m_shouldUseCredentialStorage = frame->loader().client().shouldUseCredentialStorage(frame->loader().activeDocumentLoader(), identifier);
135     m_handle = ResourceHandle::create(frame->loader().networkingContext(), request, this, false, false);
136
137     InspectorInstrumentation::continueAfterPingLoader(frame, identifier, frame->loader().activeDocumentLoader(), request, ResourceResponse());
138
139     // If the server never responds, FrameLoader won't be able to cancel this load and
140     // we'll sit here waiting forever. Set a very generous timeout, just in case.
141     m_timeout.startOneShot(60000);
142 }
143
144 PingLoader::~PingLoader()
145 {
146     if (m_handle)
147         m_handle->cancel();
148 }
149
150 }