d2d8754137aae6fb509541aa7dc1a6858b537cdf
[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 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     void dragEnded(WebCore::IntPoint clientPosition, WebCore::IntPoint globalPosition, uint64_t operation);
304
305     void beginPrinting(uint64_t frameID, const PrintInfo&);
306     void endPrinting();
307     void computePagesForPrinting(uint64_t frameID, const PrintInfo&, Vector<WebCore::IntRect>& resultPageRects, double& resultTotalScaleFactorForPrinting);
308 #if PLATFORM(MAC)
309     void drawRectToPDF(uint64_t frameID, const WebCore::IntRect&, Vector<uint8_t>& pdfData);
310 #endif
311
312     bool mainFrameHasCustomRepresentation() const;
313
314     bool canRunModal() const { return m_canRunModal; }
315     void runModal();
316
317 private:
318     WebPage(uint64_t pageID, const WebPageCreationParameters&);
319
320     virtual Type type() const { return APIType; }
321
322     void platformInitialize();
323
324     void didReceiveWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
325     CoreIPC::SyncReplyMode didReceiveSyncWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
326
327     static const char* interpretKeyEvent(const WebCore::KeyboardEvent*);
328     bool performDefaultBehaviorForKeyEvent(const WebKeyboardEvent&);
329
330     String sourceForFrame(WebFrame*);
331
332     void loadData(PassRefPtr<WebCore::SharedBuffer>, const String& MIMEType, const String& encodingName, const WebCore::KURL& baseURL, const WebCore::KURL& failingURL);
333
334     bool platformHasLocalDataForURL(const WebCore::KURL&);
335
336     // Actions
337     void tryClose();
338     void loadURL(const String&, const SandboxExtension::Handle& sandboxExtensionHandle);
339     void loadURLRequest(const WebCore::ResourceRequest&, const SandboxExtension::Handle& sandboxExtensionHandle);
340     void loadHTMLString(const String& htmlString, const String& baseURL);
341     void loadAlternateHTMLString(const String& htmlString, const String& baseURL, const String& unreachableURL);
342     void loadPlainTextString(const String&);
343     void reload(bool reloadFromOrigin);
344     void goForward(uint64_t);
345     void goBack(uint64_t);
346     void goToBackForwardItem(uint64_t);
347     void setActive(bool);
348     void setFocused(bool);
349     void setInitialFocus(bool);
350     void setWindowResizerSize(const WebCore::IntSize&);
351     void setIsInWindow(bool);
352     void mouseEvent(const WebMouseEvent&);
353     void wheelEvent(const WebWheelEvent&);
354     void keyEvent(const WebKeyboardEvent&);
355     void validateMenuItem(const String&);
356     void executeEditCommand(const String&);
357 #if ENABLE(TOUCH_EVENTS)
358     void touchEvent(const WebTouchEvent&);
359 #endif
360
361     uint64_t restoreSession(const SessionState&);
362     void restoreSessionAndNavigateToCurrentItem(const SessionState&);
363
364     void didRemoveBackForwardItem(uint64_t);
365
366     void setDrawsBackground(bool);
367     void setDrawsTransparentBackground(bool);
368
369     void getContentsAsString(uint64_t callbackID);
370     void getMainResourceDataOfFrame(uint64_t frameID, uint64_t callbackID);
371     void getResourceDataFromFrame(uint64_t frameID, const String& resourceURL, uint64_t callbackID);
372     void getRenderTreeExternalRepresentation(uint64_t callbackID);
373     void getSelectionOrContentsAsString(uint64_t callbackID);
374     void getSourceForFrame(uint64_t frameID, uint64_t callbackID);
375     void getWebArchiveOfFrame(uint64_t frameID, uint64_t callbackID);
376     void runJavaScriptInMainFrame(const String&, uint64_t callbackID);
377
378     void preferencesDidChange(const WebPreferencesStore&);
379     void platformPreferencesDidChange(const WebPreferencesStore&);
380     void updatePreferences(const WebPreferencesStore&);
381
382     void didReceivePolicyDecision(uint64_t frameID, uint64_t listenerID, uint32_t policyAction, uint64_t downloadID);
383     void setUserAgent(const String&);
384     void setCustomTextEncodingName(const String&);
385
386 #if PLATFORM(MAC)
387     void setWindowIsVisible(bool windowIsVisible);
388     void windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates, const WebCore::IntPoint& accessibilityViewCoordinates);
389 #endif
390
391     void unapplyEditCommand(uint64_t commandID);
392     void reapplyEditCommand(uint64_t commandID);
393     void didRemoveEditCommand(uint64_t commandID);
394
395     void findString(const String&, uint32_t findOptions, uint32_t maxMatchCount);
396     void hideFindUI();
397     void countStringMatches(const String&, uint32_t findOptions, uint32_t maxMatchCount);
398
399 #if PLATFORM(QT)
400     void findZoomableAreaForPoint(const WebCore::IntPoint&);
401 #endif
402
403     void didChangeSelectedIndexForActivePopupMenu(int32_t newIndex);
404     void setTextForActivePopupMenu(int32_t index);
405
406     void didChooseFilesForOpenPanel(const Vector<String>&);
407     void didCancelForOpenPanel();
408
409     void didReceiveGeolocationPermissionDecision(uint64_t geolocationID, bool allowed);
410
411     void advanceToNextMisspelling(bool startBeforeSelection);
412     void changeSpellingToWord(const String& word);
413     void unmarkAllMisspellings();
414     void unmarkAllBadGrammar();
415 #if PLATFORM(MAC)
416     void uppercaseWord();
417     void lowercaseWord();
418     void capitalizeWord();
419
420     void setSmartInsertDeleteEnabled(bool isSmartInsertDeleteEnabled) { m_isSmartInsertDeleteEnabled = isSmartInsertDeleteEnabled; }
421 #endif
422
423 #if ENABLE(CONTEXT_MENUS)
424     void didSelectItemFromActiveContextMenu(const WebContextMenuItemData&);
425 #endif
426
427     OwnPtr<WebCore::Page> m_page;
428     RefPtr<WebFrame> m_mainFrame;
429     RefPtr<InjectedBundleBackForwardList> m_backForwardList;
430
431     RefPtr<WebPageGroupProxy> m_pageGroup;
432
433     String m_userAgent;
434
435     WebCore::IntSize m_viewSize;
436     RefPtr<DrawingArea> m_drawingArea;
437
438     bool m_drawsBackground;
439     bool m_drawsTransparentBackground;
440
441     bool m_isInRedo;
442     bool m_isClosed;
443
444     bool m_tabToLinks;
445
446 #if PLATFORM(MAC)
447     // Whether the containing window is visible or not.
448     bool m_windowIsVisible;
449
450     // Whether smart insert/delete is enabled or not.
451     bool m_isSmartInsertDeleteEnabled;
452
453     // The frame of the containing window in screen coordinates.
454     WebCore::IntRect m_windowFrameInScreenCoordinates;
455
456     // The frame of the view in window coordinates.
457     WebCore::IntRect m_viewFrameInWindowCoordinates;
458
459     // The accessibility position of the view.
460     WebCore::IntPoint m_accessibilityPosition;
461     
462     // All plug-in views on this web page.
463     HashSet<PluginView*> m_pluginViews;
464     
465     RetainPtr<AccessibilityWebPageObject> m_mockAccessibilityElement;
466 #elif PLATFORM(WIN)
467     // Our view's window (in the UI process).
468     HWND m_nativeWindow;
469 #endif
470     
471     HashMap<uint64_t, RefPtr<WebEditCommand> > m_editCommandMap;
472
473     WebCore::IntSize m_windowResizerSize;
474
475     InjectedBundlePageContextMenuClient m_contextMenuClient;
476     InjectedBundlePageEditorClient m_editorClient;
477     InjectedBundlePageFormClient m_formClient;
478     InjectedBundlePageLoaderClient m_loaderClient;
479     InjectedBundlePageUIClient m_uiClient;
480
481 #if ENABLE(TILED_BACKING_STORE)
482     WebCore::IntSize m_resizesToContentsLayoutSize;
483 #endif
484
485     FindController m_findController;
486     RefPtr<PageOverlay> m_pageOverlay;
487
488 #if ENABLE(INSPECTOR)
489     OwnPtr<WebInspector> m_inspector;
490 #endif
491     RefPtr<WebPopupMenu> m_activePopupMenu;
492     RefPtr<WebContextMenu> m_contextMenu;
493     RefPtr<WebOpenPanelResultListener> m_activeOpenPanelResultListener;
494     GeolocationPermissionRequestManager m_geolocationPermissionRequestManager;
495
496     OwnPtr<WebCore::PrintContext> m_printContext;
497
498     SandboxExtensionTracker m_sandboxExtensionTracker;
499     uint64_t m_pageID;
500
501     bool m_canRunModal;
502     bool m_isRunningModal;
503 };
504
505 } // namespace WebKit
506
507 #endif // WebPage_h