Home | History | Annotate | Download | only in loader
      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/SecurityContext.h"
     37 #include "core/loader/FrameLoaderStateMachine.h"
     38 #include "core/loader/FrameLoaderTypes.h"
     39 #include "core/loader/HistoryController.h"
     40 #include "core/loader/MixedContentChecker.h"
     41 #include "core/loader/ResourceLoadNotifier.h"
     42 #include "core/loader/ResourceLoaderOptions.h"
     43 #include "core/loader/cache/CachePolicy.h"
     44 #include "core/page/LayoutMilestones.h"
     45 #include "core/platform/Timer.h"
     46 #include "wtf/Forward.h"
     47 #include "wtf/HashSet.h"
     48 #include "wtf/OwnPtr.h"
     49 
     50 namespace WebCore {
     51 
     52 class Resource;
     53 class Chrome;
     54 class DOMWrapperWorld;
     55 class DocumentLoader;
     56 class Event;
     57 class FormState;
     58 class FormSubmission;
     59 class FrameLoaderClient;
     60 class IconController;
     61 class NavigationAction;
     62 class Page;
     63 class ResourceError;
     64 class ResourceRequest;
     65 class ResourceResponse;
     66 class SecurityOrigin;
     67 class SerializedScriptValue;
     68 class StringWithDirection;
     69 class SubstituteData;
     70 
     71 struct FrameLoadRequest;
     72 struct WindowFeatures;
     73 
     74 bool isBackForwardLoadType(FrameLoadType);
     75 
     76 class FrameLoader {
     77     WTF_MAKE_NONCOPYABLE(FrameLoader);
     78 public:
     79     FrameLoader(Frame*, FrameLoaderClient*);
     80     ~FrameLoader();
     81 
     82     void init();
     83 
     84     Frame* frame() const { return m_frame; }
     85 
     86     HistoryController* history() const { return &m_history; }
     87     ResourceLoadNotifier* notifier() const { return &m_notifer; }
     88     IconController* icon() const { return m_icon.get(); }
     89     MixedContentChecker* mixedContentChecker() const { return &m_mixedContentChecker; }
     90 
     91     void prepareForHistoryNavigation();
     92 
     93     // These functions start a load. All eventually call into loadWithNavigationAction() or loadInSameDocument().
     94     void load(const FrameLoadRequest&); // The entry point for non-reload, non-history loads.
     95     void reload(ReloadPolicy = NormalReload, const KURL& overrideURL = KURL(), const String& overrideEncoding = String());
     96     void loadHistoryItem(HistoryItem*); // The entry point for all back/forward loads
     97     void submitForm(PassRefPtr<FormSubmission>); // Schedules a form submission, which will eventually call load() in the target frame.
     98 
     99     // FIXME: This doesn't really belong here, since we don't load Frames synchronously.
    100     unsigned long loadResourceSynchronously(const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
    101 
    102     HistoryItem* requestedHistoryItem() const { return m_requestedHistoryItem.get(); }
    103 
    104     static void reportLocalLoadFailed(Frame*, const String& url);
    105 
    106     // FIXME: These are all functions which stop loads. We have too many.
    107     // Warning: stopAllLoaders can and will detach the Frame out from under you. All callers need to either protect the Frame
    108     // or guarantee they won't in any way access the Frame after stopAllLoaders returns.
    109     void stopAllLoaders(ClearProvisionalItemPolicy = ShouldClearProvisionalItem);
    110     void stopForUserCancel(bool deferCheckLoadComplete = false);
    111     void stop();
    112     void stopLoading(UnloadEventPolicy);
    113     bool closeURL();
    114     void cancelAndClear();
    115     // FIXME: clear() is trying to do too many things. We should break it down into smaller functions.
    116     void clear(ClearOptions);
    117 
    118     // Sets a timer to notify the client that the initial empty document has
    119     // been accessed, and thus it is no longer safe to show a provisional URL
    120     // above the document without risking a URL spoof.
    121     void didAccessInitialDocument();
    122 
    123     // If the initial empty document is showing and has been accessed, this
    124     // cancels the timer and immediately notifies the client in cases that
    125     // waiting to notify would allow a URL spoof.
    126     void notifyIfInitialDocumentAccessed();
    127 
    128     bool isLoading() const;
    129 
    130     int numPendingOrLoadingRequests(bool recurse) const;
    131     String referrer() const;
    132     String outgoingReferrer() const;
    133     String outgoingOrigin() const;
    134 
    135     DocumentLoader* activeDocumentLoader() const;
    136     DocumentLoader* documentLoader() const { return m_documentLoader.get(); }
    137     DocumentLoader* policyDocumentLoader() const { return m_policyDocumentLoader.get(); }
    138     DocumentLoader* provisionalDocumentLoader() const { return m_provisionalDocumentLoader.get(); }
    139     FrameState state() const { return m_state; }
    140 
    141     const ResourceRequest& originalRequest() const;
    142     void receivedMainResourceError(const ResourceError&);
    143 
    144     bool willLoadMediaElementURL(KURL&);
    145 
    146     void handleFallbackContent();
    147 
    148     bool isHostedByObjectElement() const;
    149     bool isLoadingMainFrame() const;
    150 
    151     bool subframeIsLoading() const;
    152     void didChangeTitle(DocumentLoader*);
    153 
    154     bool shouldTreatURLAsSrcdocDocument(const KURL&) const;
    155 
    156     FrameLoadType loadType() const;
    157     void setLoadType(FrameLoadType loadType) { m_loadType = loadType; }
    158 
    159     CachePolicy subresourceCachePolicy() const;
    160 
    161     void didLayout(LayoutMilestones);
    162     void didFirstLayout();
    163 
    164     void loadedResourceFromMemoryCache(Resource*);
    165 
    166     void checkLoadComplete();
    167     void detachFromParent();
    168 
    169     void addExtraFieldsToRequest(ResourceRequest&);
    170 
    171     static void addHTTPOriginIfNeeded(ResourceRequest&, const String& origin);
    172 
    173     FrameLoaderClient* client() const { return m_client; }
    174 
    175     void setDefersLoading(bool);
    176 
    177     void didExplicitOpen();
    178 
    179     // Callbacks from DocumentWriter
    180     void didBeginDocument(bool dispatchWindowObjectAvailable);
    181 
    182     void receivedFirstData();
    183 
    184     String userAgent(const KURL&) const;
    185 
    186     void dispatchDidClearWindowObjectInWorld(DOMWrapperWorld*);
    187     void dispatchDidClearWindowObjectsInAllWorlds();
    188     void dispatchDocumentElementAvailable();
    189 
    190     // The following sandbox flags will be forced, regardless of changes to
    191     // the sandbox attribute of any parent frames.
    192     void forceSandboxFlags(SandboxFlags flags) { m_forcedSandboxFlags |= flags; }
    193     SandboxFlags effectiveSandboxFlags() const;
    194 
    195     bool checkIfFormActionAllowedByCSP(const KURL&) const;
    196 
    197     Frame* opener();
    198     void setOpener(Frame*);
    199 
    200     void resetMultipleFormSubmissionProtection();
    201 
    202     void frameDetached();
    203 
    204     void setOutgoingReferrer(const KURL&);
    205 
    206     void loadDone();
    207     void finishedParsing();
    208     void checkCompleted();
    209 
    210     bool isComplete() const;
    211 
    212     void setTitle(const StringWithDirection&);
    213 
    214     void commitProvisionalLoad();
    215 
    216     FrameLoaderStateMachine* stateMachine() const { return &m_stateMachine; }
    217 
    218     Frame* findFrameForNavigation(const AtomicString& name, Document* activeDocument = 0);
    219 
    220     void applyUserAgent(ResourceRequest&);
    221 
    222     bool shouldInterruptLoadForXFrameOptions(const String&, const KURL&, unsigned long requestIdentifier);
    223 
    224     void completed();
    225     bool allAncestorsAreComplete() const; // including this
    226 
    227     bool suppressOpenerInNewFrame() const { return m_suppressOpenerInNewFrame; }
    228 
    229     bool shouldClose();
    230 
    231     void started();
    232 
    233     void setContainsPlugins() { m_containsPlugins = true; }
    234     bool containsPlugins() const { return m_containsPlugins; }
    235     bool allowPlugins(ReasonForCallingAllowPlugins);
    236 
    237     enum PageDismissalType {
    238         NoDismissal = 0,
    239         BeforeUnloadDismissal = 1,
    240         PageHideDismissal = 2,
    241         UnloadDismissal = 3
    242     };
    243     PageDismissalType pageDismissalEventBeingDispatched() const { return m_pageDismissalEventBeingDispatched; }
    244 
    245     enum UpdateBackForwardListPolicy {
    246         UpdateBackForwardList,
    247         DoNotUpdateBackForwardList
    248     };
    249     void updateForSameDocumentNavigation(const KURL&, SameDocumentNavigationSource, PassRefPtr<SerializedScriptValue>, const String& title, UpdateBackForwardListPolicy);
    250 
    251 private:
    252     bool allChildrenAreComplete() const; // immediate children, not all descendants
    253 
    254     void checkTimerFired(Timer<FrameLoader>*);
    255     void didAccessInitialDocumentTimerFired(Timer<FrameLoader>*);
    256 
    257     void loadSameDocumentItem(HistoryItem*);
    258     void loadDifferentDocumentItem(HistoryItem*);
    259     void insertDummyHistoryItem();
    260 
    261     bool prepareRequestForThisFrame(FrameLoadRequest&);
    262     void setReferrerForFrameRequest(ResourceRequest&, ShouldSendReferrer);
    263     FrameLoadType determineFrameLoadType(const FrameLoadRequest&);
    264 
    265     SubstituteData defaultSubstituteDataForURL(const KURL&);
    266 
    267     bool fireBeforeUnloadEvent(Chrome&, FrameLoader*);
    268     bool hasAllowedNavigationViaBeforeUnloadConfirmationPanel() const { return m_hasAllowedNavigationViaBeforeUnloadConfirmationPanel; }
    269     void didAllowNavigationViaBeforeUnloadConfirmationPanel() { m_hasAllowedNavigationViaBeforeUnloadConfirmationPanel = true; }
    270     void clearAllowNavigationViaBeforeUnloadConfirmationPanel() { m_hasAllowedNavigationViaBeforeUnloadConfirmationPanel = false; }
    271 
    272     void checkNavigationPolicyAndContinueFragmentScroll(const NavigationAction&, bool isNewNavigation);
    273     void checkNewWindowPolicyAndContinue(PassRefPtr<FormState>, const String& frameName, const NavigationAction&);
    274 
    275     bool shouldPerformFragmentNavigation(bool isFormSubmission, const String& httpMethod, FrameLoadType, const KURL&);
    276     void scrollToFragmentWithParentBoundary(const KURL&);
    277 
    278     void checkLoadCompleteForThisFrame();
    279 
    280     void closeOldDataSources();
    281 
    282     void dispatchDidCommitLoad();
    283 
    284     // Calls continueLoadAfterNavigationPolicy
    285     void loadWithNavigationAction(const ResourceRequest&, const NavigationAction&,
    286         FrameLoadType, PassRefPtr<FormState>, const SubstituteData&, const String& overrideEncoding = String());
    287 
    288     void requestFromDelegate(ResourceRequest&, unsigned long& identifier, ResourceError&);
    289 
    290     void detachChildren();
    291     void closeAndRemoveChild(Frame*);
    292 
    293     void loadInSameDocument(const KURL&, PassRefPtr<SerializedScriptValue> stateObject, bool isNewNavigation);
    294 
    295     void scheduleCheckCompleted();
    296     void scheduleCheckLoadComplete();
    297     void startCheckCompleteTimer();
    298 
    299     bool shouldTreatURLAsSameAsCurrent(const KURL&) const;
    300 
    301     Frame* m_frame;
    302     FrameLoaderClient* m_client;
    303 
    304     // FIXME: These should be OwnPtr<T> to reduce build times and simplify
    305     // header dependencies unless performance testing proves otherwise.
    306     // Some of these could be lazily created for memory savings on devices.
    307     mutable HistoryController m_history;
    308     mutable ResourceLoadNotifier m_notifer;
    309     mutable FrameLoaderStateMachine m_stateMachine;
    310     OwnPtr<IconController> m_icon;
    311     mutable MixedContentChecker m_mixedContentChecker;
    312 
    313     class FrameProgressTracker;
    314     OwnPtr<FrameProgressTracker> m_progressTracker;
    315 
    316     FrameState m_state;
    317     FrameLoadType m_loadType;
    318 
    319     // Document loaders for the three phases of frame loading. Note that while
    320     // a new request is being loaded, the old document loader may still be referenced.
    321     // E.g. while a new request is in the "policy" state, the old document loader may
    322     // be consulted in particular as it makes sense to imply certain settings on the new loader.
    323     RefPtr<DocumentLoader> m_documentLoader;
    324     RefPtr<DocumentLoader> m_provisionalDocumentLoader;
    325     RefPtr<DocumentLoader> m_policyDocumentLoader;
    326 
    327     bool m_inStopAllLoaders;
    328 
    329     String m_outgoingReferrer;
    330 
    331     PageDismissalType m_pageDismissalEventBeingDispatched;
    332     bool m_isComplete;
    333 
    334     bool m_needsClear;
    335     bool m_containsPlugins;
    336 
    337     KURL m_submittedFormURL;
    338 
    339     Timer<FrameLoader> m_checkTimer;
    340     bool m_shouldCallCheckCompleted;
    341     bool m_shouldCallCheckLoadComplete;
    342 
    343     Frame* m_opener;
    344     HashSet<Frame*> m_openedFrames;
    345 
    346     bool m_didAccessInitialDocument;
    347     Timer<FrameLoader> m_didAccessInitialDocumentTimer;
    348     bool m_suppressOpenerInNewFrame;
    349     bool m_startingClientRedirect;
    350 
    351     SandboxFlags m_forcedSandboxFlags;
    352 
    353     bool m_hasAllowedNavigationViaBeforeUnloadConfirmationPanel;
    354 
    355     RefPtr<HistoryItem> m_requestedHistoryItem;
    356 };
    357 
    358 } // namespace WebCore
    359 
    360 #endif // FrameLoader_h
    361