WebKit2: Need API to stop loading a WKFrame
[WebKit.git] / Source / WebKit2 / WebProcess / WebPage / WebPage.h
1 /*
2  * Copyright (C) 2010, 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 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 WebPage_h
27 #define WebPage_h
28
29 #include "APIObject.h"
30 #include "DrawingArea.h"
31 #include "FindController.h"
32 #include "GeolocationPermissionRequestManager.h"
33 #include "ImageOptions.h"
34 #include "InjectedBundlePageContextMenuClient.h"
35 #include "InjectedBundlePageEditorClient.h"
36 #include "InjectedBundlePageFormClient.h"
37 #include "InjectedBundlePageLoaderClient.h"
38 #include "InjectedBundlePageUIClient.h"
39 #include "MessageSender.h"
40 #include "Plugin.h"
41 #include "SandboxExtension.h"
42 #include "WebEditCommand.h"
43 #include <WebCore/Editor.h>
44 #include <WebCore/FrameLoaderTypes.h>
45 #include <WebCore/IntRect.h>
46 #include <wtf/HashMap.h>
47 #include <wtf/OwnPtr.h>
48 #include <wtf/PassRefPtr.h>
49 #include <wtf/RefPtr.h>
50 #include <wtf/text/WTFString.h>
51
52 #if ENABLE(TOUCH_EVENTS)
53 #include <WebCore/PlatformTouchEvent.h>
54 #endif
55
56 #if PLATFORM(MAC)
57 #include <wtf/RetainPtr.h>
58 #ifdef __OBJC__
59 @class AccessibilityWebPageObject;
60 #else
61 class AccessibilityWebPageObject;
62 #endif
63 #endif
64
65 namespace CoreIPC {
66     class ArgumentDecoder;
67     class Connection;
68     class MessageID;
69 }
70
71 namespace WebCore {
72     class GraphicsContext;
73     class KeyboardEvent;
74     class Page;
75     class PrintContext;
76     class ResourceRequest;
77     class SharedBuffer;
78 }
79
80 namespace WebKit {
81
82 class DrawingArea;
83 class InjectedBundleBackForwardList;
84 class PageOverlay;
85 class PluginView;
86 class SessionState;
87 class WebContextMenu;
88 class WebContextMenuItemData;
89 class WebEvent;
90 class WebFrame;
91 class WebImage;
92 class WebInspector;
93 class WebKeyboardEvent;
94 class WebMouseEvent;
95 class WebOpenPanelResultListener;
96 class WebPageGroupProxy;
97 class WebPopupMenu;
98 class WebWheelEvent;
99 struct PrintInfo;
100 struct WebPageCreationParameters;
101 struct WebPreferencesStore;
102
103 #if ENABLE(TOUCH_EVENTS)
104 class WebTouchEvent;
105 #endif
106
107 class WebPage : public APIObject, public CoreIPC::MessageSender<WebPage> {
108 public:
109     static const Type APIType = TypeBundlePage;
110
111     static PassRefPtr<WebPage> create(uint64_t pageID, const WebPageCreationParameters&);
112     virtual ~WebPage();
113
114     // Used by MessageSender.
115     CoreIPC::Connection* connection() const;
116     uint64_t destinationID() const { return pageID(); }
117
118     void close();
119
120     WebCore::Page* corePage() const { return m_page.get(); }
121     uint64_t pageID() const { return m_pageID; }
122
123     void setSize(const WebCore::IntSize&);
124     const WebCore::IntSize& size() const { return m_viewSize; }
125
126     InjectedBundleBackForwardList* backForwardList();
127     DrawingArea* drawingArea() const { return m_drawingArea.get(); }
128
129     WebPageGroupProxy* pageGroup() const { return m_pageGroup.get(); }
130
131 #if ENABLE(INSPECTOR)
132     WebInspector* inspector();
133 #endif
134
135     // -- Called by the DrawingArea.
136     // FIXME: We could genericize these into a DrawingArea client interface. Would that be beneficial?
137     void drawRect(WebCore::GraphicsContext&, const WebCore::IntRect&);
138     void layoutIfNeeded();
139
140     // -- Called from WebCore clients.
141 #if !PLATFORM(MAC)
142     bool handleEditingKeyboardEvent(WebCore::KeyboardEvent*);
143 #endif
144     void show();
145     String userAgent() const { return m_userAgent; }
146     WebCore::IntRect windowResizerRect() const;
147     bool tabsToLinks() const { return m_tabToLinks; }
148
149     WebEditCommand* webEditCommand(uint64_t);
150     void addWebEditCommand(uint64_t, WebEditCommand*);
151     void removeWebEditCommand(uint64_t);
152     bool isInRedo() const { return m_isInRedo; }
153
154     void setActivePopupMenu(WebPopupMenu*);
155     
156     WebOpenPanelResultListener* activeOpenPanelResultListener() const { return m_activeOpenPanelResultListener.get(); }
157     void setActiveOpenPanelResultListener(PassRefPtr<WebOpenPanelResultListener>);
158
159     // -- Called from WebProcess.
160     void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
161     CoreIPC::SyncReplyMode didReceiveSyncMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
162
163     // -- InjectedBundle methods
164     void initializeInjectedBundleContextMenuClient(WKBundlePageContextMenuClient*);
165     void initializeInjectedBundleEditorClient(WKBundlePageEditorClient*);
166     void initializeInjectedBundleFormClient(WKBundlePageFormClient*);
167     void initializeInjectedBundleLoaderClient(WKBundlePageLoaderClient*);
168     void initializeInjectedBundleUIClient(WKBundlePageUIClient*);
169
170     InjectedBundlePageContextMenuClient& injectedBundleContextMenuClient() { return m_contextMenuClient; }
171     InjectedBundlePageEditorClient& injectedBundleEditorClient() { return m_editorClient; }
172     InjectedBundlePageFormClient& injectedBundleFormClient() { return m_formClient; }
173     InjectedBundlePageLoaderClient& injectedBundleLoaderClient() { return m_loaderClient; }
174     InjectedBundlePageUIClient& injectedBundleUIClient() { return m_uiClient; }
175
176     bool findStringFromInjectedBundle(const String&, FindOptions);
177
178     WebFrame* mainFrame() const { return m_mainFrame.get(); }
179     PassRefPtr<Plugin> createPlugin(const Plugin::Parameters&);
180
181     String renderTreeExternalRepresentation() const;
182     void executeEditingCommand(const String& commandName, const String& argument);
183     bool isEditingCommandEnabled(const String& commandName);
184     void clearMainFrameName();
185     void sendClose();
186
187     double textZoomFactor() const;
188     void setTextZoomFactor(double);
189     double pageZoomFactor() const;
190     void setPageZoomFactor(double);
191     void setPageAndTextZoomFactors(double pageZoomFactor, double textZoomFactor);
192
193     void scaleWebView(double scale, const WebCore::IntPoint& origin);
194     double viewScaleFactor() const;
195
196     void setUseFixedLayout(bool);
197     void setFixedLayoutSize(const WebCore::IntSize&);
198
199     bool drawsBackground() const { return m_drawsBackground; }
200     bool drawsTransparentBackground() const { return m_drawsTransparentBackground; }
201
202     void stopLoading();
203     void stopLoadingFrame(uint64_t frameID);
204     void setDefersLoading(bool deferLoading);
205
206 #if USE(ACCELERATED_COMPOSITING)
207     void changeAcceleratedCompositingMode(WebCore::GraphicsLayer*);
208     void enterAcceleratedCompositingMode(WebCore::GraphicsLayer*);
209     void exitAcceleratedCompositingMode();
210 #endif
211
212 #if PLATFORM(MAC)
213     void addPluginView(PluginView*);
214     void removePluginView(PluginView*);
215
216     bool windowIsVisible() const { return m_windowIsVisible; }
217     const WebCore::IntRect& windowFrameInScreenCoordinates() const { return m_windowFrameInScreenCoordinates; }
218     const WebCore::IntRect& viewFrameInWindowCoordinates() const { return m_viewFrameInWindowCoordinates; }
219     bool windowIsFocused() const;
220     bool interceptEditingKeyboardEvent(WebCore::KeyboardEvent*, bool);
221 #elif PLATFORM(WIN)
222     HWND nativeWindow() const { return m_nativeWindow; }
223 #endif
224
225     void installPageOverlay(PassRefPtr<PageOverlay>);
226     void uninstallPageOverlay(PageOverlay*);
227
228     PassRefPtr<WebImage> snapshotInViewCoordinates(const WebCore::IntRect&, ImageOptions);
229     PassRefPtr<WebImage> snapshotInDocumentCoordinates(const WebCore::IntRect&, ImageOptions);
230
231     static const WebEvent* currentEvent();
232
233     FindController& findController() { return m_findController; }
234     GeolocationPermissionRequestManager& geolocationPermissionRequestManager() { return m_geolocationPermissionRequestManager; }
235
236     void pageDidScroll();
237 #if ENABLE(TILED_BACKING_STORE)
238     void pageDidRequestScroll(const WebCore::IntSize& delta);
239     void setActualVisibleContentRect(const WebCore::IntRect&);
240
241     bool resizesToContentsEnabled() const { return !m_resizesToContentsLayoutSize.isEmpty(); }
242     WebCore::IntSize resizesToContentsLayoutSize() const { return m_resizesToContentsLayoutSize; }
243     void setResizesToContentsUsingLayoutSize(const WebCore::IntSize& targetLayoutSize);
244     void resizeToContentsIfNeeded();
245 #endif
246
247     WebContextMenu* contextMenu();
248     
249     bool hasLocalDataForURL(const WebCore::KURL&);
250     
251     static bool canHandleRequest(const WebCore::ResourceRequest&);
252
253     class SandboxExtensionTracker {
254     public:
255         ~SandboxExtensionTracker();
256
257         void invalidate();
258
259         void beginLoad(WebFrame*, const SandboxExtension::Handle& handle);
260         void didStartProvisionalLoad(WebFrame*);
261         void didCommitProvisionalLoad(WebFrame*);
262         void didFailProvisionalLoad(WebFrame*);
263     private:
264         RefPtr<SandboxExtension> m_pendingProvisionalSandboxExtension;
265         RefPtr<SandboxExtension> m_provisionalSandboxExtension;
266         RefPtr<SandboxExtension> m_committedSandboxExtension;
267     };
268
269     SandboxExtensionTracker& sandboxExtensionTracker() { return m_sandboxExtensionTracker; }
270
271     static void getLocationAndLengthFromRange(WebCore::Range*, uint64_t& location, uint64_t& length);
272
273 #if PLATFORM(MAC)
274     void sendAccessibilityPresenterToken(const CoreIPC::DataReference&);
275     AccessibilityWebPageObject* accessibilityRemoteObject();
276     WebCore::IntPoint accessibilityPosition() const { return m_accessibilityPosition; }    
277     
278     void sendComplexTextInputToPlugin(uint64_t pluginComplexTextInputIdentifier, const String& textInput);
279
280     void getMarkedRange(uint64_t& location, uint64_t& length);
281     void characterIndexForPoint(const WebCore::IntPoint point, uint64_t& result);
282     void firstRectForCharacterRange(uint64_t location, uint64_t length, WebCore::IntRect& resultRect);
283 #elif PLATFORM(WIN)
284     void confirmComposition(const String& compositionString);
285     void setComposition(const WTF::String& compositionString, const WTF::Vector<WebCore::CompositionUnderline>& underlines, uint64_t cursorPosition);
286     void firstRectForCharacterInSelectedRange(const uint64_t characterPosition, WebCore::IntRect& resultRect);
287     void getSelectedText(WTF::String&);
288 #endif
289
290     // FIXME: This a dummy message, to avoid breaking the build for platforms that don't require
291     // any synchronous messages, and should be removed when <rdar://problem/8775115> is fixed.
292     void dummy(bool&);
293
294 #if PLATFORM(MAC)
295     bool isSpeaking();
296     void speak(const String&);
297     void stopSpeaking();
298
299     bool isSmartInsertDeleteEnabled() const { return m_isSmartInsertDeleteEnabled; }
300 #endif
301
302     void replaceSelectionWithText(WebCore::Frame*, const String&);
303     void performDragControllerAction(uint64_t action, WebCore::IntPoint clientPosition, WebCore::IntPoint globalPosition, uint64_t draggingSourceOperationMask, const WTF::String& dragStorageName, uint32_t flags);
304     void dragEnded(WebCore::IntPoint clientPosition, WebCore::IntPoint globalPosition, uint64_t operation);
305
306     void beginPrinting(uint64_t frameID, const PrintInfo&);
307     void endPrinting();
308     void computePagesForPrinting(uint64_t frameID, const PrintInfo&, Vector<WebCore::IntRect>& resultPageRects, double& resultTotalScaleFactorForPrinting);
309 #if PLATFORM(MAC)
310     void drawRectToPDF(uint64_t frameID, const WebCore::IntRect&, Vector<uint8_t>& pdfData);
311 #endif
312
313     bool mainFrameHasCustomRepresentation() const;
314
315     bool canRunModal() const { return m_canRunModal; }
316     void runModal();
317
318 private:
319     WebPage(uint64_t pageID, const WebPageCreationParameters&);
320
321     virtual Type type() const { return APIType; }
322
323     void platformInitialize();
324
325     void didReceiveWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
326     CoreIPC::SyncReplyMode didReceiveSyncWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
327
328     static const char* interpretKeyEvent(const WebCore::KeyboardEvent*);
329     bool performDefaultBehaviorForKeyEvent(const WebKeyboardEvent&);
330
331     String sourceForFrame(WebFrame*);
332
333     void loadData(PassRefPtr<WebCore::SharedBuffer>, const String& MIMEType, const String& encodingName, const WebCore::KURL& baseURL, const WebCore::KURL& failingURL);
334
335     bool platformHasLocalDataForURL(const WebCore::KURL&);
336
337     // Actions
338     void tryClose();
339     void loadURL(const String&, const SandboxExtension::Handle& sandboxExtensionHandle);
340     void loadURLRequest(const WebCore::ResourceRequest&, const SandboxExtension::Handle& sandboxExtensionHandle);
341     void loadHTMLString(const String& htmlString, const String& baseURL);
342     void loadAlternateHTMLString(const String& htmlString, const String& baseURL, const String& unreachableURL);
343     void loadPlainTextString(const String&);
344     void reload(bool reloadFromOrigin);
345     void goForward(uint64_t);
346     void goBack(uint64_t);
347     void goToBackForwardItem(uint64_t);
348     void setActive(bool);
349     void setFocused(bool);
350     void setInitialFocus(bool);
351     void setWindowResizerSize(const WebCore::IntSize&);
352     void setIsInWindow(bool);
353     void mouseEvent(const WebMouseEvent&);
354     void wheelEvent(const WebWheelEvent&);
355     void keyEvent(const WebKeyboardEvent&);
356     void validateMenuItem(const String&);
357     void executeEditCommand(const String&);
358 #if ENABLE(TOUCH_EVENTS)
359     void touchEvent(const WebTouchEvent&);
360 #endif
361
362     uint64_t restoreSession(const SessionState&);
363     void restoreSessionAndNavigateToCurrentItem(const SessionState&);
364
365     void didRemoveBackForwardItem(uint64_t);
366
367     void setDrawsBackground(bool);
368     void setDrawsTransparentBackground(bool);
369
370     void getContentsAsString(uint64_t callbackID);
371     void getMainResourceDataOfFrame(uint64_t frameID, uint64_t callbackID);
372     void getResourceDataFromFrame(uint64_t frameID, const String& resourceURL, uint64_t callbackID);
373     void getRenderTreeExternalRepresentation(uint64_t callbackID);
374     void getSelectionOrContentsAsString(uint64_t callbackID);
375     void getSourceForFrame(uint64_t frameID, uint64_t callbackID);
376     void getWebArchiveOfFrame(uint64_t frameID, uint64_t callbackID);
377     void runJavaScriptInMainFrame(const String&, uint64_t callbackID);
378
379     void preferencesDidChange(const WebPreferencesStore&);
380     void platformPreferencesDidChange(const WebPreferencesStore&);
381     void updatePreferences(const WebPreferencesStore&);
382
383     void didReceivePolicyDecision(uint64_t frameID, uint64_t listenerID, uint32_t policyAction, uint64_t downloadID);
384     void setUserAgent(const String&);
385     void setCustomTextEncodingName(const String&);
386
387 #if PLATFORM(MAC)
388     void setWindowIsVisible(bool windowIsVisible);
389     void windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates, const WebCore::IntPoint& accessibilityViewCoordinates);
390 #endif
391
392     void unapplyEditCommand(uint64_t commandID);
393     void reapplyEditCommand(uint64_t commandID);
394     void didRemoveEditCommand(uint64_t commandID);
395
396     void findString(const String&, uint32_t findOptions, uint32_t maxMatchCount);
397     void hideFindUI();
398     void countStringMatches(const String&, uint32_t findOptions, uint32_t maxMatchCount);
399
400 #if PLATFORM(QT)
401     void findZoomableAreaForPoint(const WebCore::IntPoint&);
402 #endif
403
404     void didChangeSelectedIndexForActivePopupMenu(int32_t newIndex);
405     void setTextForActivePopupMenu(int32_t index);
406
407     void didChooseFilesForOpenPanel(const Vector<String>&);
408     void didCancelForOpenPanel();
409
410     void didReceiveGeolocationPermissionDecision(uint64_t geolocationID, bool allowed);
411
412     void advanceToNextMisspelling(bool startBeforeSelection);
413     void changeSpellingToWord(const String& word);
414     void unmarkAllMisspellings();
415     void unmarkAllBadGrammar();
416 #if PLATFORM(MAC)
417     void uppercaseWord();
418     void lowercaseWord();
419     void capitalizeWord();
420
421     void setSmartInsertDeleteEnabled(bool isSmartInsertDeleteEnabled) { m_isSmartInsertDeleteEnabled = isSmartInsertDeleteEnabled; }
422 #endif
423
424 #if ENABLE(CONTEXT_MENUS)
425     void didSelectItemFromActiveContextMenu(const WebContextMenuItemData&);
426 #endif
427
428     OwnPtr<WebCore::Page> m_page;
429     RefPtr<WebFrame> m_mainFrame;
430     RefPtr<InjectedBundleBackForwardList> m_backForwardList;
431
432     RefPtr<WebPageGroupProxy> m_pageGroup;
433
434     String m_userAgent;
435
436     WebCore::IntSize m_viewSize;
437     RefPtr<DrawingArea> m_drawingArea;
438
439     bool m_drawsBackground;
440     bool m_drawsTransparentBackground;
441
442     bool m_isInRedo;
443     bool m_isClosed;
444
445     bool m_tabToLinks;
446
447 #if PLATFORM(MAC)
448     // Whether the containing window is visible or not.
449     bool m_windowIsVisible;
450
451     // Whether smart insert/delete is enabled or not.
452     bool m_isSmartInsertDeleteEnabled;
453
454     // The frame of the containing window in screen coordinates.
455     WebCore::IntRect m_windowFrameInScreenCoordinates;
456
457     // The frame of the view in window coordinates.
458     WebCore::IntRect m_viewFrameInWindowCoordinates;
459
460     // The accessibility position of the view.
461     WebCore::IntPoint m_accessibilityPosition;
462     
463     // All plug-in views on this web page.
464     HashSet<PluginView*> m_pluginViews;
465     
466     RetainPtr<AccessibilityWebPageObject> m_mockAccessibilityElement;
467 #elif PLATFORM(WIN)
468     // Our view's window (in the UI process).
469     HWND m_nativeWindow;
470 #endif
471     
472     HashMap<uint64_t, RefPtr<WebEditCommand> > m_editCommandMap;
473
474     WebCore::IntSize m_windowResizerSize;
475
476     InjectedBundlePageContextMenuClient m_contextMenuClient;
477     InjectedBundlePageEditorClient m_editorClient;
478     InjectedBundlePageFormClient m_formClient;
479     InjectedBundlePageLoaderClient m_loaderClient;
480     InjectedBundlePageUIClient m_uiClient;
481
482 #if ENABLE(TILED_BACKING_STORE)
483     WebCore::IntSize m_resizesToContentsLayoutSize;
484 #endif
485
486     FindController m_findController;
487     RefPtr<PageOverlay> m_pageOverlay;
488
489 #if ENABLE(INSPECTOR)
490     OwnPtr<WebInspector> m_inspector;
491 #endif
492     RefPtr<WebPopupMenu> m_activePopupMenu;
493     RefPtr<WebContextMenu> m_contextMenu;
494     RefPtr<WebOpenPanelResultListener> m_activeOpenPanelResultListener;
495     GeolocationPermissionRequestManager m_geolocationPermissionRequestManager;
496
497     OwnPtr<WebCore::PrintContext> m_printContext;
498
499     SandboxExtensionTracker m_sandboxExtensionTracker;
500     uint64_t m_pageID;
501
502     bool m_canRunModal;
503     bool m_isRunningModal;
504 };
505
506 } // namespace WebKit
507
508 #endif // WebPage_h