/* * Copyright (C) 2011 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #import "config.h" #import "WKProcessGroup.h" #import "WKProcessGroupPrivate.h" #import "ObjCObjectGraph.h" #import "WKAPICast.h" #import "WKBrowsingContextControllerInternal.h" #import "WKBrowsingContextHistoryDelegate.h" #import "WKConnectionInternal.h" #import "WKContext.h" #import "WKNSString.h" #import "WKNSURL.h" #import "WKNavigationDataInternal.h" #import "WKRetainPtr.h" #import "WKStringCF.h" #import "WebFrameProxy.h" #import "WebNavigationData.h" #import using namespace WebKit; @interface WKProcessGroupData : NSObject { @public // Underlying context object. WKRetainPtr _contextRef; // Delegate for callbacks. id _delegate; } @end @implementation WKProcessGroupData @end @implementation WKProcessGroup static void didCreateConnection(WKContextRef, WKConnectionRef connectionRef, const void* clientInfo) { WKProcessGroup *processGroup = (WKProcessGroup *)clientInfo; if ([processGroup.delegate respondsToSelector:@selector(processGroup:didCreateConnectionToWebProcessPlugIn:)]) { RetainPtr connection = adoptNS([[WKConnection alloc] _initWithConnectionRef:connectionRef]); [processGroup.delegate processGroup:processGroup didCreateConnectionToWebProcessPlugIn:connection.get()]; } } static void setUpConnectionClient(WKProcessGroup *processGroup, WKContextRef contextRef) { WKContextConnectionClient connectionClient; memset(&connectionClient, 0, sizeof(connectionClient)); connectionClient.version = kWKContextConnectionClientCurrentVersion; connectionClient.clientInfo = processGroup; connectionClient.didCreateConnection = didCreateConnection; WKContextSetConnectionClient(contextRef, &connectionClient); } static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void* clientInfo) { WKProcessGroup *processGroup = (WKProcessGroup *)clientInfo; if ([processGroup.delegate respondsToSelector:@selector(processGroupWillCreateConnectionToWebProcessPlugIn:)]) { RetainPtr initializationUserData = [processGroup.delegate processGroupWillCreateConnectionToWebProcessPlugIn:processGroup]; RefPtr wkMessageBody = WebKit::ObjCObjectGraph::create(initializationUserData.get()); return (WKTypeRef)wkMessageBody.release().leakRef(); } return 0; } static void setUpInectedBundleClient(WKProcessGroup *processGroup, WKContextRef contextRef) { WKContextInjectedBundleClient injectedBundleClient; memset(&injectedBundleClient, 0, sizeof(injectedBundleClient)); injectedBundleClient.version = kWKContextInjectedBundleClientCurrentVersion; injectedBundleClient.clientInfo = processGroup; injectedBundleClient.getInjectedBundleInitializationUserData = getInjectedBundleInitializationUserData; WKContextSetInjectedBundleClient(contextRef, &injectedBundleClient); } #if WK_API_ENABLED static void didNavigateWithNavigationData(WKContextRef, WKPageRef pageRef, WKNavigationDataRef navigationDataRef, WKFrameRef frameRef, const void*) { if (!toImpl(frameRef)->isMainFrame()) return; WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef]; if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didNavigateWithNavigationData:)]) [controller.historyDelegate browsingContextController:controller didNavigateWithNavigationData:wrapper(*toImpl(navigationDataRef))]; } static void didPerformClientRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*) { if (!toImpl(frameRef)->isMainFrame()) return; WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef]; if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didPerformClientRedirectFromURL:toURL:)]) [controller.historyDelegate browsingContextController:controller didPerformClientRedirectFromURL:wrapper(*toImpl(sourceURLRef)) toURL:wrapper(*toImpl(destinationURLRef))]; } static void didPerformServerRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*) { if (!toImpl(frameRef)->isMainFrame()) return; WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef]; if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didPerformServerRedirectFromURL:toURL:)]) [controller.historyDelegate browsingContextController:controller didPerformServerRedirectFromURL:wrapper(*toImpl(sourceURLRef)) toURL:wrapper(*toImpl(destinationURLRef))]; } static void didUpdateHistoryTitle(WKContextRef, WKPageRef pageRef, WKStringRef titleRef, WKURLRef urlRef, WKFrameRef frameRef, const void*) { if (!toImpl(frameRef)->isMainFrame()) return; WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef]; if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didUpdateHistoryTitle:forURL:)]) [controller.historyDelegate browsingContextController:controller didUpdateHistoryTitle:wrapper(*toImpl(titleRef)) forURL:wrapper(*toImpl(urlRef))]; } static void setUpHistoryClient(WKProcessGroup *processGroup, WKContextRef contextRef) { WKContextHistoryClient historyClient; memset(&historyClient, 0, sizeof(historyClient)); historyClient.version = kWKContextHistoryClientCurrentVersion; historyClient.clientInfo = processGroup; historyClient.didNavigateWithNavigationData = didNavigateWithNavigationData; historyClient.didPerformClientRedirect = didPerformClientRedirect; historyClient.didPerformServerRedirect = didPerformServerRedirect; historyClient.didUpdateHistoryTitle = didUpdateHistoryTitle; WKContextSetHistoryClient(contextRef, &historyClient); } #endif // WK_API_ENABLED - (id)init { return [self initWithInjectedBundleURL:nil]; } - (id)initWithInjectedBundleURL:(NSURL *)bundleURL { self = [super init]; if (!self) return nil; _data = [[WKProcessGroupData alloc] init]; if (bundleURL) _data->_contextRef = adoptWK(WKContextCreateWithInjectedBundlePath(adoptWK(WKStringCreateWithCFString((CFStringRef)[bundleURL path])).get())); else _data->_contextRef = adoptWK(WKContextCreate()); setUpConnectionClient(self, _data->_contextRef.get()); setUpInectedBundleClient(self, _data->_contextRef.get()); #if WK_API_ENABLED setUpHistoryClient(self, _data->_contextRef.get()); #endif return self; } - (void)dealloc { WKContextSetConnectionClient(_data->_contextRef.get(), 0); WKContextSetInjectedBundleClient(_data->_contextRef.get(), 0); WKContextSetHistoryClient(_data->_contextRef.get(), 0); [_data release]; [super dealloc]; } - (id)delegate { return _data->_delegate; } - (void)setDelegate:(id)delegate { _data->_delegate = delegate; } @end @implementation WKProcessGroup (Private) - (WKContextRef)_contextRef { return _data->_contextRef.get(); } @end