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/RetainPtr.h>
35
36OBJC_CLASS NSDictionary;
37OBJC_CLASS NSString;
38
39namespace Inspector {
40
41class RemoteInspectorClient;
42class RemoteInspectorDebuggable;
43class RemoteInspectorDebuggableConnection;
44struct RemoteInspectorDebuggableInfo;
45
46class JS_EXPORT_PRIVATE RemoteInspector final : public RemoteInspectorXPCConnection::Client {
47public:
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    void setupFailed(unsigned identifier);
57
58    bool enabled() const { return m_enabled; }
59    bool hasActiveDebugSession() const { return m_hasActiveDebugSession; }
60
61    void start();
62    void stop();
63
64    bool hasParentProcessInformation() const { return m_parentProcessIdentifier != 0; }
65    pid_t parentProcessIdentifier() const { return m_parentProcessIdentifier; }
66    RetainPtr<CFDataRef> parentProcessAuditData() const { return m_parentProcessAuditData; }
67    void setParentProcessInformation(pid_t, RetainPtr<CFDataRef> auditData);
68    void setParentProcessInfomationIsDelayed();
69
70private:
71    RemoteInspector();
72
73    unsigned nextAvailableIdentifier();
74
75    enum class StopSource { API, XPCMessage };
76    void stopInternal(StopSource);
77
78    void setupXPCConnectionIfNeeded();
79
80    NSDictionary *listingForDebuggable(const RemoteInspectorDebuggableInfo&) const;
81    void pushListingNow();
82    void pushListingSoon();
83
84    void updateHasActiveDebugSession();
85
86    virtual void xpcConnectionReceivedMessage(RemoteInspectorXPCConnection*, NSString *messageName, NSDictionary *userInfo) override;
87    virtual void xpcConnectionFailed(RemoteInspectorXPCConnection*) override;
88    virtual void xpcConnectionUnhandledMessage(RemoteInspectorXPCConnection*, xpc_object_t) override;
89
90    void receivedSetupMessage(NSDictionary *userInfo);
91    void receivedDataMessage(NSDictionary *userInfo);
92    void receivedDidCloseMessage(NSDictionary *userInfo);
93    void receivedGetListingMessage(NSDictionary *userInfo);
94    void receivedIndicateMessage(NSDictionary *userInfo);
95    void receivedProxyApplicationSetupMessage(NSDictionary *userInfo);
96    void receivedConnectionDiedMessage(NSDictionary *userInfo);
97
98    static bool startEnabled;
99
100    // Debuggables can be registered from any thread at any time.
101    // Any debuggable can send messages over the XPC connection.
102    // So lock access to all maps and state as they can change
103    // from any thread.
104    std::mutex m_mutex;
105
106    HashMap<unsigned, std::pair<RemoteInspectorDebuggable*, RemoteInspectorDebuggableInfo>> m_debuggableMap;
107    HashMap<unsigned, RefPtr<RemoteInspectorDebuggableConnection>> m_connectionMap;
108    RefPtr<RemoteInspectorXPCConnection> m_xpcConnection;
109
110    dispatch_queue_t m_xpcQueue;
111    unsigned m_nextAvailableIdentifier;
112    int m_notifyToken;
113    bool m_enabled;
114    bool m_hasActiveDebugSession;
115    bool m_pushScheduled;
116
117    pid_t m_parentProcessIdentifier;
118    RetainPtr<CFDataRef> m_parentProcessAuditData;
119    bool m_shouldSendParentProcessInformation;
120};
121
122} // namespace Inspector
123
124#endif // RemoteInspector_h
125
126#endif // ENABLE(REMOTE_INSPECTOR)
127