| 1 | /* |
| 2 | * Copyright (C) 2013-2017 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 | #pragma once |
| 28 | |
| 29 | #include "InspectorFrontendRouter.h" |
| 30 | #include "InspectorProtocolTypes.h" |
| 31 | #include <functional> |
| 32 | #include <wtf/Optional.h> |
| 33 | #include <wtf/RefCounted.h> |
| 34 | #include <wtf/text/WTFString.h> |
| 35 | |
| 36 | namespace Inspector { |
| 37 | |
| 38 | class BackendDispatcher; |
| 39 | |
| 40 | typedef String ErrorString; |
| 41 | |
| 42 | class JS_EXPORT_PRIVATE SupplementalBackendDispatcher : public RefCounted<SupplementalBackendDispatcher> { |
| 43 | public: |
| 44 | SupplementalBackendDispatcher(BackendDispatcher&); |
| 45 | virtual ~SupplementalBackendDispatcher(); |
| 46 | virtual void dispatch(long requestId, const String& method, Ref<JSON::Object>&& 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(Ref<FrontendRouter>&&); |
| 54 | |
| 55 | class JS_EXPORT_PRIVATE CallbackBase : public RefCounted<CallbackBase> { |
| 56 | public: |
| 57 | CallbackBase(Ref<BackendDispatcher>&&, long requestId); |
| 58 | |
| 59 | bool isActive() const; |
| 60 | void disable() { m_alreadySent = true; } |
| 61 | |
| 62 | void sendSuccess(RefPtr<JSON::Object>&&); |
| 63 | void sendFailure(const ErrorString&); |
| 64 | |
| 65 | private: |
| 66 | Ref<BackendDispatcher> m_backendDispatcher; |
| 67 | long m_requestId; |
| 68 | bool m_alreadySent { false }; |
| 69 | }; |
| 70 | |
| 71 | bool isActive() const; |
| 72 | |
| 73 | bool hasProtocolErrors() const { return m_protocolErrors.size() > 0; } |
| 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 | |
| 87 | // Note that 'unused' is a workaround so the compiler can pick the right sendResponse based on arity. |
| 88 | // When <http://webkit.org/b/179847> is fixed or this class is renamed for the JSON::Object case, |
| 89 | // then this alternate method with a dummy parameter can be removed in favor of the one without it. |
| 90 | void sendResponse(long requestId, RefPtr<JSON::Object>&& result, bool unused); |
| 91 | void sendResponse(long requestId, RefPtr<JSON::Object>&& result); |
| 92 | void sendPendingErrors(); |
| 93 | |
| 94 | void reportProtocolError(CommonErrorCode, const String& errorMessage); |
| 95 | void reportProtocolError(Optional<long> relatedRequestId, CommonErrorCode, const String& errorMessage); |
| 96 | |
| 97 | template<typename T> |
| 98 | WTF_INTERNAL |
| 99 | T getPropertyValue(JSON::Object*, const String& name, bool* out_optionalValueFound, T defaultValue, std::function<bool(JSON::Value&, T&)>, const char* typeName); |
| 100 | |
| 101 | int getInteger(JSON::Object*, const String& name, bool* valueFound); |
| 102 | double getDouble(JSON::Object*, const String& name, bool* valueFound); |
| 103 | String getString(JSON::Object*, const String& name, bool* valueFound); |
| 104 | bool getBoolean(JSON::Object*, const String& name, bool* valueFound); |
| 105 | RefPtr<JSON::Value> getValue(JSON::Object*, const String& name, bool* valueFound); |
| 106 | RefPtr<JSON::Object> getObject(JSON::Object*, const String& name, bool* valueFound); |
| 107 | RefPtr<JSON::Array> getArray(JSON::Object*, const String& name, bool* valueFound); |
| 108 | |
| 109 | private: |
| 110 | BackendDispatcher(Ref<FrontendRouter>&&); |
| 111 | |
| 112 | Ref<FrontendRouter> m_frontendRouter; |
| 113 | HashMap<String, SupplementalBackendDispatcher*> m_dispatchers; |
| 114 | |
| 115 | // Protocol errors reported for the top-level request being processed. |
| 116 | // If processing a request triggers async responses, then any related errors will |
| 117 | // be attributed to the top-level request, but generate separate error messages. |
| 118 | Vector<std::tuple<CommonErrorCode, String>> m_protocolErrors; |
| 119 | |
| 120 | // For synchronously handled requests, avoid plumbing requestId through every |
| 121 | // call that could potentially fail with a protocol error. |
| 122 | Optional<long> m_currentRequestId { WTF::nullopt }; |
| 123 | }; |
| 124 | |
| 125 | } // namespace Inspector |
| 126 | |