1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/*
* Copyright (C) 2013 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitInjectedBundleClient.h"
#include "WebImage.h"
#include "WebKitURIRequestPrivate.h"
#include "WebKitURIResponsePrivate.h"
#include "WebKitWebContextPrivate.h"
#include "WebKitWebResourcePrivate.h"
#include "WebKitWebViewPrivate.h"
#include <wtf/gobject/GOwnPtr.h>
using namespace WebKit;
using namespace WebCore;
static void didReceiveWebViewMessageFromInjectedBundle(WebKitWebView* webView, const char* messageName, ImmutableDictionary& message)
{
if (g_str_equal(messageName, "DidInitiateLoadForResource")) {
WebFrameProxy* frame = static_cast<WebFrameProxy*>(message.get(String::fromUTF8("Frame")));
WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier")));
WebURLRequest* webRequest = static_cast<WebURLRequest*>(message.get(String::fromUTF8("Request")));
GRefPtr<WebKitURIRequest> request = adoptGRef(webkitURIRequestCreateForResourceRequest(webRequest->resourceRequest()));
webkitWebViewResourceLoadStarted(webView, frame, resourceIdentifier->value(), request.get());
} else if (g_str_equal(messageName, "DidSendRequestForResource")) {
WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier")));
GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value());
if (!resource)
return;
WebURLRequest* webRequest = static_cast<WebURLRequest*>(message.get(String::fromUTF8("Request")));
GRefPtr<WebKitURIRequest> request = adoptGRef(webkitURIRequestCreateForResourceRequest(webRequest->resourceRequest()));
WebURLResponse* webRedirectResponse = static_cast<WebURLResponse*>(message.get(String::fromUTF8("RedirectResponse")));
GRefPtr<WebKitURIResponse> redirectResponse = webRedirectResponse ? adoptGRef(webkitURIResponseCreateForResourceResponse(webRedirectResponse->resourceResponse())) : 0;
webkitWebResourceSentRequest(resource.get(), request.get(), redirectResponse.get());
} else if (g_str_equal(messageName, "DidReceiveResponseForResource")) {
WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier")));
GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value());
if (!resource)
return;
WebURLResponse* webResponse = static_cast<WebURLResponse*>(message.get(String::fromUTF8("Response")));
GRefPtr<WebKitURIResponse> response = adoptGRef(webkitURIResponseCreateForResourceResponse(webResponse->resourceResponse()));
webkitWebResourceSetResponse(resource.get(), response.get());
} else if (g_str_equal(messageName, "DidReceiveContentLengthForResource")) {
WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier")));
GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value());
if (!resource)
return;
WebUInt64* contentLength = static_cast<WebUInt64*>(message.get(String::fromUTF8("ContentLength")));
webkitWebResourceNotifyProgress(resource.get(), contentLength->value());
} else if (g_str_equal(messageName, "DidFinishLoadForResource")) {
WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier")));
GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value());
if (!resource)
return;
webkitWebResourceFinished(resource.get());
webkitWebViewRemoveLoadingWebResource(webView, resourceIdentifier->value());
} else if (g_str_equal(messageName, "DidFailLoadForResource")) {
WebUInt64* resourceIdentifier = static_cast<WebUInt64*>(message.get(String::fromUTF8("Identifier")));
GRefPtr<WebKitWebResource> resource = webkitWebViewGetLoadingWebResource(webView, resourceIdentifier->value());
if (!resource)
return;
WebError* webError = static_cast<WebError*>(message.get(String::fromUTF8("Error")));
const ResourceError& platformError = webError->platformError();
GOwnPtr<GError> resourceError(g_error_new_literal(g_quark_from_string(platformError.domain().utf8().data()),
platformError.errorCode(), platformError.localizedDescription().utf8().data()));
webkitWebResourceFailed(resource.get(), resourceError.get());
webkitWebViewRemoveLoadingWebResource(webView, resourceIdentifier->value());
} else if (g_str_equal(messageName, "DidGetSnapshot")) {
WebUInt64* callbackID = static_cast<WebUInt64*>(message.get("CallbackID"));
WebImage* image = static_cast<WebImage*>(message.get("Snapshot"));
webKitWebViewDidReceiveSnapshot(webView, callbackID->value(), image);
} else
ASSERT_NOT_REACHED();
}
static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
{
ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
ImmutableDictionary& message = *toImpl(static_cast<WKDictionaryRef>(messageBody));
CString messageNameCString = toImpl(messageName)->string().utf8();
const char* messageNameUTF8 = messageNameCString.data();
if (g_str_has_prefix(messageNameUTF8, "WebPage.")) {
WebPageProxy* page = static_cast<WebPageProxy*>(message.get(String::fromUTF8("Page")));
WebKitWebView* webView = webkitWebContextGetWebViewForPage(WEBKIT_WEB_CONTEXT(clientInfo), page);
if (!webView)
return;
didReceiveWebViewMessageFromInjectedBundle(webView, messageNameUTF8 + strlen("WebPage."), message);
} else
ASSERT_NOT_REACHED();
}
void attachInjectedBundleClientToContext(WebKitWebContext* webContext)
{
WKContextInjectedBundleClient wkInjectedBundleClient = {
kWKContextInjectedBundleClientCurrentVersion,
webContext, // clientInfo
didReceiveMessageFromInjectedBundle,
0, // didReceiveSynchronousMessageFromInjectedBundle
0 // getInjectedBundleInitializationUserData
};
WKContextSetInjectedBundleClient(toAPI(webkitWebContextGetContext(webContext)), &wkInjectedBundleClient);
}
|