]> git.saurik.com Git - apple/javascriptcore.git/blob - inspector/InspectorBackendDispatcher.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / inspector / InspectorBackendDispatcher.h
1 /*
2 * Copyright (C) 2013 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2011 The Chromium Authors. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef InspectorBackendDispatcher_h
28 #define InspectorBackendDispatcher_h
29
30 #include "InspectorProtocolTypes.h"
31 #include <wtf/RefCounted.h>
32 #include <wtf/text/WTFString.h>
33
34 namespace Inspector {
35
36 class BackendDispatcher;
37 class FrontendChannel;
38
39 typedef String ErrorString;
40
41 class SupplementalBackendDispatcher : public RefCounted<SupplementalBackendDispatcher> {
42 public:
43 SupplementalBackendDispatcher(BackendDispatcher& backendDispatcher)
44 : m_backendDispatcher(backendDispatcher) { }
45 virtual ~SupplementalBackendDispatcher() { }
46 virtual void dispatch(long callId, const String& method, Ref<InspectorObject>&& message) = 0;
47 protected:
48 Ref<BackendDispatcher> m_backendDispatcher;
49 };
50
51 class JS_EXPORT_PRIVATE BackendDispatcher : public RefCounted<BackendDispatcher> {
52 public:
53 static Ref<BackendDispatcher> create(FrontendChannel*);
54
55 class JS_EXPORT_PRIVATE CallbackBase : public RefCounted<CallbackBase> {
56 public:
57 CallbackBase(Ref<BackendDispatcher>&&, int id);
58
59 bool isActive() const;
60 void sendFailure(const ErrorString&);
61 void disable() { m_alreadySent = true; }
62
63 protected:
64 void sendIfActive(RefPtr<InspectorObject>&& partialMessage, const ErrorString& invocationError);
65
66 private:
67 Ref<BackendDispatcher> m_backendDispatcher;
68 int m_id;
69 bool m_alreadySent;
70 };
71
72 void clearFrontend() { m_frontendChannel = nullptr; }
73 bool isActive() const { return !!m_frontendChannel; }
74
75 enum CommonErrorCode {
76 ParseError = 0,
77 InvalidRequest,
78 MethodNotFound,
79 InvalidParams,
80 InternalError,
81 ServerError
82 };
83
84 void registerDispatcherForDomain(const String& domain, SupplementalBackendDispatcher*);
85 void dispatch(const String& message);
86 void sendResponse(long callId, RefPtr<InspectorObject>&& result, const ErrorString& invocationError);
87 void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage) const;
88 void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, RefPtr<Inspector::Protocol::Array<String>>&& data) const;
89
90 static int getInteger(InspectorObject*, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors);
91 static double getDouble(InspectorObject*, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors);
92 static String getString(InspectorObject*, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors);
93 static bool getBoolean(InspectorObject*, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors);
94 static RefPtr<InspectorValue> getValue(InspectorObject*, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors);
95 static RefPtr<InspectorObject> getObject(InspectorObject*, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors);
96 static RefPtr<InspectorArray> getArray(InspectorObject*, const String& name, bool* valueFound, Inspector::Protocol::Array<String>& protocolErrors);
97
98 private:
99 BackendDispatcher(FrontendChannel* FrontendChannel)
100 : m_frontendChannel(FrontendChannel) { }
101
102 FrontendChannel* m_frontendChannel;
103 HashMap<String, SupplementalBackendDispatcher*> m_dispatchers;
104 };
105
106 } // namespace Inspector
107
108 #endif // !defined(InspectorBackendDispatcher_h)