1/*
2 * Copyright (C) 2006, 2007, 2011 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WebFrame_H
27#define WebFrame_H
28
29#include "WebFrameLoaderClient.h"
30
31#include "WebKit.h"
32#include "WebDataSource.h"
33
34#include "AccessibleDocument.h"
35
36#include <WebCore/FrameWin.h>
37#include <WebCore/GraphicsContext.h>
38#include <WebCore/KURL.h>
39#include <WebCore/PlatformString.h>
40#include <WebCore/ResourceHandleClient.h>
41
42#include <WTF/RefPtr.h>
43#include <WTF/HashMap.h>
44#include <WTF/OwnPtr.h>
45
46namespace WebCore {
47    class AuthenticationChallenge;
48    class DocumentLoader;
49    class Element;
50    class Frame;
51    class GraphicsContext;
52    class HTMLFrameOwnerElement;
53    class IntRect;
54    class Page;
55    class ResourceError;
56    class SharedBuffer;
57}
58
59typedef const struct OpaqueJSContext* JSContextRef;
60typedef struct OpaqueJSValue* JSObjectRef;
61
62#if USE(CG)
63typedef struct CGContext PlatformGraphicsContext;
64#elif USE(CAIRO)
65namespace WebCore {
66class PlatformContextCairo;
67}
68typedef class WebCore::PlatformContextCairo PlatformGraphicsContext;
69#endif
70
71class WebFrame;
72class WebFramePolicyListener;
73class WebHistory;
74class WebView;
75
76interface IWebHistoryItemPrivate;
77
78WebFrame* kit(WebCore::Frame*);
79WebCore::Frame* core(WebFrame*);
80
81class DECLSPEC_UUID("{A3676398-4485-4a9d-87DC-CB5A40E6351D}") WebFrame : public IWebFrame, IWebFramePrivate, IWebDocumentText
82    , public WebFrameLoaderClient
83{
84public:
85    static WebFrame* createInstance();
86protected:
87    WebFrame();
88    ~WebFrame();
89
90public:
91    // IUnknown
92    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
93    virtual ULONG STDMETHODCALLTYPE AddRef(void);
94    virtual ULONG STDMETHODCALLTYPE Release(void);
95
96    //IWebFrame
97    virtual HRESULT STDMETHODCALLTYPE name(
98        /* [retval][out] */ BSTR *frameName);
99
100    virtual HRESULT STDMETHODCALLTYPE webView(
101        /* [retval][out] */ IWebView **view);
102
103    virtual HRESULT STDMETHODCALLTYPE frameView(
104        /* [retval][out] */ IWebFrameView **view);
105
106    virtual HRESULT STDMETHODCALLTYPE DOMDocument(
107        /* [retval][out] */ IDOMDocument** document);
108
109    virtual HRESULT STDMETHODCALLTYPE frameElement(
110        /* [retval][out] */ IDOMHTMLElement **frameElement);
111
112    virtual HRESULT STDMETHODCALLTYPE loadRequest(
113        /* [in] */ IWebURLRequest *request);
114
115    virtual HRESULT STDMETHODCALLTYPE loadData(
116        /* [in] */ IStream *data,
117        /* [in] */ BSTR mimeType,
118        /* [in] */ BSTR textEncodingName,
119        /* [in] */ BSTR url);
120
121    virtual HRESULT STDMETHODCALLTYPE loadHTMLString(
122        /* [in] */ BSTR string,
123        /* [in] */ BSTR baseURL);
124
125    virtual HRESULT STDMETHODCALLTYPE loadAlternateHTMLString(
126        /* [in] */ BSTR str,
127        /* [in] */ BSTR baseURL,
128        /* [in] */ BSTR unreachableURL);
129
130    virtual HRESULT STDMETHODCALLTYPE loadArchive(
131        /* [in] */ IWebArchive *archive);
132
133    virtual HRESULT STDMETHODCALLTYPE dataSource(
134        /* [retval][out] */ IWebDataSource **source);
135
136    virtual HRESULT STDMETHODCALLTYPE provisionalDataSource(
137        /* [retval][out] */ IWebDataSource **source);
138
139    virtual HRESULT STDMETHODCALLTYPE stopLoading( void);
140
141    virtual HRESULT STDMETHODCALLTYPE reload( void);
142
143    virtual HRESULT STDMETHODCALLTYPE findFrameNamed(
144        /* [in] */ BSTR name,
145        /* [retval][out] */ IWebFrame **frame);
146
147    virtual HRESULT STDMETHODCALLTYPE parentFrame(
148        /* [retval][out] */ IWebFrame **frame);
149
150    virtual HRESULT STDMETHODCALLTYPE childFrames(
151        /* [retval][out] */ IEnumVARIANT **enumFrames);
152
153    virtual HRESULT STDMETHODCALLTYPE currentForm(
154        /* [retval][out] */ IDOMElement **formElement);
155
156    virtual /* [local] */ JSGlobalContextRef STDMETHODCALLTYPE globalContext();
157
158    // IWebFramePrivate
159    virtual HRESULT STDMETHODCALLTYPE unused1(BSTR*) { return E_NOTIMPL; }
160    virtual HRESULT STDMETHODCALLTYPE renderTreeAsExternalRepresentation(BOOL forPrinting, BSTR *result);
161
162    virtual HRESULT STDMETHODCALLTYPE counterValueForElementById(
163        /* [in] */ BSTR id,
164        /* [retval][out] */ BSTR *result);
165
166    virtual HRESULT STDMETHODCALLTYPE pageNumberForElementById(
167        /* [in] */ BSTR id,
168        /* [in] */ float pageWidthInPixels,
169        /* [in] */ float pageHeightInPixels,
170        /* [retval][out] */ int* result);
171
172    virtual HRESULT STDMETHODCALLTYPE numberOfPages(
173        /* [in] */ float pageWidthInPixels,
174        /* [in] */ float pageHeightInPixels,
175        /* [retval][out] */ int* result);
176
177    virtual HRESULT STDMETHODCALLTYPE scrollOffset(
178        /* [retval][out] */ SIZE* offset);
179
180    virtual HRESULT STDMETHODCALLTYPE layout();
181
182    virtual HRESULT STDMETHODCALLTYPE firstLayoutDone(
183        /* [retval][out] */ BOOL* result);
184
185    virtual HRESULT STDMETHODCALLTYPE loadType(
186        /* [retval][out] */ WebFrameLoadType* type);
187
188    virtual HRESULT STDMETHODCALLTYPE pendingFrameUnloadEventCount(
189        /* [retval][out] */ UINT* result);
190
191    virtual HRESULT STDMETHODCALLTYPE unused2();
192
193    virtual HRESULT STDMETHODCALLTYPE setInPrintingMode(
194        /* [in] */ BOOL value,
195        /* [in] */ HDC printDC);
196
197    virtual HRESULT STDMETHODCALLTYPE getPrintedPageCount(
198        /* [in] */ HDC printDC,
199        /* [retval][out] */ UINT *pageCount);
200
201    virtual HRESULT STDMETHODCALLTYPE spoolPages(
202        /* [in] */ HDC printDC,
203        /* [in] */ UINT startPage,
204        /* [in] */ UINT endPage,
205        /* [retval][out] */ void* ctx);
206
207    virtual HRESULT STDMETHODCALLTYPE isFrameSet(
208        /* [retval][out] */ BOOL* result);
209
210    virtual HRESULT STDMETHODCALLTYPE string(
211        /* [retval][out] */ BSTR* result);
212
213    virtual HRESULT STDMETHODCALLTYPE size(
214        /* [retval][out] */ SIZE *size);
215
216    virtual HRESULT STDMETHODCALLTYPE hasScrollBars(
217        /* [retval][out] */ BOOL *result);
218
219    virtual HRESULT STDMETHODCALLTYPE contentBounds(
220        /* [retval][out] */ RECT *result);
221
222    virtual HRESULT STDMETHODCALLTYPE frameBounds(
223        /* [retval][out] */ RECT *result);
224
225    virtual HRESULT STDMETHODCALLTYPE isDescendantOfFrame(
226        /* [in] */ IWebFrame *ancestor,
227        /* [retval][out] */ BOOL *result);
228
229    virtual HRESULT STDMETHODCALLTYPE setAllowsScrolling(
230        /* [in] */ BOOL flag);
231
232    virtual HRESULT STDMETHODCALLTYPE allowsScrolling(
233        /* [retval][out] */ BOOL *flag);
234
235    virtual HRESULT STDMETHODCALLTYPE setIsDisconnected(
236        /* [in] */ BOOL flag);
237
238    virtual HRESULT STDMETHODCALLTYPE setExcludeFromTextSearch(
239        /* [in] */ BOOL flag);
240
241    virtual HRESULT STDMETHODCALLTYPE reloadFromOrigin();
242
243    virtual HRESULT STDMETHODCALLTYPE paintDocumentRectToContext(
244        /* [in] */ RECT rect,
245        /* [in] */ OLE_HANDLE deviceContext);
246
247    virtual HRESULT STDMETHODCALLTYPE paintScrollViewRectToContextAtPoint(
248        /* [in] */ RECT rect,
249        /* [in] */ POINT pt,
250        /* [in] */ OLE_HANDLE deviceContext);
251
252    virtual HRESULT STDMETHODCALLTYPE elementDoesAutoComplete(
253        /* [in] */ IDOMElement* element,
254        /* [retval][out] */ BOOL* result);
255
256    virtual HRESULT STDMETHODCALLTYPE pauseAnimation(BSTR animationName, IDOMNode*, double secondsFromNow, BOOL* animationWasRunning);
257    virtual HRESULT STDMETHODCALLTYPE pauseTransition(BSTR propertyName, IDOMNode*, double secondsFromNow, BOOL* transitionWasRunning);
258    virtual HRESULT STDMETHODCALLTYPE pauseSVGAnimation(BSTR elementId, IDOMNode*, double secondsFromNow, BOOL* animationWasRunning);
259    virtual HRESULT STDMETHODCALLTYPE numberOfActiveAnimations(UINT*);
260    virtual HRESULT STDMETHODCALLTYPE suspendAnimations();
261    virtual HRESULT STDMETHODCALLTYPE resumeAnimations();
262    virtual HRESULT STDMETHODCALLTYPE loadPlainTextString(BSTR string, BSTR url);
263
264    virtual HRESULT STDMETHODCALLTYPE isDisplayingStandaloneImage(BOOL*);
265
266    virtual HRESULT STDMETHODCALLTYPE allowsFollowingLink(
267        /* [in] */ BSTR url,
268        /* [retval][out] */ BOOL* result);
269
270    virtual HRESULT STDMETHODCALLTYPE stringByEvaluatingJavaScriptInScriptWorld(IWebScriptWorld*, JSObjectRef globalObjectRef, BSTR script, BSTR* evaluationResult);
271    virtual JSGlobalContextRef STDMETHODCALLTYPE globalContextForScriptWorld(IWebScriptWorld*);
272
273    virtual HRESULT STDMETHODCALLTYPE visibleContentRect(RECT*);
274
275    virtual HRESULT STDMETHODCALLTYPE layerTreeAsText(BSTR*);
276
277    virtual HRESULT STDMETHODCALLTYPE hasSpellingMarker(
278        /* [in] */ UINT from,
279        /* [in] */ UINT length,
280        /* [retval][out] */ BOOL *result);
281
282    virtual HRESULT STDMETHODCALLTYPE clearOpener();
283
284    // IWebDocumentText
285    virtual HRESULT STDMETHODCALLTYPE supportsTextEncoding(
286        /* [retval][out] */ BOOL* result);
287
288    virtual HRESULT STDMETHODCALLTYPE selectedString(
289        /* [retval][out] */ BSTR* result);
290
291    virtual HRESULT STDMETHODCALLTYPE selectAll();
292
293    virtual HRESULT STDMETHODCALLTYPE deselectAll();
294
295    // FrameLoaderClient
296    virtual void frameLoaderDestroyed();
297    virtual void makeRepresentation(WebCore::DocumentLoader*);
298    virtual void forceLayoutForNonHTML();
299    virtual void setCopiesOnScroll();
300    virtual void detachedFromParent2();
301    virtual void detachedFromParent3();
302    virtual void cancelPolicyCheck();
303    virtual void dispatchWillSendSubmitEvent(WebCore::HTMLFormElement*) { }
304    virtual void dispatchWillSubmitForm(WebCore::FramePolicyFunction, PassRefPtr<WebCore::FormState>);
305    virtual void revertToProvisionalState(WebCore::DocumentLoader*);
306    virtual void setMainFrameDocumentReady(bool);
307    virtual void willChangeTitle(WebCore::DocumentLoader*);
308    virtual void didChangeTitle(WebCore::DocumentLoader*);
309    virtual void didChangeIcons(WebCore::DocumentLoader*);
310    virtual bool canHandleRequest(const WebCore::ResourceRequest&) const;
311    virtual bool canShowMIMEType(const WTF::String& MIMEType) const;
312    virtual bool canShowMIMETypeAsHTML(const WTF::String& MIMEType) const;
313    virtual bool representationExistsForURLScheme(const WTF::String& URLScheme) const;
314    virtual WTF::String generatedMIMETypeForURLScheme(const WTF::String& URLScheme) const;
315    virtual void frameLoadCompleted();
316    virtual void restoreViewState();
317    virtual void provisionalLoadStarted();
318    virtual bool shouldTreatURLAsSameAsCurrent(const WebCore::KURL&) const;
319    virtual void addHistoryItemForFragmentScroll();
320    virtual void didFinishLoad();
321    virtual void prepareForDataSourceReplacement();
322    virtual WTF::String userAgent(const WebCore::KURL&);
323    virtual void saveViewStateToItem(WebCore::HistoryItem *);
324    virtual WebCore::ResourceError cancelledError(const WebCore::ResourceRequest&);
325    virtual WebCore::ResourceError blockedError(const WebCore::ResourceRequest&);
326    virtual WebCore::ResourceError cannotShowURLError(const WebCore::ResourceRequest&);
327    virtual WebCore::ResourceError interruptForPolicyChangeError(const WebCore::ResourceRequest&);
328    virtual WebCore::ResourceError cannotShowMIMETypeError(const WebCore::ResourceResponse&);
329    virtual WebCore::ResourceError fileDoesNotExistError(const WebCore::ResourceResponse&);
330    virtual WebCore::ResourceError pluginWillHandleLoadError(const WebCore::ResourceResponse&);
331    virtual bool shouldFallBack(const WebCore::ResourceError&);
332    virtual void dispatchDecidePolicyForResponse(WebCore::FramePolicyFunction, const WebCore::ResourceResponse&, const WebCore::ResourceRequest&);
333    virtual void dispatchDecidePolicyForNewWindowAction(WebCore::FramePolicyFunction, const WebCore::NavigationAction&, const WebCore::ResourceRequest&, PassRefPtr<WebCore::FormState>, const WTF::String& frameName);
334    virtual void dispatchDecidePolicyForNavigationAction(WebCore::FramePolicyFunction, const WebCore::NavigationAction&, const WebCore::ResourceRequest&, PassRefPtr<WebCore::FormState>);
335    virtual void dispatchUnableToImplementPolicy(const WebCore::ResourceError&);
336    virtual void download(WebCore::ResourceHandle*, const WebCore::ResourceRequest&, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
337
338    virtual bool dispatchDidLoadResourceFromMemoryCache(WebCore::DocumentLoader*, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, int length);
339    virtual void dispatchDidFailProvisionalLoad(const WebCore::ResourceError&);
340    virtual void dispatchDidFailLoad(const WebCore::ResourceError&);
341    virtual void startDownload(const WebCore::ResourceRequest&);
342
343    virtual PassRefPtr<WebCore::Widget> createJavaAppletWidget(const WebCore::IntSize&, WebCore::HTMLAppletElement*, const WebCore::KURL& baseURL, const Vector<WTF::String>& paramNames, const Vector<WTF::String>& paramValues);
344
345    virtual WebCore::ObjectContentType objectContentType(const WebCore::KURL&, const WTF::String& mimeType, bool shouldPreferPlugInsForImages);
346    virtual WTF::String overrideMediaType() const;
347
348    virtual void dispatchDidClearWindowObjectInWorld(WebCore::DOMWrapperWorld*);
349    virtual void documentElementAvailable();
350    virtual void didPerformFirstNavigation() const;
351
352    virtual void registerForIconNotification(bool listen);
353
354    virtual PassRefPtr<WebCore::FrameNetworkingContext> createNetworkingContext();
355
356    // WebFrame
357    PassRefPtr<WebCore::Frame> init(IWebView*, WebCore::Page*, WebCore::HTMLFrameOwnerElement*);
358    WebCore::Frame* impl();
359    void invalidate();
360    void unmarkAllMisspellings();
361    void unmarkAllBadGrammar();
362
363    void updateBackground();
364
365    // WebFrame (matching WebCoreFrameBridge)
366    HRESULT inViewSourceMode(BOOL *flag);
367    HRESULT setInViewSourceMode(BOOL flag);
368    HRESULT elementWithName(BSTR name, IDOMElement* form, IDOMElement** element);
369    HRESULT formForElement(IDOMElement* element, IDOMElement** form);
370    HRESULT controlsInForm(IDOMElement* form, IDOMElement** controls, int* cControls);
371    HRESULT elementIsPassword(IDOMElement* element, bool* result);
372    HRESULT searchForLabelsBeforeElement(const BSTR* labels, unsigned cLabels, IDOMElement* beforeElement, unsigned* resultDistance, BOOL* resultIsInCellAbove, BSTR* result);
373    HRESULT matchLabelsAgainstElement(const BSTR* labels, int cLabels, IDOMElement* againstElement, BSTR* result);
374    HRESULT canProvideDocumentSource(bool* result);
375
376    COMPtr<WebFramePolicyListener> setUpPolicyListener(WebCore::FramePolicyFunction function);
377    void receivedPolicyDecision(WebCore::PolicyAction);
378
379    WebCore::KURL url() const;
380
381    WebView* webView() const;
382    void setWebView(WebView*);
383
384    COMPtr<IAccessible> accessible() const;
385
386protected:
387    void loadHTMLString(BSTR string, BSTR baseURL, BSTR unreachableURL);
388    void loadData(PassRefPtr<WebCore::SharedBuffer>, BSTR mimeType, BSTR textEncodingName, BSTR baseURL, BSTR failingURL);
389    const Vector<WebCore::IntRect>& computePageRects(HDC printDC);
390    void setPrinting(bool printing, float minPageWidth, float maxPageWidth, float minPageHeight, bool adjustViewSize);
391    void headerAndFooterHeights(float*, float*);
392    WebCore::IntRect printerMarginRect(HDC);
393    void spoolPage (PlatformGraphicsContext* pctx, WebCore::GraphicsContext* spoolCtx, HDC printDC, IWebUIDelegate*, float headerHeight, float footerHeight, UINT page, UINT pageCount);
394    void drawHeader(PlatformGraphicsContext* pctx, IWebUIDelegate*, const WebCore::IntRect& pageRect, float headerHeight);
395    void drawFooter(PlatformGraphicsContext* pctx, IWebUIDelegate*, const WebCore::IntRect& pageRect, UINT page, UINT pageCount, float headerHeight, float footerHeight);
396
397protected:
398    ULONG               m_refCount;
399    class WebFramePrivate;
400    WebFramePrivate*    d;
401    bool                m_quickRedirectComing;
402    WebCore::KURL       m_originalRequestURL;
403    bool                m_inPrintingMode;
404    Vector<WebCore::IntRect> m_pageRects;
405    int m_pageHeight;   // height of the page adjusted by margins
406    mutable COMPtr<AccessibleDocument> m_accessible;
407};
408
409#endif
410