Add reply blocks to _WKRemoteObjectInterface similar to NSXPCConnection
https://bugs.webkit.org/show_bug.cgi?id=151056
rdar://problem/23222609
Reviewed by Tim Horton.
Source/WebKit2:
* Platform/spi/Cocoa/NSInvocationSPI.h:
Add NSBlockInvocation declaration.
* Shared/API/Cocoa/RemoteObjectInvocation.mm:
(WebKit::RemoteObjectInvocation::encode):
Encode true if we have a reply ID.
* Shared/API/Cocoa/RemoteObjectRegistry.h:
Add new members.
* Shared/API/Cocoa/RemoteObjectRegistry.messages.in:
Add new CallReplyBlock message.
* Shared/API/Cocoa/RemoteObjectRegistry.mm:
(WebKit::RemoteObjectRegistry::sendReplyBlock):
Just send the CallReplyBlock message.
(WebKit::RemoteObjectRegistry::callReplyBlock):
Call through to _WKRemoteObjectRegistry.
* Shared/API/Cocoa/WKRemoteObjectCoder.h:
Pass an optional reply selector.
* Shared/API/Cocoa/WKRemoteObjectCoder.mm:
(encodeInvocationArguments):
Don't hard-code the first argument index.
(encodeInvocation):
Encode block invocations.
(-[WKRemoteObjectDecoder initWithInterface:rootObjectDictionary:replyToSelector:]):
Initialize _replyToSelector.
(validateClass):
NSBlockInvocation doesn't need to conform to NSSecureCoding.
(decodeInvocationArguments):
Don't hard-code the first argument, take it as a parameter instead.
(decodeInvocation):
Decode NSBlockInvocations (reply block invocations).
(decodeObject):
Check for NSBlockInvocation.
* Shared/API/Cocoa/_WKRemoteObjectInterface.mm:
(-[_WKRemoteObjectInterface _methodSignatureForSelector:]):
Return null if we can't find the method.
(-[_WKRemoteObjectInterface _methodSignatureForReplyBlockOfSelector:]):
Look up the reply block signature and return it.
(-[_WKRemoteObjectInterface _allowedArgumentClassesForReplyBlockOfSelector:]):
Look up the allowed reply argument classes and return them.
* Shared/API/Cocoa/_WKRemoteObjectInterfaceInternal.h:
Add new methods.
* Shared/API/Cocoa/_WKRemoteObjectRegistry.mm:
(PendingReply::PendingReply):
Add new object that represents a pending reply.
(-[_WKRemoteObjectRegistry _sendInvocation:interface:]):
If the invocation has a reply block, add a pending reply to our map.
(-[_WKRemoteObjectRegistry _invokeMethod:]):
If the method we're about to invoke has a reply block, construct a special reply block that calls us back with an invocation.
Encode this invocation and send it back across the wire.
(-[_WKRemoteObjectRegistry _callReplyWithID:blockInvocation:]):
Find the pending reply, decode the reply block invocation and call it.
* Shared/API/Cocoa/_WKRemoteObjectRegistryInternal.h:
Add new methods.
Tools:
Update test.
* TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistry.mm:
(TEST):
* TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistryPlugIn.mm:
(-[RemoteObjectRegistryPlugIn sayHello:completionHandler:]):
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@192185 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit2/ChangeLog b/Source/WebKit2/ChangeLog
index f5719249..77e089f 100644
--- a/Source/WebKit2/ChangeLog
+++ b/Source/WebKit2/ChangeLog
@@ -1,3 +1,86 @@
+2015-11-09 Anders Carlsson <andersca@apple.com>
+
+ Add reply blocks to _WKRemoteObjectInterface similar to NSXPCConnection
+ https://bugs.webkit.org/show_bug.cgi?id=151056
+ rdar://problem/23222609
+
+ Reviewed by Tim Horton.
+
+ * Platform/spi/Cocoa/NSInvocationSPI.h:
+ Add NSBlockInvocation declaration.
+
+ * Shared/API/Cocoa/RemoteObjectInvocation.mm:
+ (WebKit::RemoteObjectInvocation::encode):
+ Encode true if we have a reply ID.
+
+ * Shared/API/Cocoa/RemoteObjectRegistry.h:
+ Add new members.
+
+ * Shared/API/Cocoa/RemoteObjectRegistry.messages.in:
+ Add new CallReplyBlock message.
+
+ * Shared/API/Cocoa/RemoteObjectRegistry.mm:
+ (WebKit::RemoteObjectRegistry::sendReplyBlock):
+ Just send the CallReplyBlock message.
+
+ (WebKit::RemoteObjectRegistry::callReplyBlock):
+ Call through to _WKRemoteObjectRegistry.
+
+ * Shared/API/Cocoa/WKRemoteObjectCoder.h:
+ Pass an optional reply selector.
+
+ * Shared/API/Cocoa/WKRemoteObjectCoder.mm:
+ (encodeInvocationArguments):
+ Don't hard-code the first argument index.
+
+ (encodeInvocation):
+ Encode block invocations.
+
+ (-[WKRemoteObjectDecoder initWithInterface:rootObjectDictionary:replyToSelector:]):
+ Initialize _replyToSelector.
+
+ (validateClass):
+ NSBlockInvocation doesn't need to conform to NSSecureCoding.
+
+ (decodeInvocationArguments):
+ Don't hard-code the first argument, take it as a parameter instead.
+
+ (decodeInvocation):
+ Decode NSBlockInvocations (reply block invocations).
+
+ (decodeObject):
+ Check for NSBlockInvocation.
+
+ * Shared/API/Cocoa/_WKRemoteObjectInterface.mm:
+ (-[_WKRemoteObjectInterface _methodSignatureForSelector:]):
+ Return null if we can't find the method.
+
+ (-[_WKRemoteObjectInterface _methodSignatureForReplyBlockOfSelector:]):
+ Look up the reply block signature and return it.
+
+ (-[_WKRemoteObjectInterface _allowedArgumentClassesForReplyBlockOfSelector:]):
+ Look up the allowed reply argument classes and return them.
+
+ * Shared/API/Cocoa/_WKRemoteObjectInterfaceInternal.h:
+ Add new methods.
+
+ * Shared/API/Cocoa/_WKRemoteObjectRegistry.mm:
+ (PendingReply::PendingReply):
+ Add new object that represents a pending reply.
+
+ (-[_WKRemoteObjectRegistry _sendInvocation:interface:]):
+ If the invocation has a reply block, add a pending reply to our map.
+
+ (-[_WKRemoteObjectRegistry _invokeMethod:]):
+ If the method we're about to invoke has a reply block, construct a special reply block that calls us back with an invocation.
+ Encode this invocation and send it back across the wire.
+
+ (-[_WKRemoteObjectRegistry _callReplyWithID:blockInvocation:]):
+ Find the pending reply, decode the reply block invocation and call it.
+
+ * Shared/API/Cocoa/_WKRemoteObjectRegistryInternal.h:
+ Add new methods.
+
2015-11-09 Tim Horton <timothy_horton@apple.com>
Add drawsBackground SPI to WKWebView, and get rid of drawsTransparentBackground from WebKit2
diff --git a/Source/WebKit2/Platform/spi/Cocoa/NSInvocationSPI.h b/Source/WebKit2/Platform/spi/Cocoa/NSInvocationSPI.h
index 60ef507..4e8cc5a 100644
--- a/Source/WebKit2/Platform/spi/Cocoa/NSInvocationSPI.h
+++ b/Source/WebKit2/Platform/spi/Cocoa/NSInvocationSPI.h
@@ -34,3 +34,6 @@
@end
#endif // USE(APPLE_INTERNAL_SDK)
+
+@interface NSBlockInvocation : NSInvocation
+@end
diff --git a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectInvocation.mm b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectInvocation.mm
index d9120fe..3625939 100644
--- a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectInvocation.mm
+++ b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectInvocation.mm
@@ -51,6 +51,7 @@
return;
}
+ encoder << true;
encoder << m_replyInfo->replyID;
encoder << m_replyInfo->blockSignature;
}
diff --git a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.h b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.h
index 6528c0c..c715c43 100644
--- a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.h
+++ b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.h
@@ -37,6 +37,7 @@
namespace WebKit {
class RemoteObjectInvocation;
+class UserData;
class RemoteObjectRegistry final : public IPC::MessageReceiver {
public:
@@ -44,6 +45,7 @@
~RemoteObjectRegistry();
void sendInvocation(const RemoteObjectInvocation&);
+ void sendReplyBlock(uint64_t replyID, const UserData& blockInvocation);
private:
// IPC::MessageReceiver
@@ -51,6 +53,7 @@
// Message handlers
void invokeMethod(const RemoteObjectInvocation&);
+ void callReplyBlock(uint64_t replyID, const UserData& blockInvocation);
_WKRemoteObjectRegistry *m_remoteObjectRegistry;
IPC::MessageSender& m_messageSender;
diff --git a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.messages.in b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.messages.in
index 3220e4c..6888cf3 100644
--- a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.messages.in
+++ b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.messages.in
@@ -22,4 +22,5 @@
messages -> RemoteObjectRegistry {
InvokeMethod(WebKit::RemoteObjectInvocation invocation)
+ CallReplyBlock(uint64_t replyID, WebKit::UserData blockInvocation);
}
diff --git a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.mm b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.mm
index a1c3f78..c591fb3 100644
--- a/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.mm
+++ b/Source/WebKit2/Shared/API/Cocoa/RemoteObjectRegistry.mm
@@ -29,6 +29,7 @@
#import "MessageSender.h"
#import "RemoteObjectInvocation.h"
#import "RemoteObjectRegistryMessages.h"
+#import "UserData.h"
#import "_WKRemoteObjectRegistryInternal.h"
namespace WebKit {
@@ -48,6 +49,11 @@
m_messageSender.send(Messages::RemoteObjectRegistry::InvokeMethod(invocation));
}
+void RemoteObjectRegistry::sendReplyBlock(uint64_t replyID, const UserData& blockInvocation)
+{
+ m_messageSender.send(Messages::RemoteObjectRegistry::CallReplyBlock(replyID, blockInvocation));
+}
+
void RemoteObjectRegistry::invokeMethod(const RemoteObjectInvocation& invocation)
{
#if WK_API_ENABLED
@@ -55,4 +61,9 @@
#endif
}
+void RemoteObjectRegistry::callReplyBlock(uint64_t replyID, const UserData& blockInvocation)
+{
+ [m_remoteObjectRegistry _callReplyWithID:replyID blockInvocation:blockInvocation];
+}
+
} // namespace WebKit
diff --git a/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h b/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h
index dcded62..7975260 100644
--- a/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h
+++ b/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h
@@ -41,7 +41,7 @@
@interface WKRemoteObjectDecoder : NSCoder
-- (id)initWithInterface:(_WKRemoteObjectInterface *)interface rootObjectDictionary:(const API::Dictionary*)rootObjectDictionary;
+- (id)initWithInterface:(_WKRemoteObjectInterface *)interface rootObjectDictionary:(const API::Dictionary*)rootObjectDictionary replyToSelector:(SEL)replyToSelector;
@end
diff --git a/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm b/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm
index 56107ec..9682e6b 100644
--- a/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm
+++ b/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm
@@ -33,6 +33,7 @@
#import "APIDictionary.h"
#import "APINumber.h"
#import "APIString.h"
+#import "NSInvocationSPI.h"
#import "_WKRemoteObjectInterfaceInternal.h"
#import <objc/runtime.h>
#import <wtf/RetainPtr.h>
@@ -45,6 +46,7 @@
static NSString * const selectorKey = @"selector";
static NSString * const typeStringKey = @"typeString";
+static NSString * const isReplyBlockKey = @"isReplyBlock";
static RefPtr<API::Dictionary> createEncodedObject(WKRemoteObjectEncoder *, id);
@@ -118,7 +120,7 @@
ASSERT(firstArgument <= argumentCount);
- for (NSUInteger i = 2; i < argumentCount; ++i) {
+ for (NSUInteger i = firstArgument; i < argumentCount; ++i) {
const char* type = [methodSignature getArgumentTypeAtIndex:i];
switch (*type) {
@@ -213,9 +215,14 @@
{
NSMethodSignature *methodSignature = invocation.methodSignature;
[encoder encodeObject:methodSignature._typeString forKey:typeStringKey];
- [encoder encodeObject:NSStringFromSelector(invocation.selector) forKey:selectorKey];
- encodeInvocationArguments(encoder, invocation, 2);
+ if ([invocation isKindOfClass:[NSBlockInvocation class]]) {
+ [encoder encodeBool:YES forKey:isReplyBlockKey];
+ encodeInvocationArguments(encoder, invocation, 1);
+ } else {
+ [encoder encodeObject:NSStringFromSelector(invocation.selector) forKey:selectorKey];
+ encodeInvocationArguments(encoder, invocation, 2);
+ }
}
static void encodeString(WKRemoteObjectEncoder *encoder, NSString *string)
@@ -355,13 +362,15 @@
const API::Dictionary* _rootDictionary;
const API::Dictionary* _currentDictionary;
+ SEL _replyToSelector;
+
const API::Array* _objectStream;
size_t _objectStreamPosition;
const HashSet<Class>* _allowedClasses;
}
-- (id)initWithInterface:(_WKRemoteObjectInterface *)interface rootObjectDictionary:(const API::Dictionary*)rootObjectDictionary
+- (id)initWithInterface:(_WKRemoteObjectInterface *)interface rootObjectDictionary:(const API::Dictionary*)rootObjectDictionary replyToSelector:(SEL)replyToSelector
{
if (!(self = [super init]))
return nil;
@@ -371,6 +380,8 @@
_rootDictionary = rootObjectDictionary;
_currentDictionary = _rootDictionary;
+ _replyToSelector = replyToSelector;
+
_objectStream = _rootDictionary->get<API::Array>(objectStreamKey);
return self;
@@ -442,23 +453,21 @@
checkIfClassIsAllowed(decoder, objectClass);
- // NSInvocation doesn't support NSSecureCoding, but we allow it anyway.
- if (objectClass == [NSInvocation class])
+ // NSInvocation and NSBlockInvocation don't support NSSecureCoding, but we allow them anyway.
+ if (objectClass == [NSInvocation class] || objectClass == [NSBlockInvocation class])
return;
[decoder validateClassSupportsSecureCoding:objectClass];
}
-static void decodeInvocationArguments(WKRemoteObjectDecoder *decoder, NSInvocation *invocation, const Vector<HashSet<Class>>& allowedArgumentClasses)
+static void decodeInvocationArguments(WKRemoteObjectDecoder *decoder, NSInvocation *invocation, const Vector<HashSet<Class>>& allowedArgumentClasses, NSUInteger firstArgument)
{
NSMethodSignature *methodSignature = invocation.methodSignature;
NSUInteger argumentCount = methodSignature.numberOfArguments;
- // The invocation should always have have self and _cmd arguments.
- ASSERT(argumentCount >= 2);
+ ASSERT(firstArgument <= argumentCount);
- // We ignore self and _cmd.
- for (NSUInteger i = 2; i < argumentCount; ++i) {
+ for (NSUInteger i = firstArgument; i < argumentCount; ++i) {
const char* type = [methodSignature getArgumentTypeAtIndex:i];
switch (*type) {
@@ -520,7 +529,7 @@
// Objective-C object
case '@': {
- auto& allowedClasses = allowedArgumentClasses[i - 2];
+ auto& allowedClasses = allowedArgumentClasses[i - firstArgument];
id value = decodeObjectFromObjectStream(decoder, allowedClasses);
[invocation setArgument:&value atIndex:i];
@@ -537,31 +546,52 @@
static NSInvocation *decodeInvocation(WKRemoteObjectDecoder *decoder)
{
- NSString *selectorString = [decoder decodeObjectOfClass:[NSString class] forKey:selectorKey];
- if (!selectorString)
- [NSException raise:NSInvalidUnarchiveOperationException format:@"Invocation had no selector"];
+ SEL selector = nullptr;
+ NSMethodSignature *localMethodSignature = nil;
- SEL selector = NSSelectorFromString(selectorString);
- ASSERT(selector);
+ BOOL isReplyBlock = [decoder decodeBoolForKey:isReplyBlockKey];
- NSMethodSignature *localMethodSignature = [decoder->_interface _methodSignatureForSelector:selector];
- if (!localMethodSignature)
- [NSException raise:NSInvalidUnarchiveOperationException format:@"Selector \"%@\" is not defined in the local interface", selectorString];
+ if (isReplyBlock) {
+ if (!decoder->_replyToSelector)
+ [NSException raise:NSInvalidUnarchiveOperationException format:@"%@: Received unknown reply block", decoder];
+
+ localMethodSignature = [decoder->_interface _methodSignatureForReplyBlockOfSelector:decoder->_replyToSelector];
+ if (!localMethodSignature)
+ [NSException raise:NSInvalidUnarchiveOperationException format:@"Reply block for selector \"%s\" is not defined in the local interface", sel_getName(decoder->_replyToSelector)];
+ } else {
+ NSString *selectorString = [decoder decodeObjectOfClass:[NSString class] forKey:selectorKey];
+ if (!selectorString)
+ [NSException raise:NSInvalidUnarchiveOperationException format:@"Invocation had no selector"];
+
+ selector = NSSelectorFromString(selectorString);
+ ASSERT(selector);
+
+ localMethodSignature = [decoder->_interface _methodSignatureForSelector:selector];
+ if (!localMethodSignature)
+ [NSException raise:NSInvalidUnarchiveOperationException format:@"Selector \"%@\" is not defined in the local interface", selectorString];
+ }
NSString *typeSignature = [decoder decodeObjectOfClass:[NSString class] forKey:typeStringKey];
if (!typeSignature)
[NSException raise:NSInvalidUnarchiveOperationException format:@"Invocation had no type signature"];
NSMethodSignature *remoteMethodSignature = [NSMethodSignature signatureWithObjCTypes:typeSignature.UTF8String];
+ localMethodSignature = remoteMethodSignature;
if (![localMethodSignature isEqual:remoteMethodSignature])
- [NSException raise:NSInvalidUnarchiveOperationException format:@"Local and remote method signatures are not equal for method \"%@\"", selectorString];
+ [NSException raise:NSInvalidUnarchiveOperationException format:@"Local and remote method signatures are not equal for method \"%s\"", selector ? sel_getName(selector) : "(no selector)"];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:localMethodSignature];
- const auto& allowedClasses = [decoder->_interface _allowedArgumentClassesForSelector:selector];
- decodeInvocationArguments(decoder, invocation, allowedClasses);
+ if (isReplyBlock) {
+ const auto& allowedClasses = [decoder->_interface _allowedArgumentClassesForReplyBlockOfSelector:decoder->_replyToSelector];
+ decodeInvocationArguments(decoder, invocation, allowedClasses, 1);
+ } else {
+ const auto& allowedClasses = [decoder->_interface _allowedArgumentClassesForSelector:selector];
+ decodeInvocationArguments(decoder, invocation, allowedClasses, 2);
- [invocation setArgument:&selector atIndex:1];
+ [invocation setArgument:&selector atIndex:1];
+ }
+
return invocation;
}
@@ -588,7 +618,7 @@
validateClass(decoder, objectClass);
- if (objectClass == [NSInvocation class])
+ if (objectClass == [NSInvocation class] || objectClass == [NSBlockInvocation class])
return decodeInvocation(decoder);
if (objectClass == [NSString class])
diff --git a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterface.mm b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterface.mm
index cf07fa9..798ea70 100644
--- a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterface.mm
+++ b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterface.mm
@@ -311,6 +311,9 @@
- (NSMethodSignature *)_methodSignatureForSelector:(SEL)selector
{
+ if (!_methods.contains(selector))
+ return nil;
+
const char* types = methodArgumentTypeEncodingForSelector(_protocol, selector);
if (!types)
return nil;
@@ -318,6 +321,19 @@
return [NSMethodSignature signatureWithObjCTypes:types];
}
+- (NSMethodSignature *)_methodSignatureForReplyBlockOfSelector:(SEL)selector
+{
+ auto it = _methods.find(selector);
+ if (it == _methods.end())
+ return nil;
+
+ auto& methodInfo = it->value;
+ if (!methodInfo.replyInfo)
+ return nil;
+
+ return [NSMethodSignature signatureWithObjCTypes:methodInfo.replyInfo->replySignature.data()];
+}
+
- (const Vector<HashSet<Class>>&)_allowedArgumentClassesForSelector:(SEL)selector
{
ASSERT(_methods.contains(selector));
@@ -325,6 +341,13 @@
return _methods.find(selector)->value.allowedArgumentClasses;
}
+- (const Vector<HashSet<Class>>&)_allowedArgumentClassesForReplyBlockOfSelector:(SEL)selector
+{
+ ASSERT(_methods.contains(selector));
+
+ return _methods.find(selector)->value.replyInfo->allowedReplyClasses;
+}
+
@end
#endif // WK_API_ENABLED
diff --git a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterfaceInternal.h b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterfaceInternal.h
index dff1abb..94a40c7 100644
--- a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterfaceInternal.h
+++ b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectInterfaceInternal.h
@@ -35,7 +35,9 @@
@interface _WKRemoteObjectInterface ()
- (NSMethodSignature *)_methodSignatureForSelector:(SEL)selector;
+- (NSMethodSignature *)_methodSignatureForReplyBlockOfSelector:(SEL)selector;
- (const Vector<HashSet<Class>>&)_allowedArgumentClassesForSelector:(SEL)selector;
+- (const Vector<HashSet<Class>>&)_allowedArgumentClassesForReplyBlockOfSelector:(SEL)selector;
@end
diff --git a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistry.mm b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistry.mm
index eb3788c..9eff92b 100644
--- a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistry.mm
+++ b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistry.mm
@@ -33,22 +33,48 @@
#import "Connection.h"
#import "RemoteObjectInvocation.h"
#import "RemoteObjectRegistry.h"
+#import "UserData.h"
#import "WKConnectionRef.h"
#import "WKRemoteObject.h"
#import "WKRemoteObjectCoder.h"
#import "WKSharedAPICast.h"
-#import "WebConnection.h"
#import "_WKRemoteObjectInterface.h"
+#import <objc/runtime.h>
+
+extern "C" id __NSMakeSpecialForwardingCaptureBlock(const char *signature, void (^handler)(NSInvocation *inv));
+
+static const void* replyBlockKey = &replyBlockKey;
+
+@interface NSMethodSignature ()
+- (NSString *)_typeString;
+@end
NSString * const invocationKey = @"invocation";
using namespace WebKit;
+struct PendingReply {
+ PendingReply() = default;
+
+ PendingReply(_WKRemoteObjectInterface* interface, SEL selector, id block)
+ : interface(interface)
+ , selector(selector)
+ , block(adoptNS([block copy]))
+ {
+ }
+
+ RetainPtr<_WKRemoteObjectInterface> interface;
+ SEL selector { nullptr };
+ RetainPtr<id> block;
+};
+
@implementation _WKRemoteObjectRegistry {
std::unique_ptr<RemoteObjectRegistry> _remoteObjectRegistry;
RetainPtr<NSMapTable> _remoteObjectProxies;
HashMap<String, std::pair<RetainPtr<id>, RetainPtr<_WKRemoteObjectInterface>>> _exportedObjects;
+
+ HashMap<uint64_t, PendingReply> _pendingReplies;
}
- (void)registerExportedObject:(id)object interface:(_WKRemoteObjectInterface *)interface
@@ -116,12 +142,12 @@
if (replyInfo)
[NSException raise:NSInvalidArgumentException format:@"Only one reply block is allowed per message send. (%s)", sel_getName(invocation.selector)];
- id block = nullptr;
- [invocation getArgument:&block atIndex:i];
- if (!block)
+ id replyBlock = nullptr;
+ [invocation getArgument:&replyBlock atIndex:i];
+ if (!replyBlock)
[NSException raise:NSInvalidArgumentException format:@"A NULL reply block was passed into a message. (%s)", sel_getName(invocation.selector)];
- const char* replyBlockSignature = _Block_signature(block);
+ const char* replyBlockSignature = _Block_signature(replyBlock);
if (strcmp([NSMethodSignature signatureWithObjCTypes:replyBlockSignature].methodReturnType, "v"))
[NSException raise:NSInvalidArgumentException format:@"Return value of block argument must be 'void'. (%s)", sel_getName(invocation.selector)];
@@ -131,6 +157,9 @@
// Replace the block object so we won't try to encode it.
id null = nullptr;
[invocation setArgument:&null atIndex:i];
+
+ ASSERT(!_pendingReplies.contains(replyInfo->replyID));
+ _pendingReplies.add(replyInfo->replyID, PendingReply(interface, invocation.selector, replyBlock));
}
RetainPtr<WKRemoteObjectEncoder> encoder = adoptNS([[WKRemoteObjectEncoder alloc] init]);
@@ -159,7 +188,9 @@
return;
}
- RetainPtr<WKRemoteObjectDecoder> decoder = adoptNS([[WKRemoteObjectDecoder alloc] initWithInterface:interfaceAndObject.second.get() rootObjectDictionary:encodedInvocation]);
+ RetainPtr<_WKRemoteObjectInterface> interface = interfaceAndObject.second;
+
+ auto decoder = adoptNS([[WKRemoteObjectDecoder alloc] initWithInterface:interface.get() rootObjectDictionary:encodedInvocation replyToSelector:nullptr]);
NSInvocation *invocation = nil;
@@ -169,6 +200,38 @@
NSLog(@"Exception caught during decoding of message: %@", exception);
}
+ if (auto* replyInfo = remoteObjectInvocation.replyInfo()) {
+ NSMethodSignature *methodSignature = invocation.methodSignature;
+
+ // Look for the block argument.
+ for (NSUInteger i = 0, count = methodSignature.numberOfArguments; i < count; ++i) {
+ const char *type = [methodSignature getArgumentTypeAtIndex:i];
+
+ if (strcmp(type, "@?"))
+ continue;
+
+ // We found the block.
+ // FIXME: Validate the signature.
+ NSMethodSignature *wireBlockSignature = [NSMethodSignature signatureWithObjCTypes:replyInfo->blockSignature.utf8().data()];
+
+ RetainPtr<_WKRemoteObjectRegistry> remoteObjectRegistry = self;
+ uint64_t replyID = replyInfo->replyID;
+ id replyBlock = __NSMakeSpecialForwardingCaptureBlock(wireBlockSignature._typeString.UTF8String, [interface, remoteObjectRegistry, replyID](NSInvocation *invocation) {
+
+ auto encoder = adoptNS([[WKRemoteObjectEncoder alloc] init]);
+ [encoder encodeObject:invocation forKey:invocationKey];
+
+ remoteObjectRegistry->_remoteObjectRegistry->sendReplyBlock(replyID, UserData([encoder rootObjectDictionary]));
+ });
+
+ [invocation setArgument:&replyBlock atIndex:i];
+
+ // Make sure that the block won't be destroyed before the invocation.
+ objc_setAssociatedObject(invocation, replyBlockKey, replyBlock, OBJC_ASSOCIATION_RETAIN);
+ break;
+ }
+ }
+
invocation.target = interfaceAndObject.first.get();
@try {
@@ -178,6 +241,34 @@
}
}
+- (void)_callReplyWithID:(uint64_t)replyID blockInvocation:(const WebKit::UserData&)blockInvocation
+{
+ auto encodedInvocation = blockInvocation.object();
+ if (!encodedInvocation || encodedInvocation->type() != API::Object::Type::Dictionary)
+ return;
+
+ auto it = _pendingReplies.find(replyID);
+ if (it == _pendingReplies.end())
+ return;
+
+ auto pendingReply = it->value;
+ _pendingReplies.remove(it);
+
+ auto decoder = adoptNS([[WKRemoteObjectDecoder alloc] initWithInterface:pendingReply.interface.get() rootObjectDictionary:static_cast<API::Dictionary*>(encodedInvocation) replyToSelector:pendingReply.selector]);
+
+ NSInvocation *replyInvocation = nil;
+
+ @try {
+ replyInvocation = [decoder decodeObjectOfClass:[NSInvocation class] forKey:invocationKey];
+ } @catch (NSException *exception) {
+ NSLog(@"Exception caught during decoding of reply: %@", exception);
+ return;
+ }
+
+ [replyInvocation setTarget:pendingReply.block.get()];
+ [replyInvocation invoke];
+}
+
@end
#endif // WK_API_ENABLED
diff --git a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistryInternal.h b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistryInternal.h
index e0ee619..48ff7dd 100644
--- a/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistryInternal.h
+++ b/Source/WebKit2/Shared/API/Cocoa/_WKRemoteObjectRegistryInternal.h
@@ -34,6 +34,7 @@
namespace WebKit {
class RemoteObjectInvocation;
class RemoteObjectRegistry;
+class UserData;
}
@interface _WKRemoteObjectRegistry ()
@@ -46,6 +47,8 @@
- (void)_sendInvocation:(NSInvocation *)invocation interface:(_WKRemoteObjectInterface *)interface;
- (void)_invokeMethod:(const WebKit::RemoteObjectInvocation&)invocation;
+- (void)_callReplyWithID:(uint64_t)replyID blockInvocation:(const WebKit::UserData&)blockInvocation;
+
@end
#endif // WK_API_ENABLED
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index 7436178..4669b56 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,18 @@
+2015-11-09 Anders Carlsson <andersca@apple.com>
+
+ Add reply blocks to _WKRemoteObjectInterface similar to NSXPCConnection
+ https://bugs.webkit.org/show_bug.cgi?id=151056
+ rdar://problem/23222609
+
+ Reviewed by Tim Horton.
+
+ Update test.
+
+ * TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistry.mm:
+ (TEST):
+ * TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistryPlugIn.mm:
+ (-[RemoteObjectRegistryPlugIn sayHello:completionHandler:]):
+
2015-11-09 Tim Horton <timothy_horton@apple.com>
Add drawsBackground SPI to WKWebView, and get rid of drawsTransparentBackground from WebKit2
diff --git a/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistry.mm b/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistry.mm
index f6d7ff4..3c75a62 100644
--- a/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistry.mm
+++ b/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistry.mm
@@ -60,9 +60,14 @@
}];
TestWebKitAPI::Util::run(&isDone);
- [object sayHello:@"Hello Again!" completionHandler:^(NSString *) {
- // FIXME: Check the string here.
+ isDone = false;
+ [object sayHello:@"Hello Again!" completionHandler:^(NSString *result) {
+ EXPECT_TRUE([result isKindOfClass:[NSString class]]);
+ EXPECT_WK_STREQ(result, @"Your string was 'Hello Again!'");
+ isDone = true;
}];
+
+ TestWebKitAPI::Util::run(&isDone);
}
}
diff --git a/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistryPlugIn.mm b/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistryPlugIn.mm
index e758094..ee7f0e3 100644
--- a/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistryPlugIn.mm
+++ b/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/RemoteObjectRegistryPlugIn.mm
@@ -64,8 +64,7 @@
- (void)sayHello:(NSString *)hello completionHandler:(void (^)(NSString *))completionHandler
{
- // FIXME: Actually call this.
- // completionHandler([NSString stringWithFormat:@"Your string was '%@'", hello]);
+ completionHandler([NSString stringWithFormat:@"Your string was '%@'", hello]);
}
@end