[iOS] Eliminate race between XPC connection queue and Notification queue
[WebKit.git] / Source / JavaScriptCore / inspector / remote / RemoteInspector.h
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 #if ENABLE(REMOTE_INSPECTOR)
27
28 #ifndef RemoteInspector_h
29 #define RemoteInspector_h
30
31 #import "RemoteInspectorXPCConnection.h"
32 #import <wtf/Forward.h>
33 #import <wtf/HashMap.h>
34 #import <wtf/NeverDestroyed.h>
35 #import <wtf/Threading.h>
36
37 OBJC_CLASS NSDictionary;
38 OBJC_CLASS NSString;
39
40 namespace Inspector {
41
42 class RemoteInspectorDebuggable;
43 class RemoteInspectorDebuggableConnection;
44 struct RemoteInspectorDebuggableInfo;
45
46 class JS_EXPORT_PRIVATE RemoteInspector final : public RemoteInspectorXPCConnection::Client {
47 public:
48     static void startDisabled();
49     static RemoteInspector& shared();
50     friend class NeverDestroyed<RemoteInspector>;
51
52     void registerDebuggable(RemoteInspectorDebuggable*);
53     void unregisterDebuggable(RemoteInspectorDebuggable*);
54     void updateDebuggable(RemoteInspectorDebuggable*);
55     void sendMessageToRemoteFrontend(unsigned identifier, const String& message);
56
57     bool enabled() const { return m_enabled; }
58     bool hasActiveDebugSession() const { return m_hasActiveDebugSession; }
59
60     void start();
61     void stop();
62
63 private:
64     RemoteInspector();
65
66     unsigned nextAvailableIdentifier();
67
68     void setupXPCConnectionIfNeeded();
69
70     NSDictionary *listingForDebuggable(const RemoteInspectorDebuggableInfo&) const;
71     void pushListingNow();
72     void pushListingSoon();
73
74     void updateHasActiveDebugSession();
75
76     virtual void xpcConnectionReceivedMessage(RemoteInspectorXPCConnection*, NSString *messageName, NSDictionary *userInfo) override;
77     virtual void xpcConnectionFailed(RemoteInspectorXPCConnection*) override;
78     virtual void xpcConnectionUnhandledMessage(RemoteInspectorXPCConnection*, xpc_object_t) override;
79
80     void receivedSetupMessage(NSDictionary *userInfo);
81     void receivedDataMessage(NSDictionary *userInfo);
82     void receivedDidCloseMessage(NSDictionary *userInfo);
83     void receivedGetListingMessage(NSDictionary *userInfo);
84     void receivedIndicateMessage(NSDictionary *userInfo);
85     void receivedConnectionDiedMessage(NSDictionary *userInfo);
86
87     static bool startEnabled;
88
89     // Debuggables can be registered from any thread at any time.
90     // Any debuggable can send messages over the XPC connection.
91     // So lock access to all maps and state as they can change
92     // from any thread.
93     Mutex m_lock;
94
95     HashMap<unsigned, std::pair<RemoteInspectorDebuggable*, RemoteInspectorDebuggableInfo>> m_debuggableMap;
96     HashMap<unsigned, RefPtr<RemoteInspectorDebuggableConnection>> m_connectionMap;
97     RefPtr<RemoteInspectorXPCConnection> m_xpcConnection;
98     dispatch_queue_t m_xpcQueue;
99     unsigned m_nextAvailableIdentifier;
100     int m_notifyToken;
101     bool m_enabled;
102     bool m_hasActiveDebugSession;
103     bool m_pushScheduled;
104 };
105
106 } // namespace Inspector
107
108 #endif // RemoteInspector_h
109
110 #endif // ENABLE(REMOTE_INSPECTOR)