// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // TestWebViewDelegate class: // This class implements the WebViewDelegate methods for the test shell. One // instance is owned by each TestShell. #ifndef WEBKIT_TOOLS_TEST_SHELL_TEST_WEBVIEW_DELEGATE_H_ #define WEBKIT_TOOLS_TEST_SHELL_TEST_WEBVIEW_DELEGATE_H_ #include "build/build_config.h" #if defined(OS_WIN) #include #endif #include #if defined(OS_LINUX) #include #endif #include "base/basictypes.h" #include "base/linked_ptr.h" #if defined(OS_MACOSX) #include "webkit/api/public/WebRect.h" #include "webkit/api/public/WebPopupMenuInfo.h" #endif #include "webkit/glue/webcursor.h" #include "webkit/glue/webview_delegate.h" #if defined(OS_WIN) #include "webkit/tools/test_shell/drag_delegate.h" #include "webkit/tools/test_shell/drop_delegate.h" #endif #include "webkit/tools/test_shell/test_navigation_controller.h" struct ContextMenuMediaParams; struct WebPreferences; class GURL; class TestShell; class WebWidgetHost; class TestWebViewDelegate : public WebViewDelegate { public: struct CapturedContextMenuEvent { CapturedContextMenuEvent(ContextNodeType in_node_type, int in_x, int in_y) : node_type(in_node_type), x(in_x), y(in_y) { } ContextNodeType node_type; int x; int y; }; typedef std::vector CapturedContextMenuEvents; // WebViewDelegate virtual WebView* CreateWebView(WebView* webview, bool user_gesture, const GURL& creator_url); virtual WebKit::WebWidget* CreatePopupWidget( WebView* webview, bool activatable); #if defined(OS_MACOSX) virtual WebKit::WebWidget* CreatePopupWidgetWithInfo( WebView* webview, const WebKit::WebPopupMenuInfo& info); #endif virtual WebPluginDelegate* CreatePluginDelegate( WebView* webview, const GURL& url, const std::string& mime_type, const std::string& clsid, std::string* actual_mime_type); #if defined(OS_LINUX) virtual void CreatedPluginWindow(gfx::PluginWindowHandle id); virtual void WillDestroyPluginWindow(gfx::PluginWindowHandle id); #endif virtual WebKit::WebMediaPlayer* CreateWebMediaPlayer( WebKit::WebMediaPlayerClient* client); virtual WebKit::WebWorker* CreateWebWorker(WebKit::WebWorkerClient* client); virtual void OpenURL(WebView* webview, const GURL& url, const GURL& referrer, WebKit::WebNavigationPolicy policy); virtual void DidMovePlugin(const WebPluginGeometry& move); virtual void RunJavaScriptAlert(WebKit::WebFrame* webframe, const std::wstring& message); virtual bool RunJavaScriptConfirm(WebKit::WebFrame* webframe, const std::wstring& message); virtual bool RunJavaScriptPrompt(WebKit::WebFrame* webframe, const std::wstring& message, const std::wstring& default_value, std::wstring* result); virtual void SetStatusbarText(WebView* webview, const std::wstring& message); virtual void AddMessageToConsole(WebView* webview, const std::wstring& message, unsigned int line_no, const std::wstring& source_id); virtual void StartDragging(WebView* webview, const WebKit::WebDragData& drag_data); virtual void ShowContextMenu(WebView* webview, ContextNodeType node_type, int x, int y, const GURL& link_url, const GURL& image_url, const GURL& page_url, const GURL& frame_url, const ContextMenuMediaParams& media_params, const std::wstring& selection_text, const std::wstring& misspelled_word, int edit_flags, const std::string& security_info, const std::string& frame_charset); virtual void DidCreateDataSource(WebKit::WebFrame* frame, WebKit::WebDataSource* ds); virtual void DidStartProvisionalLoadForFrame( WebView* webview, WebKit::WebFrame* frame, NavigationGesture gesture); virtual void DidReceiveProvisionalLoadServerRedirect( WebView* webview, WebKit::WebFrame* frame); virtual void DidFailProvisionalLoadWithError( WebView* webview, const WebKit::WebURLError& error, WebKit::WebFrame* frame); virtual void DidCommitLoadForFrame( WebView* webview, WebKit::WebFrame* frame, bool is_new_navigation); virtual void DidReceiveTitle(WebView* webview, const std::wstring& title, WebKit::WebFrame* frame); virtual void DidFinishDocumentLoadForFrame(WebView* webview, WebKit::WebFrame* frame); virtual void DidHandleOnloadEventsForFrame(WebView* webview, WebKit::WebFrame* frame); virtual void DidChangeLocationWithinPageForFrame(WebView* webview, WebKit::WebFrame* frame, bool is_new_navigation); virtual void DidReceiveIconForFrame(WebView* webview, WebKit::WebFrame* frame); virtual void WillPerformClientRedirect(WebView* webview, WebKit::WebFrame* frame, const GURL& src_url, const GURL& dest_url, unsigned int delay_seconds, unsigned int fire_date); virtual void DidCancelClientRedirect(WebView* webview, WebKit::WebFrame* frame); virtual void DidFinishLoadForFrame(WebView* webview, WebKit::WebFrame* frame); virtual void DidFailLoadWithError(WebView* webview, const WebKit::WebURLError& error, WebKit::WebFrame* for_frame); virtual void AssignIdentifierToRequest(WebKit::WebFrame* webframe, uint32 identifier, const WebKit::WebURLRequest& request); virtual void WillSendRequest(WebKit::WebFrame* webframe, uint32 identifier, WebKit::WebURLRequest* request, const WebKit::WebURLResponse& redirect_response); virtual void DidReceiveResponse(WebKit::WebFrame* webframe, uint32 identifier, const WebKit::WebURLResponse& response); virtual void DidFinishLoading(WebKit::WebFrame* webframe, uint32 identifier); virtual void DidFailLoadingWithError(WebKit::WebFrame* webframe, uint32 identifier, const WebKit::WebURLError& error); virtual bool ShouldBeginEditing(WebView* webview, std::wstring range); virtual bool ShouldEndEditing(WebView* webview, std::wstring range); virtual bool ShouldInsertNode(WebView* webview, std::wstring node, std::wstring range, std::wstring action); virtual bool ShouldInsertText(WebView* webview, std::wstring text, std::wstring range, std::wstring action); virtual bool ShouldChangeSelectedRange(WebView* webview, std::wstring fromRange, std::wstring toRange, std::wstring affinity, bool stillSelecting); virtual bool ShouldDeleteRange(WebView* webview, std::wstring range); virtual bool ShouldApplyStyle(WebView* webview, std::wstring style, std::wstring range); virtual bool SmartInsertDeleteEnabled(); virtual bool IsSelectTrailingWhitespaceEnabled(); virtual void DidBeginEditing(); virtual void DidChangeSelection(bool is_empty_selection); virtual void DidChangeContents(); virtual void DidEndEditing(); virtual void DidStartLoading(WebView* webview); virtual void DidStopLoading(WebView* webview); virtual void WindowObjectCleared(WebKit::WebFrame* webframe); virtual WebKit::WebNavigationPolicy PolicyForNavigationAction( WebView* webview, WebKit::WebFrame* frame, const WebKit::WebURLRequest& request, WebKit::WebNavigationType type, WebKit::WebNavigationPolicy default_policy, bool is_redirect); virtual void NavigateBackForwardSoon(int offset); virtual int GetHistoryBackListCount(); virtual int GetHistoryForwardListCount(); // WebWidgetClient virtual void didInvalidateRect(const WebKit::WebRect& rect); virtual void didScrollRect(int dx, int dy, const WebKit::WebRect& clip_rect); virtual void didFocus(); virtual void didBlur(); virtual void didChangeCursor(const WebKit::WebCursorInfo& cursor); virtual void closeWidgetSoon(); virtual void show(WebKit::WebNavigationPolicy policy); virtual void runModal(); virtual WebKit::WebRect windowRect(); virtual void setWindowRect(const WebKit::WebRect& rect); virtual WebKit::WebRect rootWindowRect(); virtual WebKit::WebRect windowResizerRect(); virtual WebKit::WebScreenInfo screenInfo(); TestWebViewDelegate(TestShell* shell); void Reset(); void SetSmartInsertDeleteEnabled(bool enabled); void SetSelectTrailingWhitespaceEnabled(bool enabled); // Additional accessors WebKit::WebFrame* top_loading_frame() { return top_loading_frame_; } #if defined(OS_WIN) IDropTarget* drop_delegate() { return drop_delegate_.get(); } IDropSource* drag_delegate() { return drag_delegate_.get(); } #endif const CapturedContextMenuEvents& captured_context_menu_events() const { return captured_context_menu_events_; } void clear_captured_context_menu_events() { captured_context_menu_events_.clear(); } void set_pending_extra_data(TestShellExtraData* extra_data) { pending_extra_data_.reset(extra_data); } // Methods for modifying WebPreferences void SetUserStyleSheetEnabled(bool is_enabled); void SetUserStyleSheetLocation(const GURL& location); // Sets the webview as a drop target. void RegisterDragDrop(); void RevokeDragDrop(); void ResetDragDrop(); void SetCustomPolicyDelegate(bool is_custom, bool is_permissive); void WaitForPolicyDelegate(); void set_block_redirects(bool block_redirects) { block_redirects_ = block_redirects; } bool block_redirects() const { return block_redirects_; } protected: // Called the title of the page changes. // Can be used to update the title of the window. void SetPageTitle(const std::wstring& title); // Called when the URL of the page changes. // Extracts the URL and forwards on to SetAddressBarURL(). void UpdateAddressBar(WebView* webView); // Called when the URL of the page changes. // Should be used to update the text of the URL bar. void SetAddressBarURL(const GURL& url); // Show a JavaScript alert as a popup message. // The caller should test whether we're in layout test mode and only // call this function when we really want a message to pop up. void ShowJavaScriptAlert(const std::wstring& message); // In the Mac code, this is called to trigger the end of a test after the // page has finished loading. From here, we can generate the dump for the // test. void LocationChangeDone(WebKit::WebFrame*); WebWidgetHost* GetWidgetHost(); void UpdateForCommittedLoad(WebKit::WebFrame* webframe, bool is_new_navigation); void UpdateURL(WebKit::WebFrame* frame); void UpdateSessionHistory(WebKit::WebFrame* frame); void UpdateSelectionClipboard(bool is_empty_selection); // Get a string suitable for dumping a frame to the console. std::wstring GetFrameDescription(WebKit::WebFrame* webframe); private: // Causes navigation actions just printout the intended navigation instead // of taking you to the page. This is used for cases like mailto, where you // don't actually want to open the mail program. bool policy_delegate_enabled_; // Toggles the behavior of the policy delegate. If true, then navigations // will be allowed. Otherwise, they will be ignored (dropped). bool policy_delegate_is_permissive_; // If true, the policy delegate will signal layout test completion. bool policy_delegate_should_notify_done_; // Non-owning pointer. The delegate is owned by the host. TestShell* shell_; // This is non-NULL IFF a load is in progress. WebKit::WebFrame* top_loading_frame_; // For tracking session history. See RenderView. int page_id_; int last_page_id_updated_; linked_ptr pending_extra_data_; // Maps resource identifiers to a descriptive string. typedef std::map ResourceMap; ResourceMap resource_identifier_map_; std::string GetResourceDescription(uint32 identifier); CapturedContextMenuEvents captured_context_menu_events_; WebCursor current_cursor_; #if defined(OS_WIN) // Classes needed by drag and drop. scoped_refptr drag_delegate_; scoped_refptr drop_delegate_; #endif #if defined(OS_LINUX) // The type of cursor the window is currently using. // Used for judging whether a new SetCursor call is actually changing the // cursor. GdkCursorType cursor_type_; #endif #if defined(OS_MACOSX) linked_ptr popup_menu_info_; WebKit::WebRect popup_bounds_; #endif // true if we want to enable smart insert/delete. bool smart_insert_delete_enabled_; // true if we want to enable selection of trailing whitespaces bool select_trailing_whitespace_enabled_; // true if we should block any redirects bool block_redirects_; }; #endif // WEBKIT_TOOLS_TEST_SHELL_TEST_WEBVIEW_DELEGATE_H_