Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 Google 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 are
      6  * met:
      7  *
      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
     13  * distribution.
     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.
     17  *
     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.
     29  */
     30 
     31 #ifndef WebViewImpl_h
     32 #define WebViewImpl_h
     33 
     34 // FIXME: Remove these relative paths once consumers from glue are removed.
     35 #include "../public/WebNavigationPolicy.h"
     36 #include "../public/WebPoint.h"
     37 #include "../public/WebSize.h"
     38 #include "../public/WebString.h"
     39 #include "../public/WebView.h"
     40 
     41 #include "BackForwardListClientImpl.h"
     42 #include "ChromeClientImpl.h"
     43 #include "ContextMenuClientImpl.h"
     44 #include "DragClientImpl.h"
     45 #include "EditorClientImpl.h"
     46 #include "InspectorClientImpl.h"
     47 #include "NotificationPresenterImpl.h"
     48 
     49 #include <wtf/OwnPtr.h>
     50 #include <wtf/RefCounted.h>
     51 
     52 namespace WebCore {
     53 class ChromiumDataObject;
     54 class Frame;
     55 class HistoryItem;
     56 class HitTestResult;
     57 class KeyboardEvent;
     58 class Page;
     59 class PlatformKeyboardEvent;
     60 class PopupContainer;
     61 class PopupMenuClient;
     62 class Range;
     63 class RenderTheme;
     64 class Widget;
     65 }
     66 
     67 namespace WebKit {
     68 class AutocompletePopupMenuClient;
     69 class AutoFillPopupMenuClient;
     70 class ContextMenuClientImpl;
     71 class SuggestionsPopupMenuClient;
     72 class WebAccessibilityObject;
     73 class WebDevToolsAgentPrivate;
     74 class WebFrameImpl;
     75 class WebKeyboardEvent;
     76 class WebMouseEvent;
     77 class WebMouseWheelEvent;
     78 class WebSettingsImpl;
     79 
     80 class WebViewImpl : public WebView, public RefCounted<WebViewImpl> {
     81 public:
     82     // WebWidget methods:
     83     virtual void close();
     84     virtual WebSize size() { return m_size; }
     85     virtual void resize(const WebSize&);
     86     virtual void layout();
     87     virtual void paint(WebCanvas*, const WebRect&);
     88     virtual bool handleInputEvent(const WebInputEvent&);
     89     virtual void mouseCaptureLost();
     90     virtual void setFocus(bool enable);
     91     virtual bool handleCompositionEvent(WebCompositionCommand command,
     92                                         int cursorPosition,
     93                                         int targetStart,
     94                                         int targetEnd,
     95                                         const WebString& text);
     96     virtual bool queryCompositionStatus(bool* enabled,
     97                                         WebRect* caretRect);
     98     virtual void setTextDirection(WebTextDirection direction);
     99 
    100     // WebView methods:
    101     virtual void initializeMainFrame(WebFrameClient*);
    102     virtual WebSettings* settings();
    103     virtual WebString pageEncoding() const;
    104     virtual void setPageEncoding(const WebString& encoding);
    105     virtual bool isTransparent() const;
    106     virtual void setIsTransparent(bool value);
    107     virtual bool tabsToLinks() const;
    108     virtual void setTabsToLinks(bool value);
    109     virtual bool tabKeyCyclesThroughElements() const;
    110     virtual void setTabKeyCyclesThroughElements(bool value);
    111     virtual bool isActive() const;
    112     virtual void setIsActive(bool value);
    113     virtual bool dispatchBeforeUnloadEvent();
    114     virtual void dispatchUnloadEvent();
    115     virtual WebFrame* mainFrame();
    116     virtual WebFrame* findFrameByName(
    117         const WebString& name, WebFrame* relativeToFrame);
    118     virtual WebFrame* focusedFrame();
    119     virtual void setFocusedFrame(WebFrame* frame);
    120     virtual void setInitialFocus(bool reverse);
    121     virtual void clearFocusedNode();
    122     virtual int zoomLevel();
    123     virtual int setZoomLevel(bool textOnly, int zoomLevel);
    124     virtual void performMediaPlayerAction(
    125         const WebMediaPlayerAction& action,
    126         const WebPoint& location);
    127     virtual void copyImageAt(const WebPoint& point);
    128     virtual void dragSourceEndedAt(
    129         const WebPoint& clientPoint,
    130         const WebPoint& screenPoint,
    131         WebDragOperation operation);
    132     virtual void dragSourceSystemDragEnded();
    133     virtual WebDragOperation dragTargetDragEnter(
    134         const WebDragData& dragData, int identity,
    135         const WebPoint& clientPoint,
    136         const WebPoint& screenPoint,
    137         WebDragOperationsMask operationsAllowed);
    138     virtual WebDragOperation dragTargetDragOver(
    139         const WebPoint& clientPoint,
    140         const WebPoint& screenPoint,
    141         WebDragOperationsMask operationsAllowed);
    142     virtual void dragTargetDragLeave();
    143     virtual void dragTargetDrop(
    144         const WebPoint& clientPoint,
    145         const WebPoint& screenPoint);
    146     virtual int dragIdentity();
    147     virtual bool setDropEffect(bool accept);
    148     virtual unsigned long createUniqueIdentifierForRequest();
    149     virtual void inspectElementAt(const WebPoint& point);
    150     virtual WebString inspectorSettings() const;
    151     virtual void setInspectorSettings(const WebString& settings);
    152     virtual WebDevToolsAgent* devToolsAgent();
    153     virtual void setDevToolsAgent(WebDevToolsAgent*);
    154     virtual WebAccessibilityObject accessibilityObject();
    155     virtual void applyAutofillSuggestions(
    156         const WebNode&,
    157         const WebVector<WebString>& suggestions,
    158         int defaultSuggestionIndex);
    159     virtual void applyAutoFillSuggestions(
    160         const WebNode&,
    161         const WebVector<WebString>& names,
    162         const WebVector<WebString>& labels,
    163         int defaultSuggestionIndex);
    164     virtual void applyAutocompleteSuggestions(
    165         const WebNode&,
    166         const WebVector<WebString>& suggestions,
    167         int defaultSuggestionIndex);
    168     virtual void hideAutofillPopup();
    169     virtual void hideSuggestionsPopup();
    170     virtual void setScrollbarColors(unsigned inactiveColor,
    171                                     unsigned activeColor,
    172                                     unsigned trackColor);
    173     virtual void setSelectionColors(unsigned activeBackgroundColor,
    174                                     unsigned activeForegroundColor,
    175                                     unsigned inactiveBackgroundColor,
    176                                     unsigned inactiveForegroundColor);
    177     virtual void performCustomContextMenuAction(unsigned action);
    178     virtual void addUserScript(const WebString& sourceCode,
    179                                bool runAtStart);
    180     virtual void removeAllUserContent();
    181 
    182     // WebViewImpl
    183 
    184     void setIgnoreInputEvents(bool newValue);
    185     WebDevToolsAgentPrivate* devToolsAgentPrivate() { return m_devToolsAgent.get(); }
    186 
    187     const WebPoint& lastMouseDownPoint() const
    188     {
    189         return m_lastMouseDownPoint;
    190     }
    191 
    192     WebCore::Frame* focusedWebCoreFrame();
    193 
    194     // Returns the currently focused Node or null if no node has focus.
    195     WebCore::Node* focusedWebCoreNode();
    196 
    197     static WebViewImpl* fromPage(WebCore::Page*);
    198 
    199     WebViewClient* client()
    200     {
    201         return m_client;
    202     }
    203 
    204     // Returns the page object associated with this view.  This may be null when
    205     // the page is shutting down, but will be valid at all other times.
    206     WebCore::Page* page() const
    207     {
    208         return m_page.get();
    209     }
    210 
    211     WebCore::RenderTheme* theme() const;
    212 
    213     // Returns the main frame associated with this view.  This may be null when
    214     // the page is shutting down, but will be valid at all other times.
    215     WebFrameImpl* mainFrameImpl();
    216 
    217     // History related methods:
    218     void setCurrentHistoryItem(WebCore::HistoryItem*);
    219     WebCore::HistoryItem* previousHistoryItem();
    220     void observeNewNavigation();
    221 
    222     // Event related methods:
    223     void mouseMove(const WebMouseEvent&);
    224     void mouseLeave(const WebMouseEvent&);
    225     void mouseDown(const WebMouseEvent&);
    226     void mouseUp(const WebMouseEvent&);
    227     void mouseContextMenu(const WebMouseEvent&);
    228     void mouseDoubleClick(const WebMouseEvent&);
    229     void mouseWheel(const WebMouseWheelEvent&);
    230     bool keyEvent(const WebKeyboardEvent&);
    231     bool charEvent(const WebKeyboardEvent&);
    232 
    233     // Handles context menu events orignated via the the keyboard. These
    234     // include the VK_APPS virtual key and the Shift+F10 combine.  Code is
    235     // based on the Webkit function bool WebView::handleContextMenuEvent(WPARAM
    236     // wParam, LPARAM lParam) in webkit\webkit\win\WebView.cpp. The only
    237     // significant change in this function is the code to convert from a
    238     // Keyboard event to the Right Mouse button down event.
    239     bool sendContextMenuEvent(const WebKeyboardEvent&);
    240 
    241     // Notifies the WebView that a load has been committed.  isNewNavigation
    242     // will be true if a new session history item should be created for that
    243     // load.
    244     void didCommitLoad(bool* isNewNavigation);
    245 
    246     bool contextMenuAllowed() const
    247     {
    248         return m_contextMenuAllowed;
    249     }
    250 
    251     // Set the disposition for how this webview is to be initially shown.
    252     void setInitialNavigationPolicy(WebNavigationPolicy policy)
    253     {
    254         m_initialNavigationPolicy = policy;
    255     }
    256     WebNavigationPolicy initialNavigationPolicy() const
    257     {
    258         return m_initialNavigationPolicy;
    259     }
    260 
    261     // Determines whether a page should e.g. be opened in a background tab.
    262     // Returns false if it has no opinion, in which case it doesn't set *policy.
    263     static bool navigationPolicyFromMouseEvent(
    264         unsigned short button,
    265         bool ctrl,
    266         bool shift,
    267         bool alt,
    268         bool meta,
    269         WebNavigationPolicy*);
    270 
    271     // Start a system drag and drop operation.
    272     void startDragging(
    273         const WebPoint& eventPos,
    274         const WebDragData& dragData,
    275         WebDragOperationsMask dragSourceOperationMask);
    276 
    277     void suggestionsPopupDidHide()
    278     {
    279         m_suggestionsPopupShowing = false;
    280     }
    281 
    282 #if ENABLE(NOTIFICATIONS)
    283     // Returns the provider of desktop notifications.
    284     NotificationPresenterImpl* notificationPresenterImpl();
    285 #endif
    286 
    287     // Tries to scroll a frame or any parent of a frame. Returns true if the view
    288     // was scrolled.
    289     bool propagateScroll(WebCore::ScrollDirection, WebCore::ScrollGranularity);
    290 
    291     // HACK: currentInputEvent() is for ChromeClientImpl::show(), until we can
    292     // fix WebKit to pass enough information up into ChromeClient::show() so we
    293     // can decide if the window.open event was caused by a middle-mouse click
    294     static const WebInputEvent* currentInputEvent()
    295     {
    296         return m_currentInputEvent;
    297     }
    298 
    299 private:
    300     friend class WebView;  // So WebView::Create can call our constructor
    301     friend class WTF::RefCounted<WebViewImpl>;
    302 
    303     WebViewImpl(WebViewClient* client);
    304     ~WebViewImpl();
    305 
    306     // Returns true if the event was actually processed.
    307     bool keyEventDefault(const WebKeyboardEvent&);
    308 
    309     // Returns true if the autocomple has consumed the event.
    310     bool autocompleteHandleKeyEvent(const WebKeyboardEvent&);
    311 
    312     // Repaints the suggestions popup.  Should be called when the suggestions
    313     // have changed.  Note that this should only be called when the suggestions
    314     // popup is showing.
    315     void refreshSuggestionsPopup();
    316 
    317     // Returns true if the view was scrolled.
    318     bool scrollViewWithKeyboard(int keyCode, int modifiers);
    319 
    320     // Converts |pos| from window coordinates to contents coordinates and gets
    321     // the HitTestResult for it.
    322     WebCore::HitTestResult hitTestResultForWindowPos(const WebCore::IntPoint&);
    323 
    324     WebViewClient* m_client;
    325 
    326     BackForwardListClientImpl m_backForwardListClientImpl;
    327     ChromeClientImpl m_chromeClientImpl;
    328     ContextMenuClientImpl m_contextMenuClientImpl;
    329     DragClientImpl m_dragClientImpl;
    330     EditorClientImpl m_editorClientImpl;
    331     InspectorClientImpl m_inspectorClientImpl;
    332 
    333     WebSize m_size;
    334 
    335     WebPoint m_lastMousePosition;
    336     OwnPtr<WebCore::Page> m_page;
    337 
    338     // This flag is set when a new navigation is detected.  It is used to satisfy
    339     // the corresponding argument to WebFrameClient::didCommitProvisionalLoad.
    340     bool m_observedNewNavigation;
    341 #ifndef NDEBUG
    342     // Used to assert that the new navigation we observed is the same navigation
    343     // when we make use of m_observedNewNavigation.
    344     const WebCore::DocumentLoader* m_newNavigationLoader;
    345 #endif
    346 
    347     // An object that can be used to manipulate m_page->settings() without linking
    348     // against WebCore.  This is lazily allocated the first time GetWebSettings()
    349     // is called.
    350     OwnPtr<WebSettingsImpl> m_webSettings;
    351 
    352     // A copy of the web drop data object we received from the browser.
    353     RefPtr<WebCore::ChromiumDataObject> m_currentDragData;
    354 
    355     // The point relative to the client area where the mouse was last pressed
    356     // down. This is used by the drag client to determine what was under the
    357     // mouse when the drag was initiated. We need to track this here in
    358     // WebViewImpl since DragClient::startDrag does not pass the position the
    359     // mouse was at when the drag was initiated, only the current point, which
    360     // can be misleading as it is usually not over the element the user actually
    361     // dragged by the time a drag is initiated.
    362     WebPoint m_lastMouseDownPoint;
    363 
    364     // Keeps track of the current zoom level.  0 means no zoom, positive numbers
    365     // mean zoom in, negative numbers mean zoom out.
    366     int m_zoomLevel;
    367 
    368     bool m_contextMenuAllowed;
    369 
    370     bool m_doingDragAndDrop;
    371 
    372     bool m_ignoreInputEvents;
    373 
    374     // Webkit expects keyPress events to be suppressed if the associated keyDown
    375     // event was handled. Safari implements this behavior by peeking out the
    376     // associated WM_CHAR event if the keydown was handled. We emulate
    377     // this behavior by setting this flag if the keyDown was handled.
    378     bool m_suppressNextKeypressEvent;
    379 
    380     // The policy for how this webview is to be initially shown.
    381     WebNavigationPolicy m_initialNavigationPolicy;
    382 
    383     // Represents whether or not this object should process incoming IME events.
    384     bool m_imeAcceptEvents;
    385 
    386     // True while dispatching system drag and drop events to drag/drop targets
    387     // within this WebView.
    388     bool m_dragTargetDispatch;
    389 
    390     // Valid when m_dragTargetDispatch is true; the identity of the drag data
    391     // copied from the WebDropData object sent from the browser process.
    392     int m_dragIdentity;
    393 
    394     // Valid when m_dragTargetDispatch is true.  Used to override the default
    395     // browser drop effect with the effects "none" or "copy".
    396     enum DragTargetDropEffect {
    397         DropEffectDefault = -1,
    398         DropEffectNone,
    399         DropEffectCopy
    400     } m_dropEffect;
    401 
    402     // The available drag operations (copy, move link...) allowed by the source.
    403     WebDragOperation m_operationsAllowed;
    404 
    405     // The current drag operation as negotiated by the source and destination.
    406     // When not equal to DragOperationNone, the drag data can be dropped onto the
    407     // current drop target in this WebView (the drop target can accept the drop).
    408     WebDragOperation m_dragOperation;
    409 
    410     // Whether a suggestions popup is currently showing.
    411     bool m_suggestionsPopupShowing;
    412 
    413     // A pointer to the current suggestions popup menu client.  This can be
    414     // either an AutoFillPopupMenuClient or an AutocompletePopupMenuClient.  We
    415     // do not own this pointer.
    416     SuggestionsPopupMenuClient* m_suggestionsPopupClient;
    417 
    418     // The AutoFill popup client.
    419     OwnPtr<AutoFillPopupMenuClient> m_autoFillPopupClient;
    420 
    421     // The Autocomplete popup client.
    422     OwnPtr<AutocompletePopupMenuClient> m_autocompletePopupClient;
    423 
    424     // A pointer to the current suggestions popup.  We do not own this pointer.
    425     WebCore::PopupContainer* m_suggestionsPopup;
    426 
    427     // The AutoFill suggestions popup.
    428     RefPtr<WebCore::PopupContainer> m_autoFillPopup;
    429 
    430     // The AutoComplete suggestions popup.
    431     RefPtr<WebCore::PopupContainer> m_autocompletePopup;
    432 
    433     OwnPtr<WebDevToolsAgentPrivate> m_devToolsAgent;
    434 
    435     // Whether the webview is rendering transparently.
    436     bool m_isTransparent;
    437 
    438     // Whether the user can press tab to focus links.
    439     bool m_tabsToLinks;
    440 
    441     // Inspector settings.
    442     WebString m_inspectorSettings;
    443 
    444 #if ENABLE(NOTIFICATIONS)
    445     // The provider of desktop notifications;
    446     NotificationPresenterImpl m_notificationPresenter;
    447 #endif
    448 
    449     static const WebInputEvent* m_currentInputEvent;
    450 };
    451 
    452 } // namespace WebKit
    453 
    454 #endif
    455