1/*
2 * Copyright (C) 2006, 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4 * Copyright (C) Research In Motion Limited 2009. All rights reserved.
5 * Copyright (C) 2011 Google Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1.  Redistributions of source code must retain the above copyright
12 *     notice, this list of conditions and the following disclaimer.
13 * 2.  Redistributions in binary form must reproduce the above copyright
14 *     notice, this list of conditions and the following disclaimer in the
15 *     documentation and/or other materials provided with the distribution.
16 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 *     its contributors may be used to endorse or promote products derived
18 *     from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef FrameLoader_h
33#define FrameLoader_h
34
35#include "core/dom/IconURL.h"
36#include "core/dom/SandboxFlags.h"
37#include "core/dom/SecurityContext.h"
38#include "core/fetch/ResourceLoaderOptions.h"
39#include "core/loader/FrameLoaderStateMachine.h"
40#include "core/loader/FrameLoaderTypes.h"
41#include "core/loader/HistoryItem.h"
42#include "core/loader/MixedContentChecker.h"
43#include "platform/Timer.h"
44#include "platform/heap/Handle.h"
45#include "platform/network/ResourceRequest.h"
46#include "wtf/Forward.h"
47#include "wtf/HashSet.h"
48#include "wtf/OwnPtr.h"
49
50namespace blink {
51
52class DocumentLoader;
53class FetchContext;
54class FormState;
55class Frame;
56class FrameLoaderClient;
57class NavigationAction;
58class ProgressTracker;
59class ResourceError;
60class SerializedScriptValue;
61class SubstituteData;
62
63struct FrameLoadRequest;
64
65bool isBackForwardLoadType(FrameLoadType);
66
67class FrameLoader FINAL {
68    WTF_MAKE_NONCOPYABLE(FrameLoader);
69    DISALLOW_ALLOCATION();
70public:
71    static ResourceRequest requestFromHistoryItem(HistoryItem*, ResourceRequestCachePolicy);
72
73    FrameLoader(LocalFrame*);
74    ~FrameLoader();
75
76    void init();
77
78    LocalFrame* frame() const { return m_frame; }
79
80    MixedContentChecker* mixedContentChecker() const { return &m_mixedContentChecker; }
81    ProgressTracker& progress() const { return *m_progressTracker; }
82
83    // These functions start a load. All eventually call into loadWithNavigationAction() or loadInSameDocument().
84    void load(const FrameLoadRequest&); // The entry point for non-reload, non-history loads.
85    void reload(ReloadPolicy = NormalReload, const KURL& overrideURL = KURL(), const AtomicString& overrideEncoding = nullAtom, ClientRedirectPolicy = NotClientRedirect);
86    void loadHistoryItem(HistoryItem*, HistoryLoadType = HistoryDifferentDocumentLoad, ResourceRequestCachePolicy = UseProtocolCachePolicy); // The entry point for all back/forward loads
87
88    static void reportLocalLoadFailed(LocalFrame*, const String& url);
89
90    // FIXME: These are all functions which stop loads. We have too many.
91    // Warning: stopAllLoaders can and will detach the LocalFrame out from under you. All callers need to either protect the LocalFrame
92    // or guarantee they won't in any way access the LocalFrame after stopAllLoaders returns.
93    void stopAllLoaders();
94    void stopLoading();
95    bool closeURL();
96
97    // FIXME: clear() is trying to do too many things. We should break it down into smaller functions.
98    void clear();
99
100    void replaceDocumentWhileExecutingJavaScriptURL(const String& source, Document* ownerDocument);
101
102    // Sets a timer to notify the client that the initial empty document has
103    // been accessed, and thus it is no longer safe to show a provisional URL
104    // above the document without risking a URL spoof.
105    void didAccessInitialDocument();
106
107    // If the initial empty document is showing and has been accessed, this
108    // cancels the timer and immediately notifies the client in cases that
109    // waiting to notify would allow a URL spoof.
110    void notifyIfInitialDocumentAccessed();
111
112    DocumentLoader* documentLoader() const { return m_documentLoader.get(); }
113    DocumentLoader* policyDocumentLoader() const { return m_policyDocumentLoader.get(); }
114    DocumentLoader* provisionalDocumentLoader() const { return m_provisionalDocumentLoader.get(); }
115    FrameState state() const { return m_state; }
116    FetchContext& fetchContext() const { return *m_fetchContext; }
117
118    void receivedMainResourceError(const ResourceError&);
119
120    bool isLoadingMainFrame() const;
121
122    bool shouldTreatURLAsSameAsCurrent(const KURL&) const;
123    bool shouldTreatURLAsSrcdocDocument(const KURL&) const;
124
125    FrameLoadType loadType() const;
126    void setLoadType(FrameLoadType loadType) { m_loadType = loadType; }
127
128    void checkLoadComplete();
129
130    FrameLoaderClient* client() const;
131
132    void setDefersLoading(bool);
133
134    void didExplicitOpen();
135
136    // Callbacks from DocumentWriter
137    void didBeginDocument(bool dispatchWindowObjectAvailable);
138
139    void receivedFirstData();
140
141    String userAgent(const KURL&) const;
142
143    void dispatchDidClearWindowObjectInMainWorld();
144    void dispatchDidClearDocumentOfWindowObject();
145    void dispatchDocumentElementAvailable();
146
147    // The following sandbox flags will be forced, regardless of changes to
148    // the sandbox attribute of any parent frames.
149    void forceSandboxFlags(SandboxFlags flags) { m_forcedSandboxFlags |= flags; }
150    SandboxFlags effectiveSandboxFlags() const;
151
152    Frame* opener();
153    void setOpener(LocalFrame*);
154
155    void detachFromParent();
156
157    void loadDone();
158    void finishedParsing();
159    void checkCompleted();
160
161    void commitProvisionalLoad();
162
163    FrameLoaderStateMachine* stateMachine() const { return &m_stateMachine; }
164
165    LocalFrame* findFrameForNavigation(const AtomicString& name, Document* activeDocument);
166
167    void applyUserAgent(ResourceRequest&);
168
169    bool shouldInterruptLoadForXFrameOptions(const String&, const KURL&, unsigned long requestIdentifier);
170
171    bool allAncestorsAreComplete() const; // including this
172
173    bool shouldClose();
174
175    bool allowPlugins(ReasonForCallingAllowPlugins);
176
177    void updateForSameDocumentNavigation(const KURL&, SameDocumentNavigationSource, PassRefPtr<SerializedScriptValue>, FrameLoadType);
178
179    HistoryItem* currentItem() const { return m_currentItem.get(); }
180    void saveScrollState();
181    void clearScrollPositionAndViewState();
182
183    void restoreScrollPositionAndViewState();
184
185    void trace(Visitor*);
186
187private:
188    bool allChildrenAreComplete() const; // immediate children, not all descendants
189
190    void completed();
191
192    void checkTimerFired(Timer<FrameLoader>*);
193    void didAccessInitialDocumentTimerFired(Timer<FrameLoader>*);
194
195    bool prepareRequestForThisFrame(FrameLoadRequest&);
196    void setReferrerForFrameRequest(ResourceRequest&, ShouldSendReferrer, Document*);
197    FrameLoadType determineFrameLoadType(const FrameLoadRequest&);
198    bool isScriptTriggeredFormSubmissionInChildFrame(const FrameLoadRequest&) const;
199
200    SubstituteData defaultSubstituteDataForURL(const KURL&);
201
202    bool shouldPerformFragmentNavigation(bool isFormSubmission, const String& httpMethod, FrameLoadType, const KURL&);
203    void scrollToFragmentWithParentBoundary(const KURL&);
204
205    bool checkLoadCompleteForThisFrame();
206
207    // Calls continueLoadAfterNavigationPolicy
208    void loadWithNavigationAction(const NavigationAction&, FrameLoadType, PassRefPtrWillBeRawPtr<FormState>,
209        const SubstituteData&, ContentSecurityPolicyCheck shouldCheckMainWorldContentSecurityPolicy, ClientRedirectPolicy = NotClientRedirect, const AtomicString& overrideEncoding = nullAtom);
210
211    bool validateTransitionNavigationMode();
212    bool dispatchNavigationTransitionData();
213    void detachClient();
214
215    void setHistoryItemStateForCommit(HistoryCommitType, bool isPushOrReplaceState = false, PassRefPtr<SerializedScriptValue> = nullptr);
216
217    void loadInSameDocument(const KURL&, PassRefPtr<SerializedScriptValue> stateObject, FrameLoadType, ClientRedirectPolicy);
218
219    void scheduleCheckCompleted();
220
221    RawPtrWillBeMember<LocalFrame> m_frame;
222
223    // FIXME: These should be OwnPtr<T> to reduce build times and simplify
224    // header dependencies unless performance testing proves otherwise.
225    // Some of these could be lazily created for memory savings on devices.
226    mutable FrameLoaderStateMachine m_stateMachine;
227    mutable MixedContentChecker m_mixedContentChecker;
228
229    OwnPtrWillBeMember<ProgressTracker> m_progressTracker;
230
231    FrameState m_state;
232    FrameLoadType m_loadType;
233
234    // Document loaders for the three phases of frame loading. Note that while
235    // a new request is being loaded, the old document loader may still be referenced.
236    // E.g. while a new request is in the "policy" state, the old document loader may
237    // be consulted in particular as it makes sense to imply certain settings on the new loader.
238    RefPtr<DocumentLoader> m_documentLoader;
239    RefPtr<DocumentLoader> m_provisionalDocumentLoader;
240    RefPtr<DocumentLoader> m_policyDocumentLoader;
241    OwnPtrWillBeMember<FetchContext> m_fetchContext;
242
243    RefPtr<HistoryItem> m_currentItem;
244    RefPtr<HistoryItem> m_provisionalItem;
245    struct DeferredHistoryLoad {
246        DeferredHistoryLoad(HistoryItem* item, HistoryLoadType type, ResourceRequestCachePolicy cachePolicy)
247            : m_item(item)
248            , m_type(type)
249            , m_cachePolicy(cachePolicy)
250        {
251        }
252
253        DeferredHistoryLoad() { }
254
255        bool isValid() { return m_item; }
256
257        RefPtr<HistoryItem> m_item;
258        HistoryLoadType m_type;
259        ResourceRequestCachePolicy m_cachePolicy;
260    };
261    DeferredHistoryLoad m_deferredHistoryLoad;
262
263    bool m_inStopAllLoaders;
264
265    Timer<FrameLoader> m_checkTimer;
266
267    bool m_didAccessInitialDocument;
268    Timer<FrameLoader> m_didAccessInitialDocumentTimer;
269
270    SandboxFlags m_forcedSandboxFlags;
271};
272
273} // namespace blink
274
275#endif // FrameLoader_h
276