608a18bfce618a6745e691c74db73faf4e6edb6c
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / FrameFetchContext.cpp
1 /*
2  * Copyright (C) 2013 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 #include "config.h"
32 #include "core/loader/FrameFetchContext.h"
33
34 #include "core/dom/Document.h"
35 #include "core/frame/LocalFrame.h"
36 #include "core/inspector/InspectorInstrumentation.h"
37 #include "core/inspector/InspectorTraceEvents.h"
38 #include "core/loader/DocumentLoader.h"
39 #include "core/loader/FrameLoader.h"
40 #include "core/loader/FrameLoaderClient.h"
41 #include "core/loader/ProgressTracker.h"
42 #include "core/page/Page.h"
43 #include "core/frame/Settings.h"
44 #include "platform/weborigin/SecurityPolicy.h"
45
46 namespace blink {
47
48 FrameFetchContext::FrameFetchContext(LocalFrame* frame)
49     : m_frame(frame)
50 {
51 }
52
53 void FrameFetchContext::reportLocalLoadFailed(const KURL& url)
54 {
55     FrameLoader::reportLocalLoadFailed(m_frame, url.elidedString());
56 }
57
58 void FrameFetchContext::addAdditionalRequestHeaders(Document* document, ResourceRequest& request, FetchResourceType type)
59 {
60     bool isMainResource = type == FetchMainResource;
61     if (!isMainResource) {
62         String outgoingReferrer;
63         String outgoingOrigin;
64         if (request.httpReferrer().isNull()) {
65             outgoingReferrer = document->outgoingReferrer();
66             outgoingOrigin = document->outgoingOrigin();
67         } else {
68             outgoingReferrer = request.httpReferrer();
69             outgoingOrigin = SecurityOrigin::createFromString(outgoingReferrer)->toString();
70         }
71
72         outgoingReferrer = SecurityPolicy::generateReferrerHeader(document->referrerPolicy(), request.url(), outgoingReferrer);
73         if (outgoingReferrer.isEmpty())
74             request.clearHTTPReferrer();
75         else if (!request.httpReferrer())
76             request.setHTTPReferrer(Referrer(outgoingReferrer, document->referrerPolicy()));
77
78         request.addHTTPOriginIfNeeded(AtomicString(outgoingOrigin));
79     }
80
81     // The remaining modifications are only necessary for HTTP and HTTPS.
82     if (!request.url().isEmpty() && !request.url().protocolIsInHTTPFamily())
83         return;
84
85     m_frame->loader().applyUserAgent(request);
86 }
87
88 void FrameFetchContext::setFirstPartyForCookies(ResourceRequest& request)
89 {
90     if (m_frame->tree().top()->isLocalFrame())
91         request.setFirstPartyForCookies(toLocalFrame(m_frame->tree().top())->document()->firstPartyForCookies());
92 }
93
94 CachePolicy FrameFetchContext::cachePolicy(Document* document) const
95 {
96     if (document && document->loadEventFinished())
97         return CachePolicyVerify;
98
99     FrameLoadType loadType = m_frame->loader().loadType();
100     if (loadType == FrameLoadTypeReloadFromOrigin)
101         return CachePolicyReload;
102
103     Frame* parentFrame = m_frame->tree().parent();
104     if (parentFrame && parentFrame->isLocalFrame()) {
105         CachePolicy parentCachePolicy = toLocalFrame(parentFrame)->loader().fetchContext().cachePolicy(toLocalFrame(parentFrame)->document());
106         if (parentCachePolicy != CachePolicyVerify)
107             return parentCachePolicy;
108     }
109
110     if (loadType == FrameLoadTypeReload)
111         return CachePolicyRevalidate;
112
113     DocumentLoader* loader = document ? document->loader() : 0;
114     if (loader && loader->request().cachePolicy() == ReturnCacheDataElseLoad)
115         return CachePolicyHistoryBuffer;
116     return CachePolicyVerify;
117
118 }
119
120 // FIXME(http://crbug.com/274173):
121 // |loader| can be null if the resource is loaded from imported document.
122 // This means inspector, which uses DocumentLoader as an grouping entity,
123 // cannot see imported documents.
124 inline DocumentLoader* FrameFetchContext::ensureLoader(DocumentLoader* loader)
125 {
126     return loader ? loader : m_frame->loader().documentLoader();
127 }
128
129 void FrameFetchContext::dispatchDidChangeResourcePriority(unsigned long identifier, ResourceLoadPriority loadPriority, int intraPriorityValue)
130 {
131     m_frame->loader().client()->dispatchDidChangeResourcePriority(identifier, loadPriority, intraPriorityValue);
132 }
133
134 void FrameFetchContext::dispatchWillSendRequest(DocumentLoader* loader, unsigned long identifier, ResourceRequest& request, const ResourceResponse& redirectResponse, const FetchInitiatorInfo& initiatorInfo)
135 {
136     m_frame->loader().applyUserAgent(request);
137     m_frame->loader().client()->dispatchWillSendRequest(loader, identifier, request, redirectResponse);
138     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceSendRequest", "data", InspectorSendRequestEvent::data(identifier, m_frame, request));
139     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
140     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
141     InspectorInstrumentation::willSendRequest(m_frame, identifier, ensureLoader(loader), request, redirectResponse, initiatorInfo);
142 }
143
144 void FrameFetchContext::dispatchDidLoadResourceFromMemoryCache(const ResourceRequest& request, const ResourceResponse& response)
145 {
146     m_frame->loader().client()->dispatchDidLoadResourceFromMemoryCache(request, response);
147 }
148
149 void FrameFetchContext::dispatchDidReceiveResponse(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r, ResourceLoader* resourceLoader)
150 {
151     m_frame->loader().progress().incrementProgress(identifier, r);
152     m_frame->loader().client()->dispatchDidReceiveResponse(loader, identifier, r);
153     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceReceiveResponse", "data", InspectorReceiveResponseEvent::data(identifier, m_frame, r));
154     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
155     InspectorInstrumentation::didReceiveResourceResponse(m_frame, identifier, ensureLoader(loader), r, resourceLoader);
156 }
157
158 void FrameFetchContext::dispatchDidReceiveData(DocumentLoader*, unsigned long identifier, const char* data, int dataLength, int encodedDataLength)
159 {
160     m_frame->loader().progress().incrementProgress(identifier, data, dataLength);
161     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceReceivedData", "data", InspectorReceiveDataEvent::data(identifier, m_frame, encodedDataLength));
162     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
163     InspectorInstrumentation::didReceiveData(m_frame, identifier, data, dataLength, encodedDataLength);
164 }
165
166 void FrameFetchContext::dispatchDidDownloadData(DocumentLoader*, unsigned long identifier, int dataLength, int encodedDataLength)
167 {
168     m_frame->loader().progress().incrementProgress(identifier, 0, dataLength);
169     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceReceivedData", "data", InspectorReceiveDataEvent::data(identifier, m_frame, encodedDataLength));
170     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
171     InspectorInstrumentation::didReceiveData(m_frame, identifier, 0, dataLength, encodedDataLength);
172 }
173
174 void FrameFetchContext::dispatchDidFinishLoading(DocumentLoader* loader, unsigned long identifier, double finishTime, int64_t encodedDataLength)
175 {
176     m_frame->loader().progress().completeProgress(identifier);
177     m_frame->loader().client()->dispatchDidFinishLoading(loader, identifier);
178
179     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(identifier, finishTime, false));
180     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
181     InspectorInstrumentation::didFinishLoading(m_frame, identifier, ensureLoader(loader), finishTime, encodedDataLength);
182 }
183
184 void FrameFetchContext::dispatchDidFail(DocumentLoader* loader, unsigned long identifier, const ResourceError& error)
185 {
186     m_frame->loader().progress().completeProgress(identifier);
187     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(identifier, 0, true));
188     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
189     InspectorInstrumentation::didFailLoading(m_frame, identifier, error);
190 }
191
192 void FrameFetchContext::sendRemainingDelegateMessages(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& response, int dataLength)
193 {
194     if (!response.isNull())
195         dispatchDidReceiveResponse(ensureLoader(loader), identifier, response);
196
197     if (dataLength > 0)
198         dispatchDidReceiveData(ensureLoader(loader), identifier, 0, dataLength, 0);
199
200     dispatchDidFinishLoading(ensureLoader(loader), identifier, 0, 0);
201 }
202
203 } // namespace blink