2 * Copyright (C) 2013 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "RemoteInspectorXPCConnection.h"
29 #if ENABLE(REMOTE_INSPECTOR)
31 #import <Foundation/Foundation.h>
32 #import <wtf/Assertions.h>
34 #import <wtf/RetainPtr.h>
35 #import <wtf/spi/darwin/XPCSPI.h>
37 #if __has_include(<CoreFoundation/CFXPCBridge.h>)
38 #import <CoreFoundation/CFXPCBridge.h>
41 xpc_object_t _CFXPCCreateXPCMessageWithCFObject(CFTypeRef);
42 CFTypeRef _CFXPCCreateCFObjectFromXPCMessage(xpc_object_t);
48 // Constants private to this file for message serialization on both ends.
49 #define RemoteInspectorXPCConnectionMessageNameKey @"messageName"
50 #define RemoteInspectorXPCConnectionUserInfoKey @"userInfo"
51 #define RemoteInspectorXPCConnectionSerializedMessageKey "msgData"
53 RemoteInspectorXPCConnection::RemoteInspectorXPCConnection(xpc_connection_t connection, dispatch_queue_t queue, Client* client)
54 : m_connection(connection)
59 dispatch_retain(m_queue);
61 xpc_retain(m_connection);
62 xpc_connection_set_target_queue(m_connection, m_queue);
63 xpc_connection_set_event_handler(m_connection, ^(xpc_object_t object) {
67 // Balanced by deref when the xpc_connection receives XPC_ERROR_CONNECTION_INVALID.
70 xpc_connection_resume(m_connection);
73 RemoteInspectorXPCConnection::~RemoteInspectorXPCConnection()
76 ASSERT(!m_connection);
80 void RemoteInspectorXPCConnection::close()
82 std::lock_guard<std::mutex> lock(m_mutex);
86 void RemoteInspectorXPCConnection::closeFromMessage()
91 dispatch_async(m_queue, ^{
92 std::lock_guard<std::mutex> lock(m_mutex);
93 // This will trigger one last XPC_ERROR_CONNECTION_INVALID event on the queue and deref us.
98 void RemoteInspectorXPCConnection::closeOnQueue()
101 xpc_connection_cancel(m_connection);
102 xpc_release(m_connection);
103 m_connection = nullptr;
107 dispatch_release(m_queue);
112 NSDictionary *RemoteInspectorXPCConnection::deserializeMessage(xpc_object_t object)
114 if (xpc_get_type(object) != XPC_TYPE_DICTIONARY)
117 xpc_object_t xpcDictionary = xpc_dictionary_get_value(object, RemoteInspectorXPCConnectionSerializedMessageKey);
118 if (!xpcDictionary || xpc_get_type(xpcDictionary) != XPC_TYPE_DICTIONARY) {
119 std::lock_guard<std::mutex> lock(m_mutex);
121 m_client->xpcConnectionUnhandledMessage(this, object);
125 RetainPtr<CFDictionaryRef> dictionary = adoptCF((CFDictionaryRef)_CFXPCCreateCFObjectFromXPCMessage(xpcDictionary));
126 ASSERT_WITH_MESSAGE(dictionary, "Unable to deserialize xpc message");
127 return (NSDictionary *)dictionary.autorelease();
130 void RemoteInspectorXPCConnection::handleEvent(xpc_object_t object)
132 if (xpc_get_type(object) == XPC_TYPE_ERROR) {
134 std::lock_guard<std::mutex> lock(m_mutex);
136 m_client->xpcConnectionFailed(this);
143 if (object == XPC_ERROR_CONNECTION_INVALID) {
144 // This is the last event we will ever receive from the connection.
145 // This balances the ref() in the constructor.
151 NSDictionary *dataDictionary = deserializeMessage(object);
155 NSString *message = [dataDictionary objectForKey:RemoteInspectorXPCConnectionMessageNameKey];
156 NSDictionary *userInfo = [dataDictionary objectForKey:RemoteInspectorXPCConnectionUserInfoKey];
157 std::lock_guard<std::mutex> lock(m_mutex);
159 m_client->xpcConnectionReceivedMessage(this, message, userInfo);
162 void RemoteInspectorXPCConnection::sendMessage(NSString *messageName, NSDictionary *userInfo)
168 NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObject:messageName forKey:RemoteInspectorXPCConnectionMessageNameKey];
170 [dictionary setObject:userInfo forKey:RemoteInspectorXPCConnectionUserInfoKey];
172 xpc_object_t xpcDictionary = _CFXPCCreateXPCMessageWithCFObject((CFDictionaryRef)dictionary);
173 ASSERT_WITH_MESSAGE(xpcDictionary && xpc_get_type(xpcDictionary) == XPC_TYPE_DICTIONARY, "Unable to serialize xpc message");
177 xpc_object_t msg = xpc_dictionary_create(nullptr, nullptr, 0);
178 xpc_dictionary_set_value(msg, RemoteInspectorXPCConnectionSerializedMessageKey, xpcDictionary);
179 xpc_release(xpcDictionary);
181 xpc_connection_send_message(m_connection, msg);
186 } // namespace Inspector
188 #endif // ENABLE(REMOTE_INSPECTOR)