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