Move API files shared between Mac and iOS to the Cocoa directory
[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 "WKProcessGroupInternal.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 "WKContext.h"
38 #import "WKNSString.h"
39 #import "WKNSURL.h"
40 #import "WKNavigationDataInternal.h"
41 #import "WKRetainPtr.h"
42 #import "WKStringCF.h"
43 #import "WebFrameProxy.h"
44 #import <wtf/RetainPtr.h>
45
46 #if PLATFORM(IOS)
47 #import "WKAPICast.h"
48 #import "WKGeolocationProviderIOS.h"
49 #import <WebCore/WebCoreThreadSystemInterface.h>
50 #endif
51
52 using namespace WebKit;
53
54 @implementation WKProcessGroup {
55     std::aligned_storage<sizeof(WebContext), std::alignment_of<WebContext>::value>::type _context;
56
57 #if PLATFORM(IOS)
58     RetainPtr<WKGeolocationProviderIOS> _geolocationProvider;
59 #endif // PLATFORM(IOS)
60 }
61
62 static void didCreateConnection(WKContextRef, WKConnectionRef connectionRef, const void* clientInfo)
63 {
64     WKProcessGroup *processGroup = (WKProcessGroup *)clientInfo;
65     if ([processGroup.delegate respondsToSelector:@selector(processGroup:didCreateConnectionToWebProcessPlugIn:)]) {
66         RetainPtr<WKConnection> connection = adoptNS([[WKConnection alloc] _initWithConnectionRef:connectionRef]);
67         [processGroup.delegate processGroup:processGroup didCreateConnectionToWebProcessPlugIn:connection.get()];
68     }
69 }
70
71 static void setUpConnectionClient(WKProcessGroup *processGroup, WKContextRef contextRef)
72 {
73     WKContextConnectionClient connectionClient;
74     memset(&connectionClient, 0, sizeof(connectionClient));
75
76     connectionClient.version = kWKContextConnectionClientCurrentVersion;
77     connectionClient.clientInfo = processGroup;
78     connectionClient.didCreateConnection = didCreateConnection;
79
80     WKContextSetConnectionClient(contextRef, &connectionClient);
81 }
82
83 static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void* clientInfo)
84 {
85     WKProcessGroup *processGroup = (WKProcessGroup *)clientInfo;
86     if ([processGroup.delegate respondsToSelector:@selector(processGroupWillCreateConnectionToWebProcessPlugIn:)]) {
87         RetainPtr<id> initializationUserData = [processGroup.delegate processGroupWillCreateConnectionToWebProcessPlugIn:processGroup];
88         RefPtr<WebKit::ObjCObjectGraph> wkMessageBody = WebKit::ObjCObjectGraph::create(initializationUserData.get());
89         return (WKTypeRef)wkMessageBody.release().leakRef();
90     }
91
92     return 0;
93 }
94
95 static void setUpInectedBundleClient(WKProcessGroup *processGroup, WKContextRef contextRef)
96 {
97     WKContextInjectedBundleClient injectedBundleClient;
98     memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
99
100     injectedBundleClient.version = kWKContextInjectedBundleClientCurrentVersion;
101     injectedBundleClient.clientInfo = processGroup;
102     injectedBundleClient.getInjectedBundleInitializationUserData = getInjectedBundleInitializationUserData;
103
104     WKContextSetInjectedBundleClient(contextRef, &injectedBundleClient);
105 }
106
107 static void didNavigateWithNavigationData(WKContextRef, WKPageRef pageRef, WKNavigationDataRef navigationDataRef, WKFrameRef frameRef, const void*)
108 {
109     if (!toImpl(frameRef)->isMainFrame())
110         return;
111
112     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
113     if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didNavigateWithNavigationData:)])
114         [controller.historyDelegate browsingContextController:controller didNavigateWithNavigationData:wrapper(*toImpl(navigationDataRef))];
115 }
116
117 static void didPerformClientRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*)
118 {
119     if (!toImpl(frameRef)->isMainFrame())
120         return;
121
122     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
123     if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didPerformClientRedirectFromURL:toURL:)])
124         [controller.historyDelegate browsingContextController:controller didPerformClientRedirectFromURL:wrapper(*toImpl(sourceURLRef)) toURL:wrapper(*toImpl(destinationURLRef))];
125 }
126
127 static void didPerformServerRedirect(WKContextRef, WKPageRef pageRef, WKURLRef sourceURLRef, WKURLRef destinationURLRef, WKFrameRef frameRef, const void*)
128 {
129     if (!toImpl(frameRef)->isMainFrame())
130         return;
131
132     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
133     if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didPerformServerRedirectFromURL:toURL:)])
134         [controller.historyDelegate browsingContextController:controller didPerformServerRedirectFromURL:wrapper(*toImpl(sourceURLRef)) toURL:wrapper(*toImpl(destinationURLRef))];
135 }
136
137 static void didUpdateHistoryTitle(WKContextRef, WKPageRef pageRef, WKStringRef titleRef, WKURLRef urlRef, WKFrameRef frameRef, const void*)
138 {
139     if (!toImpl(frameRef)->isMainFrame())
140         return;
141
142     WKBrowsingContextController *controller = [WKBrowsingContextController _browsingContextControllerForPageRef:pageRef];
143     if ([controller.historyDelegate respondsToSelector:@selector(browsingContextController:didUpdateHistoryTitle:forURL:)])
144         [controller.historyDelegate browsingContextController:controller didUpdateHistoryTitle:wrapper(*toImpl(titleRef)) forURL:wrapper(*toImpl(urlRef))];
145 }
146
147 static void setUpHistoryClient(WKProcessGroup *processGroup, WKContextRef contextRef)
148 {
149     WKContextHistoryClient historyClient;
150     memset(&historyClient, 0, sizeof(historyClient));
151
152     historyClient.version = kWKContextHistoryClientCurrentVersion;
153     historyClient.clientInfo = processGroup;
154     historyClient.didNavigateWithNavigationData = didNavigateWithNavigationData;
155     historyClient.didPerformClientRedirect = didPerformClientRedirect;
156     historyClient.didPerformServerRedirect = didPerformServerRedirect;
157     historyClient.didUpdateHistoryTitle = didUpdateHistoryTitle;
158
159     WKContextSetHistoryClient(contextRef, &historyClient);
160 }
161
162 - (id)init
163 {
164     return [self initWithInjectedBundleURL:nil];
165 }
166
167 - (id)initWithInjectedBundleURL:(NSURL *)bundleURL
168 {
169     self = [super init];
170     if (!self)
171         return nil;
172
173 #if PLATFORM(IOS)
174     // FIXME: Remove once <rdar://problem/15256572> is fixed.
175     InitWebCoreThreadSystemInterface();
176 #endif
177
178     API::Object::constructInWrapper<WebContext>(self, bundleURL ? String([bundleURL path]) : String());
179
180     setUpConnectionClient(self, toAPI(reinterpret_cast<WebContext*>(&_context)));
181     setUpInectedBundleClient(self, toAPI(reinterpret_cast<WebContext*>(&_context)));
182     setUpHistoryClient(self, toAPI(reinterpret_cast<WebContext*>(&_context)));
183
184     return self;
185 }
186
187 - (void)dealloc
188 {
189     reinterpret_cast<WebContext*>(&_context)->~WebContext();
190
191     [super dealloc];
192 }
193
194 #pragma mark WKObject protocol implementation
195
196 - (API::Object&)_apiObject
197 {
198     return *reinterpret_cast<API::Object*>(&_context);
199 }
200
201 @end
202
203 @implementation WKProcessGroup (Private)
204
205 - (WKContextRef)_contextRef
206 {
207     return toAPI(reinterpret_cast<WebContext*>(&_context));
208 }
209
210 #if PLATFORM(IOS)
211 - (WKGeolocationProviderIOS *)_geolocationProvider
212 {
213     if (!_geolocationProvider)
214         _geolocationProvider = adoptNS([[WKGeolocationProviderIOS alloc] initWithContext:reinterpret_cast<WebContext*>(&_context)]);
215     return _geolocationProvider.get();
216 }
217 #endif // PLATFORM(IOS)
218
219 @end
220
221 #endif // WK_API_ENABLED