| 1 | /* |
| 2 | * Copyright (C) 2013 Adenilson Cavalcanti <cavalcantii@gmail.com> |
| 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 "config.h" |
| 27 | |
| 28 | #if WK_HAVE_C_SPI |
| 29 | |
| 30 | #include "PlatformUtilities.h" |
| 31 | #include "PlatformWebView.h" |
| 32 | #include "Test.h" |
| 33 | #include <WebKit/WKPagePrivate.h> |
| 34 | #include <WebKit/WKRetainPtr.h> |
| 35 | #include <signal.h> |
| 36 | |
| 37 | namespace TestWebKitAPI { |
| 38 | |
| 39 | static bool loadBeforeCrash = false; |
| 40 | static bool loadAfterCrash = false; |
| 41 | static bool calledCrashHandler = false; |
| 42 | |
| 43 | static void didFinishLoad(WKPageRef page, WKNavigationRef, WKTypeRef userData, const void* clientInfo) |
| 44 | { |
| 45 | // First load before WebProcess was terminated. |
| 46 | if (!loadBeforeCrash) { |
| 47 | loadBeforeCrash = true; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Next load after WebProcess was terminated (hopefully |
| 52 | // it will be correctly re-spawned). |
| 53 | EXPECT_EQ(static_cast<uint32_t>(kWKFrameLoadStateFinished), WKFrameGetFrameLoadState(WKPageGetMainFrame(page))); |
| 54 | EXPECT_FALSE(loadAfterCrash); |
| 55 | |
| 56 | // Set it, otherwise the loop will not end. |
| 57 | loadAfterCrash = true; |
| 58 | } |
| 59 | |
| 60 | static void didCrash(WKPageRef page, const void*) |
| 61 | { |
| 62 | // Test if first load actually worked. |
| 63 | EXPECT_TRUE(loadBeforeCrash); |
| 64 | |
| 65 | // Reload should re-spawn webprocess. |
| 66 | WKPageReload(page); |
| 67 | } |
| 68 | |
| 69 | TEST(WebKit, ReloadPageAfterCrash) |
| 70 | { |
| 71 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
| 72 | PlatformWebView webView(context.get()); |
| 73 | |
| 74 | WKPageNavigationClientV0 loaderClient; |
| 75 | memset(&loaderClient, 0, sizeof(loaderClient)); |
| 76 | |
| 77 | loaderClient.base.version = 0; |
| 78 | loaderClient.didFinishNavigation = didFinishLoad; |
| 79 | loaderClient.webProcessDidCrash = didCrash; |
| 80 | |
| 81 | WKPageSetPageNavigationClient(webView.page(), &loaderClient.base); |
| 82 | |
| 83 | WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("about:blank" )); |
| 84 | // Load a blank page and next kills WebProcess. |
| 85 | WKPageLoadURL(webView.page(), url.get()); |
| 86 | Util::run(&loadBeforeCrash); |
| 87 | WKPageTerminate(webView.page()); |
| 88 | |
| 89 | // Let's try load a page and see what happens. |
| 90 | WKPageLoadURL(webView.page(), url.get()); |
| 91 | Util::run(&loadAfterCrash); |
| 92 | } |
| 93 | |
| 94 | #if !PLATFORM(WIN) |
| 95 | |
| 96 | static void nullJavaScriptCallback(WKSerializedScriptValueRef, WKErrorRef, void*) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | static void didCrashCheckFrames(WKPageRef page, const void*) |
| 101 | { |
| 102 | // Test if first load actually worked. |
| 103 | EXPECT_TRUE(loadBeforeCrash); |
| 104 | |
| 105 | EXPECT_TRUE(!WKPageGetMainFrame(page)); |
| 106 | EXPECT_TRUE(!WKPageGetFocusedFrame(page)); |
| 107 | EXPECT_TRUE(!WKPageGetFrameSetLargestFrame(page)); |
| 108 | |
| 109 | calledCrashHandler = true; |
| 110 | } |
| 111 | |
| 112 | TEST(WebKit, FocusedFrameAfterCrash) |
| 113 | { |
| 114 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
| 115 | PlatformWebView webView(context.get()); |
| 116 | |
| 117 | WKPageNavigationClientV0 loaderClient; |
| 118 | memset(&loaderClient, 0, sizeof(loaderClient)); |
| 119 | |
| 120 | loaderClient.base.version = 0; |
| 121 | loaderClient.didFinishNavigation = didFinishLoad; |
| 122 | loaderClient.webProcessDidCrash = didCrashCheckFrames; |
| 123 | |
| 124 | WKPageSetPageNavigationClient(webView.page(), &loaderClient.base); |
| 125 | |
| 126 | WKRetainPtr<WKURLRef> url = adoptWK(Util::createURLForResource("many-iframes" , "html" )); |
| 127 | WKPageLoadURL(webView.page(), url.get()); |
| 128 | Util::run(&loadBeforeCrash); |
| 129 | |
| 130 | EXPECT_FALSE(!WKPageGetMainFrame(webView.page())); |
| 131 | |
| 132 | WKRetainPtr<WKStringRef> javaScriptString = adoptWK(WKStringCreateWithUTF8CString("frames[2].focus()" )); |
| 133 | WKPageRunJavaScriptInMainFrame(webView.page(), javaScriptString.get(), 0, nullJavaScriptCallback); |
| 134 | |
| 135 | while (!WKPageGetFocusedFrame(webView.page())) |
| 136 | Util::spinRunLoop(10); |
| 137 | |
| 138 | kill(WKPageGetProcessIdentifier(webView.page()), 9); |
| 139 | |
| 140 | Util::run(&calledCrashHandler); |
| 141 | } |
| 142 | |
| 143 | TEST(WebKit, FrameSetLargestFrameAfterCrash) |
| 144 | { |
| 145 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
| 146 | PlatformWebView webView(context.get()); |
| 147 | |
| 148 | WKPageNavigationClientV0 loaderClient; |
| 149 | memset(&loaderClient, 0, sizeof(loaderClient)); |
| 150 | |
| 151 | loaderClient.base.version = 0; |
| 152 | loaderClient.didFinishNavigation = didFinishLoad; |
| 153 | loaderClient.webProcessDidCrash = didCrashCheckFrames; |
| 154 | |
| 155 | WKPageSetPageNavigationClient(webView.page(), &loaderClient.base); |
| 156 | |
| 157 | WKRetainPtr<WKURLRef> baseURL = adoptWK(WKURLCreateWithUTF8CString("about:blank" )); |
| 158 | WKRetainPtr<WKStringRef> htmlString = Util::toWK("<frameset cols='25%,*,25%'><frame src='about:blank'><frame src='about:blank'><frame src='about:blank'></frameset>" ); |
| 159 | |
| 160 | WKPageLoadHTMLString(webView.page(), htmlString.get(), baseURL.get()); |
| 161 | Util::run(&loadBeforeCrash); |
| 162 | |
| 163 | EXPECT_FALSE(!WKPageGetMainFrame(webView.page())); |
| 164 | |
| 165 | while (!WKPageGetFrameSetLargestFrame(webView.page())) |
| 166 | Util::spinRunLoop(10); |
| 167 | |
| 168 | kill(WKPageGetProcessIdentifier(webView.page()), 9); |
| 169 | |
| 170 | Util::run(&calledCrashHandler); |
| 171 | } |
| 172 | |
| 173 | #endif // !PLATFORM(WIN) |
| 174 | |
| 175 | } // namespace TestWebKitAPI |
| 176 | |
| 177 | #endif |
| 178 | |