| 1 | /* |
| 2 | * Copyright (C) 2013, 2017 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 Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 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 | #include "WebKitInjectedBundleClient.h" |
| 22 | |
| 23 | #include "APIInjectedBundleClient.h" |
| 24 | #include "WebImage.h" |
| 25 | #include "WebKitPrivate.h" |
| 26 | #include "WebKitURIRequestPrivate.h" |
| 27 | #include "WebKitURIResponsePrivate.h" |
| 28 | #include "WebKitWebContextPrivate.h" |
| 29 | #include "WebKitWebResourcePrivate.h" |
| 30 | #include "WebKitWebViewPrivate.h" |
| 31 | #include <wtf/glib/GUniquePtr.h> |
| 32 | |
| 33 | using namespace WebKit; |
| 34 | using namespace WebCore; |
| 35 | |
| 36 | class WebKitInjectedBundleClient final : public API::InjectedBundleClient { |
| 37 | public: |
| 38 | explicit WebKitInjectedBundleClient(WebKitWebContext* webContext) |
| 39 | : m_webContext(webContext) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | static void didReceiveWebViewMessageFromInjectedBundle(WebKitWebView* webView, const char* messageName, API::Dictionary& message) |
| 45 | { |
| 46 | if (g_str_equal(messageName, "DidInitiateLoadForResource" )) { |
| 47 | WebFrameProxy* frame = static_cast<WebFrameProxy*>(message.get(String::fromUTF8("Frame" ))); |
| 48 | API::UInt64* resourceIdentifier = static_cast<API::UInt64*>(message.get(String::fromUTF8("Identifier" ))); |
| 49 | API::URLRequest* webRequest = static_cast<API::URLRequest*>(message.get(String::fromUTF8("Request" ))); |
| 50 | GRefPtr<WebKitURIRequest> request = adoptGRef(webkitURIRequestCreateForResourceRequest(webRequest->resourceRequest())); |
| 51 | |
| 52 | webkitWebViewResourceLoadStarted(webView, frame, resourceIdentifier->value(), request.get()); |
| 53 | } else if (g_str_equal(messageName, "DidSendRequestForResource" )) { |
| 54 | API::UInt64* resourceIdentifier = static_cast<API::UInt64*>(message.get(String::fromUTF8("Identifier" ))); |
| 55 | GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); |
| 56 | if (!resource) |
| 57 | return; |
| 58 | |
| 59 | API::URLRequest* webRequest = static_cast<API::URLRequest*>(message.get(String::fromUTF8("Request" ))); |
| 60 | GRefPtr<WebKitURIRequest> request = adoptGRef(webkitURIRequestCreateForResourceRequest(webRequest->resourceRequest())); |
| 61 | API::URLResponse* webRedirectResponse = static_cast<API::URLResponse*>(message.get(String::fromUTF8("RedirectResponse" ))); |
| 62 | GRefPtr<WebKitURIResponse> redirectResponse = webRedirectResponse ? adoptGRef(webkitURIResponseCreateForResourceResponse(webRedirectResponse->resourceResponse())) : 0; |
| 63 | |
| 64 | webkitWebResourceSentRequest(resource.get(), request.get(), redirectResponse.get()); |
| 65 | } else if (g_str_equal(messageName, "DidReceiveResponseForResource" )) { |
| 66 | API::UInt64* resourceIdentifier = static_cast<API::UInt64*>(message.get(String::fromUTF8("Identifier" ))); |
| 67 | GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); |
| 68 | if (!resource) |
| 69 | return; |
| 70 | |
| 71 | API::URLResponse* webResponse = static_cast<API::URLResponse*>(message.get(String::fromUTF8("Response" ))); |
| 72 | GRefPtr<WebKitURIResponse> response = adoptGRef(webkitURIResponseCreateForResourceResponse(webResponse->resourceResponse())); |
| 73 | |
| 74 | webkitWebResourceSetResponse(resource.get(), response.get()); |
| 75 | } else if (g_str_equal(messageName, "DidReceiveContentLengthForResource" )) { |
| 76 | API::UInt64* resourceIdentifier = static_cast<API::UInt64*>(message.get(String::fromUTF8("Identifier" ))); |
| 77 | GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); |
| 78 | if (!resource) |
| 79 | return; |
| 80 | |
| 81 | API::UInt64* contentLength = static_cast<API::UInt64*>(message.get(String::fromUTF8("ContentLength" ))); |
| 82 | webkitWebResourceNotifyProgress(resource.get(), contentLength->value()); |
| 83 | } else if (g_str_equal(messageName, "DidFinishLoadForResource" )) { |
| 84 | API::UInt64* resourceIdentifier = static_cast<API::UInt64*>(message.get(String::fromUTF8("Identifier" ))); |
| 85 | GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); |
| 86 | if (!resource) |
| 87 | return; |
| 88 | |
| 89 | webkitWebResourceFinished(resource.get()); |
| 90 | webkitWebViewRemoveLoadingWebResource(webView, resourceIdentifier->value()); |
| 91 | } else if (g_str_equal(messageName, "DidFailLoadForResource" )) { |
| 92 | API::UInt64* resourceIdentifier = static_cast<API::UInt64*>(message.get(String::fromUTF8("Identifier" ))); |
| 93 | GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); |
| 94 | if (!resource) |
| 95 | return; |
| 96 | |
| 97 | API::Error* webError = static_cast<API::Error*>(message.get(String::fromUTF8("Error" ))); |
| 98 | const ResourceError& platformError = webError->platformError(); |
| 99 | GUniquePtr<GError> resourceError(g_error_new_literal(g_quark_from_string(platformError.domain().utf8().data()), |
| 100 | toWebKitError(platformError.errorCode()), platformError.localizedDescription().utf8().data())); |
| 101 | if (platformError.tlsErrors()) |
| 102 | webkitWebResourceFailedWithTLSErrors(resource.get(), static_cast<GTlsCertificateFlags>(platformError.tlsErrors()), platformError.certificate()); |
| 103 | else |
| 104 | webkitWebResourceFailed(resource.get(), resourceError.get()); |
| 105 | |
| 106 | webkitWebViewRemoveLoadingWebResource(webView, resourceIdentifier->value()); |
| 107 | #if PLATFORM(GTK) |
| 108 | } else if (g_str_equal(messageName, "DidGetSnapshot" )) { |
| 109 | API::UInt64* callbackID = static_cast<API::UInt64*>(message.get("CallbackID" )); |
| 110 | WebImage* image = static_cast<WebImage*>(message.get("Snapshot" )); |
| 111 | webKitWebViewDidReceiveSnapshot(webView, callbackID->value(), image); |
| 112 | #endif |
| 113 | } else |
| 114 | ASSERT_NOT_REACHED(); |
| 115 | } |
| 116 | |
| 117 | void didReceiveMessageFromInjectedBundle(WebProcessPool&, const String& messageName, API::Object* messageBody) override |
| 118 | { |
| 119 | ASSERT(messageBody->type() == API::Object::Type::Dictionary); |
| 120 | API::Dictionary& message = *static_cast<API::Dictionary*>(messageBody); |
| 121 | |
| 122 | CString messageNameUTF8 = messageName.utf8(); |
| 123 | if (g_str_has_prefix(messageNameUTF8.data(), "WebPage." )) { |
| 124 | WebPageProxy* page = static_cast<WebPageProxy*>(message.get(String::fromUTF8("Page" ))); |
| 125 | WebKitWebView* webView = webkitWebContextGetWebViewForPage(m_webContext, page); |
| 126 | if (!webView) |
| 127 | return; |
| 128 | |
| 129 | didReceiveWebViewMessageFromInjectedBundle(webView, messageNameUTF8.data() + strlen("WebPage." ), message); |
| 130 | } else |
| 131 | ASSERT_NOT_REACHED(); |
| 132 | } |
| 133 | |
| 134 | RefPtr<API::Object> getInjectedBundleInitializationUserData(WebProcessPool&) override |
| 135 | { |
| 136 | GRefPtr<GVariant> data = webkitWebContextInitializeWebExtensions(m_webContext); |
| 137 | GUniquePtr<gchar> dataString(g_variant_print(data.get(), TRUE)); |
| 138 | return API::String::create(String::fromUTF8(dataString.get())); |
| 139 | } |
| 140 | |
| 141 | WebKitWebContext* m_webContext; |
| 142 | }; |
| 143 | |
| 144 | void attachInjectedBundleClientToContext(WebKitWebContext* webContext) |
| 145 | { |
| 146 | webkitWebContextGetProcessPool(webContext).setInjectedBundleClient(std::make_unique<WebKitInjectedBundleClient>(webContext)); |
| 147 | } |
| 148 | |