1/*
2 * Copyright (C) 2010 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WebFrame_h
27#define WebFrame_h
28
29#include "APIObject.h"
30#include "ImmutableArray.h"
31#include "WKBase.h"
32#include "WebFrameLoaderClient.h"
33#include <JavaScriptCore/JSBase.h>
34#include <WebCore/FrameLoaderClient.h>
35#include <WebCore/FrameLoaderTypes.h>
36#include <WebCore/PolicyChecker.h>
37#include <wtf/Forward.h>
38#include <wtf/PassRefPtr.h>
39#include <wtf/RefPtr.h>
40#include <wtf/RetainPtr.h>
41
42namespace WebCore {
43class Frame;
44class HTMLFrameOwnerElement;
45class IntPoint;
46class IntRect;
47class KURL;
48}
49
50namespace WebKit {
51
52class InjectedBundleHitTestResult;
53class InjectedBundleNodeHandle;
54class InjectedBundleRangeHandle;
55class InjectedBundleScriptWorld;
56class WebPage;
57
58class WebFrame : public TypedAPIObject<APIObject::TypeBundleFrame> {
59public:
60    static PassRefPtr<WebFrame> createMainFrame(WebPage*);
61    static PassRefPtr<WebFrame> createSubframe(WebPage*, const String& frameName, WebCore::HTMLFrameOwnerElement*);
62    ~WebFrame();
63
64    // Called when the FrameLoaderClient (and therefore the WebCore::Frame) is being torn down.
65    void invalidate();
66
67    WebPage* page() const;
68    WebCore::Frame* coreFrame() const { return m_coreFrame; }
69
70    uint64_t frameID() const { return m_frameID; }
71
72    uint64_t setUpPolicyListener(WebCore::FramePolicyFunction);
73    void invalidatePolicyListener();
74    void didReceivePolicyDecision(uint64_t listenerID, WebCore::PolicyAction, uint64_t downloadID);
75
76    void startDownload(const WebCore::ResourceRequest&);
77    void convertMainResourceLoadToDownload(WebCore::DocumentLoader*, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
78
79    String source() const;
80    String contentsAsString() const;
81    String selectionAsString() const;
82
83    WebCore::IntSize size() const;
84
85    // WKBundleFrame API and SPI functions
86    bool isMainFrame() const;
87    String name() const;
88    String url() const;
89    String innerText() const;
90    bool isFrameSet() const;
91    WebFrame* parentFrame() const;
92    PassRefPtr<ImmutableArray> childFrames();
93    JSGlobalContextRef jsContext();
94    JSGlobalContextRef jsContextForWorld(InjectedBundleScriptWorld*);
95    WebCore::IntRect contentBounds() const;
96    WebCore::IntRect visibleContentBounds() const;
97    WebCore::IntRect visibleContentBoundsExcludingScrollbars() const;
98    WebCore::IntSize scrollOffset() const;
99    bool hasHorizontalScrollbar() const;
100    bool hasVerticalScrollbar() const;
101    PassRefPtr<InjectedBundleHitTestResult> hitTest(const WebCore::IntPoint) const;
102    bool getDocumentBackgroundColor(double* red, double* green, double* blue, double* alpha);
103    bool containsAnyFormElements() const;
104    bool containsAnyFormControls() const;
105    void stopLoading();
106    bool handlesPageScaleGesture() const;
107
108    static WebFrame* frameForContext(JSContextRef);
109
110    JSValueRef jsWrapperForWorld(InjectedBundleNodeHandle*, InjectedBundleScriptWorld*);
111    JSValueRef jsWrapperForWorld(InjectedBundleRangeHandle*, InjectedBundleScriptWorld*);
112
113    static String counterValue(JSObjectRef element);
114
115    String layerTreeAsText() const;
116
117    unsigned pendingUnloadCount() const;
118
119    bool allowsFollowingLink(const WebCore::KURL&) const;
120
121    String provisionalURL() const;
122    String suggestedFilenameForResourceWithURL(const WebCore::KURL&) const;
123    String mimeTypeForResourceWithURL(const WebCore::KURL&) const;
124
125    void setTextDirection(const String&);
126
127    // Simple listener class used by plug-ins to know when frames finish or fail loading.
128    class LoadListener {
129    public:
130        virtual ~LoadListener() { }
131
132        virtual void didFinishLoad(WebFrame*) = 0;
133        virtual void didFailLoad(WebFrame*, bool wasCancelled) = 0;
134    };
135    void setLoadListener(LoadListener* loadListener) { m_loadListener = loadListener; }
136    LoadListener* loadListener() const { return m_loadListener; }
137
138#if PLATFORM(MAC)
139    typedef bool (*FrameFilterFunction)(WKBundleFrameRef, WKBundleFrameRef subframe, void* context);
140    RetainPtr<CFDataRef> webArchiveData(FrameFilterFunction, void* context);
141#endif
142
143private:
144    static PassRefPtr<WebFrame> create();
145    WebFrame();
146
147    void init(WebPage*, const String& frameName, WebCore::HTMLFrameOwnerElement*);
148
149    WebCore::Frame* m_coreFrame;
150
151    uint64_t m_policyListenerID;
152    WebCore::FramePolicyFunction m_policyFunction;
153    uint64_t m_policyDownloadID;
154
155    WebFrameLoaderClient m_frameLoaderClient;
156    LoadListener* m_loadListener;
157
158    uint64_t m_frameID;
159};
160
161} // namespace WebKit
162
163#endif // WebFrame_h
164