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 "InspectorValues.h" 31#include <wtf/PassRefPtr.h> 32#include <wtf/RefCounted.h> 33#include <wtf/text/WTFString.h> 34 35namespace Inspector { 36 37class InspectorBackendDispatcher; 38class InspectorFrontendChannel; 39typedef String ErrorString; 40 41class InspectorSupplementalBackendDispatcher : public RefCounted<InspectorSupplementalBackendDispatcher> { 42public: 43 InspectorSupplementalBackendDispatcher(InspectorBackendDispatcher* backendDispatcher) : m_backendDispatcher(backendDispatcher) { } 44 virtual ~InspectorSupplementalBackendDispatcher() { } 45 virtual void dispatch(long callId, const String& method, PassRefPtr<InspectorObject> message) = 0; 46protected: 47 RefPtr<InspectorBackendDispatcher> m_backendDispatcher; 48}; 49 50class JS_EXPORT_PRIVATE InspectorBackendDispatcher : public RefCounted<InspectorBackendDispatcher> { 51public: 52 static PassRefPtr<InspectorBackendDispatcher> create(InspectorFrontendChannel*); 53 54 class JS_EXPORT_PRIVATE CallbackBase : public RefCounted<CallbackBase> { 55 public: 56 CallbackBase(PassRefPtr<InspectorBackendDispatcher>, int id); 57 58 bool isActive() const; 59 void sendFailure(const ErrorString&); 60 void disable() { m_alreadySent = true; } 61 62 protected: 63 void sendIfActive(PassRefPtr<InspectorObject> partialMessage, const ErrorString& invocationError); 64 65 private: 66 RefPtr<InspectorBackendDispatcher> m_backendDispatcher; 67 int m_id; 68 bool m_alreadySent; 69 }; 70 71 void clearFrontend() { m_inspectorFrontendChannel = nullptr; } 72 bool isActive() const { return !!m_inspectorFrontendChannel; } 73 74 enum CommonErrorCode { 75 ParseError = 0, 76 InvalidRequest, 77 MethodNotFound, 78 InvalidParams, 79 InternalError, 80 ServerError 81 }; 82 83 void registerDispatcherForDomain(const String& domain, InspectorSupplementalBackendDispatcher*); 84 void dispatch(const String& message); 85 void sendResponse(long callId, PassRefPtr<InspectorObject> result, const ErrorString& invocationError); 86 void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage) const; 87 void reportProtocolError(const long* const callId, CommonErrorCode, const String& errorMessage, PassRefPtr<InspectorArray> data) const; 88 89 static int getInt(InspectorObject*, const String& name, bool* valueFound, InspectorArray* protocolErrors); 90 static double getDouble(InspectorObject*, const String& name, bool* valueFound, InspectorArray* protocolErrors); 91 static String getString(InspectorObject*, const String& name, bool* valueFound, InspectorArray* protocolErrors); 92 static bool getBoolean(InspectorObject*, const String& name, bool* valueFound, InspectorArray* protocolErrors); 93 static PassRefPtr<InspectorObject> getObject(InspectorObject*, const String& name, bool* valueFound, InspectorArray* protocolErrors); 94 static PassRefPtr<InspectorArray> getArray(InspectorObject*, const String& name, bool* valueFound, InspectorArray* protocolErrors); 95 96private: 97 InspectorBackendDispatcher(InspectorFrontendChannel* inspectorFrontendChannel) : m_inspectorFrontendChannel(inspectorFrontendChannel) { } 98 99 InspectorFrontendChannel* m_inspectorFrontendChannel; 100 HashMap<String, InspectorSupplementalBackendDispatcher*> m_dispatchers; 101}; 102 103} // namespace Inspector 104 105#endif // !defined(InspectorBackendDispatcher_h) 106