| 1 | /* | 
|---|
| 2 | * Copyright (C) 2014 Igalia S.L. | 
|---|
| 3 | * | 
|---|
| 4 | * This library is free software; you can redistribute it and/or | 
|---|
| 5 | * modify it under the terms of the GNU Lesser General Public | 
|---|
| 6 | * License as published by the Free Software Foundation; either | 
|---|
| 7 | * version 2,1 of the License, or (at your option) any later version. | 
|---|
| 8 | * | 
|---|
| 9 | * This library is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 12 | * Library General Public License for more details. | 
|---|
| 13 | * | 
|---|
| 14 | * You should have received a copy of the GNU Library General Public License | 
|---|
| 15 | * along with this library; see the file COPYING.LIB.  If not, write to | 
|---|
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 
|---|
| 17 | * Boston, MA 02110-1301, USA. | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #include "config.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "TestMain.h" | 
|---|
| 23 | #include "WebKitTestBus.h" | 
|---|
| 24 | #include "WebViewTest.h" | 
|---|
| 25 | #include <wtf/Vector.h> | 
|---|
| 26 |  | 
|---|
| 27 | static const unsigned numViews = 2; | 
|---|
| 28 | static WebKitTestBus* bus; | 
|---|
| 29 |  | 
|---|
| 30 | class MultiprocessTest: public Test { | 
|---|
| 31 | public: | 
|---|
| 32 | MAKE_GLIB_TEST_FIXTURE(MultiprocessTest); | 
|---|
| 33 |  | 
|---|
| 34 | MultiprocessTest() | 
|---|
| 35 | : m_mainLoop(g_main_loop_new(nullptr, TRUE)) | 
|---|
| 36 | , m_initializeWebExtensionsSignalCount(0) | 
|---|
| 37 | , m_webViewBusNames(numViews) | 
|---|
| 38 | , m_webViews(numViews) | 
|---|
| 39 | { | 
|---|
| 40 | webkit_web_context_set_process_model(m_webContext.get(), WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | void initializeWebExtensions() override | 
|---|
| 44 | { | 
|---|
| 45 | Test::initializeWebExtensions(); | 
|---|
| 46 | m_initializeWebExtensionsSignalCount++; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | static void loadChanged(WebKitWebView* webView, WebKitLoadEvent loadEvent, MultiprocessTest* test) | 
|---|
| 50 | { | 
|---|
| 51 | if (loadEvent != WEBKIT_LOAD_FINISHED) | 
|---|
| 52 | return; | 
|---|
| 53 | g_signal_handlers_disconnect_by_func(webView, reinterpret_cast<void*>(loadChanged), test); | 
|---|
| 54 | g_main_loop_quit(test->m_mainLoop); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | void loadWebViewAndWaitUntilLoaded(unsigned index) | 
|---|
| 58 | { | 
|---|
| 59 | g_assert_cmpuint(index, <, numViews); | 
|---|
| 60 |  | 
|---|
| 61 | m_webViews[index] = Test::adoptView(Test::createWebView(m_webContext.get())); | 
|---|
| 62 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(m_webViews[index].get())); | 
|---|
| 63 |  | 
|---|
| 64 | m_webViewBusNames[index] = GUniquePtr<char>(g_strdup_printf( "org.webkit.gtk.WebExtensionTest%u", Test::s_webExtensionID)); | 
|---|
| 65 |  | 
|---|
| 66 | webkit_web_view_load_html(m_webViews[index].get(), "<html></html>", nullptr); | 
|---|
| 67 | g_signal_connect(m_webViews[index].get(), "load-changed", G_CALLBACK(loadChanged), this); | 
|---|
| 68 | g_main_loop_run(m_mainLoop); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | unsigned webProcessPid(unsigned index) | 
|---|
| 72 | { | 
|---|
| 73 | g_assert_cmpuint(index, <, numViews); | 
|---|
| 74 |  | 
|---|
| 75 | GRefPtr<GDBusProxy> proxy = adoptGRef(bus->createProxy(m_webViewBusNames[index].get(), | 
|---|
| 76 | "/org/webkit/gtk/WebExtensionTest", "org.webkit.gtk.WebExtensionTest", m_mainLoop)); | 
|---|
| 77 |  | 
|---|
| 78 | GRefPtr<GVariant> result = adoptGRef(g_dbus_proxy_call_sync( | 
|---|
| 79 | proxy.get(), | 
|---|
| 80 | "GetProcessIdentifier", | 
|---|
| 81 | nullptr, | 
|---|
| 82 | G_DBUS_CALL_FLAGS_NONE, | 
|---|
| 83 | -1, nullptr, nullptr)); | 
|---|
| 84 | g_assert_nonnull(result); | 
|---|
| 85 |  | 
|---|
| 86 | guint32 identifier = 0; | 
|---|
| 87 | g_variant_get(result.get(), "(u)", &identifier); | 
|---|
| 88 | return identifier; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | #if PLATFORM(GTK) | 
|---|
| 92 | static void nameVanishedCallback(GDBusConnection* connection, const gchar* name, gpointer userData) | 
|---|
| 93 | { | 
|---|
| 94 | g_main_loop_quit(static_cast<GMainLoop*>(userData)); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void destroyWebViewAndWaitUntilWebProcessFinishes(unsigned index) | 
|---|
| 98 | { | 
|---|
| 99 | g_assert_cmpuint(index, <, numViews); | 
|---|
| 100 |  | 
|---|
| 101 | unsigned watcherID = g_bus_watch_name_on_connection(bus->connection(), m_webViewBusNames[index].get(), G_BUS_NAME_WATCHER_FLAGS_NONE, | 
|---|
| 102 | nullptr, nameVanishedCallback, m_mainLoop, nullptr); | 
|---|
| 103 | gtk_widget_destroy(GTK_WIDGET(m_webViews[index].get())); | 
|---|
| 104 | g_main_loop_run(m_mainLoop); | 
|---|
| 105 | g_bus_unwatch_name(watcherID); | 
|---|
| 106 | } | 
|---|
| 107 | #endif | 
|---|
| 108 |  | 
|---|
| 109 | GMainLoop* m_mainLoop; | 
|---|
| 110 | unsigned m_initializeWebExtensionsSignalCount; | 
|---|
| 111 | Vector<GUniquePtr<char>, numViews> m_webViewBusNames; | 
|---|
| 112 | Vector<GRefPtr<WebKitWebView>, numViews> m_webViews; | 
|---|
| 113 | }; | 
|---|
| 114 |  | 
|---|
| 115 | static void testProcessPerWebView(MultiprocessTest* test, gconstpointer) | 
|---|
| 116 | { | 
|---|
| 117 | // Create two web views. As we are in multiprocess mode, there must be | 
|---|
| 118 | // two web processes, running an instance of the web extension each. | 
|---|
| 119 | // The initialize-web-extensions must have been called twice, and the | 
|---|
| 120 | // identifiers generated for them must be different (and their reported | 
|---|
| 121 | // process identifiers). | 
|---|
| 122 |  | 
|---|
| 123 | for (unsigned i = 0; i < numViews; i++) { | 
|---|
| 124 | test->loadWebViewAndWaitUntilLoaded(i); | 
|---|
| 125 | g_assert_true(WEBKIT_IS_WEB_VIEW(test->m_webViews[i].get())); | 
|---|
| 126 | g_assert_nonnull(test->m_webViewBusNames[i]); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | g_assert_cmpuint(test->m_initializeWebExtensionsSignalCount, ==, numViews); | 
|---|
| 130 | g_assert_cmpstr(test->m_webViewBusNames[0].get(), !=, test->m_webViewBusNames[1].get()); | 
|---|
| 131 | g_assert_cmpuint(test->webProcessPid(0), !=, test->webProcessPid(1)); | 
|---|
| 132 |  | 
|---|
| 133 | #if PLATFORM(GTK) | 
|---|
| 134 | // Check that web processes finish when the web view is destroyed even when it's not finalized. | 
|---|
| 135 | // See https://bugs.webkit.org/show_bug.cgi?id=129783. | 
|---|
| 136 | for (unsigned i = 0; i < numViews; i++) { | 
|---|
| 137 | GRefPtr<WebKitWebView> webView = test->m_webViews[i]; | 
|---|
| 138 | test->destroyWebViewAndWaitUntilWebProcessFinishes(i); | 
|---|
| 139 | } | 
|---|
| 140 | #endif | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | class UIClientMultiprocessTest: public Test { | 
|---|
| 144 | public: | 
|---|
| 145 | MAKE_GLIB_TEST_FIXTURE(UIClientMultiprocessTest); | 
|---|
| 146 |  | 
|---|
| 147 | enum WebViewEvents { | 
|---|
| 148 | Create, | 
|---|
| 149 | ReadyToShow, | 
|---|
| 150 | Close | 
|---|
| 151 | }; | 
|---|
| 152 |  | 
|---|
| 153 | static WebKitWebView* viewCreateCallback(WebKitWebView* webView, WebKitNavigationAction*, UIClientMultiprocessTest* test) | 
|---|
| 154 | { | 
|---|
| 155 | return test->viewCreate(webView); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | static void viewReadyToShowCallback(WebKitWebView* webView, UIClientMultiprocessTest* test) | 
|---|
| 159 | { | 
|---|
| 160 | test->viewReadyToShow(webView); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | static void viewCloseCallback(WebKitWebView* webView, UIClientMultiprocessTest* test) | 
|---|
| 164 | { | 
|---|
| 165 | test->viewClose(webView); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | UIClientMultiprocessTest() | 
|---|
| 169 | : m_mainLoop(g_main_loop_new(nullptr, TRUE)) | 
|---|
| 170 | , m_initializeWebExtensionsSignalCount(0) | 
|---|
| 171 | { | 
|---|
| 172 | webkit_web_context_set_process_model(m_webContext.get(), WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); | 
|---|
| 173 | m_webView = WEBKIT_WEB_VIEW(Test::createWebView(m_webContext.get())); | 
|---|
| 174 | #if PLATFORM(GTK) | 
|---|
| 175 | g_object_ref_sink(m_webView); | 
|---|
| 176 | #endif | 
|---|
| 177 | webkit_settings_set_javascript_can_open_windows_automatically(webkit_web_view_get_settings(m_webView), TRUE); | 
|---|
| 178 |  | 
|---|
| 179 | g_signal_connect(m_webView, "create", G_CALLBACK(viewCreateCallback), this); | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | ~UIClientMultiprocessTest() | 
|---|
| 183 | { | 
|---|
| 184 | g_signal_handlers_disconnect_matched(m_webView, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this); | 
|---|
| 185 | g_object_unref(m_webView); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | void initializeWebExtensions() override | 
|---|
| 189 | { | 
|---|
| 190 | Test::initializeWebExtensions(); | 
|---|
| 191 | m_initializeWebExtensionsSignalCount++; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | WebKitWebView* viewCreate(WebKitWebView* webView) | 
|---|
| 195 | { | 
|---|
| 196 | g_assert_true(webView == m_webView); | 
|---|
| 197 |  | 
|---|
| 198 | auto* newWebView = Test::createWebView(webView); | 
|---|
| 199 | #if PLATFORM(GTK) | 
|---|
| 200 | g_object_ref_sink(newWebView); | 
|---|
| 201 | #endif | 
|---|
| 202 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(newWebView)); | 
|---|
| 203 | m_webViewEvents.append(Create); | 
|---|
| 204 |  | 
|---|
| 205 | g_signal_connect(newWebView, "ready-to-show", G_CALLBACK(viewReadyToShowCallback), this); | 
|---|
| 206 | g_signal_connect(newWebView, "close", G_CALLBACK(viewCloseCallback), this); | 
|---|
| 207 |  | 
|---|
| 208 | return WEBKIT_WEB_VIEW(newWebView); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | void viewReadyToShow(WebKitWebView* webView) | 
|---|
| 212 | { | 
|---|
| 213 | g_assert_true(m_webView != webView); | 
|---|
| 214 | m_webViewEvents.append(ReadyToShow); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | void viewClose(WebKitWebView* webView) | 
|---|
| 218 | { | 
|---|
| 219 | g_assert_true(m_webView != webView); | 
|---|
| 220 |  | 
|---|
| 221 | m_webViewEvents.append(Close); | 
|---|
| 222 | g_object_unref(webView); | 
|---|
| 223 | g_main_loop_quit(m_mainLoop); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | void waitUntilNewWebViewClose() | 
|---|
| 227 | { | 
|---|
| 228 | g_main_loop_run(m_mainLoop); | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | WebKitWebView* m_webView; | 
|---|
| 232 | GMainLoop* m_mainLoop; | 
|---|
| 233 | unsigned m_initializeWebExtensionsSignalCount; | 
|---|
| 234 | Vector<WebViewEvents> m_webViewEvents; | 
|---|
| 235 | }; | 
|---|
| 236 |  | 
|---|
| 237 | static void testMultiprocessWebViewCreateReadyClose(UIClientMultiprocessTest* test, gconstpointer) | 
|---|
| 238 | { | 
|---|
| 239 | webkit_web_view_load_html(test->m_webView, "<html><body onLoad=\"window.open().close();\"></html>", nullptr); | 
|---|
| 240 | test->waitUntilNewWebViewClose(); | 
|---|
| 241 |  | 
|---|
| 242 | Vector<UIClientMultiprocessTest::WebViewEvents>& events = test->m_webViewEvents; | 
|---|
| 243 | g_assert_cmpint(events.size(), ==, 3); | 
|---|
| 244 | g_assert_cmpint(events[0], ==, UIClientMultiprocessTest::Create); | 
|---|
| 245 | g_assert_cmpint(events[1], ==, UIClientMultiprocessTest::ReadyToShow); | 
|---|
| 246 | g_assert_cmpint(events[2], ==, UIClientMultiprocessTest::Close); | 
|---|
| 247 |  | 
|---|
| 248 | g_assert_cmpuint(test->m_initializeWebExtensionsSignalCount, ==, 1); | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | static void testWebProcessLimit(MultiprocessTest* test, gconstpointer) | 
|---|
| 252 | { | 
|---|
| 253 | g_assert_cmpuint(webkit_web_context_get_web_process_count_limit(test->m_webContext.get()), ==, 0); | 
|---|
| 254 |  | 
|---|
| 255 | webkit_web_context_set_web_process_count_limit(test->m_webContext.get(), 1); | 
|---|
| 256 | g_assert_cmpuint(webkit_web_context_get_web_process_count_limit(test->m_webContext.get()), ==, 1); | 
|---|
| 257 |  | 
|---|
| 258 | // Create two web views but there should be only one web process. | 
|---|
| 259 | for (unsigned i = 0; i < numViews; i++) { | 
|---|
| 260 | test->loadWebViewAndWaitUntilLoaded(i); | 
|---|
| 261 | g_assert_true(WEBKIT_IS_WEB_VIEW(test->m_webViews[i].get())); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | g_assert_cmpuint(test->m_initializeWebExtensionsSignalCount, ==, 1); | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | void beforeAll() | 
|---|
| 268 | { | 
|---|
| 269 | // Check that default setting is the one stated in the documentation | 
|---|
| 270 | g_assert_cmpuint(webkit_web_context_get_process_model(webkit_web_context_get_default()), | 
|---|
| 271 | ==, WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS); | 
|---|
| 272 |  | 
|---|
| 273 | webkit_web_context_set_process_model(webkit_web_context_get_default(), | 
|---|
| 274 | WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); | 
|---|
| 275 |  | 
|---|
| 276 | // Check that the getter returns the newly-set value | 
|---|
| 277 | g_assert_cmpuint(webkit_web_context_get_process_model(webkit_web_context_get_default()), | 
|---|
| 278 | ==, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); | 
|---|
| 279 |  | 
|---|
| 280 | bus = new WebKitTestBus(); | 
|---|
| 281 | if (!bus->run()) | 
|---|
| 282 | return; | 
|---|
| 283 |  | 
|---|
| 284 | MultiprocessTest::add( "WebKitWebContext", "process-per-web-view", testProcessPerWebView); | 
|---|
| 285 | UIClientMultiprocessTest::add( "WebKitWebView", "multiprocess-create-ready-close", testMultiprocessWebViewCreateReadyClose); | 
|---|
| 286 | MultiprocessTest::add( "WebKitWebContext", "web-process-limit", testWebProcessLimit); | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | void afterAll() | 
|---|
| 290 | { | 
|---|
| 291 | delete bus; | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|