Title: [133558] trunk/Source/WebKit2
Revision
133558
Author
[email protected]
Date
2012-11-05 21:52:30 -0800 (Mon, 05 Nov 2012)

Log Message

Need a way to send and receive messages between the WKWebProcessPlugIn object and the client
https://bugs.webkit.org/show_bug.cgi?id=101282

Reviewed by Anders Carlsson.

API wise, this adds:
    - A connection property on the WKWebProcessPlugInController
    - A method to send messages across a WKConnection (sendMessageWithName:body).

It also implements message sending and receiving for WKConnection. Message bodies are currently
restricted to being object graphs with values that are property list types, but in the future, should
support all types conforming to the NSSecureCoding protocol.

* Shared/WebConnection.cpp:
(WebKit::WebConnection::postMessage):
(WebKit::WebConnection::handleMessage):
* Shared/WebConnection.h:
(WebConnection):
* Shared/WebConnection.messages.in:
Switch HandleMessage to be a variadic message to avoid an extra copy and support attachments correctly.

* Shared/mac/ArgumentCodersMac.h:
* Shared/mac/ArgumentCodersMac.mm:
(CoreIPC::typeFromObject):
(CoreIPC::encode):
(CoreIPC::decode):
Add support for encoding/decoding NSArray, NSDate and NSData.

* UIProcess/API/mac/WKConnection.h:
* UIProcess/API/mac/WKConnection.mm:
(-[WKConnection sendMessageWithName:body:]):
(didReceiveMessage):
Implement message sending and receiving.

* WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.h:
* WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.mm:
(-[WKWebProcessPlugInController _initWithPrincipalClassInstance:bundleRef:]):
(-[WKWebProcessPlugInController connection]):
Add a wrapper for the WKConnectionRef vended via WKBundleGetApplicationConnection() and expose it as
API on the WKWebProcessPlugInController.

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (133557 => 133558)


--- trunk/Source/WebKit2/ChangeLog	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/ChangeLog	2012-11-06 05:52:30 UTC (rev 133558)
@@ -1,3 +1,46 @@
+2012-11-05  Sam Weinig  <[email protected]>
+
+        Need a way to send and receive messages between the WKWebProcessPlugIn object and the client
+        https://bugs.webkit.org/show_bug.cgi?id=101282
+
+        Reviewed by Anders Carlsson.
+
+        API wise, this adds:
+            - A connection property on the WKWebProcessPlugInController
+            - A method to send messages across a WKConnection (sendMessageWithName:body).
+
+        It also implements message sending and receiving for WKConnection. Message bodies are currently
+        restricted to being object graphs with values that are property list types, but in the future, should
+        support all types conforming to the NSSecureCoding protocol.
+
+        * Shared/WebConnection.cpp:
+        (WebKit::WebConnection::postMessage):
+        (WebKit::WebConnection::handleMessage):
+        * Shared/WebConnection.h:
+        (WebConnection):
+        * Shared/WebConnection.messages.in:
+        Switch HandleMessage to be a variadic message to avoid an extra copy and support attachments correctly.
+
+        * Shared/mac/ArgumentCodersMac.h:
+        * Shared/mac/ArgumentCodersMac.mm:
+        (CoreIPC::typeFromObject):
+        (CoreIPC::encode):
+        (CoreIPC::decode):
+        Add support for encoding/decoding NSArray, NSDate and NSData.
+
+        * UIProcess/API/mac/WKConnection.h:
+        * UIProcess/API/mac/WKConnection.mm:
+        (-[WKConnection sendMessageWithName:body:]):
+        (didReceiveMessage):
+        Implement message sending and receiving.
+
+        * WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.h:
+        * WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.mm:
+        (-[WKWebProcessPlugInController _initWithPrincipalClassInstance:bundleRef:]):
+        (-[WKWebProcessPlugInController connection]):
+        Add a wrapper for the WKConnectionRef vended via WKBundleGetApplicationConnection() and expose it as
+        API on the WKWebProcessPlugInController.
+
 2012-11-05  Jinwoo Song  <[email protected]>
 
         [EFL][WK2] Fix wrong coding style in the boilerplates

Modified: trunk/Source/WebKit2/Shared/WebConnection.cpp (133557 => 133558)


--- trunk/Source/WebKit2/Shared/WebConnection.cpp	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/Shared/WebConnection.cpp	2012-11-06 05:52:30 UTC (rev 133558)
@@ -53,23 +53,21 @@
     if (!m_connection)
         return;
 
-    OwnPtr<CoreIPC::ArgumentEncoder> messageData = CoreIPC::ArgumentEncoder::create();
-    messageData->encode(messageName);
-    encodeMessageBody(*messageData, messageBody);
+    OwnPtr<CoreIPC::MessageEncoder> encoder = CoreIPC::MessageEncoder::create(Messages::WebConnection::HandleMessage::receiverName(), Messages::WebConnection::HandleMessage::name(), 0);
+    encoder->encode(messageName);
+    encodeMessageBody(*encoder, messageBody);
 
-    m_connection->send(Messages::WebConnection::HandleMessage(CoreIPC::DataReference(messageData->buffer(), messageData->bufferSize())), 0);
+    m_connection->sendMessage(CoreIPC::MessageID(Messages::WebConnection::HandleMessage::messageID), encoder.release());
 }
 
-void WebConnection::handleMessage(const CoreIPC::DataReference& messageData)
+void WebConnection::handleMessage(CoreIPC::MessageDecoder& decoder)
 {
-    OwnPtr<CoreIPC::ArgumentDecoder> decoder = CoreIPC::ArgumentDecoder::create(messageData.data(), messageData.size());
-
     String messageName;
-    if (!decoder->decode(messageName))
+    if (!decoder.decode(messageName))
         return;
 
     RefPtr<APIObject> messageBody;
-    if (!decodeMessageBody(*decoder, messageBody))
+    if (!decodeMessageBody(decoder, messageBody))
         return;
 
     m_client.didReceiveMessage(this, messageName, messageBody.get());

Modified: trunk/Source/WebKit2/Shared/WebConnection.h (133557 => 133558)


--- trunk/Source/WebKit2/Shared/WebConnection.h	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/Shared/WebConnection.h	2012-11-06 05:52:30 UTC (rev 133558)
@@ -63,7 +63,7 @@
 
     // Implemented in generated WebConnectionMessageReceiver.cpp
     void didReceiveWebConnectionMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::MessageDecoder&);
-    void handleMessage(const CoreIPC::DataReference& messageData);
+    void handleMessage(CoreIPC::MessageDecoder&);
 
     RefPtr<CoreIPC::Connection> m_connection;
     WebConnectionClient m_client;

Modified: trunk/Source/WebKit2/Shared/WebConnection.messages.in (133557 => 133558)


--- trunk/Source/WebKit2/Shared/WebConnection.messages.in	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/Shared/WebConnection.messages.in	2012-11-06 05:52:30 UTC (rev 133558)
@@ -21,5 +21,5 @@
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 messages -> WebConnection {
-    HandleMessage(CoreIPC::DataReference messageData)
+    HandleMessage() Variadic
 }

Modified: trunk/Source/WebKit2/Shared/mac/ArgumentCodersMac.h (133557 => 133558)


--- trunk/Source/WebKit2/Shared/mac/ArgumentCodersMac.h	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/Shared/mac/ArgumentCodersMac.h	2012-11-06 05:52:30 UTC (rev 133558)
@@ -33,6 +33,10 @@
 class ArgumentEncoder;
 class ArgumentDecoder;
 
+// id
+void encode(ArgumentEncoder&, id);
+bool decode(ArgumentDecoder*, RetainPtr<id>&);
+
 // NSAttributedString
 void encode(ArgumentEncoder&, NSAttributedString *);
 bool decode(ArgumentDecoder*, RetainPtr<NSAttributedString>&);
@@ -47,6 +51,10 @@
 void encode(ArgumentEncoder&, NSDictionary *);
 bool decode(ArgumentDecoder*, RetainPtr<NSDictionary>&);
 
+// NSArray
+void encode(ArgumentEncoder&, NSArray *);
+bool decode(ArgumentDecoder*, RetainPtr<NSArray>&);
+
 #if USE(APPKIT)
 // NSFont
 void encode(ArgumentEncoder&, NSFont *);
@@ -61,6 +69,14 @@
 void encode(ArgumentEncoder&, NSString *);
 bool decode(ArgumentDecoder*, RetainPtr<NSString>&);
 
+// NSDate
+void encode(ArgumentEncoder&, NSDate *);
+bool decode(ArgumentDecoder*, RetainPtr<NSDate>&);
+
+// NSData
+void encode(ArgumentEncoder&, NSData *);
+bool decode(ArgumentDecoder*, RetainPtr<NSData>&);
+
 } // namespace CoreIPC
 
 #endif // ArgumentCodersMac_h

Modified: trunk/Source/WebKit2/Shared/mac/ArgumentCodersMac.mm (133557 => 133558)


--- trunk/Source/WebKit2/Shared/mac/ArgumentCodersMac.mm	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/Shared/mac/ArgumentCodersMac.mm	2012-11-06 05:52:30 UTC (rev 133558)
@@ -43,11 +43,14 @@
     NSColorType,
 #endif
     NSDictionaryType,
+    NSArrayType,
 #if USE(APPKIT)
     NSFontType,
 #endif
     NSNumberType,
     NSStringType,
+    NSDateType,
+    NSDataType,
     Unknown,
 };
 
@@ -71,12 +74,18 @@
         return NSNumberType;
     if ([object isKindOfClass:[NSString class]])
         return NSStringType;
+    if ([object isKindOfClass:[NSArray class]])
+        return NSArrayType;
+    if ([object isKindOfClass:[NSDate class]])
+        return NSDateType;
+    if ([object isKindOfClass:[NSData class]])
+        return NSDataType;
 
     ASSERT_NOT_REACHED();
     return Unknown;
 }
 
-static void encode(ArgumentEncoder& encoder, id object)
+void encode(ArgumentEncoder& encoder, id object)
 {
     NSType type = typeFromObject(object);
     encoder.encodeEnum(type);
@@ -104,6 +113,15 @@
     case NSStringType:
         encode(encoder, static_cast<NSString *>(object));
         return;
+    case NSArrayType:
+        encode(encoder, static_cast<NSArray *>(object));
+        return;
+    case NSDateType:
+        encode(encoder, static_cast<NSDate *>(object));
+        return;
+    case NSDataType:
+        encode(encoder, static_cast<NSData *>(object));
+        return;
     case Unknown:
         break;
     }
@@ -111,7 +129,7 @@
     ASSERT_NOT_REACHED();
 }
 
-static bool decode(ArgumentDecoder* decoder, RetainPtr<id>& result)
+bool decode(ArgumentDecoder* decoder, RetainPtr<id>& result)
 {
     NSType type;
     if (!decoder->decodeEnum(type))
@@ -164,6 +182,27 @@
         result = string;
         return true;
     }
+    case NSArrayType: {
+        RetainPtr<NSArray> array;
+        if (!decode(decoder, array))
+            return false;
+        result = array;
+        return true;
+    }
+    case NSDateType: {
+        RetainPtr<NSDate> date;
+        if (!decode(decoder, date))
+            return false;
+        result = date;
+        return true;
+    }
+    case NSDataType: {
+        RetainPtr<NSData> data;
+        if (!decode(decoder, data))
+            return false;
+        result = data;
+        return true;
+    }
     case Unknown:
         ASSERT_NOT_REACHED();
         return false;
@@ -359,4 +398,69 @@
     return true;
 }
 
+void encode(ArgumentEncoder& encoder, NSArray *array)
+{
+    NSUInteger size = [array count];
+    encoder << static_cast<uint64_t>(size);
+
+    for (NSUInteger i = 0; i < size; ++i) {
+        id value = [array objectAtIndex:i];
+
+        // Ignore values we don't recognize.
+        if (typeFromObject(value) == Unknown)
+            continue;
+
+        encode(encoder, value);
+    }
+}
+
+bool decode(ArgumentDecoder* decoder, RetainPtr<NSArray>& result)
+{
+    uint64_t size;
+    if (!decoder->decodeUInt64(size))
+        return false;
+
+    RetainPtr<NSMutableArray> array = adoptNS([[NSMutableArray alloc] initWithCapacity:size]);
+    for (uint64_t i = 0; i < size; ++i) {
+        RetainPtr<id> value;
+        if (!decode(decoder, value))
+            return false;
+
+        [array.get() addObject:value.get()];
+    }
+
+    result.adoptNS(array.leakRef());
+    return true;
+}
+
+void encode(ArgumentEncoder& encoder, NSDate *date)
+{
+    encode(encoder, (CFDateRef)date);
+}
+
+bool decode(ArgumentDecoder* decoder, RetainPtr<NSDate>& result)
+{
+    RetainPtr<CFDateRef> date;
+    if (!decode(decoder, date))
+        return false;
+
+    result.adoptCF((NSDate *)date.leakRef());
+    return true;
+}
+
+void encode(ArgumentEncoder& encoder, NSData *data)
+{
+    encode(encoder, (CFDataRef)data);
+}
+
+bool decode(ArgumentDecoder* decoder, RetainPtr<NSData>& result)
+{
+    RetainPtr<CFDataRef> data;
+    if (!decode(decoder, data))
+        return false;
+
+    result.adoptCF((NSData *)data.leakRef());
+    return true;
+}
+
 } // namespace CoreIPC

Modified: trunk/Source/WebKit2/UIProcess/API/mac/WKConnection.h (133557 => 133558)


--- trunk/Source/WebKit2/UIProcess/API/mac/WKConnection.h	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/UIProcess/API/mac/WKConnection.h	2012-11-06 05:52:30 UTC (rev 133558)
@@ -41,6 +41,8 @@
     WKConnectionData *_data;
 }
 
+- (void)sendMessageWithName:(NSString *)messageName body:(id)messageBody;
+
 #pragma mark Delegates
 
 @property(assign) id<WKConnectionDelegate> delegate;

Modified: trunk/Source/WebKit2/UIProcess/API/mac/WKConnection.mm (133557 => 133558)


--- trunk/Source/WebKit2/UIProcess/API/mac/WKConnection.mm	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/UIProcess/API/mac/WKConnection.mm	2012-11-06 05:52:30 UTC (rev 133558)
@@ -27,11 +27,18 @@
 #import "WKConnection.h"
 #import "WKConnectionInternal.h"
 
+#import "ArgumentCodersMac.h"
+#import "ArgumentDecoder.h"
+#import "ArgumentEncoder.h"
 #import "WKConnectionRef.h"
+#import "WKData.h"
 #import "WKRetainPtr.h"
+#import "WKString.h"
 #import "WKStringCF.h"
 #import <wtf/RetainPtr.h>
 
+using namespace WebKit;
+
 @interface WKConnectionData : NSObject {
 @public
     // Underlying connection object.
@@ -55,6 +62,17 @@
     [super dealloc];
 }
 
+- (void)sendMessageWithName:(NSString *)messageName body:(id)messageBody
+{
+    OwnPtr<CoreIPC::ArgumentEncoder> messageData = CoreIPC::ArgumentEncoder::create();
+    encode(*messageData, messageBody);
+
+    WKRetainPtr<WKStringRef> wkMessageName = adoptWK(WKStringCreateWithCFString((CFStringRef)messageName));
+    WKRetainPtr<WKDataRef> wkMessageBody = adoptWK(WKDataCreate(messageData->buffer(), messageData->bufferSize()));
+
+    WKConnectionPostMessage(_data->_connectionRef.get(), wkMessageName.get(), wkMessageBody.get());
+}
+
 #pragma mark Delegates
 
 - (id<WKConnectionDelegate>)delegate
@@ -75,9 +93,17 @@
 {
     WKConnection *connection = (WKConnection *)clientInfo;
     if ([connection.delegate respondsToSelector:@selector(connection:didReceiveMessageWithName:body:)]) {
-        // FIXME: Add messageBody conversion.
         RetainPtr<CFStringRef> cfMessageName = adoptCF(WKStringCopyCFString(kCFAllocatorDefault, messageName));
-        [connection.delegate connection:connection didReceiveMessageWithName:(NSString *)cfMessageName.get() body:nil];
+
+        WKDataRef messageData = (WKDataRef)messageBody;
+        OwnPtr<CoreIPC::ArgumentDecoder> decoder = CoreIPC::ArgumentDecoder::create(WKDataGetBytes(messageData), WKDataGetSize(messageData));
+
+        RetainPtr<id> messageDictionary;
+        // FIXME: Don't just silently drop decoding failures on the ground.
+        if (!decode(decoder.get(), messageDictionary))
+            return;
+
+        [connection.delegate connection:connection didReceiveMessageWithName:(NSString *)cfMessageName.get() body:messageDictionary.get()];
     }
 }
 

Modified: trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.h (133557 => 133558)


--- trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.h	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.h	2012-11-06 05:52:30 UTC (rev 133558)
@@ -28,6 +28,7 @@
 #import <Foundation/Foundation.h>
 #import <WebKit2/WKBase.h>
 
+@class WKConnection;
 @class WKWebProcessPlugInController;
 @class WKWebProcessPlugInBrowserContextController;
 
@@ -40,6 +41,9 @@
 
 WK_EXPORT
 @interface WKWebProcessPlugInController : NSObject
+
+@property(readonly) WKConnection *connection;
+
 @end
 
 #endif // defined(__LP64__) && defined(__clang__)

Modified: trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.mm (133557 => 133558)


--- trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.mm	2012-11-06 05:04:18 UTC (rev 133557)
+++ trunk/Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.mm	2012-11-06 05:52:30 UTC (rev 133558)
@@ -31,6 +31,7 @@
 #import "WKWebProcessPlugInInternal.h"
 
 #import "InjectedBundle.h"
+#import "WKConnectionInternal.h"
 #import "WKBundle.h"
 #import "WKBundleAPICast.h"
 #import "WKRetainPtr.h"
@@ -43,6 +44,7 @@
     RetainPtr<id<WKWebProcessPlugIn> > _principalClassInstance;
     WKRetainPtr<WKBundleRef> _bundleRef;
     BundlePageWrapperCache _bundlePageWrapperCache;
+    RetainPtr<WKConnection *> _connectionWrapper;
 }
 @end
 
@@ -111,6 +113,7 @@
 
     _principalClassInstance = principalClassInstance;
     _bundleRef = bundleRef;
+    _connectionWrapper = adoptNS([[WKConnection alloc] _initWithConnectionRef:WKBundleGetApplicationConnection(_bundleRef.get())]);
 
     ASSERT_WITH_MESSAGE(!sharedInstance, "WKWebProcessPlugInController initialized multiple times.");
     sharedInstance = self;
@@ -124,6 +127,11 @@
 
 @implementation WKWebProcessPlugInController
 
+- (WKConnection *)connection
+{
+    return _connectionWrapper.get();
+}
+
 @end
 
 #endif // defined(__LP64__) && defined(__clang__)
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to