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 //#include "DispatcherBase.h" | 5 //#include "DispatcherBase.h" |
6 //#include "Parser.h" | 6 //#include "Parser.h" |
7 | 7 |
8 {% for namespace in config.protocol.namespace %} | 8 {% for namespace in config.protocol.namespace %} |
9 namespace {{namespace}} { | 9 namespace {{namespace}} { |
10 {% endfor %} | 10 {% endfor %} |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 sendResponse(callId, invocationError, nullptr, std::move(result)); | 83 sendResponse(callId, invocationError, nullptr, std::move(result)); |
84 } | 84 } |
85 | 85 |
86 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
) | 86 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
) |
87 { | 87 { |
88 sendResponse(callId, invocationError, nullptr, DictionaryValue::create()); | 88 sendResponse(callId, invocationError, nullptr, DictionaryValue::create()); |
89 } | 89 } |
90 | 90 |
91 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId,
DispatcherBase::CommonErrorCode code, const String& errorMessage, ErrorSupport*
errors) | 91 static void reportProtocolErrorTo(FrontendChannel* frontendChannel, int callId,
DispatcherBase::CommonErrorCode code, const String& errorMessage, ErrorSupport*
errors) |
92 { | 92 { |
| 93 if (!frontendChannel) |
| 94 return; |
93 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; | 95 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; |
94 error->setInteger("code", code); | 96 error->setInteger("code", code); |
95 error->setString("message", errorMessage); | 97 error->setString("message", errorMessage); |
96 DCHECK(error); | 98 DCHECK(error); |
97 if (errors && errors->hasErrors()) | 99 if (errors && errors->hasErrors()) |
98 error->setString("data", errors->errors()); | 100 error->setString("data", errors->errors()); |
99 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); | 101 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); |
100 message->setObject("error", std::move(error)); | 102 message->setObject("error", std::move(error)); |
101 message->setInteger("id", callId); | 103 message->setInteger("id", callId); |
102 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); | 104 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); |
103 } | 105 } |
104 | 106 |
105 void DispatcherBase::reportProtocolError(int callId, CommonErrorCode code, const
String& errorMessage, ErrorSupport* errors) | 107 void DispatcherBase::reportProtocolError(int callId, CommonErrorCode code, const
String& errorMessage, ErrorSupport* errors) |
106 { | 108 { |
107 if (m_frontendChannel) | 109 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, errors)
; |
108 reportProtocolErrorTo(m_frontendChannel, callId, code, errorMessage, err
ors); | |
109 } | 110 } |
110 | 111 |
111 void DispatcherBase::clearFrontend() | 112 void DispatcherBase::clearFrontend() |
112 { | 113 { |
113 m_frontendChannel = nullptr; | 114 m_frontendChannel = nullptr; |
114 for (auto& weak : m_weakPtrs) | 115 for (auto& weak : m_weakPtrs) |
115 weak->dispose(); | 116 weak->dispose(); |
116 m_weakPtrs.clear(); | 117 m_weakPtrs.clear(); |
117 } | 118 } |
118 | 119 |
(...skipping 15 matching lines...) Expand all Loading... |
134 void UberDispatcher::dispatch(std::unique_ptr<Value> parsedMessage) | 135 void UberDispatcher::dispatch(std::unique_ptr<Value> parsedMessage) |
135 { | 136 { |
136 if (!parsedMessage) | 137 if (!parsedMessage) |
137 return; | 138 return; |
138 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); | 139 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); |
139 if (!messageObject) | 140 if (!messageObject) |
140 return; | 141 return; |
141 | 142 |
142 int callId = 0; | 143 int callId = 0; |
143 protocol::Value* callIdValue = messageObject->get("id"); | 144 protocol::Value* callIdValue = messageObject->get("id"); |
144 bool success = callIdValue->asInteger(&callId); | 145 bool success = callIdValue && callIdValue->asInteger(&callId); |
145 if (!success) | 146 if (!success) |
146 return; | 147 return; |
147 | 148 |
148 protocol::Value* methodValue = messageObject->get("method"); | 149 protocol::Value* methodValue = messageObject->get("method"); |
149 String method; | 150 String method; |
150 success = methodValue && methodValue->asString(&method); | 151 success = methodValue && methodValue->asString(&method); |
151 if (!success) | 152 if (!success) |
152 return; | 153 return; |
153 | 154 |
154 size_t dotIndex = method.find("."); | 155 size_t dotIndex = method.find("."); |
155 if (dotIndex == StringUtil::kNotFound) { | 156 if (dotIndex == StringUtil::kNotFound) { |
156 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); | 157 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); |
157 return; | 158 return; |
158 } | 159 } |
159 String domain = StringUtil::substring(method, 0, dotIndex); | 160 String domain = StringUtil::substring(method, 0, dotIndex); |
160 auto it = m_dispatchers.find(domain); | 161 auto it = m_dispatchers.find(domain); |
161 if (it == m_dispatchers.end()) { | 162 if (it == m_dispatchers.end()) { |
162 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); | 163 reportProtocolErrorTo(m_frontendChannel, callId, DispatcherBase::MethodN
otFound, "'" + method + "' wasn't found", nullptr); |
163 return; | 164 return; |
164 } | 165 } |
165 it->second->dispatch(callId, method, std::move(messageObject)); | 166 it->second->dispatch(callId, method, std::move(messageObject)); |
166 } | 167 } |
167 | 168 |
168 UberDispatcher::~UberDispatcher() = default; | 169 UberDispatcher::~UberDispatcher() = default; |
169 | 170 |
170 {% for namespace in config.protocol.namespace %} | 171 {% for namespace in config.protocol.namespace %} |
171 } // namespace {{namespace}} | 172 } // namespace {{namespace}} |
172 {% endfor %} | 173 {% endfor %} |
OLD | NEW |