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