OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // TestWebViewDelegate class: | |
6 // This class implements the WebViewDelegate methods for the test shell. One | |
7 // instance is owned by each TestShell. | |
8 | |
9 #ifndef WEBKIT_TOOLS_TEST_SHELL_TEST_WEBVIEW_DELEGATE_H_ | |
10 #define WEBKIT_TOOLS_TEST_SHELL_TEST_WEBVIEW_DELEGATE_H_ | |
11 | |
12 #include <map> | |
13 #include <set> | |
14 #include <string> | |
15 #include <vector> | |
16 | |
17 #include "base/basictypes.h" | |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/memory/weak_ptr.h" | |
20 #include "base/string16.h" | |
21 #include "build/build_config.h" | |
22 #include "third_party/WebKit/Source/Platform/chromium/public/WebFileSystem.h" | |
23 #include "third_party/WebKit/Source/Platform/chromium/public/WebFileSystemType.h
" | |
24 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" | |
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h" | |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" | |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" | |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupType.h" | |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" | |
30 #include "webkit/glue/webcursor.h" | |
31 #include "webkit/plugins/npapi/webplugin_page_delegate.h" | |
32 #include "webkit/tools/test_shell/mock_spellcheck.h" | |
33 #include "webkit/tools/test_shell/test_navigation_controller.h" | |
34 | |
35 #if defined(OS_MACOSX) | |
36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h" | |
37 #endif | |
38 | |
39 #if defined(OS_WIN) | |
40 #include <windows.h> | |
41 | |
42 #include "webkit/tools/test_shell/drop_delegate.h" | |
43 #endif | |
44 | |
45 #if defined(TOOLKIT_GTK) | |
46 #include <gdk/gdk.h> | |
47 #endif | |
48 | |
49 class GURL; | |
50 class TestShell; | |
51 class WebWidgetHost; | |
52 | |
53 namespace WebKit { | |
54 class WebDeviceOrientationClient; | |
55 class WebStorageNamespace; | |
56 struct WebWindowFeatures; | |
57 } | |
58 | |
59 class TestWebViewDelegate : public WebKit::WebViewClient, | |
60 public WebKit::WebFrameClient, | |
61 public webkit::npapi::WebPluginPageDelegate, | |
62 public base::SupportsWeakPtr<TestWebViewDelegate> { | |
63 public: | |
64 struct CapturedContextMenuEvent { | |
65 CapturedContextMenuEvent(int in_node_type, | |
66 int in_x, | |
67 int in_y) | |
68 : node_type(in_node_type), | |
69 x(in_x), | |
70 y(in_y) { | |
71 } | |
72 | |
73 int node_type; | |
74 int x; | |
75 int y; | |
76 }; | |
77 | |
78 typedef std::vector<CapturedContextMenuEvent> CapturedContextMenuEvents; | |
79 | |
80 // WebKit::WebViewClient | |
81 virtual WebKit::WebView* createView( | |
82 WebKit::WebFrame* creator, | |
83 const WebKit::WebURLRequest& request, | |
84 const WebKit::WebWindowFeatures& features, | |
85 const WebKit::WebString& frame_name, | |
86 WebKit::WebNavigationPolicy policy); | |
87 virtual WebKit::WebWidget* createPopupMenu(WebKit::WebPopupType popup_type); | |
88 virtual WebKit::WebWidget* createPopupMenu( | |
89 const WebKit::WebPopupMenuInfo& info); | |
90 virtual WebKit::WebStorageNamespace* createSessionStorageNamespace( | |
91 unsigned quota); | |
92 virtual void didAddMessageToConsole( | |
93 const WebKit::WebConsoleMessage& message, | |
94 const WebKit::WebString& source_name, unsigned source_line); | |
95 virtual void didStartLoading(); | |
96 virtual void didStopLoading(); | |
97 virtual bool shouldBeginEditing(const WebKit::WebRange& range); | |
98 virtual bool shouldEndEditing(const WebKit::WebRange& range); | |
99 virtual bool shouldInsertNode( | |
100 const WebKit::WebNode& node, const WebKit::WebRange& range, | |
101 WebKit::WebEditingAction action); | |
102 virtual bool shouldInsertText( | |
103 const WebKit::WebString& text, const WebKit::WebRange& range, | |
104 WebKit::WebEditingAction action); | |
105 virtual bool shouldChangeSelectedRange( | |
106 const WebKit::WebRange& from, const WebKit::WebRange& to, | |
107 WebKit::WebTextAffinity affinity, bool still_selecting); | |
108 virtual bool shouldDeleteRange(const WebKit::WebRange& range); | |
109 virtual bool shouldApplyStyle( | |
110 const WebKit::WebString& style, const WebKit::WebRange& range); | |
111 virtual void didBeginEditing(); | |
112 virtual void didChangeSelection(bool is_selection_empty); | |
113 virtual void didChangeContents(); | |
114 virtual void didEndEditing(); | |
115 virtual bool handleCurrentKeyboardEvent(); | |
116 virtual void spellCheck( | |
117 const WebKit::WebString& text, int& misspelledOffset, | |
118 int& misspelledLength); | |
119 virtual WebKit::WebString autoCorrectWord( | |
120 const WebKit::WebString& misspelled_word); | |
121 virtual void runModalAlertDialog( | |
122 WebKit::WebFrame* frame, const WebKit::WebString& message); | |
123 virtual bool runModalConfirmDialog( | |
124 WebKit::WebFrame* frame, const WebKit::WebString& message); | |
125 virtual bool runModalPromptDialog( | |
126 WebKit::WebFrame* frame, const WebKit::WebString& message, | |
127 const WebKit::WebString& default_value, WebKit::WebString* actual_value); | |
128 virtual bool runModalBeforeUnloadDialog( | |
129 WebKit::WebFrame* frame, const WebKit::WebString& message); | |
130 virtual void setStatusText(const WebKit::WebString& text); | |
131 virtual void startDragging( | |
132 WebKit::WebFrame* frame, const WebKit::WebDragData& data, | |
133 WebKit::WebDragOperationsMask mask, const WebKit::WebImage& image, | |
134 const WebKit::WebPoint& offset); | |
135 virtual void navigateBackForwardSoon(int offset); | |
136 virtual int historyBackListCount(); | |
137 virtual int historyForwardListCount(); | |
138 virtual WebKit::WebNotificationPresenter* notificationPresenter(); | |
139 virtual WebKit::WebGeolocationClient* geolocationClient(); | |
140 virtual WebKit::WebDeviceOrientationClient* deviceOrientationClient(); | |
141 virtual WebKit::WebSpeechInputController* speechInputController( | |
142 WebKit::WebSpeechInputListener*); | |
143 | |
144 // WebKit::WebWidgetClient | |
145 virtual void didInvalidateRect(const WebKit::WebRect& rect); | |
146 virtual void didScrollRect(int dx, int dy, | |
147 const WebKit::WebRect& clip_rect); | |
148 virtual void scheduleComposite(); | |
149 virtual void scheduleAnimation(); | |
150 virtual void didFocus(); | |
151 virtual void didBlur(); | |
152 virtual void didChangeCursor(const WebKit::WebCursorInfo& cursor); | |
153 virtual void closeWidgetSoon(); | |
154 virtual void show(WebKit::WebNavigationPolicy policy); | |
155 virtual void runModal(); | |
156 virtual WebKit::WebRect windowRect(); | |
157 virtual void setWindowRect(const WebKit::WebRect& rect); | |
158 virtual WebKit::WebRect rootWindowRect(); | |
159 virtual WebKit::WebRect windowResizerRect(); | |
160 virtual WebKit::WebScreenInfo screenInfo(); | |
161 | |
162 // WebKit::WebFrameClient | |
163 virtual WebKit::WebPlugin* createPlugin( | |
164 WebKit::WebFrame*, const WebKit::WebPluginParams&); | |
165 virtual WebKit::WebMediaPlayer* createMediaPlayer( | |
166 WebKit::WebFrame*, const WebKit::WebURL&, WebKit::WebMediaPlayerClient*); | |
167 virtual WebKit::WebApplicationCacheHost* createApplicationCacheHost( | |
168 WebKit::WebFrame*, WebKit::WebApplicationCacheHostClient*); | |
169 virtual bool allowPlugins(WebKit::WebFrame* frame, bool enabled_per_settings); | |
170 virtual bool allowImage(WebKit::WebFrame* frame, | |
171 bool enabled_per_settings, | |
172 const WebKit::WebURL& image_url); | |
173 virtual void loadURLExternally( | |
174 WebKit::WebFrame*, const WebKit::WebURLRequest&, | |
175 WebKit::WebNavigationPolicy); | |
176 virtual WebKit::WebNavigationPolicy decidePolicyForNavigation( | |
177 WebKit::WebFrame*, WebKit::WebDataSource::ExtraData*, | |
178 const WebKit::WebURLRequest&, WebKit::WebNavigationType, | |
179 WebKit::WebNavigationPolicy default_policy, bool isRedirect); | |
180 // DEPRECATED | |
181 virtual WebKit::WebNavigationPolicy decidePolicyForNavigation( | |
182 WebKit::WebFrame*, const WebKit::WebURLRequest&, | |
183 WebKit::WebNavigationType, WebKit::WebNavigationPolicy default_policy, | |
184 bool isRedirect); | |
185 virtual bool canHandleRequest( | |
186 WebKit::WebFrame*, const WebKit::WebURLRequest&); | |
187 virtual WebKit::WebURLError cannotHandleRequestError( | |
188 WebKit::WebFrame*, const WebKit::WebURLRequest& request); | |
189 virtual WebKit::WebURLError cancelledError( | |
190 WebKit::WebFrame*, const WebKit::WebURLRequest& request); | |
191 virtual void unableToImplementPolicyWithError( | |
192 WebKit::WebFrame*, const WebKit::WebURLError&); | |
193 virtual void willPerformClientRedirect( | |
194 WebKit::WebFrame*, const WebKit::WebURL& from, const WebKit::WebURL& to, | |
195 double interval, double fire_time); | |
196 virtual void didCancelClientRedirect(WebKit::WebFrame*); | |
197 virtual void didCreateDataSource( | |
198 WebKit::WebFrame*, WebKit::WebDataSource*); | |
199 virtual void didStartProvisionalLoad(WebKit::WebFrame*); | |
200 virtual void didReceiveServerRedirectForProvisionalLoad(WebKit::WebFrame*); | |
201 virtual void didFailProvisionalLoad( | |
202 WebKit::WebFrame*, const WebKit::WebURLError&); | |
203 virtual void didCommitProvisionalLoad( | |
204 WebKit::WebFrame*, bool is_new_navigation); | |
205 virtual void didReceiveTitle( | |
206 WebKit::WebFrame*, const WebKit::WebString& title, | |
207 WebKit::WebTextDirection direction); | |
208 virtual void didFinishDocumentLoad(WebKit::WebFrame*); | |
209 virtual void didHandleOnloadEvents(WebKit::WebFrame*); | |
210 virtual void didFinishLoad(WebKit::WebFrame*); | |
211 virtual void didNavigateWithinPage( | |
212 WebKit::WebFrame*, bool is_new_navigation); | |
213 virtual void didChangeLocationWithinPage( | |
214 WebKit::WebFrame*); | |
215 virtual void assignIdentifierToRequest( | |
216 WebKit::WebFrame*, unsigned identifier, const WebKit::WebURLRequest&); | |
217 virtual void willSendRequest( | |
218 WebKit::WebFrame*, unsigned identifier, WebKit::WebURLRequest&, | |
219 const WebKit::WebURLResponse& redirectResponse); | |
220 virtual void didReceiveResponse( | |
221 WebKit::WebFrame*, unsigned identifier, const WebKit::WebURLResponse&); | |
222 virtual void didFinishResourceLoad( | |
223 WebKit::WebFrame*, unsigned identifier); | |
224 virtual void didFailResourceLoad( | |
225 WebKit::WebFrame*, unsigned identifier, const WebKit::WebURLError&); | |
226 virtual void didDisplayInsecureContent(WebKit::WebFrame* frame); | |
227 virtual void didRunInsecureContent( | |
228 WebKit::WebFrame* frame, | |
229 const WebKit::WebSecurityOrigin& origin, | |
230 const WebKit::WebURL& target_url); | |
231 virtual bool allowScript(WebKit::WebFrame* frame, bool enabled_per_settings); | |
232 virtual void openFileSystem( | |
233 WebKit::WebFrame* frame, | |
234 WebKit::WebFileSystemType type, | |
235 long long size, | |
236 bool create, | |
237 WebKit::WebFileSystemCallbacks* callbacks); | |
238 | |
239 // webkit::npapi::WebPluginPageDelegate | |
240 virtual webkit::npapi::WebPluginDelegate* CreatePluginDelegate( | |
241 const base::FilePath& url, | |
242 const std::string& mime_type) OVERRIDE; | |
243 virtual WebKit::WebPlugin* CreatePluginReplacement( | |
244 const base::FilePath& file_path) OVERRIDE; | |
245 virtual void CreatedPluginWindow( | |
246 gfx::PluginWindowHandle handle) OVERRIDE; | |
247 virtual void WillDestroyPluginWindow( | |
248 gfx::PluginWindowHandle handle) OVERRIDE; | |
249 virtual void DidMovePlugin( | |
250 const webkit::npapi::WebPluginGeometry& move) OVERRIDE; | |
251 virtual void DidStartLoadingForPlugin() OVERRIDE {} | |
252 virtual void DidStopLoadingForPlugin() OVERRIDE {} | |
253 virtual WebKit::WebCookieJar* GetCookieJar() OVERRIDE; | |
254 | |
255 explicit TestWebViewDelegate(TestShell* shell); | |
256 virtual ~TestWebViewDelegate(); | |
257 void Reset(); | |
258 | |
259 // Additional accessors | |
260 #if defined(OS_WIN) | |
261 IDropTarget* drop_delegate() { return drop_delegate_.get(); } | |
262 #endif | |
263 | |
264 void set_pending_extra_data(TestShellExtraData* extra_data) { | |
265 pending_extra_data_.reset(extra_data); | |
266 } | |
267 | |
268 // Methods for modifying WebPreferences | |
269 void SetUserStyleSheetEnabled(bool is_enabled); | |
270 void SetUserStyleSheetLocation(const GURL& location); | |
271 void SetAuthorAndUserStylesEnabled(bool is_enabled); | |
272 | |
273 // Sets the webview as a drop target. | |
274 void RegisterDragDrop(); | |
275 void RevokeDragDrop(); | |
276 | |
277 void ResetDragDrop(); | |
278 | |
279 void SetCustomPolicyDelegate(bool is_custom, bool is_permissive); | |
280 void WaitForPolicyDelegate(); | |
281 | |
282 void set_clear_header(const std::string& header) { | |
283 clear_headers_.insert(header); | |
284 } | |
285 const std::set<std::string>& clear_headers() { | |
286 return clear_headers_; | |
287 } | |
288 | |
289 void set_block_redirects(bool block_redirects) { | |
290 block_redirects_ = block_redirects; | |
291 } | |
292 bool block_redirects() const { | |
293 return block_redirects_; | |
294 } | |
295 | |
296 void set_request_return_null(bool request_return_null) { | |
297 request_return_null_ = request_return_null; | |
298 } | |
299 bool request_return_null() const { | |
300 return request_return_null_; | |
301 } | |
302 | |
303 void SetEditCommand(const std::string& name, const std::string& value) { | |
304 edit_command_name_ = name; | |
305 edit_command_value_ = value; | |
306 } | |
307 | |
308 void ClearEditCommand() { | |
309 edit_command_name_.clear(); | |
310 edit_command_value_.clear(); | |
311 } | |
312 | |
313 void ClearContextMenuData(); | |
314 | |
315 const WebKit::WebContextMenuData* last_context_menu_data() const { | |
316 return last_context_menu_data_.get(); | |
317 } | |
318 | |
319 MockSpellCheck* mock_spellcheck() { | |
320 return &mock_spellcheck_; | |
321 } | |
322 | |
323 private: | |
324 // Called the title of the page changes. | |
325 // Can be used to update the title of the window. | |
326 void SetPageTitle(const base::string16& title); | |
327 | |
328 // Called when the URL of the page changes. | |
329 // Extracts the URL and forwards on to SetAddressBarURL(). | |
330 void UpdateAddressBar(WebKit::WebView* webView); | |
331 | |
332 // Called when the URL of the page changes. | |
333 // Should be used to update the text of the URL bar. | |
334 void SetAddressBarURL(const GURL& url); | |
335 | |
336 // Show a JavaScript alert as a popup message. | |
337 // The caller should test whether we're in layout test mode and only | |
338 // call this function when we really want a message to pop up. | |
339 void ShowJavaScriptAlert(const base::string16& message); | |
340 | |
341 // Tests that require moving or resizing the main window (via resizeTo() or | |
342 // moveTo()) pass in Chrome even though Chrome disregards move requests for | |
343 // non-popup windows (see TabContents::RequestMove()). These functions allow | |
344 // the test shell to mimic its behavior. If setWindowRect() is called for | |
345 // the main window, the passed in WebRect is saved as fake_rect_ and we return | |
346 // it instead of the real window dimensions whenever rootWindowRect() is | |
347 // called. | |
348 WebKit::WebRect fake_window_rect(); | |
349 void set_fake_window_rect(const WebKit::WebRect&); | |
350 | |
351 WebWidgetHost* GetWidgetHost(); | |
352 | |
353 void UpdateForCommittedLoad(WebKit::WebFrame* frame, bool is_new_navigation); | |
354 void UpdateURL(WebKit::WebFrame* frame); | |
355 void UpdateSessionHistory(WebKit::WebFrame* frame); | |
356 void UpdateSelectionClipboard(bool is_empty_selection); | |
357 | |
358 // Get a string suitable for dumping a frame to the console. | |
359 base::string16 GetFrameDescription(WebKit::WebFrame* webframe); | |
360 | |
361 // Causes navigation actions just printout the intended navigation instead | |
362 // of taking you to the page. This is used for cases like mailto, where you | |
363 // don't actually want to open the mail program. | |
364 bool policy_delegate_enabled_; | |
365 | |
366 // Toggles the behavior of the policy delegate. If true, then navigations | |
367 // will be allowed. Otherwise, they will be ignored (dropped). | |
368 bool policy_delegate_is_permissive_; | |
369 | |
370 // If true, the policy delegate will signal layout test completion. | |
371 bool policy_delegate_should_notify_done_; | |
372 | |
373 // Non-owning pointer. The delegate is owned by the host. | |
374 TestShell* shell_; | |
375 | |
376 // For tracking session history. See RenderView. | |
377 int page_id_; | |
378 int last_page_id_updated_; | |
379 | |
380 scoped_ptr<TestShellExtraData> pending_extra_data_; | |
381 | |
382 // Maps resource identifiers to a descriptive string. | |
383 typedef std::map<uint32, std::string> ResourceMap; | |
384 ResourceMap resource_identifier_map_; | |
385 std::string GetResourceDescription(uint32 identifier); | |
386 | |
387 scoped_ptr<WebKit::WebContextMenuData> last_context_menu_data_; | |
388 | |
389 WebCursor current_cursor_; | |
390 | |
391 WebKit::WebRect fake_rect_; | |
392 bool using_fake_rect_; | |
393 | |
394 #if defined(OS_WIN) | |
395 // Classes needed by drag and drop. | |
396 scoped_refptr<TestDropDelegate> drop_delegate_; | |
397 #endif | |
398 | |
399 #if defined(TOOLKIT_GTK) | |
400 // The type of cursor the window is currently using. | |
401 // Used for judging whether a new SetCursor call is actually changing the | |
402 // cursor. | |
403 GdkCursorType cursor_type_; | |
404 #endif | |
405 | |
406 #if defined(OS_MACOSX) | |
407 scoped_ptr<WebKit::WebPopupMenuInfo> popup_menu_info_; | |
408 WebKit::WebRect popup_bounds_; | |
409 #endif | |
410 | |
411 // Set of headers to clear in willSendRequest. | |
412 std::set<std::string> clear_headers_; | |
413 | |
414 // true if we should block any redirects | |
415 bool block_redirects_; | |
416 | |
417 // true if we should block (set an empty request for) any requests | |
418 bool request_return_null_; | |
419 | |
420 // Edit command associated to the current keyboard event. | |
421 std::string edit_command_name_; | |
422 std::string edit_command_value_; | |
423 | |
424 // The mock spellchecker used in TestWebViewDelegate::spellCheck(). | |
425 MockSpellCheck mock_spellcheck_; | |
426 | |
427 DISALLOW_COPY_AND_ASSIGN(TestWebViewDelegate); | |
428 }; | |
429 | |
430 #endif // WEBKIT_TOOLS_TEST_SHELL_TEST_WEBVIEW_DELEGATE_H_ | |
OLD | NEW |