OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef {{"_".join(config.protocol.namespace)}}_DispatcherBase_h | 5 #ifndef {{"_".join(config.protocol.namespace)}}_DispatcherBase_h |
6 #define {{"_".join(config.protocol.namespace)}}_DispatcherBase_h | 6 #define {{"_".join(config.protocol.namespace)}}_DispatcherBase_h |
7 | 7 |
8 //#include "BackendCallback.h" | |
9 //#include "Collections.h" | 8 //#include "Collections.h" |
10 //#include "ErrorSupport.h" | 9 //#include "ErrorSupport.h" |
11 //#include "Forward.h" | 10 //#include "Forward.h" |
12 //#include "Values.h" | 11 //#include "Values.h" |
13 | 12 |
14 {% for namespace in config.protocol.namespace %} | 13 {% for namespace in config.protocol.namespace %} |
15 namespace {{namespace}} { | 14 namespace {{namespace}} { |
16 {% endfor %} | 15 {% endfor %} |
17 | 16 |
18 class WeakPtr; | 17 class WeakPtr; |
19 | 18 |
| 19 class {{config.lib.export_macro}} DispatchResponse { |
| 20 public: |
| 21 enum Status { |
| 22 kSuccess = 0, |
| 23 kError = 1, |
| 24 kFallThrough = 2, |
| 25 kAsync = 3 |
| 26 }; |
| 27 |
| 28 enum ErrorCode { |
| 29 kParseError = -32700, |
| 30 kInvalidRequest = -32600, |
| 31 kMethodNotFound = -32601, |
| 32 kInvalidParams = -32602, |
| 33 kInternalError = -32603, |
| 34 kServerError = -32000, |
| 35 }; |
| 36 |
| 37 Status status() const { return m_status; } |
| 38 const String& errorMessage() const { return m_errorMessage; } |
| 39 ErrorCode errorCode() const { return m_errorCode; } |
| 40 bool isSuccess() const { return m_status == kSuccess; } |
| 41 |
| 42 static DispatchResponse OK(); |
| 43 static DispatchResponse Error(const String&); |
| 44 static DispatchResponse InternalError(); |
| 45 static DispatchResponse FallThrough(); |
| 46 |
| 47 private: |
| 48 Status m_status; |
| 49 String m_errorMessage; |
| 50 ErrorCode m_errorCode; |
| 51 }; |
| 52 |
20 class {{config.lib.export_macro}} DispatcherBase { | 53 class {{config.lib.export_macro}} DispatcherBase { |
21 PROTOCOL_DISALLOW_COPY(DispatcherBase); | 54 PROTOCOL_DISALLOW_COPY(DispatcherBase); |
22 public: | 55 public: |
23 static const char kInvalidRequest[]; | 56 static const char kInvalidParamsString[]; |
24 class {{config.lib.export_macro}} WeakPtr { | 57 class {{config.lib.export_macro}} WeakPtr { |
25 public: | 58 public: |
26 explicit WeakPtr(DispatcherBase*); | 59 explicit WeakPtr(DispatcherBase*); |
27 ~WeakPtr(); | 60 ~WeakPtr(); |
28 DispatcherBase* get() { return m_dispatcher; } | 61 DispatcherBase* get() { return m_dispatcher; } |
29 void dispose() { m_dispatcher = nullptr; } | 62 void dispose() { m_dispatcher = nullptr; } |
30 | 63 |
31 private: | 64 private: |
32 DispatcherBase* m_dispatcher; | 65 DispatcherBase* m_dispatcher; |
33 }; | 66 }; |
34 | 67 |
35 class {{config.lib.export_macro}} Callback : public protocol::BackendCallbac
k { | 68 class {{config.lib.export_macro}} Callback { |
36 public: | 69 public: |
37 Callback(std::unique_ptr<WeakPtr> backendImpl, int callId); | 70 Callback(std::unique_ptr<WeakPtr> backendImpl, int callId); |
38 virtual ~Callback(); | 71 virtual ~Callback(); |
39 void dispose(); | 72 void dispose(); |
40 | 73 |
41 protected: | 74 protected: |
42 void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMess
age, const ErrorString& invocationError); | 75 void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMess
age, const DispatchResponse& response); |
43 | 76 |
44 private: | 77 private: |
45 std::unique_ptr<WeakPtr> m_backendImpl; | 78 std::unique_ptr<WeakPtr> m_backendImpl; |
46 int m_callId; | 79 int m_callId; |
47 }; | 80 }; |
48 | 81 |
49 explicit DispatcherBase(FrontendChannel*); | 82 explicit DispatcherBase(FrontendChannel*); |
50 virtual ~DispatcherBase(); | 83 virtual ~DispatcherBase(); |
51 | 84 |
52 enum CommonErrorCode { | |
53 ParseError = -32700, | |
54 InvalidRequest = -32600, | |
55 MethodNotFound = -32601, | |
56 InvalidParams = -32602, | |
57 InternalError = -32603, | |
58 ServerError = -32000, | |
59 }; | |
60 | |
61 static bool getCommandName(const String& message, String* result); | 85 static bool getCommandName(const String& message, String* result); |
62 | 86 |
63 virtual void dispatch(int callId, const String& method, std::unique_ptr<prot
ocol::DictionaryValue> messageObject) = 0; | 87 virtual DispatchResponse::Status dispatch(int callId, const String& method,
std::unique_ptr<protocol::DictionaryValue> messageObject) = 0; |
64 | 88 |
65 void sendResponse(int callId, const ErrorString&, ErrorSupport*, std::unique
_ptr<protocol::DictionaryValue> result); | 89 void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<proto
col::DictionaryValue> result); |
66 void sendResponse(int callId, const ErrorString&, std::unique_ptr<protocol::
DictionaryValue> result); | 90 void sendResponse(int callId, const DispatchResponse&); |
67 void sendResponse(int callId, const ErrorString&); | |
68 | 91 |
69 void reportProtocolError(int callId, CommonErrorCode, const String& errorMes
sage, ErrorSupport* errors); | 92 void reportProtocolError(int callId, DispatchResponse::ErrorCode, const Stri
ng& errorMessage, ErrorSupport* errors); |
70 void clearFrontend(); | 93 void clearFrontend(); |
71 | 94 |
72 std::unique_ptr<WeakPtr> weakPtr(); | 95 std::unique_ptr<WeakPtr> weakPtr(); |
73 | 96 |
74 private: | 97 private: |
75 FrontendChannel* m_frontendChannel; | 98 FrontendChannel* m_frontendChannel; |
76 protocol::HashSet<WeakPtr*> m_weakPtrs; | 99 protocol::HashSet<WeakPtr*> m_weakPtrs; |
77 }; | 100 }; |
78 | 101 |
79 class {{config.lib.export_macro}} UberDispatcher { | 102 class {{config.lib.export_macro}} UberDispatcher { |
80 PROTOCOL_DISALLOW_COPY(UberDispatcher); | 103 PROTOCOL_DISALLOW_COPY(UberDispatcher); |
81 public: | 104 public: |
82 explicit UberDispatcher(FrontendChannel*); | 105 explicit UberDispatcher(FrontendChannel*); |
83 void registerBackend(const String& name, std::unique_ptr<protocol::Dispatche
rBase>); | 106 void registerBackend(const String& name, std::unique_ptr<protocol::Dispatche
rBase>); |
84 void dispatch(std::unique_ptr<Value> message); | 107 DispatchResponse::Status dispatch(std::unique_ptr<Value> message); |
85 FrontendChannel* channel() { return m_frontendChannel; } | 108 FrontendChannel* channel() { return m_frontendChannel; } |
86 virtual ~UberDispatcher(); | 109 virtual ~UberDispatcher(); |
87 | 110 |
88 private: | 111 private: |
89 FrontendChannel* m_frontendChannel; | 112 FrontendChannel* m_frontendChannel; |
90 protocol::HashMap<String, std::unique_ptr<protocol::DispatcherBase>> m_dispa
tchers; | 113 protocol::HashMap<String, std::unique_ptr<protocol::DispatcherBase>> m_dispa
tchers; |
91 }; | 114 }; |
92 | 115 |
93 {% for namespace in config.protocol.namespace %} | 116 {% for namespace in config.protocol.namespace %} |
94 } // namespace {{namespace}} | 117 } // namespace {{namespace}} |
95 {% endfor %} | 118 {% endfor %} |
96 | 119 |
97 #endif // !defined({{"_".join(config.protocol.namespace)}}_DispatcherBase_h) | 120 #endif // !defined({{"_".join(config.protocol.namespace)}}_DispatcherBase_h) |
OLD | NEW |