1/* 2 * Copyright (C) 2013 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 "WebImage.h" 24#include "WebKitURIRequestPrivate.h" 25#include "WebKitURIResponsePrivate.h" 26#include "WebKitWebContextPrivate.h" 27#include "WebKitWebResourcePrivate.h" 28#include "WebKitWebViewPrivate.h" 29#include <wtf/gobject/GOwnPtr.h> 30 31using namespace WebKit; 32using namespace WebCore; 33 34static void didReceiveWebViewMessageFromInjectedBundle(WebKitWebView* webView, const char* messageName, ImmutableDictionary& message) 35{ 36 if (g_str_equal(messageName, "DidInitiateLoadForResource")) { 37 WebFrameProxy* frame = static_cast<WebFrameProxy*>(message.get(String::fromUTF8("Frame"))); 38 WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier"))); 39 WebURLRequest* webRequest = static_cast<WebURLRequest*>(message.get(String::fromUTF8("Request"))); 40 GRefPtr<WebKitURIRequest> request = adoptGRef(webkitURIRequestCreateForResourceRequest(webRequest->resourceRequest())); 41 42 webkitWebViewResourceLoadStarted(webView, frame, resourceIdentifier->value(), request.get()); 43 } else if (g_str_equal(messageName, "DidSendRequestForResource")) { 44 WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier"))); 45 GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); 46 if (!resource) 47 return; 48 49 WebURLRequest* webRequest = static_cast<WebURLRequest*>(message.get(String::fromUTF8("Request"))); 50 GRefPtr<WebKitURIRequest> request = adoptGRef(webkitURIRequestCreateForResourceRequest(webRequest->resourceRequest())); 51 WebURLResponse* webRedirectResponse = static_cast<WebURLResponse*>(message.get(String::fromUTF8("RedirectResponse"))); 52 GRefPtr<WebKitURIResponse> redirectResponse = webRedirectResponse ? adoptGRef(webkitURIResponseCreateForResourceResponse(webRedirectResponse->resourceResponse())) : 0; 53 54 webkitWebResourceSentRequest(resource.get(), request.get(), redirectResponse.get()); 55 } else if (g_str_equal(messageName, "DidReceiveResponseForResource")) { 56 WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier"))); 57 GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); 58 if (!resource) 59 return; 60 61 WebURLResponse* webResponse = static_cast<WebURLResponse*>(message.get(String::fromUTF8("Response"))); 62 GRefPtr<WebKitURIResponse> response = adoptGRef(webkitURIResponseCreateForResourceResponse(webResponse->resourceResponse())); 63 64 webkitWebResourceSetResponse(resource.get(), response.get()); 65 } else if (g_str_equal(messageName, "DidReceiveContentLengthForResource")) { 66 WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier"))); 67 GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); 68 if (!resource) 69 return; 70 71 WebUInt64* contentLength = static_cast<WebUInt64*>(message.get(String::fromUTF8("ContentLength"))); 72 webkitWebResourceNotifyProgress(resource.get(), contentLength->value()); 73 } else if (g_str_equal(messageName, "DidFinishLoadForResource")) { 74 WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier"))); 75 GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); 76 if (!resource) 77 return; 78 79 webkitWebResourceFinished(resource.get()); 80 webkitWebViewRemoveLoadingWebResource(webView, resourceIdentifier->value()); 81 } else if (g_str_equal(messageName, "DidFailLoadForResource")) { 82 WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier"))); 83 GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value()); 84 if (!resource) 85 return; 86 87 WebError* webError = static_cast<WebError*>(message.get(String::fromUTF8("Error"))); 88 const ResourceError& platformError = webError->platformError(); 89 GOwnPtr<GError> resourceError(g_error_new_literal(g_quark_from_string(platformError.domain().utf8().data()), 90 platformError.errorCode(), platformError.localizedDescription().utf8().data())); 91 92 webkitWebResourceFailed(resource.get(), resourceError.get()); 93 webkitWebViewRemoveLoadingWebResource(webView, resourceIdentifier->value()); 94 } else if (g_str_equal(messageName, "DidGetSnapshot")) { 95 WebUInt64* callbackID = static_cast<WebUInt64*>(message.get("CallbackID")); 96 WebImage* image = static_cast<WebImage*>(message.get("Snapshot")); 97 webKitWebViewDidReceiveSnapshot(webView, callbackID->value(), image); 98 } else 99 ASSERT_NOT_REACHED(); 100} 101 102static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo) 103{ 104 ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID()); 105 ImmutableDictionary& message = *toImpl(static_cast<WKDictionaryRef>(messageBody)); 106 107 CString messageNameCString = toImpl(messageName)->string().utf8(); 108 const char* messageNameUTF8 = messageNameCString.data(); 109 110 if (g_str_has_prefix(messageNameUTF8, "WebPage.")) { 111 WebPageProxy* page = static_cast<WebPageProxy*>(message.get(String::fromUTF8("Page"))); 112 WebKitWebView* webView = webkitWebContextGetWebViewForPage(WEBKIT_WEB_CONTEXT(clientInfo), page); 113 if (!webView) 114 return; 115 116 didReceiveWebViewMessageFromInjectedBundle(webView, messageNameUTF8 + strlen("WebPage."), message); 117 } else 118 ASSERT_NOT_REACHED(); 119} 120 121void attachInjectedBundleClientToContext(WebKitWebContext* webContext) 122{ 123 WKContextInjectedBundleClient wkInjectedBundleClient = { 124 kWKContextInjectedBundleClientCurrentVersion, 125 webContext, // clientInfo 126 didReceiveMessageFromInjectedBundle, 127 0, // didReceiveSynchronousMessageFromInjectedBundle 128 0 // getInjectedBundleInitializationUserData 129 }; 130 WKContextSetInjectedBundleClient(toAPI(webkitWebContextGetContext(webContext)), &wkInjectedBundleClient); 131} 132