Reviewed by Anders Carlsson and Darin Adler.
[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/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 setDefersLoading(bool deferLoading);
204
205 #if USE(ACCELERATED_COMPOSITING)
206     void changeAcceleratedCompositingMode(WebCore::GraphicsLayer*);
207     void enterAcceleratedCompositingMode(WebCore::GraphicsLayer*);
208     void exitAcceleratedCompositingMode();
209 #endif
210
211 #if PLATFORM(MAC)
212     void addPluginView(PluginView*);
213     void removePluginView(PluginView*);
214
215     bool windowIsVisible() const { return m_windowIsVisible; }
216     const WebCore::IntRect& windowFrameInScreenCoordinates() const { return m_windowFrameInScreenCoordinates; }
217     const WebCore::IntRect& viewFrameInWindowCoordinates() const { return m_viewFrameInWindowCoordinates; }
218     bool windowIsFocused() const;
219     bool interceptEditingKeyboardEvent(WebCore::KeyboardEvent*, bool);
220 #elif PLATFORM(WIN)
221     HWND nativeWindow() const { return m_nativeWindow; }
222 #endif
223
224     void installPageOverlay(PassRefPtr<PageOverlay>);
225     void uninstallPageOverlay(PageOverlay*);
226
227     PassRefPtr<WebImage> snapshotInViewCoordinates(const WebCore::IntRect&, ImageOptions);
228     PassRefPtr<WebImage> snapshotInDocumentCoordinates(const WebCore::IntRect&, ImageOptions);
229
230     static const WebEvent* currentEvent();
231
232     FindController& findController() { return m_findController; }
233     GeolocationPermissionRequestManager& geolocationPermissionRequestManager() { return m_geolocationPermissionRequestManager; }
234
235     void pageDidScroll();
236 #if ENABLE(TILED_BACKING_STORE)
237     void pageDidRequestScroll(const WebCore::IntSize& delta);
238     void setActualVisibleContentRect(const WebCore::IntRect&);
239
240     bool resizesToContentsEnabled() const { return !m_resizesToContentsLayoutSize.isEmpty(); }
241     WebCore::IntSize resizesToContentsLayoutSize() const { return m_resizesToContentsLayoutSize; }
242     void setResizesToContentsUsingLayoutSize(const WebCore::IntSize& targetLayoutSize);
243     void resizeToContentsIfNeeded();
244 #endif
245
246     WebContextMenu* contextMenu();
247     
248     bool hasLocalDataForURL(const WebCore::KURL&);
249     
250     static bool canHandleRequest(const WebCore::ResourceRequest&);
251
252     class SandboxExtensionTracker {
253     public:
254         ~SandboxExtensionTracker();
255
256         void invalidate();
257
258         void beginLoad(WebFrame*, const SandboxExtension::Handle& handle);
259         void didStartProvisionalLoad(WebFrame*);
260         void didCommitProvisionalLoad(WebFrame*);
261         void didFailProvisionalLoad(WebFrame*);
262     private:
263         RefPtr<SandboxExtension> m_pendingProvisionalSandboxExtension;
264         RefPtr<SandboxExtension> m_provisionalSandboxExtension;
265         RefPtr<SandboxExtension> m_committedSandboxExtension;
266     };
267
268     SandboxExtensionTracker& sandboxExtensionTracker() { return m_sandboxExtensionTracker; }
269
270     static void getLocationAndLengthFromRange(WebCore::Range*, uint64_t& location, uint64_t& length);
271
272 #if PLATFORM(MAC)
273     void sendAccessibilityPresenterToken(const CoreIPC::DataReference&);
274     AccessibilityWebPageObject* accessibilityRemoteObject();
275     WebCore::IntPoint accessibilityPosition() const { return m_accessibilityPosition; }    
276     
277     void sendComplexTextInputToPlugin(uint64_t pluginComplexTextInputIdentifier, const String& textInput);
278
279     void getMarkedRange(uint64_t& location, uint64_t& length);
280     void characterIndexForPoint(const WebCore::IntPoint point, uint64_t& result);
281     void firstRectForCharacterRange(uint64_t location, uint64_t length, WebCore::IntRect& resultRect);
282 #elif PLATFORM(WIN)
283     void confirmComposition(const String& compositionString);
284     void setComposition(const WTF::String& compositionString, const WTF::Vector<WebCore::CompositionUnderline>& underlines, uint64_t cursorPosition);
285     void firstRectForCharacterInSelectedRange(const uint64_t characterPosition, WebCore::IntRect& resultRect);
286     void getSelectedText(WTF::String&);
287 #endif
288
289     // FIXME: This a dummy message, to avoid breaking the build for platforms that don't require
290     // any synchronous messages, and should be removed when <rdar://problem/8775115> is fixed.
291     void dummy(bool&);
292
293 #if PLATFORM(MAC)
294     bool isSpeaking();
295     void speak(const String&);
296     void stopSpeaking();
297
298     bool isSmartInsertDeleteEnabled() const { return m_isSmartInsertDeleteEnabled; }
299 #endif
300
301     void replaceSelectionWithText(WebCore::Frame*, const String&);
302     void performDragControllerAction(uint64_t action, WebCore::IntPoint clientPosition, WebCore::IntPoint globalPosition, uint64_t draggingSourceOperationMask, const WTF::String& dragStorageName, uint32_t flags);
303
304     void beginPrinting(uint64_t frameID, const PrintInfo&);
305     void endPrinting();
306     void computePagesForPrinting(uint64_t frameID, const PrintInfo&, Vector<WebCore::IntRect>& resultPageRects, double& resultTotalScaleFactorForPrinting);
307 #if PLATFORM(MAC)
308     void drawRectToPDF(uint64_t frameID, const WebCore::IntRect&, Vector<uint8_t>& pdfData);
309 #endif
310
311     bool mainFrameHasCustomRepresentation() const;
312
313 private:
314     WebPage(uint64_t pageID, const WebPageCreationParameters&);
315
316     virtual Type type() const { return APIType; }
317
318     void platformInitialize();
319
320     void didReceiveWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
321     CoreIPC::SyncReplyMode didReceiveSyncWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
322
323     static const char* interpretKeyEvent(const WebCore::KeyboardEvent*);
324     bool performDefaultBehaviorForKeyEvent(const WebKeyboardEvent&);
325
326     String sourceForFrame(WebFrame*);
327
328     void loadData(PassRefPtr<WebCore::SharedBuffer>, const String& MIMEType, const String& encodingName, const WebCore::KURL& baseURL, const WebCore::KURL& failingURL);
329
330     bool platformHasLocalDataForURL(const WebCore::KURL&);
331
332     // Actions
333     void tryClose();
334     void loadURL(const String&, const SandboxExtension::Handle& sandboxExtensionHandle);
335     void loadURLRequest(const WebCore::ResourceRequest&, const SandboxExtension::Handle& sandboxExtensionHandle);
336     void loadHTMLString(const String& htmlString, const String& baseURL);
337     void loadAlternateHTMLString(const String& htmlString, const String& baseURL, const String& unreachableURL);
338     void loadPlainTextString(const String&);
339     void reload(bool reloadFromOrigin);
340     void goForward(uint64_t);
341     void goBack(uint64_t);
342     void goToBackForwardItem(uint64_t);
343     void setActive(bool);
344     void setFocused(bool);
345     void setInitialFocus(bool);
346     void setWindowResizerSize(const WebCore::IntSize&);
347     void setIsInWindow(bool);
348     void mouseEvent(const WebMouseEvent&);
349     void wheelEvent(const WebWheelEvent&);
350     void keyEvent(const WebKeyboardEvent&);
351     void validateMenuItem(const String&);
352     void executeEditCommand(const String&);
353 #if ENABLE(TOUCH_EVENTS)
354     void touchEvent(const WebTouchEvent&);
355 #endif
356
357     uint64_t restoreSession(const SessionState&);
358     void restoreSessionAndNavigateToCurrentItem(const SessionState&);
359
360     void didRemoveBackForwardItem(uint64_t);
361
362     void setDrawsBackground(bool);
363     void setDrawsTransparentBackground(bool);
364
365     void getContentsAsString(uint64_t callbackID);
366     void getMainResourceDataOfFrame(uint64_t frameID, uint64_t callbackID);
367     void getRenderTreeExternalRepresentation(uint64_t callbackID);
368     void getSelectionOrContentsAsString(uint64_t callbackID);
369     void getSourceForFrame(uint64_t frameID, uint64_t callbackID);
370     void getWebArchiveOfFrame(uint64_t frameID, uint64_t callbackID);
371     void runJavaScriptInMainFrame(const String&, uint64_t callbackID);
372
373     void preferencesDidChange(const WebPreferencesStore&);
374     void platformPreferencesDidChange(const WebPreferencesStore&);
375     void updatePreferences(const WebPreferencesStore&);
376
377     void didReceivePolicyDecision(uint64_t frameID, uint64_t listenerID, uint32_t policyAction, uint64_t downloadID);
378     void setUserAgent(const String&);
379     void setCustomTextEncodingName(const String&);
380
381 #if PLATFORM(MAC)
382     void setWindowIsVisible(bool windowIsVisible);
383     void windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates, const WebCore::IntPoint& accessibilityViewCoordinates);
384 #endif
385
386     void unapplyEditCommand(uint64_t commandID);
387     void reapplyEditCommand(uint64_t commandID);
388     void didRemoveEditCommand(uint64_t commandID);
389
390     void findString(const String&, uint32_t findOptions, uint32_t maxMatchCount);
391     void hideFindUI();
392     void countStringMatches(const String&, uint32_t findOptions, uint32_t maxMatchCount);
393
394 #if PLATFORM(QT)
395     void findZoomableAreaForPoint(const WebCore::IntPoint&);
396 #endif
397
398     void didChangeSelectedIndexForActivePopupMenu(int32_t newIndex);
399     void setTextForActivePopupMenu(int32_t index);
400
401     void didChooseFilesForOpenPanel(const Vector<String>&);
402     void didCancelForOpenPanel();
403
404     void didReceiveGeolocationPermissionDecision(uint64_t geolocationID, bool allowed);
405
406     void advanceToNextMisspelling(bool startBeforeSelection);
407     void changeSpellingToWord(const String& word);
408     void unmarkAllMisspellings();
409     void unmarkAllBadGrammar();
410 #if PLATFORM(MAC)
411     void uppercaseWord();
412     void lowercaseWord();
413     void capitalizeWord();
414
415     void setSmartInsertDeleteEnabled(bool isSmartInsertDeleteEnabled) { m_isSmartInsertDeleteEnabled = isSmartInsertDeleteEnabled; }
416 #endif
417
418 #if ENABLE(CONTEXT_MENUS)
419     void didSelectItemFromActiveContextMenu(const WebContextMenuItemData&);
420 #endif
421
422     OwnPtr<WebCore::Page> m_page;
423     RefPtr<WebFrame> m_mainFrame;
424     RefPtr<InjectedBundleBackForwardList> m_backForwardList;
425
426     RefPtr<WebPageGroupProxy> m_pageGroup;
427
428     String m_userAgent;
429
430     WebCore::IntSize m_viewSize;
431     RefPtr<DrawingArea> m_drawingArea;
432
433     bool m_drawsBackground;
434     bool m_drawsTransparentBackground;
435
436     bool m_isInRedo;
437     bool m_isClosed;
438
439     bool m_tabToLinks;
440
441 #if PLATFORM(MAC)
442     // Whether the containing window is visible or not.
443     bool m_windowIsVisible;
444
445     // Whether smart insert/delete is enabled or not.
446     bool m_isSmartInsertDeleteEnabled;
447
448     // The frame of the containing window in screen coordinates.
449     WebCore::IntRect m_windowFrameInScreenCoordinates;
450
451     // The frame of the view in window coordinates.
452     WebCore::IntRect m_viewFrameInWindowCoordinates;
453
454     // The accessibility position of the view.
455     WebCore::IntPoint m_accessibilityPosition;
456     
457     // All plug-in views on this web page.
458     HashSet<PluginView*> m_pluginViews;
459     
460     RetainPtr<AccessibilityWebPageObject> m_mockAccessibilityElement;
461 #elif PLATFORM(WIN)
462     // Our view's window (in the UI process).
463     HWND m_nativeWindow;
464 #endif
465     
466     HashMap<uint64_t, RefPtr<WebEditCommand> > m_editCommandMap;
467
468     WebCore::IntSize m_windowResizerSize;
469
470     InjectedBundlePageContextMenuClient m_contextMenuClient;
471     InjectedBundlePageEditorClient m_editorClient;
472     InjectedBundlePageFormClient m_formClient;
473     InjectedBundlePageLoaderClient m_loaderClient;
474     InjectedBundlePageUIClient m_uiClient;
475
476 #if ENABLE(TILED_BACKING_STORE)
477     WebCore::IntSize m_resizesToContentsLayoutSize;
478 #endif
479
480     FindController m_findController;
481     RefPtr<PageOverlay> m_pageOverlay;
482
483 #if ENABLE(INSPECTOR)
484     OwnPtr<WebInspector> m_inspector;
485 #endif
486     RefPtr<WebPopupMenu> m_activePopupMenu;
487     RefPtr<WebContextMenu> m_contextMenu;
488     RefPtr<WebOpenPanelResultListener> m_activeOpenPanelResultListener;
489     GeolocationPermissionRequestManager m_geolocationPermissionRequestManager;
490
491     OwnPtr<WebCore::PrintContext> m_printContext;
492
493     SandboxExtensionTracker m_sandboxExtensionTracker;
494     uint64_t m_pageID;
495 };
496
497 } // namespace WebKit
498
499 #endif // WebPage_h