1 /* 2 * Copyright (C) 2010 Apple 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "PlatformUtilities.h" 27 28 #include <WebKit2/WKStringCF.h> 29 #include <WebKit2/WKURLCF.h> 30 #include <wtf/RetainPtr.h> 31 32 #if USE(CFNETWORK) 33 #include <WebKit2/WKURLResponseCF.h> 34 #endif 35 36 namespace TestWebKitAPI { 37 namespace Util { 38 39 #ifdef DEBUG_ALL 40 const char* injectedBundleDLL = "TestWebKitAPIInjectedBundle_debug.dll"; 41 #else 42 const char* injectedBundleDLL = "TestWebKitAPIInjectedBundle.dll"; 43 #endif 44 45 void run(bool* done) 46 { 47 while (!*done) { 48 MSG msg; 49 BOOL result = ::GetMessageW(&msg, 0, 0, 0); 50 if (!result || result == -1) 51 break; 52 53 if (shouldTranslateMessage(msg)) 54 ::TranslateMessage(&msg); 55 ::DispatchMessage(&msg); 56 } 57 } 58 59 bool shouldTranslateMessage(const MSG& msg) 60 { 61 // Only these four messages are actually translated by ::TranslateMessage or ::TranslateAccelerator. 62 // It's useless (though harmless) to call those functions for other messages, so we always allow other messages to be translated. 63 if (msg.message != WM_KEYDOWN && msg.message != WM_SYSKEYDOWN && msg.message != WM_KEYUP && msg.message != WM_SYSKEYUP) 64 return true; 65 66 wchar_t className[256]; 67 if (!::GetClassNameW(msg.hwnd, className, ARRAYSIZE(className))) 68 return true; 69 70 // Don't call TranslateMessage() on key events destined for a WebKit2 view, WebKit will do this if it doesn't handle the message. 71 // It would be nice to use some API here instead of hard-coding the window class name. 72 return wcscmp(className, L"WebKit2WebViewWindowClass"); 73 } 74 75 void sleep(double seconds) 76 { 77 ::Sleep(seconds * 1000); 78 } 79 80 RetainPtr<CFStringRef> cf(const char* utf8String) 81 { 82 return RetainPtr<CFStringRef>(AdoptCF, CFStringCreateWithCString(kCFAllocatorDefault, utf8String, kCFStringEncodingUTF8)); 83 } 84 85 WKStringRef createInjectedBundlePath() 86 { 87 RetainPtr<CFURLRef> executableURL(AdoptCF, CFBundleCopyExecutableURL(CFBundleGetMainBundle())); 88 RetainPtr<CFURLRef> executableContainerURL(AdoptCF, CFURLCreateCopyDeletingLastPathComponent(0, executableURL.get())); 89 RetainPtr<CFStringRef> dllFilename(AdoptCF, CFStringCreateWithCStringNoCopy(0, injectedBundleDLL, kCFStringEncodingWindowsLatin1, kCFAllocatorNull)); 90 RetainPtr<CFURLRef> bundleURL(AdoptCF, CFURLCreateCopyAppendingPathComponent(0, executableContainerURL.get(), dllFilename.get(), false)); 91 RetainPtr<CFStringRef> bundlePath(AdoptCF, CFURLCopyFileSystemPath(bundleURL.get(), kCFURLWindowsPathStyle)); 92 return WKStringCreateWithCFString(bundlePath.get()); 93 } 94 95 WKURLRef createURLForResource(const char* resource, const char* extension) 96 { 97 RetainPtr<CFURLRef> url(AdoptCF, CFBundleCopyResourceURL(CFBundleGetMainBundle(), cf(resource).get(), cf(extension).get(), 0)); 98 return WKURLCreateWithCFURL(url.get()); 99 } 100 101 WKURLRef URLForNonExistentResource() 102 { 103 return WKURLCreateWithUTF8CString("file:///does-not-exist.html"); 104 } 105 106 WKRetainPtr<WKStringRef> MIMETypeForWKURLResponse(WKURLResponseRef wkResponse) 107 { 108 #if USE(CFNETWORK) 109 RetainPtr<CFURLResponseRef> response(AdoptCF, WKURLResponseCopyCFURLResponse(0, wkResponse)); 110 return adoptWK(WKStringCreateWithCFString(CFURLResponseGetMIMEType(response.get()))); 111 #else 112 return 0; 113 #endif 114 } 115 116 bool isKeyDown(WKNativeEventPtr event) 117 { 118 return event->message == WM_KEYDOWN; 119 } 120 121 } // namespace Util 122 } // namespace TestWebKitAPI 123