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