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