2 * Copyright (C) 2011 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.
27 #import "WKConnectionInternal.h"
31 #import "ObjCObjectGraph.h"
32 #import "WKRetainPtr.h"
33 #import "WKSharedAPICast.h"
34 #import "WKStringCF.h"
35 #import <wtf/RetainPtr.h>
36 #import <wtf/WeakObjCPtr.h>
37 #import <wtf/text/WTFString.h>
39 using namespace WebKit;
41 @implementation WKConnection {
42 WeakObjCPtr<id <WKConnectionDelegate>> _delegate;
47 self._connection.~WebConnection();
52 static void didReceiveMessage(WKConnectionRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
54 auto connection = (__bridge WKConnection *)clientInfo;
55 auto delegate = connection->_delegate.get();
57 if ([delegate respondsToSelector:@selector(connection:didReceiveMessageWithName:body:)]) {
58 RetainPtr<CFStringRef> nsMessageName = adoptCF(WKStringCopyCFString(kCFAllocatorDefault, messageName));
59 RetainPtr<id> nsMessageBody = static_cast<ObjCObjectGraph*>(toImpl(messageBody))->rootObject();
60 [delegate connection:connection didReceiveMessageWithName:(__bridge NSString *)nsMessageName.get() body:nsMessageBody.get()];
64 static void didClose(WKConnectionRef, const void* clientInfo)
66 auto connection = (__bridge WKConnection *)clientInfo;
67 auto delegate = connection->_delegate.get();
69 if ([delegate respondsToSelector:@selector(connectionDidClose:)])
70 [delegate connectionDidClose:connection];
73 static void setUpClient(WKConnection *wrapper, WebConnection& connection)
75 WKConnectionClientV0 client;
76 memset(&client, 0, sizeof(client));
78 client.base.version = 0;
79 client.base.clientInfo = (__bridge void*)wrapper;
80 client.didReceiveMessage = didReceiveMessage;
81 client.didClose = didClose;
83 connection.initializeConnectionClient(&client.base);
86 - (id <WKConnectionDelegate>)delegate
88 return _delegate.getAutoreleased();
91 - (void)setDelegate:(id <WKConnectionDelegate>)delegate
95 setUpClient(self, self._connection);
97 self._connection.initializeConnectionClient(nullptr);
100 - (void)sendMessageWithName:(NSString *)messageName body:(id)messageBody
102 RefPtr<ObjCObjectGraph> wkMessageBody = ObjCObjectGraph::create(messageBody);
103 self._connection.postMessage(messageName, wkMessageBody.get());
106 - (WebConnection&)_connection
108 return static_cast<WebConnection&>(API::Object::fromWKObjectExtraSpace(self));
111 #pragma mark WKObject protocol implementation
113 - (API::Object&)_apiObject
115 return API::Object::fromWKObjectExtraSpace(self);
120 #endif // WK_API_ENABLED