WKProcessClass should create a WebContext
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / Cocoa / WKProcessGroup.mm
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "WKProcessGroupPrivate.h"
28
29 #if WK_API_ENABLED
30
31 #import "APINavigationData.h"
32 #import "ObjCObjectGraph.h"
33 #import "WKAPICast.h"
34 #import "WKBrowsingContextControllerInternal.h"
35 #import "WKBrowsingContextHistoryDelegate.h"
36 #import "WKConnectionInternal.h"
37 #import "WKNSString.h"
38 #import "WKNSURL.h"
39 #import "WKNavigationDataInternal.h"
40 #import "WKRetainPtr.h"
41 #import "WKStringCF.h"
42 #import "WeakObjCPtr.h"
43 #import "WebCertificateInfo.h"
44 #import "WebContext.h"
45 #import "WebFrameProxy.h"
46 #import <wtf/RetainPtr.h>
47
48 #if PLATFORM(IOS)
49 #import "WKAPICast.h"
50 #import "WKGeolocationProviderIOS.h"
51 #import <WebCore/WebCoreThreadSystemInterface.h>
52 #endif
53
54 using namespace WebKit;
55
56 @implementation WKProcessGroup {
57     RefPtr<WebContext> _context;
58
59     WeakObjCPtr<id <WKProcessGroupDelegate>> _delegate;
60
61 #if PLATFORM(IOS)
62     RetainPtr<WKGeolocationProviderIOS> _geolocationProvider;
63 #endif // PLATFORM(IOS)
64 }
65
66 static void didCreateConnection(WKContextRef, WKConnectionRef connectionRef, const void* clientInfo)
67 {
68     WKProcessGroup *processGroup = (WKProcessGroup *)clientInfo;
69     auto delegate = processGroup->_delegate.get();
70
71     if ([delegate respondsToSelector:@selector(processGroup:didCreateConnectionToWebProcessPlugIn:)])
72         [delegate processGroup:processGroup didCreateConnectionToWebProcessPlugIn:wrapper(*toImpl(connectionRef))];
73 }
74
75 static void setUpConnectionClient(WKProcessGroup *processGroup, WKContextRef contextRef)
76 {
77     WKContextConnectionClientV0 connectionClient;
78     memset(&connectionClient, 0, sizeof(connectionClient));
79
80     connectionClient.base.version = 0;
81     connectionClient.base.clientInfo = processGroup;
82     connectionClient.didCreateConnection = didCreateConnection;
83
84     WKContextSetConnectionClient(contextRef, &connectionClient.base);
85 }
86
87 static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void* clientInfo)
88 {
89     WKProcessGroup *processGroup = (WKProcessGroup *)clientInfo;
90     auto delegate = processGroup->_delegate.get();
91
92     if ([delegate respondsToSelector:@selector(processGroupWillCreateConnectionToWebProcessPlugIn:)]) {
93         RetainPtr<id> initializationUserData = [delegate processGroupWillCreateConnectionToWebProcessPlugIn:processGroup];
94         RefPtr<WebKit::ObjCObjectGraph> wkMessageBody = WebKit::ObjCObjectGraph::create(initializationUserData.get());
95         return (WKTypeRef)wkMessageBody.release().leakRef();
96     }
97
98     return 0;
99 }
100
101 static void setUpInectedBundleClient(WKProcessGroup *processGroup, WKContextRef contextRef)
102 {
103     WKContextInjectedBundleClientV1 injectedBundleClient;
104     memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
105
106     injectedBundleClient.base.version = 1;
107     injectedBundleClient.base.clientInfo = processGroup;
108     injectedBundleClient.getInjectedBundleInitializationUserData = getInjectedBundleInitializationUserData;
109
110     WKContextSetInjectedBundleClient(contextRef, &injectedBundleClient.base);
111 }
112
113 static void didNavigateWithNavigationData(WKContextRef, WKPageRef pageRef, WKNavigationDataRef navigationDataRef, WKFrameRef frameRef, const void*)
114 {
115     if (!toImpl(frameRef)->isMainFrame())
116         return;
117
118     WKBrowsingContextController *controller = wrapper(*toImpl(pageRef));
119     auto historyDelegate = controller->_historyDelegate.get();
120
121     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didNavigateWithNavigationData:)])
122         [historyDelegate browsingContextController:controller didNavigateWithNavigationData:wrapper(*toImpl(navigationDataRef))];
123 }
124
125 static void didPerformClientRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*)
126 {
127     if (!toImpl(frameRef)->isMainFrame())
128         return;
129
130     WKBrowsingContextController *controller = wrapper(*toImpl(pageRef));
131     auto historyDelegate = controller->_historyDelegate.get();
132
133     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didPerformClientRedirectFromURL:toURL:)])
134         [historyDelegate browsingContextController:controller didPerformClientRedirectFromURL:wrapper(*toImpl(sourceURLRef)) toURL:wrapper(*toImpl(destinationURLRef))];
135 }
136
137 static void didPerformServerRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*)
138 {
139     if (!toImpl(frameRef)->isMainFrame())
140         return;
141
142     WKBrowsingContextController *controller = wrapper(*toImpl(pageRef));
143     auto historyDelegate = controller->_historyDelegate.get();
144
145     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didPerformServerRedirectFromURL:toURL:)])
146         [historyDelegate browsingContextController:controller didPerformServerRedirectFromURL:wrapper(*toImpl(sourceURLRef)) toURL:wrapper(*toImpl(destinationURLRef))];
147 }
148
149 static void didUpdateHistoryTitle(WKContextRef, WKPageRef pageRef, WKStringRef titleRef, WKURLRef urlRef, WKFrameRef frameRef, const void*)
150 {
151     if (!toImpl(frameRef)->isMainFrame())
152         return;
153
154     WKBrowsingContextController *controller = wrapper(*toImpl(pageRef));
155     auto historyDelegate = controller->_historyDelegate.get();
156
157     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didUpdateHistoryTitle:forURL:)])
158         [historyDelegate browsingContextController:controller didUpdateHistoryTitle:wrapper(*toImpl(titleRef)) forURL:wrapper(*toImpl(urlRef))];
159 }
160
161 static void setUpHistoryClient(WKProcessGroup *processGroup, WKContextRef contextRef)
162 {
163     WKContextHistoryClientV0 historyClient;
164     memset(&historyClient, 0, sizeof(historyClient));
165
166     historyClient.base.version = 0;
167     historyClient.base.clientInfo = processGroup;
168     historyClient.didNavigateWithNavigationData = didNavigateWithNavigationData;
169     historyClient.didPerformClientRedirect = didPerformClientRedirect;
170     historyClient.didPerformServerRedirect = didPerformServerRedirect;
171     historyClient.didUpdateHistoryTitle = didUpdateHistoryTitle;
172
173     WKContextSetHistoryClient(contextRef, &historyClient.base);
174 }
175
176 - (id)init
177 {
178     return [self initWithInjectedBundleURL:nil];
179 }
180
181 - (id)initWithInjectedBundleURL:(NSURL *)bundleURL
182 {
183     self = [super init];
184     if (!self)
185         return nil;
186
187 #if PLATFORM(IOS)
188     // FIXME: Remove once <rdar://problem/15256572> is fixed.
189     InitWebCoreThreadSystemInterface();
190 #endif
191
192     _context = WebContext::create(bundleURL ? String([bundleURL path]) : String());
193
194     setUpConnectionClient(self, toAPI(_context.get()));
195     setUpInectedBundleClient(self, toAPI(_context.get()));
196     setUpHistoryClient(self, toAPI(_context.get()));
197 #if PLATFORM(IOS)
198     _context->setUsesNetworkProcess(true);
199     _context->setProcessModel(ProcessModelMultipleSecondaryProcesses);
200 #endif
201
202     return self;
203 }
204
205 - (id <WKProcessGroupDelegate>)delegate
206 {
207     return _delegate.getAutoreleased();
208 }
209
210 - (void)setDelegate:(id <WKProcessGroupDelegate>)delegate
211 {
212     _delegate = delegate;
213 }
214
215 @end
216
217 @implementation WKProcessGroup (Private)
218
219 - (WKContextRef)_contextRef
220 {
221     return toAPI(_context.get());
222 }
223
224 - (void)_setAllowsSpecificHTTPSCertificate:(NSArray *)certificateChain forHost:(NSString *)host
225 {
226     _context->allowSpecificHTTPSCertificateForHost(WebCertificateInfo::create(WebCore::CertificateInfo((CFArrayRef)certificateChain)).get(), host);
227 }
228
229 #if PLATFORM(IOS)
230 - (WKGeolocationProviderIOS *)_geolocationProvider
231 {
232     if (!_geolocationProvider)
233         _geolocationProvider = adoptNS([[WKGeolocationProviderIOS alloc] initWithContext:_context.get()]);
234     return _geolocationProvider.get();
235 }
236 #endif // PLATFORM(IOS)
237
238 @end
239
240 #endif // WK_API_ENABLED