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 WebViewHost_h 32 #define WebViewHost_h 33 34 #include "MockSpellCheck.h" 35 #include "Task.h" 36 #include "TestNavigationController.h" 37 #include "WebAccessibilityNotification.h" 38 #include "WebCursorInfo.h" 39 #include "WebFrameClient.h" 40 #include "WebSpellCheckClient.h" 41 #include "WebViewClient.h" 42 #include <wtf/HashMap.h> 43 #include <wtf/HashSet.h> 44 #include <wtf/Vector.h> 45 #include <wtf/text/WTFString.h> 46 47 class LayoutTestController; 48 class SkCanvas; 49 class TestShell; 50 51 namespace WebKit { 52 class WebFrame; 53 class WebDeviceOrientationClient; 54 class WebDeviceOrientationClientMock; 55 class WebGeolocationClient; 56 class WebGeolocationClientMock; 57 class WebGeolocationServiceMock; 58 class WebSpeechInputController; 59 class WebSpeechInputControllerMock; 60 class WebSpeechInputListener; 61 class WebURL; 62 struct WebRect; 63 struct WebURLError; 64 struct WebWindowFeatures; 65 } 66 67 class WebViewHost : public WebKit::WebSpellCheckClient, public WebKit::WebViewClient, public WebKit::WebFrameClient, public NavigationHost { 68 public: 69 WebViewHost(TestShell* shell); 70 ~WebViewHost(); 71 void setWebWidget(WebKit::WebWidget*); 72 WebKit::WebView* webView() const; 73 WebKit::WebWidget* webWidget() const; 74 void reset(); 75 void setSelectTrailingWhitespaceEnabled(bool); 76 void setSmartInsertDeleteEnabled(bool); 77 void waitForPolicyDelegate(); 78 void setCustomPolicyDelegate(bool, bool); 79 WebKit::WebFrame* topLoadingFrame() { return m_topLoadingFrame; } 80 void setBlockRedirects(bool block) { m_blocksRedirects = block; } 81 void setRequestReturnNull(bool returnNull) { m_requestReturnNull = returnNull; } 82 void setEditCommand(const std::string& name, const std::string& value); 83 void clearEditCommand(); 84 void setPendingExtraData(TestShellExtraData*); 85 86 void paintRect(const WebKit::WebRect&); 87 void updatePaintRect(const WebKit::WebRect&); 88 void paintInvalidatedRegion(); 89 SkCanvas* canvas(); 90 void displayRepaintMask(); 91 92 void loadURLForFrame(const WebKit::WebURL&, const WebKit::WebString& frameName); 93 TestNavigationController* navigationController() { return m_navigationController.get(); } 94 95 void addClearHeader(const WTF::String& header) { m_clearHeaders.add(header); } 96 const HashSet<WTF::String>& clearHeaders() const { return m_clearHeaders; } 97 void closeWidget(); 98 99 WebKit::WebContextMenuData* lastContextMenuData() const; 100 void clearContextMenuData(); 101 102 WebKit::WebSpeechInputControllerMock* speechInputControllerMock() { return m_speechInputControllerMock.get(); } 103 104 // NavigationHost 105 virtual bool navigate(const TestNavigationEntry&, bool reload); 106 107 // WebKit::WebSpellCheckClient 108 virtual void spellCheck(const WebKit::WebString&, int& offset, int& length, WebKit::WebVector<WebKit::WebString>* optionalSuggestions); 109 virtual void requestCheckingOfText(const WebKit::WebString&, WebKit::WebTextCheckingCompletion*); 110 virtual WebKit::WebString autoCorrectWord(const WebKit::WebString&); 111 112 // WebKit::WebViewClient 113 virtual WebKit::WebView* createView(WebKit::WebFrame*, const WebKit::WebURLRequest&, const WebKit::WebWindowFeatures&, const WebKit::WebString&); 114 virtual WebKit::WebWidget* createPopupMenu(WebKit::WebPopupType); 115 virtual WebKit::WebWidget* createPopupMenu(const WebKit::WebPopupMenuInfo&); 116 virtual WebKit::WebStorageNamespace* createSessionStorageNamespace(unsigned quota); 117 virtual void didAddMessageToConsole(const WebKit::WebConsoleMessage&, const WebKit::WebString& sourceName, unsigned sourceLine); 118 virtual void didStartLoading(); 119 virtual void didStopLoading(); 120 virtual bool shouldBeginEditing(const WebKit::WebRange&); 121 virtual bool shouldEndEditing(const WebKit::WebRange&); 122 virtual bool shouldInsertNode(const WebKit::WebNode&, const WebKit::WebRange&, WebKit::WebEditingAction); 123 virtual bool shouldInsertText(const WebKit::WebString&, const WebKit::WebRange&, WebKit::WebEditingAction); 124 virtual bool shouldChangeSelectedRange(const WebKit::WebRange& from, const WebKit::WebRange& to, WebKit::WebTextAffinity, bool stillSelecting); 125 virtual bool shouldDeleteRange(const WebKit::WebRange&); 126 virtual bool shouldApplyStyle(const WebKit::WebString& style, const WebKit::WebRange&); 127 virtual bool isSmartInsertDeleteEnabled(); 128 virtual bool isSelectTrailingWhitespaceEnabled(); 129 virtual void didBeginEditing(); 130 virtual void didChangeSelection(bool isSelectionEmpty); 131 virtual void didChangeContents(); 132 virtual void didEndEditing(); 133 virtual bool handleCurrentKeyboardEvent(); 134 virtual void runModalAlertDialog(WebKit::WebFrame*, const WebKit::WebString&); 135 virtual bool runModalConfirmDialog(WebKit::WebFrame*, const WebKit::WebString&); 136 virtual bool runModalPromptDialog(WebKit::WebFrame*, const WebKit::WebString& message, const WebKit::WebString& defaultValue, WebKit::WebString* actualValue); 137 virtual bool runModalBeforeUnloadDialog(WebKit::WebFrame*, const WebKit::WebString&); 138 virtual void showContextMenu(WebKit::WebFrame*, const WebKit::WebContextMenuData&); 139 virtual void setStatusText(const WebKit::WebString&); 140 virtual void startDragging(const WebKit::WebDragData&, WebKit::WebDragOperationsMask, const WebKit::WebImage&, const WebKit::WebPoint&); 141 virtual void navigateBackForwardSoon(int offset); 142 virtual int historyBackListCount(); 143 virtual int historyForwardListCount(); 144 virtual void postAccessibilityNotification(const WebKit::WebAccessibilityObject&, WebKit::WebAccessibilityNotification); 145 virtual WebKit::WebNotificationPresenter* notificationPresenter(); 146 virtual WebKit::WebGeolocationClient* geolocationClient(); 147 virtual WebKit::WebSpeechInputController* speechInputController(WebKit::WebSpeechInputListener*); 148 virtual WebKit::WebDeviceOrientationClient* deviceOrientationClient(); 149 150 // WebKit::WebWidgetClient 151 virtual void didInvalidateRect(const WebKit::WebRect&); 152 virtual void didScrollRect(int dx, int dy, const WebKit::WebRect&); 153 virtual void scheduleComposite(); 154 #if ENABLE(REQUEST_ANIMATION_FRAME) 155 virtual void scheduleAnimation(); 156 #endif 157 virtual void didFocus(); 158 virtual void didBlur(); 159 virtual void didChangeCursor(const WebKit::WebCursorInfo&); 160 virtual void closeWidgetSoon(); 161 virtual void show(WebKit::WebNavigationPolicy); 162 virtual void runModal(); 163 virtual WebKit::WebRect windowRect(); 164 virtual void setWindowRect(const WebKit::WebRect&); 165 virtual WebKit::WebRect rootWindowRect(); 166 virtual WebKit::WebRect windowResizerRect(); 167 virtual WebKit::WebScreenInfo screenInfo(); 168 169 // WebKit::WebFrameClient 170 virtual WebKit::WebPlugin* createPlugin(WebKit::WebFrame*, const WebKit::WebPluginParams&); 171 virtual WebKit::WebWorker* createWorker(WebKit::WebFrame*, WebKit::WebWorkerClient*); 172 virtual WebKit::WebMediaPlayer* createMediaPlayer(WebKit::WebFrame*, WebKit::WebMediaPlayerClient*); 173 virtual WebKit::WebApplicationCacheHost* createApplicationCacheHost(WebKit::WebFrame*, WebKit::WebApplicationCacheHostClient*); 174 virtual bool allowPlugins(WebKit::WebFrame*, bool enabledPerSettings); 175 virtual bool allowImages(WebKit::WebFrame*, bool enabledPerSettings); 176 virtual void loadURLExternally(WebKit::WebFrame*, const WebKit::WebURLRequest&, WebKit::WebNavigationPolicy); 177 virtual WebKit::WebNavigationPolicy decidePolicyForNavigation( 178 WebKit::WebFrame*, const WebKit::WebURLRequest&, 179 WebKit::WebNavigationType, const WebKit::WebNode&, 180 WebKit::WebNavigationPolicy, bool isRedirect); 181 virtual bool canHandleRequest(WebKit::WebFrame*, const WebKit::WebURLRequest&); 182 virtual WebKit::WebURLError cannotHandleRequestError(WebKit::WebFrame*, const WebKit::WebURLRequest&); 183 virtual WebKit::WebURLError cancelledError(WebKit::WebFrame*, const WebKit::WebURLRequest&); 184 virtual void unableToImplementPolicyWithError(WebKit::WebFrame*, const WebKit::WebURLError&); 185 virtual void willPerformClientRedirect( 186 WebKit::WebFrame*, const WebKit::WebURL& from, const WebKit::WebURL& to, 187 double interval, double fireTime); 188 virtual void didCancelClientRedirect(WebKit::WebFrame*); 189 virtual void didCreateDataSource(WebKit::WebFrame*, WebKit::WebDataSource*); 190 virtual void didStartProvisionalLoad(WebKit::WebFrame*); 191 virtual void didReceiveServerRedirectForProvisionalLoad(WebKit::WebFrame*); 192 virtual void didFailProvisionalLoad(WebKit::WebFrame*, const WebKit::WebURLError&); 193 virtual void didCommitProvisionalLoad(WebKit::WebFrame*, bool isNewNavigation); 194 virtual void didClearWindowObject(WebKit::WebFrame*); 195 virtual void didReceiveTitle(WebKit::WebFrame*, const WebKit::WebString&, WebKit::WebTextDirection); 196 virtual void didFinishDocumentLoad(WebKit::WebFrame*); 197 virtual void didHandleOnloadEvents(WebKit::WebFrame*); 198 virtual void didFailLoad(WebKit::WebFrame*, const WebKit::WebURLError&); 199 virtual void didFinishLoad(WebKit::WebFrame*); 200 virtual void didNavigateWithinPage(WebKit::WebFrame*, bool isNewNavigation); 201 virtual void didChangeLocationWithinPage(WebKit::WebFrame*); 202 virtual void assignIdentifierToRequest(WebKit::WebFrame*, unsigned identifier, const WebKit::WebURLRequest&); 203 virtual void removeIdentifierForRequest(unsigned identifier); 204 virtual void willSendRequest(WebKit::WebFrame*, unsigned identifier, WebKit::WebURLRequest&, const WebKit::WebURLResponse&); 205 virtual void didReceiveResponse(WebKit::WebFrame*, unsigned identifier, const WebKit::WebURLResponse&); 206 virtual void didFinishResourceLoad(WebKit::WebFrame*, unsigned identifier); 207 virtual void didFailResourceLoad(WebKit::WebFrame*, unsigned identifier, const WebKit::WebURLError&); 208 virtual void didDisplayInsecureContent(WebKit::WebFrame*); 209 virtual void didRunInsecureContent(WebKit::WebFrame*, const WebKit::WebSecurityOrigin&, const WebKit::WebURL&); 210 virtual bool allowScript(WebKit::WebFrame*, bool enabledPerSettings); 211 virtual void openFileSystem(WebKit::WebFrame*, WebKit::WebFileSystem::Type, long long size, bool create, WebKit::WebFileSystemCallbacks*); 212 213 WebKit::WebDeviceOrientationClientMock* deviceOrientationClientMock(); 214 215 // Spellcheck related helper APIs 216 MockSpellCheck* mockSpellCheck(); 217 void finishLastTextCheck(); 218 219 // Geolocation client mocks for LayoutTestController 220 WebKit::WebGeolocationClientMock* geolocationClientMock(); 221 222 // Pending task list, Note taht the method is referred from MethodTask class. 223 TaskList* taskList() { return &m_taskList; } 224 225 private: 226 227 class HostMethodTask : public MethodTask<WebViewHost> { 228 public: 229 typedef void (WebViewHost::*CallbackMethodType)(); 230 HostMethodTask(WebViewHost* object, CallbackMethodType callback) 231 : MethodTask<WebViewHost>(object) 232 , m_callback(callback) 233 { } 234 235 virtual void runIfValid() { (m_object->*m_callback)(); } 236 237 private: 238 CallbackMethodType m_callback; 239 }; 240 241 LayoutTestController* layoutTestController() const; 242 243 // Called the title of the page changes. 244 // Can be used to update the title of the window. 245 void setPageTitle(const WebKit::WebString&); 246 247 // Called when the URL of the page changes. 248 // Extracts the URL and forwards on to SetAddressBarURL(). 249 void updateAddressBar(WebKit::WebView*); 250 251 // Called when the URL of the page changes. 252 // Should be used to update the text of the URL bar. 253 void setAddressBarURL(const WebKit::WebURL&); 254 255 // In the Mac code, this is called to trigger the end of a test after the 256 // page has finished loading. From here, we can generate the dump for the 257 // test. 258 void locationChangeDone(WebKit::WebFrame*); 259 260 void updateForCommittedLoad(WebKit::WebFrame*, bool isNewNavigation); 261 void updateURL(WebKit::WebFrame*); 262 void updateSessionHistory(WebKit::WebFrame*); 263 264 // Dumping a frame to the console. 265 void printFrameDescription(WebKit::WebFrame*); 266 267 // Dumping the user gesture status to the console. 268 void printFrameUserGestureStatus(WebKit::WebFrame*, const char*); 269 270 bool hasWindow() const { return m_hasWindow; } 271 void resetScrollRect(); 272 void discardBackingStore(); 273 274 // Causes navigation actions just printout the intended navigation instead 275 // of taking you to the page. This is used for cases like mailto, where you 276 // don't actually want to open the mail program. 277 bool m_policyDelegateEnabled; 278 279 // Toggles the behavior of the policy delegate. If true, then navigations 280 // will be allowed. Otherwise, they will be ignored (dropped). 281 bool m_policyDelegateIsPermissive; 282 283 // If true, the policy delegate will signal layout test completion. 284 bool m_policyDelegateShouldNotifyDone; 285 286 // Non-owning pointer. The WebViewHost instance is owned by this TestShell instance. 287 TestShell* m_shell; 288 289 // This delegate works for the following widget. 290 WebKit::WebWidget* m_webWidget; 291 292 // This is non-0 IFF a load is in progress. 293 WebKit::WebFrame* m_topLoadingFrame; 294 295 // For tracking session history. See RenderView. 296 int m_pageId; 297 int m_lastPageIdUpdated; 298 299 OwnPtr<TestShellExtraData> m_pendingExtraData; 300 301 // Maps resource identifiers to a descriptive string. 302 typedef HashMap<unsigned, std::string> ResourceMap; 303 ResourceMap m_resourceIdentifierMap; 304 void printResourceDescription(unsigned identifier); 305 306 WebKit::WebCursorInfo m_currentCursor; 307 308 bool m_hasWindow; 309 bool m_inModalLoop; 310 WebKit::WebRect m_windowRect; 311 312 // true if we want to enable smart insert/delete. 313 bool m_smartInsertDeleteEnabled; 314 315 // true if we want to enable selection of trailing whitespaces 316 bool m_selectTrailingWhitespaceEnabled; 317 318 // Set of headers to clear in willSendRequest. 319 HashSet<WTF::String> m_clearHeaders; 320 321 // true if we should block any redirects 322 bool m_blocksRedirects; 323 324 // true if we should block (set an empty request for) any requests 325 bool m_requestReturnNull; 326 327 // Edit command associated to the current keyboard event. 328 std::string m_editCommandName; 329 std::string m_editCommandValue; 330 331 // The mock spellchecker used in spellCheck(). 332 MockSpellCheck m_spellcheck; 333 334 // Painting. 335 OwnPtr<SkCanvas> m_canvas; 336 WebKit::WebRect m_paintRect; 337 bool m_isPainting; 338 339 OwnPtr<WebKit::WebContextMenuData> m_lastContextMenuData; 340 341 // Geolocation 342 OwnPtr<WebKit::WebGeolocationClientMock> m_geolocationClientMock; 343 344 OwnPtr<WebKit::WebDeviceOrientationClientMock> m_deviceOrientationClientMock; 345 OwnPtr<WebKit::WebSpeechInputControllerMock> m_speechInputControllerMock; 346 347 OwnPtr<TestNavigationController*> m_navigationController; 348 349 WebKit::WebString m_lastRequestedTextCheckString; 350 WebKit::WebTextCheckingCompletion* m_lastRequestedTextCheckingCompletion; 351 352 TaskList m_taskList; 353 }; 354 355 #endif // WebViewHost_h 356