Unreviewed, rolling out r240630.
[WebKit-https.git] / Source / WebKit / 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 "APIProcessPoolConfiguration.h"
33 #import "ObjCObjectGraph.h"
34 #import "WKAPICast.h"
35 #import "WKBrowsingContextControllerInternal.h"
36 #import "WKBrowsingContextHistoryDelegate.h"
37 #import "WKConnectionInternal.h"
38 #import "WKNSString.h"
39 #import "WKNSURL.h"
40 #import "WKNavigationDataInternal.h"
41 #import "WKRetainPtr.h"
42 #import "WKStringCF.h"
43 #import "WebCertificateInfo.h"
44 #import "WebFrameProxy.h"
45 #import "WebProcessPool.h"
46 #import <wtf/RetainPtr.h>
47 #import <wtf/WeakObjCPtr.h>
48
49 #if PLATFORM(IOS_FAMILY)
50 #import "WKAPICast.h"
51 #import "WKGeolocationProviderIOS.h"
52 #import <WebCore/WebCoreThreadSystemInterface.h>
53 #endif
54
55 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
56 @implementation WKProcessGroup {
57 IGNORE_WARNINGS_END
58     RefPtr<WebKit::WebProcessPool> _processPool;
59
60     WeakObjCPtr<id <WKProcessGroupDelegate>> _delegate;
61
62 #if PLATFORM(IOS_FAMILY)
63     RetainPtr<WKGeolocationProviderIOS> _geolocationProvider;
64 #endif // PLATFORM(IOS_FAMILY)
65 }
66
67 static void didCreateConnection(WKContextRef, WKConnectionRef connectionRef, const void* clientInfo)
68 {
69     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
70     auto processGroup = (__bridge WKProcessGroup *)clientInfo;
71     ALLOW_DEPRECATED_DECLARATIONS_END
72     auto delegate = processGroup->_delegate.get();
73
74     if ([delegate respondsToSelector:@selector(processGroup:didCreateConnectionToWebProcessPlugIn:)])
75         [delegate processGroup:processGroup didCreateConnectionToWebProcessPlugIn:wrapper(*WebKit::toImpl(connectionRef))];
76 }
77
78 static void setUpConnectionClient(WKProcessGroup *processGroup, WKContextRef contextRef)
79 {
80     WKContextConnectionClientV0 connectionClient;
81     memset(&connectionClient, 0, sizeof(connectionClient));
82
83     connectionClient.base.version = 0;
84     connectionClient.base.clientInfo = (__bridge CFTypeRef)processGroup;
85     connectionClient.didCreateConnection = didCreateConnection;
86
87     WKContextSetConnectionClient(contextRef, &connectionClient.base);
88 }
89
90 static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void* clientInfo)
91 {
92     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
93     auto processGroup = (__bridge WKProcessGroup *)clientInfo;
94     ALLOW_DEPRECATED_DECLARATIONS_END
95     auto delegate = processGroup->_delegate.get();
96
97     if ([delegate respondsToSelector:@selector(processGroupWillCreateConnectionToWebProcessPlugIn:)]) {
98         RetainPtr<id> initializationUserData = [delegate processGroupWillCreateConnectionToWebProcessPlugIn:processGroup];
99
100         return toAPI(&WebKit::ObjCObjectGraph::create(initializationUserData.get()).leakRef());
101     }
102
103     return 0;
104 }
105
106 static void setUpInjectedBundleClient(WKProcessGroup *processGroup, WKContextRef contextRef)
107 {
108     WKContextInjectedBundleClientV1 injectedBundleClient;
109     memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
110
111     injectedBundleClient.base.version = 1;
112     injectedBundleClient.base.clientInfo = (__bridge void*)processGroup;
113     injectedBundleClient.getInjectedBundleInitializationUserData = getInjectedBundleInitializationUserData;
114
115     WKContextSetInjectedBundleClient(contextRef, &injectedBundleClient.base);
116 }
117
118 static void didNavigateWithNavigationData(WKContextRef, WKPageRef pageRef, WKNavigationDataRef navigationDataRef, WKFrameRef frameRef, const void*)
119 {
120     if (!WebKit::toImpl(frameRef)->isMainFrame())
121         return;
122
123     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
124     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
125     ALLOW_DEPRECATED_DECLARATIONS_END
126     auto historyDelegate = controller->_historyDelegate.get();
127
128     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didNavigateWithNavigationData:)])
129         [historyDelegate browsingContextController:controller didNavigateWithNavigationData:wrapper(*WebKit::toImpl(navigationDataRef))];
130 }
131
132 static void didPerformClientRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*)
133 {
134     if (!WebKit::toImpl(frameRef)->isMainFrame())
135         return;
136
137     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
138     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
139     ALLOW_DEPRECATED_DECLARATIONS_END
140     auto historyDelegate = controller->_historyDelegate.get();
141
142     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didPerformClientRedirectFromURL:toURL:)])
143         [historyDelegate browsingContextController:controller didPerformClientRedirectFromURL:wrapper(*WebKit::toImpl(sourceURLRef)) toURL:wrapper(*WebKit::toImpl(destinationURLRef))];
144 }
145
146 static void didPerformServerRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*)
147 {
148     if (!WebKit::toImpl(frameRef)->isMainFrame())
149         return;
150
151     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
152     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
153     ALLOW_DEPRECATED_DECLARATIONS_END
154     auto historyDelegate = controller->_historyDelegate.get();
155
156     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didPerformServerRedirectFromURL:toURL:)])
157         [historyDelegate browsingContextController:controller didPerformServerRedirectFromURL:wrapper(*WebKit::toImpl(sourceURLRef)) toURL:wrapper(*WebKit::toImpl(destinationURLRef))];
158 }
159
160 static void didUpdateHistoryTitle(WKContextRef, WKPageRef pageRef, WKStringRef titleRef, WKURLRef urlRef, WKFrameRef frameRef, const void*)
161 {
162     if (!WebKit::toImpl(frameRef)->isMainFrame())
163         return;
164
165     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
166     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
167     ALLOW_DEPRECATED_DECLARATIONS_END
168     auto historyDelegate = controller->_historyDelegate.get();
169
170     if ([historyDelegate respondsToSelector:@selector(browsingContextController:didUpdateHistoryTitle:forURL:)])
171         [historyDelegate browsingContextController:controller didUpdateHistoryTitle:wrapper(*WebKit::toImpl(titleRef)) forURL:wrapper(*WebKit::toImpl(urlRef))];
172 }
173
174 static void setUpHistoryClient(WKProcessGroup *processGroup, WKContextRef contextRef)
175 {
176     WKContextHistoryClientV0 historyClient;
177     memset(&historyClient, 0, sizeof(historyClient));
178
179     historyClient.base.version = 0;
180     historyClient.base.clientInfo = (__bridge CFTypeRef)processGroup;
181     historyClient.didNavigateWithNavigationData = didNavigateWithNavigationData;
182     historyClient.didPerformClientRedirect = didPerformClientRedirect;
183     historyClient.didPerformServerRedirect = didPerformServerRedirect;
184     historyClient.didUpdateHistoryTitle = didUpdateHistoryTitle;
185
186     WKContextSetHistoryClient(contextRef, &historyClient.base);
187 }
188
189 - (id)init
190 {
191     return [self initWithInjectedBundleURL:nil];
192 }
193
194 - (id)initWithInjectedBundleURL:(NSURL *)bundleURL
195 {
196     self = [super init];
197     if (!self)
198         return nil;
199
200     auto configuration = API::ProcessPoolConfiguration::createWithLegacyOptions();
201     configuration->setInjectedBundlePath(bundleURL ? String(bundleURL.path) : String());
202
203     _processPool = WebKit::WebProcessPool::create(configuration);
204
205     setUpConnectionClient(self, toAPI(_processPool.get()));
206     setUpInjectedBundleClient(self, toAPI(_processPool.get()));
207     setUpHistoryClient(self, toAPI(_processPool.get()));
208
209     return self;
210 }
211
212 - (id <WKProcessGroupDelegate>)delegate
213 {
214     return _delegate.getAutoreleased();
215 }
216
217 - (void)setDelegate:(id <WKProcessGroupDelegate>)delegate
218 {
219     _delegate = delegate;
220 }
221
222 @end
223
224 ALLOW_DEPRECATED_DECLARATIONS_BEGIN
225 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
226 @implementation WKProcessGroup (Private)
227 IGNORE_WARNINGS_END
228
229 - (WKContextRef)_contextRef
230 {
231     return toAPI(_processPool.get());
232 }
233
234 - (void)_setAllowsSpecificHTTPSCertificate:(NSArray *)certificateChain forHost:(NSString *)host
235 {
236     _processPool->allowSpecificHTTPSCertificateForHost(WebKit::WebCertificateInfo::create(WebCore::CertificateInfo((__bridge CFArrayRef)certificateChain)).ptr(), host);
237 }
238
239 #if PLATFORM(IOS_FAMILY)
240 - (WKGeolocationProviderIOS *)_geolocationProvider
241 {
242     if (!_geolocationProvider)
243         _geolocationProvider = adoptNS([[WKGeolocationProviderIOS alloc] initWithProcessPool:*_processPool.get()]);
244     return _geolocationProvider.get();
245 }
246 #endif // PLATFORM(IOS_FAMILY)
247
248 @end
249 ALLOW_DEPRECATED_DECLARATIONS_END
250
251 #endif // WK_API_ENABLED