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 namespace blink { | 5 //#include "DispatcherBase.h" |
6 namespace protocol { | 6 //#include "FrontendChannel.h" |
| 7 //#include "Parser.h" |
| 8 |
| 9 {% for namespace in config.protocol.namespace %} |
| 10 namespace {{namespace}} { |
| 11 {% endfor %} |
7 | 12 |
8 // static | 13 // static |
9 const char DispatcherBase::kInvalidRequest[] = "Invalid request"; | 14 const char DispatcherBase::kInvalidRequest[] = "Invalid request"; |
10 | 15 |
11 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } | 16 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } |
12 | 17 |
13 DispatcherBase::WeakPtr::~WeakPtr() | 18 DispatcherBase::WeakPtr::~WeakPtr() |
14 { | 19 { |
15 if (m_dispatcher) | 20 if (m_dispatcher) |
16 m_dispatcher->m_weakPtrs.erase(this); | 21 m_dispatcher->m_weakPtrs.erase(this); |
(...skipping 20 matching lines...) Expand all Loading... |
37 | 42 |
38 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) | 43 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) |
39 : m_frontendChannel(frontendChannel) { } | 44 : m_frontendChannel(frontendChannel) { } |
40 | 45 |
41 DispatcherBase::~DispatcherBase() | 46 DispatcherBase::~DispatcherBase() |
42 { | 47 { |
43 clearFrontend(); | 48 clearFrontend(); |
44 } | 49 } |
45 | 50 |
46 // static | 51 // static |
47 bool DispatcherBase::getCommandName(const String16& message, String16* result) | 52 bool DispatcherBase::getCommandName(const String& message, String* result) |
48 { | 53 { |
49 std::unique_ptr<protocol::Value> value = parseJSON(message); | 54 std::unique_ptr<protocol::Value> value = parseJSON(message); |
50 if (!value) | 55 if (!value) |
51 return false; | 56 return false; |
52 | 57 |
53 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); | 58 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); |
54 if (!object) | 59 if (!object) |
55 return false; | 60 return false; |
56 | 61 |
57 if (!object->getString("method", result)) | 62 if (!object->getString("method", result)) |
(...skipping 19 matching lines...) Expand all Loading... |
77 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
, std::unique_ptr<protocol::DictionaryValue> result) | 82 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
, std::unique_ptr<protocol::DictionaryValue> result) |
78 { | 83 { |
79 sendResponse(callId, invocationError, nullptr, std::move(result)); | 84 sendResponse(callId, invocationError, nullptr, std::move(result)); |
80 } | 85 } |
81 | 86 |
82 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
) | 87 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
) |
83 { | 88 { |
84 sendResponse(callId, invocationError, nullptr, DictionaryValue::create()); | 89 sendResponse(callId, invocationError, nullptr, DictionaryValue::create()); |
85 } | 90 } |
86 | 91 |
87 static void reportProtocolError(FrontendChannel* frontendChannel, int callId, Di
spatcherBase::CommonErrorCode code, const String16& errorMessage, ErrorSupport*
errors) | 92 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId,
DispatcherBase::CommonErrorCode code, const String& errorMessage, ErrorSupport*
errors) |
88 { | 93 { |
89 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; | 94 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; |
90 error->setInteger("code", code); | 95 error->setInteger("code", code); |
91 error->setString("message", errorMessage); | 96 error->setString("message", errorMessage); |
92 DCHECK(error); | 97 DCHECK(error); |
93 if (errors && errors->hasErrors()) | 98 if (errors && errors->hasErrors()) |
94 error->setString("data", errors->errors()); | 99 error->setString("data", errors->errors()); |
95 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); | 100 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); |
96 message->setObject("error", std::move(error)); | 101 message->setObject("error", std::move(error)); |
97 message->setInteger("id", callId); | 102 message->setInteger("id", callId); |
98 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); | 103 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); |
99 } | 104 } |
100 | 105 |
101 void DispatcherBase::reportProtocolError(int callId, CommonErrorCode code, const
String16& errorMessage, ErrorSupport* errors) | 106 void DispatcherBase::reportProtocolError(int callId, CommonErrorCode code, const
String& errorMessage, ErrorSupport* errors) |
102 { | 107 { |
103 if (m_frontendChannel) | 108 if (m_frontendChannel) |
104 ::blink::protocol::reportProtocolError(m_frontendChannel, callId, code,
errorMessage, errors); | 109 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, err
ors); |
105 } | 110 } |
106 | 111 |
107 void DispatcherBase::clearFrontend() | 112 void DispatcherBase::clearFrontend() |
108 { | 113 { |
109 m_frontendChannel = nullptr; | 114 m_frontendChannel = nullptr; |
110 for (auto& weak : m_weakPtrs) | 115 for (auto& weak : m_weakPtrs) |
111 weak->dispose(); | 116 weak->dispose(); |
112 m_weakPtrs.clear(); | 117 m_weakPtrs.clear(); |
113 } | 118 } |
114 | 119 |
115 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() | 120 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() |
116 { | 121 { |
117 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); | 122 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); |
118 m_weakPtrs.insert(weak.get()); | 123 m_weakPtrs.insert(weak.get()); |
119 return weak; | 124 return weak; |
120 } | 125 } |
121 | 126 |
122 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) | 127 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) |
123 : m_frontendChannel(frontendChannel) { } | 128 : m_frontendChannel(frontendChannel) { } |
124 | 129 |
125 void UberDispatcher::registerBackend(const String16& name, std::unique_ptr<proto
col::DispatcherBase> dispatcher) | 130 void UberDispatcher::registerBackend(const String& name, std::unique_ptr<protoco
l::DispatcherBase> dispatcher) |
126 { | 131 { |
127 m_dispatchers[name] = std::move(dispatcher); | 132 m_dispatchers[name] = std::move(dispatcher); |
128 } | 133 } |
129 | 134 |
130 void UberDispatcher::dispatch(const String16& message) | 135 void UberDispatcher::dispatch(const String& message) |
131 { | 136 { |
132 std::unique_ptr<protocol::Value> parsedMessage = parseJSON(message); | 137 std::unique_ptr<protocol::Value> parsedMessage = parseJSON(message); |
133 if (!parsedMessage) | 138 if (!parsedMessage) |
134 return; | 139 return; |
135 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); | 140 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); |
136 if (!messageObject) | 141 if (!messageObject) |
137 return; | 142 return; |
138 | 143 |
139 int callId = 0; | 144 int callId = 0; |
140 protocol::Value* callIdValue = messageObject->get("id"); | 145 protocol::Value* callIdValue = messageObject->get("id"); |
141 bool success = callIdValue->asInteger(&callId); | 146 bool success = callIdValue->asInteger(&callId); |
142 if (!success) | 147 if (!success) |
143 return; | 148 return; |
144 | 149 |
145 protocol::Value* methodValue = messageObject->get("method"); | 150 protocol::Value* methodValue = messageObject->get("method"); |
146 String16 method; | 151 String method; |
147 success = methodValue && methodValue->asString(&method); | 152 success = methodValue && methodValue->asString(&method); |
148 if (!success) | 153 if (!success) |
149 return; | 154 return; |
150 | 155 |
151 size_t dotIndex = method.find("."); | 156 size_t dotIndex = method.find("."); |
152 if (dotIndex == String16::kNotFound) { | 157 if (dotIndex == StringUtil::kNotFound) { |
153 reportProtocolError(m_frontendChannel, callId, DispatcherBase::MethodNot
Found, "'" + method + "' wasn't found", nullptr); | 158 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); |
154 return; | 159 return; |
155 } | 160 } |
156 String16 domain = method.substring(0, dotIndex); | 161 String domain = StringUtil::substring(method, 0, dotIndex); |
157 auto it = m_dispatchers.find(domain); | 162 auto it = m_dispatchers.find(domain); |
158 if (it == m_dispatchers.end()) { | 163 if (it == m_dispatchers.end()) { |
159 reportProtocolError(m_frontendChannel, callId, DispatcherBase::MethodNot
Found, "'" + method + "' wasn't found", nullptr); | 164 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); |
160 return; | 165 return; |
161 } | 166 } |
162 it->second->dispatch(callId, method, std::move(messageObject)); | 167 it->second->dispatch(callId, method, std::move(messageObject)); |
163 } | 168 } |
164 | 169 |
165 UberDispatcher::~UberDispatcher() = default; | 170 UberDispatcher::~UberDispatcher() = default; |
166 | 171 |
167 } // namespace protocol | 172 {% for namespace in config.protocol.namespace %} |
168 } // namespace blink | 173 } // namespace {{namespace}} |
| 174 {% endfor %} |
OLD | NEW |