2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "WebNavigationPolicy.h"
38 #include "WebString.h"
41 #include "ChromeClientImpl.h"
42 #include "ContextMenuClientImpl.h"
43 #include "DragClientImpl.h"
44 #include "EditorClientImpl.h"
45 #include "GraphicsContext3D.h"
46 #include "GraphicsLayer.h"
47 #include "InspectorClientImpl.h"
49 #include "NotificationPresenterImpl.h"
50 #include "PageOverlay.h"
51 #include "cc/CCLayerTreeHost.h"
52 #include <wtf/OwnPtr.h>
53 #include <wtf/RefCounted.h>
56 class ChromiumDataObject;
59 class GraphicsContext3D;
63 class NonCompositedContentHost;
65 class PlatformKeyboardEvent;
67 class PopupMenuClient;
71 #if ENABLE(GESTURE_RECOGNIZER)
72 class PlatformGestureRecognizer;
77 class AutocompletePopupMenuClient;
78 class AutofillPopupMenuClient;
79 class ContextMenuClientImpl;
80 class DeviceOrientationClientProxy;
81 class DragScrollTimer;
82 class GeolocationClientProxy;
83 class SpeechInputClientImpl;
84 class WebAccessibilityObject;
85 class WebDevToolsAgentClient;
86 class WebDevToolsAgentPrivate;
88 class WebGestureEvent;
90 class WebKeyboardEvent;
92 class WebMouseWheelEvent;
93 class WebSettingsImpl;
96 class WebViewImpl : public WebView, public WebCore::CCLayerTreeHostClient, public RefCounted<WebViewImpl> {
100 virtual WebSize size() { return m_size; }
101 virtual void willStartLiveResize();
102 virtual void resize(const WebSize&);
103 virtual void willEndLiveResize();
104 virtual void animate(double frameBeginTime);
105 virtual void layout();
106 virtual void paint(WebCanvas*, const WebRect&);
107 virtual void themeChanged();
108 virtual void composite(bool finish);
109 virtual bool handleInputEvent(const WebInputEvent&);
110 virtual void mouseCaptureLost();
111 virtual void setFocus(bool enable);
112 virtual bool setComposition(
113 const WebString& text,
114 const WebVector<WebCompositionUnderline>& underlines,
117 virtual bool confirmComposition();
118 virtual bool confirmComposition(const WebString& text);
119 virtual bool compositionRange(size_t* location, size_t* length);
120 virtual WebTextInputType textInputType();
121 virtual bool getSelectionOffsetsAndTextInEditableContent(WebString&, size_t& focus, size_t& anchor) const;
122 virtual WebRect caretOrSelectionBounds();
123 virtual bool selectionRange(WebPoint& start, WebPoint& end) const;
124 virtual bool caretOrSelectionRange(size_t* location, size_t* length);
125 virtual void setTextDirection(WebTextDirection direction);
126 virtual bool isAcceleratedCompositingActive() const;
129 virtual void initializeMainFrame(WebFrameClient*);
130 virtual void setAutofillClient(WebAutofillClient*);
131 virtual void setDevToolsAgentClient(WebDevToolsAgentClient*);
132 virtual void setPermissionClient(WebPermissionClient*);
133 virtual void setSpellCheckClient(WebSpellCheckClient*);
134 virtual WebSettings* settings();
135 virtual WebString pageEncoding() const;
136 virtual void setPageEncoding(const WebString& encoding);
137 virtual bool isTransparent() const;
138 virtual void setIsTransparent(bool value);
139 virtual bool tabsToLinks() const;
140 virtual void setTabsToLinks(bool value);
141 virtual bool tabKeyCyclesThroughElements() const;
142 virtual void setTabKeyCyclesThroughElements(bool value);
143 virtual bool isActive() const;
144 virtual void setIsActive(bool value);
145 virtual void setDomainRelaxationForbidden(bool, const WebString& scheme);
146 virtual bool dispatchBeforeUnloadEvent();
147 virtual void dispatchUnloadEvent();
148 virtual WebFrame* mainFrame();
149 virtual WebFrame* findFrameByName(
150 const WebString& name, WebFrame* relativeToFrame);
151 virtual WebFrame* focusedFrame();
152 virtual void setFocusedFrame(WebFrame* frame);
153 virtual void setInitialFocus(bool reverse);
154 virtual void clearFocusedNode();
155 virtual void scrollFocusedNodeIntoView();
156 virtual double zoomLevel();
157 virtual double setZoomLevel(bool textOnly, double zoomLevel);
158 virtual void zoomLimitsChanged(double minimumZoomLevel,
159 double maximumZoomLevel);
160 virtual float pageScaleFactor() const;
161 virtual void scalePage(float scaleFactor, WebPoint origin);
162 virtual bool isFixedLayoutModeEnabled() const;
163 virtual void enableFixedLayoutMode(bool enable);
164 virtual WebSize fixedLayoutSize() const;
165 virtual void setFixedLayoutSize(const WebSize&);
166 virtual void performMediaPlayerAction(
167 const WebMediaPlayerAction& action,
168 const WebPoint& location);
169 virtual void copyImageAt(const WebPoint& point);
170 virtual void dragSourceEndedAt(
171 const WebPoint& clientPoint,
172 const WebPoint& screenPoint,
173 WebDragOperation operation);
174 virtual void dragSourceMovedTo(
175 const WebPoint& clientPoint,
176 const WebPoint& screenPoint,
177 WebDragOperation operation);
178 virtual void dragSourceSystemDragEnded();
179 virtual WebDragOperation dragTargetDragEnter(
181 const WebPoint& clientPoint,
182 const WebPoint& screenPoint,
183 WebDragOperationsMask operationsAllowed);
184 virtual WebDragOperation dragTargetDragOver(
185 const WebPoint& clientPoint,
186 const WebPoint& screenPoint,
187 WebDragOperationsMask operationsAllowed);
188 virtual void dragTargetDragLeave();
189 virtual void dragTargetDrop(
190 const WebPoint& clientPoint,
191 const WebPoint& screenPoint);
192 virtual unsigned long createUniqueIdentifierForRequest();
193 virtual void inspectElementAt(const WebPoint& point);
194 virtual WebString inspectorSettings() const;
195 virtual void setInspectorSettings(const WebString& settings);
196 virtual bool inspectorSetting(const WebString& key, WebString* value) const;
197 virtual void setInspectorSetting(const WebString& key,
198 const WebString& value);
199 virtual WebDevToolsAgent* devToolsAgent();
200 virtual WebAccessibilityObject accessibilityObject();
201 virtual void applyAutofillSuggestions(
203 const WebVector<WebString>& names,
204 const WebVector<WebString>& labels,
205 const WebVector<WebString>& icons,
206 const WebVector<int>& uniqueIDs,
208 virtual void hidePopups();
209 virtual void setScrollbarColors(unsigned inactiveColor,
210 unsigned activeColor,
211 unsigned trackColor);
212 virtual void setSelectionColors(unsigned activeBackgroundColor,
213 unsigned activeForegroundColor,
214 unsigned inactiveBackgroundColor,
215 unsigned inactiveForegroundColor);
216 virtual void performCustomContextMenuAction(unsigned action);
217 virtual void exitFullscreen();
219 // CCLayerTreeHostClient
220 virtual void animateAndLayout(double frameBeginTime);
221 virtual PassOwnPtr<WebCore::CCThread> createCompositorThread();
222 virtual PassRefPtr<WebCore::GraphicsContext3D> createLayerTreeHostContext3D();
223 virtual PassOwnPtr<WebCore::LayerPainterChromium> createRootLayerPainter();
224 virtual void didRecreateGraphicsContext(bool success);
225 #if !USE(THREADED_COMPOSITING)
226 virtual void scheduleComposite();
231 void setIgnoreInputEvents(bool newValue);
232 WebDevToolsAgentPrivate* devToolsAgentPrivate() { return m_devToolsAgent.get(); }
234 PageOverlay* pageOverlay() const { return m_pageOverlay.get(); }
235 void setPageOverlayClient(PageOverlay::PageOverlayClient*);
237 void setOverlayLayer(WebCore::GraphicsLayer*);
239 const WebPoint& lastMouseDownPoint() const
241 return m_lastMouseDownPoint;
244 WebCore::Frame* focusedWebCoreFrame() const;
246 // Returns the currently focused Node or null if no node has focus.
247 WebCore::Node* focusedWebCoreNode();
249 static WebViewImpl* fromPage(WebCore::Page*);
251 WebViewClient* client()
256 WebAutofillClient* autofillClient()
258 return m_autofillClient;
261 WebPermissionClient* permissionClient()
263 return m_permissionClient;
266 WebSpellCheckClient* spellCheckClient()
268 return m_spellCheckClient;
271 // Returns the page object associated with this view. This may be null when
272 // the page is shutting down, but will be valid at all other times.
273 WebCore::Page* page() const
278 WebCore::RenderTheme* theme() const;
280 // Returns the main frame associated with this view. This may be null when
281 // the page is shutting down, but will be valid at all other times.
282 WebFrameImpl* mainFrameImpl();
284 // History related methods:
285 void observeNewNavigation();
287 // Event related methods:
288 void mouseMove(const WebMouseEvent&);
289 void mouseLeave(const WebMouseEvent&);
290 void mouseDown(const WebMouseEvent&);
291 void mouseUp(const WebMouseEvent&);
292 void mouseContextMenu(const WebMouseEvent&);
293 void mouseDoubleClick(const WebMouseEvent&);
294 bool mouseWheel(const WebMouseWheelEvent&);
295 bool gestureEvent(const WebGestureEvent&);
296 bool keyEvent(const WebKeyboardEvent&);
297 bool charEvent(const WebKeyboardEvent&);
298 bool touchEvent(const WebTouchEvent&);
300 // Handles context menu events orignated via the the keyboard. These
301 // include the VK_APPS virtual key and the Shift+F10 combine. Code is
302 // based on the Webkit function bool WebView::handleContextMenuEvent(WPARAM
303 // wParam, LPARAM lParam) in webkit\webkit\win\WebView.cpp. The only
304 // significant change in this function is the code to convert from a
305 // Keyboard event to the Right Mouse button down event.
306 bool sendContextMenuEvent(const WebKeyboardEvent&);
308 // Notifies the WebView that a load has been committed. isNewNavigation
309 // will be true if a new session history item should be created for that
311 void didCommitLoad(bool* isNewNavigation);
313 // Returns true if popup menus should be rendered by the browser, false if
314 // they should be rendered by WebKit (which is the default).
315 static bool useExternalPopupMenus();
317 bool contextMenuAllowed() const
319 return m_contextMenuAllowed;
322 // Set the disposition for how this webview is to be initially shown.
323 void setInitialNavigationPolicy(WebNavigationPolicy policy)
325 m_initialNavigationPolicy = policy;
327 WebNavigationPolicy initialNavigationPolicy() const
329 return m_initialNavigationPolicy;
332 // Determines whether a page should e.g. be opened in a background tab.
333 // Returns false if it has no opinion, in which case it doesn't set *policy.
334 static bool navigationPolicyFromMouseEvent(
335 unsigned short button,
340 WebNavigationPolicy*);
342 // Start a system drag and drop operation.
344 const WebDragData& dragData,
345 WebDragOperationsMask mask,
346 const WebImage& dragImage,
347 const WebPoint& dragImageOffset);
349 void autofillPopupDidHide()
351 m_autofillPopupShowing = false;
354 #if ENABLE(NOTIFICATIONS)
355 // Returns the provider of desktop notifications.
356 NotificationPresenterImpl* notificationPresenterImpl();
359 // Tries to scroll a frame or any parent of a frame. Returns true if the view
361 bool propagateScroll(WebCore::ScrollDirection, WebCore::ScrollGranularity);
363 // Notification that a popup was opened/closed.
364 void popupOpened(WebCore::PopupContainer* popupContainer);
365 void popupClosed(WebCore::PopupContainer* popupContainer);
367 void hideAutofillPopup();
369 // Returns the input event we're currently processing. This is used in some
370 // cases where the WebCore DOM event doesn't have the information we need.
371 static const WebInputEvent* currentInputEvent()
373 return m_currentInputEvent;
376 #if USE(ACCELERATED_COMPOSITING)
377 bool allowsAcceleratedCompositing();
378 bool pageHasRTLStyle() const;
379 void setRootGraphicsLayer(WebCore::GraphicsLayer*);
380 void setRootLayerNeedsDisplay();
381 void scrollRootLayerRect(const WebCore::IntSize& scrollDelta, const WebCore::IntRect& clipRect);
382 void invalidateRootLayerRect(const WebCore::IntRect&);
383 void doUpdateAndComposite();
386 // Returns the onscreen 3D context used by the compositor. This is
387 // used by the renderer's code to set up resource sharing between
388 // the compositor's context and subordinate contexts for APIs like
389 // WebGL. Returns 0 if compositing support is not compiled in.
390 virtual WebGraphicsContext3D* graphicsContext3D();
392 virtual void setVisibilityState(WebPageVisibilityState, bool);
394 WebCore::PopupContainer* selectPopup() const { return m_selectPopup.get(); }
396 // Returns true if the event leads to scrolling.
397 static bool mapKeyCodeForScroll(int keyCode,
398 WebCore::ScrollDirection* scrollDirection,
399 WebCore::ScrollGranularity* scrollGranularity);
401 // Called by a full frame plugin inside this view to inform it that its
402 // zoom level has been updated. The plugin should only call this function
403 // if the zoom change was triggered by the browser, it's only needed in case
404 // a plugin can update its own zoom, say because of its own UI.
405 void fullFramePluginZoomLevelChanged(double zoomLevel);
407 #if ENABLE(GESTURE_RECOGNIZER)
408 void resetGestureRecognizer();
411 void loseCompositorContext(int numTimes);
414 friend class WebView; // So WebView::Create can call our constructor
415 friend class WTF::RefCounted<WebViewImpl>;
422 WebViewImpl(WebViewClient*);
423 virtual ~WebViewImpl();
425 // Returns true if the event was actually processed.
426 bool keyEventDefault(const WebKeyboardEvent&);
428 // Returns true if the autocomple has consumed the event.
429 bool autocompleteHandleKeyEvent(const WebKeyboardEvent&);
431 // Repaints the Autofill popup. Should be called when the suggestions
432 // have changed. Note that this should only be called when the Autofill
434 void refreshAutofillPopup();
436 // Returns true if the view was scrolled.
437 bool scrollViewWithKeyboard(int keyCode, int modifiers);
439 void hideSelectPopup();
441 // Converts |pos| from window coordinates to contents coordinates and gets
442 // the HitTestResult for it.
443 WebCore::HitTestResult hitTestResultForWindowPos(const WebCore::IntPoint&);
445 // Consolidate some common code between starting a drag over a target and
446 // updating a drag over a target. If we're starting a drag, |isEntering|
448 WebDragOperation dragTargetDragEnterOrOver(const WebPoint& clientPoint,
449 const WebPoint& screenPoint,
452 #if USE(ACCELERATED_COMPOSITING)
453 void setIsAcceleratedCompositingActive(bool);
454 #if !USE(THREADED_COMPOSITING)
457 void doPixelReadbackToCanvas(WebCanvas*, const WebCore::IntRect&);
458 void reallocateRenderer();
459 void updateLayerTreeViewport();
462 WebViewClient* m_client;
463 WebAutofillClient* m_autofillClient;
464 WebPermissionClient* m_permissionClient;
465 WebSpellCheckClient* m_spellCheckClient;
467 ChromeClientImpl m_chromeClientImpl;
468 ContextMenuClientImpl m_contextMenuClientImpl;
469 DragClientImpl m_dragClientImpl;
470 EditorClientImpl m_editorClientImpl;
471 InspectorClientImpl m_inspectorClientImpl;
475 WebPoint m_lastMousePosition;
476 OwnPtr<WebCore::Page> m_page;
478 // This flag is set when a new navigation is detected. It is used to satisfy
479 // the corresponding argument to WebFrameClient::didCommitProvisionalLoad.
480 bool m_observedNewNavigation;
482 // Used to assert that the new navigation we observed is the same navigation
483 // when we make use of m_observedNewNavigation.
484 const WebCore::DocumentLoader* m_newNavigationLoader;
487 // An object that can be used to manipulate m_page->settings() without linking
488 // against WebCore. This is lazily allocated the first time GetWebSettings()
490 OwnPtr<WebSettingsImpl> m_webSettings;
492 // A copy of the web drop data object we received from the browser.
493 RefPtr<WebCore::ChromiumDataObject> m_currentDragData;
495 // The point relative to the client area where the mouse was last pressed
496 // down. This is used by the drag client to determine what was under the
497 // mouse when the drag was initiated. We need to track this here in
498 // WebViewImpl since DragClient::startDrag does not pass the position the
499 // mouse was at when the drag was initiated, only the current point, which
500 // can be misleading as it is usually not over the element the user actually
501 // dragged by the time a drag is initiated.
502 WebPoint m_lastMouseDownPoint;
504 // Keeps track of the current zoom level. 0 means no zoom, positive numbers
505 // mean zoom in, negative numbers mean zoom out.
508 double m_minimumZoomLevel;
510 double m_maximumZoomLevel;
512 bool m_contextMenuAllowed;
514 bool m_doingDragAndDrop;
516 bool m_ignoreInputEvents;
518 // Webkit expects keyPress events to be suppressed if the associated keyDown
519 // event was handled. Safari implements this behavior by peeking out the
520 // associated WM_CHAR event if the keydown was handled. We emulate
521 // this behavior by setting this flag if the keyDown was handled.
522 bool m_suppressNextKeypressEvent;
524 // The policy for how this webview is to be initially shown.
525 WebNavigationPolicy m_initialNavigationPolicy;
527 // Represents whether or not this object should process incoming IME events.
528 bool m_imeAcceptEvents;
530 // The available drag operations (copy, move link...) allowed by the source.
531 WebDragOperation m_operationsAllowed;
533 // The current drag operation as negotiated by the source and destination.
534 // When not equal to DragOperationNone, the drag data can be dropped onto the
535 // current drop target in this WebView (the drop target can accept the drop).
536 WebDragOperation m_dragOperation;
538 // Whether an Autofill popup is currently showing.
539 bool m_autofillPopupShowing;
541 // The Autofill popup client.
542 OwnPtr<AutofillPopupMenuClient> m_autofillPopupClient;
544 // The Autofill popup.
545 RefPtr<WebCore::PopupContainer> m_autofillPopup;
547 // The popup associated with a select element.
548 RefPtr<WebCore::PopupContainer> m_selectPopup;
550 OwnPtr<WebDevToolsAgentPrivate> m_devToolsAgent;
551 OwnPtr<PageOverlay> m_pageOverlay;
553 // Whether the webview is rendering transparently.
554 bool m_isTransparent;
556 // Whether the user can press tab to focus links.
559 // Inspector settings.
560 WebString m_inspectorSettings;
562 typedef HashMap<WTF::String, WTF::String> SettingsMap;
563 OwnPtr<SettingsMap> m_inspectorSettingsMap;
564 OwnPtr<DragScrollTimer> m_dragScrollTimer;
566 #if ENABLE(NOTIFICATIONS)
567 // The provider of desktop notifications;
568 NotificationPresenterImpl m_notificationPresenter;
571 // If set, the (plugin) node which has mouse capture.
572 RefPtr<WebCore::Node> m_mouseCaptureNode;
574 #if USE(ACCELERATED_COMPOSITING)
575 WebCore::IntRect m_rootLayerScrollDamage;
576 OwnPtr<WebCore::NonCompositedContentHost> m_nonCompositedContentHost;
577 RefPtr<WebCore::CCLayerTreeHost> m_layerTreeHost;
578 WebCore::GraphicsLayer* m_rootGraphicsLayer;
579 bool m_isAcceleratedCompositingActive;
580 bool m_compositorCreationFailed;
581 // If true, the graphics context is being restored.
582 bool m_recreatingGraphicsContext;
584 static const WebInputEvent* m_currentInputEvent;
586 #if ENABLE(INPUT_SPEECH)
587 OwnPtr<SpeechInputClientImpl> m_speechInputClient;
589 // If we attempt to fetch the on-screen GraphicsContext3D before
590 // the compositor has been turned on, we need to instantiate it
591 // early. This member holds on to the GC3D in this case.
592 RefPtr<WebCore::GraphicsContext3D> m_temporaryOnscreenGraphicsContext3D;
593 OwnPtr<DeviceOrientationClientProxy> m_deviceOrientationClientProxy;
594 OwnPtr<GeolocationClientProxy> m_geolocationClientProxy;
596 #if ENABLE(GESTURE_RECOGNIZER)
597 OwnPtr<WebCore::PlatformGestureRecognizer> m_gestureRecognizer;
601 } // namespace WebKit