<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[158699] trunk/Source/WebKit2</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/158699">158699</a></dd>
<dt>Author</dt> <dd>andersca@apple.com</dd>
<dt>Date</dt> <dd>2013-11-05 15:58:36 -0800 (Tue, 05 Nov 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>WKRemoteObjectDecoder should know which interface it's decoding for
https://bugs.webkit.org/show_bug.cgi?id=123833

Reviewed by Andreas Kling.

Move the interface identifier out into the root object body dictionary and make
the encoded invocation a sibling of it. That way we can look up the interface before
creating the decoder.

Also, manage exported objects and interfaces in a separate hash map.

* Shared/API/Cocoa/WKRemoteObjectCoder.h:
* Shared/API/Cocoa/WKRemoteObjectCoder.mm:
(-[WKRemoteObjectDecoder initWithInterface:rootObjectDictionary:WebKit::]):
(decodeInvocation):
(decodeObject):
* Shared/API/Cocoa/WKRemoteObjectRegistry.mm:
(-[WKRemoteObjectRegistry registerExportedObject:interface:]):
(-[WKRemoteObjectRegistry unregisterExportedObject:interface:]):
(-[WKRemoteObjectRegistry _sendInvocation:interface:]):
(-[WKRemoteObjectRegistry _handleMessageWithName:body:]):
(-[WKRemoteObjectRegistry _invokeMessageWithInterfaceIdentifier:encodedInvocation:]):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2SharedAPICocoaWKRemoteObjectCoderh">trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h</a></li>
<li><a href="#trunkSourceWebKit2SharedAPICocoaWKRemoteObjectCodermm">trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm</a></li>
<li><a href="#trunkSourceWebKit2SharedAPICocoaWKRemoteObjectRegistrymm">trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectRegistry.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (158698 => 158699)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2013-11-05 23:45:43 UTC (rev 158698)
+++ trunk/Source/WebKit2/ChangeLog        2013-11-05 23:58:36 UTC (rev 158699)
</span><span class="lines">@@ -1,5 +1,30 @@
</span><span class="cx"> 2013-11-05  Anders Carlsson  &lt;andersca@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        WKRemoteObjectDecoder should know which interface it's decoding for
+        https://bugs.webkit.org/show_bug.cgi?id=123833
+
+        Reviewed by Andreas Kling.
+
+        Move the interface identifier out into the root object body dictionary and make 
+        the encoded invocation a sibling of it. That way we can look up the interface before
+        creating the decoder.
+        
+        Also, manage exported objects and interfaces in a separate hash map.
+
+        * Shared/API/Cocoa/WKRemoteObjectCoder.h:
+        * Shared/API/Cocoa/WKRemoteObjectCoder.mm:
+        (-[WKRemoteObjectDecoder initWithInterface:rootObjectDictionary:WebKit::]):
+        (decodeInvocation):
+        (decodeObject):
+        * Shared/API/Cocoa/WKRemoteObjectRegistry.mm:
+        (-[WKRemoteObjectRegistry registerExportedObject:interface:]):
+        (-[WKRemoteObjectRegistry unregisterExportedObject:interface:]):
+        (-[WKRemoteObjectRegistry _sendInvocation:interface:]):
+        (-[WKRemoteObjectRegistry _handleMessageWithName:body:]):
+        (-[WKRemoteObjectRegistry _invokeMessageWithInterfaceIdentifier:encodedInvocation:]):
+
+2013-11-05  Anders Carlsson  &lt;andersca@apple.com&gt;
+
</ins><span class="cx">         Add support for decoding NSStrings
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=123829
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2SharedAPICocoaWKRemoteObjectCoderh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h (158698 => 158699)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h        2013-11-05 23:45:43 UTC (rev 158698)
+++ trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.h        2013-11-05 23:58:36 UTC (rev 158699)
</span><span class="lines">@@ -31,6 +31,8 @@
</span><span class="cx"> class ImmutableDictionary;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+@class WKRemoteObjectInterface;
+
</ins><span class="cx"> @interface WKRemoteObjectEncoder : NSCoder
</span><span class="cx"> 
</span><span class="cx"> - (WebKit::ImmutableDictionary*)rootObjectDictionary;
</span><span class="lines">@@ -39,7 +41,7 @@
</span><span class="cx"> 
</span><span class="cx"> @interface WKRemoteObjectDecoder : NSCoder
</span><span class="cx"> 
</span><del>-- (id)initWithRootObjectDictionary:(WebKit::ImmutableDictionary*)rootObjectDictionary;
</del><ins>+- (id)initWithInterface:(WKRemoteObjectInterface *)interface rootObjectDictionary:(const WebKit::ImmutableDictionary*)rootObjectDictionary;
</ins><span class="cx"> 
</span><span class="cx"> @end
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2SharedAPICocoaWKRemoteObjectCodermm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm (158698 => 158699)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm        2013-11-05 23:45:43 UTC (rev 158698)
+++ trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectCoder.mm        2013-11-05 23:58:36 UTC (rev 158699)
</span><span class="lines">@@ -262,19 +262,23 @@
</span><span class="cx"> @end
</span><span class="cx"> 
</span><span class="cx"> @implementation WKRemoteObjectDecoder {
</span><del>-    RefPtr&lt;ImmutableDictionary&gt; _rootDictionary;
</del><ins>+    RetainPtr&lt;WKRemoteObjectInterface&gt; _interface;
+
+    const ImmutableDictionary* _rootDictionary;
</ins><span class="cx">     const ImmutableDictionary* _currentDictionary;
</span><span class="cx"> 
</span><span class="cx">     NSSet *_allowedClasses;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-- (id)initWithRootObjectDictionary:(ImmutableDictionary*)rootObjectDictionary
</del><ins>+- (id)initWithInterface:(WKRemoteObjectInterface *)interface rootObjectDictionary:(const WebKit::ImmutableDictionary*)rootObjectDictionary
</ins><span class="cx"> {
</span><span class="cx">     if (!(self = [super init]))
</span><span class="cx">         return nil;
</span><span class="cx"> 
</span><ins>+    _interface = interface;
+
</ins><span class="cx">     _rootDictionary = rootObjectDictionary;
</span><del>-    _currentDictionary = _rootDictionary.get();
</del><ins>+    _currentDictionary = _rootDictionary;
</ins><span class="cx"> 
</span><span class="cx">     return self;
</span><span class="cx"> }
</span><span class="lines">@@ -330,6 +334,12 @@
</span><span class="cx">     [decoder validateClassSupportsSecureCoding:objectClass];
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static NSInvocation *decodeInvocation(WKRemoteObjectDecoder *decoder)
+{
+    // FIXME: Implement this.
+    return nil;
+}
+
</ins><span class="cx"> static id decodeObject(WKRemoteObjectDecoder *decoder)
</span><span class="cx"> {
</span><span class="cx">     WebString* classNameString = decoder-&gt;_currentDictionary-&gt;get&lt;WebString&gt;(classNameKey);
</span><span class="lines">@@ -344,7 +354,10 @@
</span><span class="cx"> 
</span><span class="cx">     validateClass(decoder, objectClass);
</span><span class="cx"> 
</span><del>-    RetainPtr&lt;id&gt; result = [objectClass alloc];
</del><ins>+    if (objectClass == [NSInvocation class])
+        return decodeInvocation(decoder);
+
+    RetainPtr&lt;id&gt; result = [objectClass allocWithZone:decoder.zone];
</ins><span class="cx">     if (!result)
</span><span class="cx">         [NSException raise:NSInvalidUnarchiveOperationException format:@&quot;Class \&quot;%s\&quot; returned nil from +alloc while being decoded&quot;, className.data()];
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2SharedAPICocoaWKRemoteObjectRegistrymm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectRegistry.mm (158698 => 158699)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectRegistry.mm        2013-11-05 23:45:43 UTC (rev 158698)
+++ trunk/Source/WebKit2/Shared/API/Cocoa/WKRemoteObjectRegistry.mm        2013-11-05 23:58:36 UTC (rev 158699)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> 
</span><span class="cx"> #import &quot;Connection.h&quot;
</span><span class="cx"> #import &quot;ImmutableDictionary.h&quot;
</span><ins>+#import &quot;MutableDictionary.h&quot;
</ins><span class="cx"> #import &quot;WKConnectionRef.h&quot;
</span><span class="cx"> #import &quot;WKRemoteObject.h&quot;
</span><span class="cx"> #import &quot;WKRemoteObjectCoder.h&quot;
</span><span class="lines">@@ -39,23 +40,31 @@
</span><span class="cx"> 
</span><span class="cx"> const char* const messageName = &quot;WKRemoteObjectRegistryMessage&quot;;
</span><span class="cx"> 
</span><del>-NSString * const interfaceIdentifierKey = @&quot;interfaceIdentifier&quot;;
</del><ins>+const char* const encodedInvocationKey = &quot;encodedInvocation&quot;;
+const char* const interfaceIdentifierKey = &quot;interfaceIdentifier&quot;;
</ins><span class="cx"> 
</span><ins>+NSString * const invocationKey = @&quot;invocation&quot;;
+
</ins><span class="cx"> using namespace WebKit;
</span><span class="cx"> 
</span><span class="cx"> @implementation WKRemoteObjectRegistry {
</span><span class="cx">     RefPtr&lt;WebConnection&gt; _connection;
</span><span class="cx">     RetainPtr&lt;NSMapTable&gt; _remoteObjectProxies;
</span><ins>+    HashMap&lt;String, std::pair&lt;RetainPtr&lt;id&gt;, RetainPtr&lt;WKRemoteObjectInterface&gt;&gt;&gt; _exportedObjects;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (void)registerExportedObject:(id)object interface:(WKRemoteObjectInterface *)interface
</span><span class="cx"> {
</span><del>-    // FIXME: Implement.
</del><ins>+    ASSERT(!_exportedObjects.contains(interface.identifier));
+    _exportedObjects.add(interface.identifier, std::make_pair&lt;RetainPtr&lt;id&gt;, RetainPtr&lt;WKRemoteObjectInterface&gt;&gt;(object, interface));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (void)unregisterExportedObject:(id)object interface:(WKRemoteObjectInterface *)interface
</span><span class="cx"> {
</span><del>-    // FIXME: Implement.
</del><ins>+    ASSERT(_exportedObjects.get(interface.identifier).first == object);
+    ASSERT(_exportedObjects.get(interface.identifier).second == interface);
+
+    _exportedObjects.remove(interface.identifier);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (id)remoteObjectProxyWithInterface:(WKRemoteObjectInterface *)interface
</span><span class="lines">@@ -76,11 +85,13 @@
</span><span class="cx"> - (void)_sendInvocation:(NSInvocation *)invocation interface:(WKRemoteObjectInterface *)interface
</span><span class="cx"> {
</span><span class="cx">     RetainPtr&lt;WKRemoteObjectEncoder&gt; encoder = adoptNS([[WKRemoteObjectEncoder alloc] init]);
</span><ins>+    [encoder encodeObject:invocation forKey:invocationKey];
</ins><span class="cx"> 
</span><del>-    [encoder encodeObject:interface.identifier forKey:interfaceIdentifierKey];
-    [encoder encodeObject:invocation forKey:@&quot;invocation&quot;];
</del><ins>+    RefPtr&lt;MutableDictionary&gt; body = MutableDictionary::create();
+    body-&gt;set(interfaceIdentifierKey, WebString::create(interface.identifier));
+    body-&gt;set(encodedInvocationKey, [encoder rootObjectDictionary]);
</ins><span class="cx"> 
</span><del>-    [self _sendMessageWithBody:[encoder rootObjectDictionary]];
</del><ins>+    [self _sendMessageWithBody:body.release()];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (void)_sendMessageWithBody:(PassRefPtr&lt;ImmutableDictionary&gt;)body
</span><span class="lines">@@ -110,19 +121,32 @@
</span><span class="cx">     if (!toImpl(body) || toImpl(body)-&gt;type() != APIObject::TypeDictionary)
</span><span class="cx">         return NO;
</span><span class="cx"> 
</span><del>-    [self _invokeMessageWithBody:toImpl((WKDictionaryRef)body)];
</del><ins>+    const ImmutableDictionary* dictionary = toImpl(static_cast&lt;WKDictionaryRef&gt;(body));
+
+    WebString* interfaceIdentifier = dictionary-&gt;get&lt;WebString&gt;(interfaceIdentifierKey);
+    if (!interfaceIdentifier)
+        return NO;
+
+    const ImmutableDictionary* encodedInvocation = dictionary-&gt;get&lt;ImmutableDictionary&gt;(encodedInvocationKey);
+    if (!encodedInvocationKey)
+        return NO;
+
+    [self _invokeMessageWithInterfaceIdentifier:interfaceIdentifier-&gt;string() encodedInvocation:encodedInvocation];
</ins><span class="cx">     return YES;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-- (void)_invokeMessageWithBody:(ImmutableDictionary*)body
</del><ins>+- (void)_invokeMessageWithInterfaceIdentifier:(const String&amp;)interfaceIdentifier encodedInvocation:(const ImmutableDictionary*)encodedInvocation
</ins><span class="cx"> {
</span><del>-    RetainPtr&lt;WKRemoteObjectDecoder&gt; decoder = adoptNS([[WKRemoteObjectDecoder alloc] initWithRootObjectDictionary:body]);
</del><ins>+    auto interfaceAndObject = _exportedObjects.get(interfaceIdentifier);
+    if (!interfaceAndObject.second) {
+        NSLog(@&quot;Did not find a registered object for the interface \&quot;%@\&quot;&quot;, (NSString *)interfaceIdentifier);
+        return;
+    }
</ins><span class="cx"> 
</span><del>-    NSString *interfaceIdentifier = nil;
</del><ins>+    RetainPtr&lt;WKRemoteObjectDecoder&gt; decoder = adoptNS([[WKRemoteObjectDecoder alloc] initWithInterface:interfaceAndObject.first.get() rootObjectDictionary:encodedInvocation]);
</ins><span class="cx"> 
</span><del>-    // FIXME: Decode the invocation.
</del><span class="cx">     @try {
</span><del>-        interfaceIdentifier = [decoder decodeObjectOfClass:[NSString class] forKey:interfaceIdentifierKey];
</del><ins>+        // FIXME: Decode the invocation.
</ins><span class="cx">     } @catch (NSException *exception) {
</span><span class="cx">         NSLog(@&quot;Exception caught during decoding of message: %@&quot;, exception);
</span><span class="cx">     }
</span></span></pre>
</div>
</div>

</body>
</html>