2
* Copyright (C) 2012 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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.
28
#if defined(__LP64__) && defined(__clang__)
30
#import "WKWebProcessPlugIn.h"
31
#import "WKWebProcessPlugInInternal.h"
33
#import "InjectedBundle.h"
34
#import "WKConnectionInternal.h"
36
#import "WKBundleAPICast.h"
37
#import "WKRetainPtr.h"
38
#import "WKWebProcessPlugInBrowserContextControllerInternal.h"
39
#import <wtf/RetainPtr.h>
41
typedef HashMap<WKBundlePageRef, RetainPtr<WKWebProcessPlugInBrowserContextController *> > BundlePageWrapperCache;
43
@interface WKWebProcessPlugInController () {
44
RetainPtr<id<WKWebProcessPlugIn> > _principalClassInstance;
45
WKRetainPtr<WKBundleRef> _bundleRef;
46
BundlePageWrapperCache _bundlePageWrapperCache;
47
RetainPtr<WKConnection *> _connectionWrapper;
51
@implementation WKWebProcessPlugInController (Internal)
53
static void didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
55
WKWebProcessPlugInController *plugInController = (WKWebProcessPlugInController *)clientInfo;
56
id<WKWebProcessPlugIn> principalClassInstance = plugInController->_principalClassInstance.get();
58
if ([principalClassInstance respondsToSelector:@selector(webProcessPlugIn:didCreateBrowserContextController:)]) {
59
ASSERT(!plugInController->_bundlePageWrapperCache.contains(page));
61
WKWebProcessPlugInBrowserContextController* browserContextController = [[WKWebProcessPlugInBrowserContextController alloc] _initWithBundlePageRef:page];
62
plugInController->_bundlePageWrapperCache.set(page, browserContextController);
64
[principalClassInstance webProcessPlugIn:plugInController didCreateBrowserContextController:browserContextController];
68
static void willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
70
WKWebProcessPlugInController *plugInController = (WKWebProcessPlugInController *)clientInfo;
71
id<WKWebProcessPlugIn> principalClassInstance = plugInController->_principalClassInstance.get();
73
// If we never added the bundle page to the cache, which can happen if webProcessPlugIn:didCreateBrowserContextController: is not implemented,
74
// there is no reason to call webProcessPlugIn:willDestroyBrowserContextController:, so don't.
75
BundlePageWrapperCache::iterator it = plugInController->_bundlePageWrapperCache.find(page);
76
if (it == plugInController->_bundlePageWrapperCache.end()) {
77
ASSERT(![principalClassInstance respondsToSelector:@selector(webProcessPlugIn:didCreateBrowserContextController:)]);
81
if ([principalClassInstance respondsToSelector:@selector(webProcessPlugIn:willDestroyBrowserContextController:)])
82
[principalClassInstance webProcessPlugIn:plugInController willDestroyBrowserContextController:it->value.get()];
84
plugInController->_bundlePageWrapperCache.remove(it);
87
static void setUpBundleClient(WKWebProcessPlugInController *plugInController, WKBundleRef bundleRef)
89
WKBundleClient bundleClient;
90
memset(&bundleClient, 0, sizeof(bundleClient));
92
bundleClient.version = kWKBundleClientCurrentVersion;
93
bundleClient.clientInfo = plugInController;
94
bundleClient.didCreatePage = didCreatePage;
95
bundleClient.willDestroyPage = willDestroyPage;
97
WKBundleSetClient(bundleRef, &bundleClient);
100
static WKWebProcessPlugInController *sharedInstance;
102
+ (WKWebProcessPlugInController *)_shared
104
ASSERT_WITH_MESSAGE(sharedInstance, "+[WKWebProcessPlugIn _shared] called without first initializing it.");
105
return sharedInstance;
108
- (id)_initWithPrincipalClassInstance:(id<WKWebProcessPlugIn>)principalClassInstance bundleRef:(WKBundleRef)bundleRef
114
_principalClassInstance = principalClassInstance;
115
_bundleRef = bundleRef;
116
_connectionWrapper = adoptNS([[WKConnection alloc] _initWithConnectionRef:WKBundleGetApplicationConnection(_bundleRef.get())]);
118
ASSERT_WITH_MESSAGE(!sharedInstance, "WKWebProcessPlugInController initialized multiple times.");
119
sharedInstance = self;
121
setUpBundleClient(self, bundleRef);
126
- (WKWebProcessPlugInBrowserContextController *)_browserContextControllerForBundlePageRef:(WKBundlePageRef)pageRef
128
ASSERT(_bundlePageWrapperCache.contains(pageRef));
129
return _bundlePageWrapperCache.get(pageRef).get();
134
@implementation WKWebProcessPlugInController
136
- (WKConnection *)connection
138
return _connectionWrapper.get();
143
#endif // defined(__LP64__) && defined(__clang__)