]> git.saurik.com Git - apple/javascriptcore.git/blob - inspector/remote/RemoteInspectorXPCConnection.mm
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / inspector / remote / RemoteInspectorXPCConnection.mm
1 /*
2 * Copyright (C) 2013 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #import "config.h"
27 #import "RemoteInspectorXPCConnection.h"
28
29 #if ENABLE(REMOTE_INSPECTOR)
30
31 #import <Foundation/Foundation.h>
32 #import <wtf/Assertions.h>
33 #import <wtf/Ref.h>
34 #import <wtf/RetainPtr.h>
35 #import <wtf/spi/darwin/XPCSPI.h>
36
37 #if __has_include(<CoreFoundation/CFXPCBridge.h>)
38 #import <CoreFoundation/CFXPCBridge.h>
39 #else
40 extern "C" {
41 xpc_object_t _CFXPCCreateXPCMessageWithCFObject(CFTypeRef);
42 CFTypeRef _CFXPCCreateCFObjectFromXPCMessage(xpc_object_t);
43 }
44 #endif
45
46 namespace Inspector {
47
48 // Constants private to this file for message serialization on both ends.
49 #define RemoteInspectorXPCConnectionMessageNameKey @"messageName"
50 #define RemoteInspectorXPCConnectionUserInfoKey @"userInfo"
51 #define RemoteInspectorXPCConnectionSerializedMessageKey "msgData"
52
53 RemoteInspectorXPCConnection::RemoteInspectorXPCConnection(xpc_connection_t connection, dispatch_queue_t queue, Client* client)
54 : m_connection(connection)
55 , m_queue(queue)
56 , m_client(client)
57 , m_closed(false)
58 {
59 dispatch_retain(m_queue);
60
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) {
64 handleEvent(object);
65 });
66
67 // Balanced by deref when the xpc_connection receives XPC_ERROR_CONNECTION_INVALID.
68 ref();
69
70 xpc_connection_resume(m_connection);
71 }
72
73 RemoteInspectorXPCConnection::~RemoteInspectorXPCConnection()
74 {
75 ASSERT(!m_client);
76 ASSERT(!m_connection);
77 ASSERT(m_closed);
78 }
79
80 void RemoteInspectorXPCConnection::close()
81 {
82 std::lock_guard<std::mutex> lock(m_mutex);
83 closeFromMessage();
84 }
85
86 void RemoteInspectorXPCConnection::closeFromMessage()
87 {
88 m_closed = true;
89 m_client = nullptr;
90
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.
94 closeOnQueue();
95 });
96 }
97
98 void RemoteInspectorXPCConnection::closeOnQueue()
99 {
100 if (m_connection) {
101 xpc_connection_cancel(m_connection);
102 xpc_release(m_connection);
103 m_connection = nullptr;
104 }
105
106 if (m_queue) {
107 dispatch_release(m_queue);
108 m_queue = nullptr;
109 }
110 }
111
112 NSDictionary *RemoteInspectorXPCConnection::deserializeMessage(xpc_object_t object)
113 {
114 if (xpc_get_type(object) != XPC_TYPE_DICTIONARY)
115 return nil;
116
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);
120 if (m_client)
121 m_client->xpcConnectionUnhandledMessage(this, object);
122 return nil;
123 }
124
125 RetainPtr<CFDictionaryRef> dictionary = adoptCF((CFDictionaryRef)_CFXPCCreateCFObjectFromXPCMessage(xpcDictionary));
126 ASSERT_WITH_MESSAGE(dictionary, "Unable to deserialize xpc message");
127 return (NSDictionary *)dictionary.autorelease();
128 }
129
130 void RemoteInspectorXPCConnection::handleEvent(xpc_object_t object)
131 {
132 if (xpc_get_type(object) == XPC_TYPE_ERROR) {
133 {
134 std::lock_guard<std::mutex> lock(m_mutex);
135 if (m_client)
136 m_client->xpcConnectionFailed(this);
137
138 m_closed = true;
139 m_client = nullptr;
140 closeOnQueue();
141 }
142
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.
146 deref();
147 }
148 return;
149 }
150
151 NSDictionary *dataDictionary = deserializeMessage(object);
152 if (!dataDictionary)
153 return;
154
155 NSString *message = [dataDictionary objectForKey:RemoteInspectorXPCConnectionMessageNameKey];
156 NSDictionary *userInfo = [dataDictionary objectForKey:RemoteInspectorXPCConnectionUserInfoKey];
157 std::lock_guard<std::mutex> lock(m_mutex);
158 if (m_client)
159 m_client->xpcConnectionReceivedMessage(this, message, userInfo);
160 }
161
162 void RemoteInspectorXPCConnection::sendMessage(NSString *messageName, NSDictionary *userInfo)
163 {
164 ASSERT(!m_closed);
165 if (m_closed)
166 return;
167
168 NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObject:messageName forKey:RemoteInspectorXPCConnectionMessageNameKey];
169 if (userInfo)
170 [dictionary setObject:userInfo forKey:RemoteInspectorXPCConnectionUserInfoKey];
171
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");
174 if (!xpcDictionary)
175 return;
176
177 xpc_object_t msg = xpc_dictionary_create(nullptr, nullptr, 0);
178 xpc_dictionary_set_value(msg, RemoteInspectorXPCConnectionSerializedMessageKey, xpcDictionary);
179 xpc_release(xpcDictionary);
180
181 xpc_connection_send_message(m_connection, msg);
182
183 xpc_release(msg);
184 }
185
186 } // namespace Inspector
187
188 #endif // ENABLE(REMOTE_INSPECTOR)