objective-cfoundationxpcnsxpcconnection

Pass xpc_object_t through NSXPCConnection


I am new to XPC service programming, and I am trying to share a IOSurface created in a XPC service to the host application. I can setup a simple host and service using NSXPCConnection API, and now I think I am supposed to use these two methods (found in IOSurfaceAPI.h) to pass the IOSurface to the host:

IOSurfaceRef IOSurfaceLookupFromXPCObject(xpc_object_t xobj);
xpc_object_t IOSurfaceCreateXPCObject(IOSurfaceRef aSurface);

Is there a way I can pass the xpc_object_t using the NSXPCConnection API, or should I work with the C API (as defined in xpc.h)?


Solution

  • Good question. The "Daemons and Services Programming Guide" states that XPC-proxied methods can handle only the following data types:

    xpc_object_t is just a typedef for void *, so it doesn't meet any of those criteria, and I don't see any way to wrap it in something that does. (Trying to convert it to data, or cast it to an appropriately-sized int, won't work.)

    On the other hand, on 10.8, xpc_object_t also is enough of an Objective-C object to be retained/released with ARC, to be added to Objective-C collections, etc.

    It would be easy enough to try passing an xpc_object_t over your protocol, just to see if Apple left an undocumented loophole. Try it!

    Otherwise, the lower-level XPC API will definitely work:

     xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
     xpc_object_t xpcSurface = IOSurfaceCreateXPCObject(aSurface);
     xpc_dictionary_set_value(message, "surface", xpcSurface);
     xpc_connection_send_message(connection, message);