Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/loader/PingLoader.h"
34
35 #include "core/FetchInitiatorTypeNames.h"
36 #include "core/dom/Document.h"
37 #include "core/fetch/FetchContext.h"
38 #include "core/frame/FrameConsole.h"
39 #include "core/frame/LocalFrame.h"
40 #include "core/inspector/InspectorInstrumentation.h"
41 #include "core/inspector/InspectorTraceEvents.h"
42 #include "core/loader/FrameLoader.h"
43 #include "core/loader/FrameLoaderClient.h"
44 #include "core/loader/UniqueIdentifier.h"
45 #include "core/page/Page.h"
46 #include "platform/exported/WrappedResourceRequest.h"
47 #include "platform/network/ResourceError.h"
48 #include "platform/network/ResourceRequest.h"
49 #include "platform/network/ResourceResponse.h"
50 #include "platform/weborigin/SecurityOrigin.h"
51 #include "platform/weborigin/SecurityPolicy.h"
52 #include "public/platform/Platform.h"
53 #include "public/platform/WebURLLoader.h"
54 #include "public/platform/WebURLRequest.h"
55 #include "public/platform/WebURLResponse.h"
56 #include "wtf/OwnPtr.h"
57
58 namespace blink {
59
60 void PingLoader::loadImage(LocalFrame* frame, const KURL& url)
61 {
62     if (!frame->document()->securityOrigin()->canDisplay(url)) {
63         FrameLoader::reportLocalLoadFailed(frame, url.string());
64         return;
65     }
66
67     ResourceRequest request(url);
68     request.setRequestContext(blink::WebURLRequest::RequestContextPing);
69     request.setHTTPHeaderField("Cache-Control", "max-age=0");
70     frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document(), request, FetchSubresource);
71     frame->loader().fetchContext().setFirstPartyForCookies(request);
72
73     FetchInitiatorInfo initiatorInfo;
74     initiatorInfo.name = FetchInitiatorTypeNames::ping;
75     PingLoader::start(frame, request, initiatorInfo);
76 }
77
78 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing
79 void PingLoader::sendLinkAuditPing(LocalFrame* frame, const KURL& pingURL, const KURL& destinationURL)
80 {
81     ResourceRequest request(pingURL);
82     request.setRequestContext(blink::WebURLRequest::RequestContextPing);
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().fetchContext().addAdditionalRequestHeaders(frame->document(), request, FetchSubresource);
88     frame->loader().fetchContext().setFirstPartyForCookies(request);
89
90     RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL);
91     // addAdditionalRequestHeaders() will have added a referrer for same origin requests,
92     // but the spec omits the referrer for same origin.
93     if (frame->document()->securityOrigin()->isSameSchemeHostPort(pingOrigin.get()))
94         request.clearHTTPReferrer();
95
96     request.setHTTPHeaderField("Ping-To", AtomicString(destinationURL.string()));
97
98     // Ping-From follows the same rules as the default referrer beahavior for subresource requests.
99     // FIXME: Should Ping-From obey ReferrerPolicy?
100     if (!SecurityPolicy::shouldHideReferrer(pingURL, frame->document()->url().string()))
101         request.setHTTPHeaderField("Ping-From", AtomicString(frame->document()->url().string()));
102
103     FetchInitiatorInfo initiatorInfo;
104     initiatorInfo.name = FetchInitiatorTypeNames::ping;
105     PingLoader::start(frame, request, initiatorInfo);
106 }
107
108 void PingLoader::sendViolationReport(LocalFrame* frame, const KURL& reportURL, PassRefPtr<FormData> report, ViolationReportType type)
109 {
110     ResourceRequest request(reportURL);
111     request.setRequestContext(blink::WebURLRequest::RequestContextPing);
112     request.setHTTPMethod("POST");
113     request.setHTTPContentType(type == ContentSecurityPolicyViolationReport ? "application/csp-report" : "application/json");
114     request.setHTTPBody(report);
115     frame->loader().fetchContext().addAdditionalRequestHeaders(frame->document(), request, FetchSubresource);
116     frame->loader().fetchContext().setFirstPartyForCookies(request);
117
118     FetchInitiatorInfo initiatorInfo;
119     initiatorInfo.name = FetchInitiatorTypeNames::violationreport;
120     PingLoader::start(frame, request, initiatorInfo, SecurityOrigin::create(reportURL)->isSameSchemeHostPort(frame->document()->securityOrigin()) ? AllowStoredCredentials : DoNotAllowStoredCredentials);
121 }
122
123 void PingLoader::start(LocalFrame* frame, ResourceRequest& request, const FetchInitiatorInfo& initiatorInfo, StoredCredentials credentialsAllowed)
124 {
125     if (!frame->loader().mixedContentChecker()->canRunInsecureContent(frame->document()->securityOrigin(), request.url()))
126         return;
127
128     // Leak the ping loader, since it will kill itself as soon as it receives a response.
129     new PingLoader(frame, request, initiatorInfo, credentialsAllowed);
130 }
131
132 PingLoader::PingLoader(LocalFrame* frame, ResourceRequest& request, const FetchInitiatorInfo& initiatorInfo, StoredCredentials credentialsAllowed)
133     : PageLifecycleObserver(frame->page())
134     , m_timeout(this, &PingLoader::timeout)
135     , m_url(request.url())
136     , m_identifier(createUniqueIdentifier())
137 {
138     frame->loader().client()->didDispatchPingLoader(request.url());
139
140     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceSendRequest", "data", InspectorSendRequestEvent::data(m_identifier, frame, request));
141     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
142     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
143     InspectorInstrumentation::willSendRequest(frame, m_identifier, frame->loader().documentLoader(), request, ResourceResponse(), initiatorInfo);
144
145     m_loader = adoptPtr(blink::Platform::current()->createURLLoader());
146     ASSERT(m_loader);
147     blink::WrappedResourceRequest wrappedRequest(request);
148     wrappedRequest.setAllowStoredCredentials(credentialsAllowed == AllowStoredCredentials);
149     m_loader->loadAsynchronously(wrappedRequest, this);
150
151     // If the server never responds, FrameLoader won't be able to cancel this load and
152     // we'll sit here waiting forever. Set a very generous timeout, just in case.
153     m_timeout.startOneShot(60000, FROM_HERE);
154 }
155
156 PingLoader::~PingLoader()
157 {
158     if (m_loader)
159         m_loader->cancel();
160 }
161
162 void PingLoader::didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse& response)
163 {
164     if (Page* page = this->page()) {
165         TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(m_identifier, 0, true));
166         const ResourceResponse& resourceResponse = response.toResourceResponse();
167         InspectorInstrumentation::didReceiveResourceResponse(page->deprecatedLocalMainFrame(), m_identifier, 0, resourceResponse, 0);
168         didFailLoading(page);
169     }
170     delete this;
171 }
172
173 void PingLoader::didReceiveData(blink::WebURLLoader*, const char*, int, int)
174 {
175     if (Page* page = this->page()) {
176         TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(m_identifier, 0, true));
177         didFailLoading(page);
178     }
179     delete this;
180 }
181
182 void PingLoader::didFinishLoading(blink::WebURLLoader*, double, int64_t)
183 {
184     if (Page* page = this->page()) {
185         TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(m_identifier, 0, true));
186         didFailLoading(page);
187     }
188     delete this;
189 }
190
191 void PingLoader::didFail(blink::WebURLLoader*, const blink::WebURLError& resourceError)
192 {
193     if (Page* page = this->page()) {
194         TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(m_identifier, 0, true));
195         didFailLoading(page);
196     }
197     delete this;
198 }
199
200 void PingLoader::timeout(Timer<PingLoader>*)
201 {
202     if (Page* page = this->page()) {
203         TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(m_identifier, 0, true));
204         didFailLoading(page);
205     }
206     delete this;
207 }
208
209 void PingLoader::didFailLoading(Page* page)
210 {
211     LocalFrame* frame = page->deprecatedLocalMainFrame();
212     InspectorInstrumentation::didFailLoading(frame, m_identifier, ResourceError::cancelledError(m_url));
213     // Notification to FrameConsole should come AFTER Resource Agent notification, front-end relies on this.
214     frame->console().didFailLoading(m_identifier, ResourceError::cancelledError(m_url));
215 }
216
217 }