OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/inspector_protocol/DispatcherBase.h" |
| 6 |
| 7 #include "platform/inspector_protocol/FrontendChannel.h" |
| 8 #include "platform/inspector_protocol/Parser.h" |
| 9 |
| 10 namespace blink { |
| 11 namespace protocol { |
| 12 |
| 13 DispatcherBase::WeakPtr::WeakPtr(DispatcherBase* dispatcher) : m_dispatcher(disp
atcher) { } |
| 14 |
| 15 DispatcherBase::WeakPtr::~WeakPtr() |
| 16 { |
| 17 if (m_dispatcher) |
| 18 m_dispatcher->m_weakPtrs.remove(this); |
| 19 } |
| 20 |
| 21 DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> back
endImpl, int callId) |
| 22 : m_backendImpl(std::move(backendImpl)) |
| 23 , m_callId(callId) { } |
| 24 |
| 25 DispatcherBase::Callback::~Callback() = default; |
| 26 |
| 27 void DispatcherBase::Callback::dispose() |
| 28 { |
| 29 m_backendImpl = nullptr; |
| 30 } |
| 31 |
| 32 void DispatcherBase::Callback::sendIfActive(std::unique_ptr<protocol::Dictionary
Value> partialMessage, const ErrorString& invocationError) |
| 33 { |
| 34 if (!m_backendImpl->get()) |
| 35 return; |
| 36 m_backendImpl->get()->sendResponse(m_callId, invocationError, nullptr, std::
move(partialMessage)); |
| 37 m_backendImpl = nullptr; |
| 38 } |
| 39 |
| 40 DispatcherBase::DispatcherBase(FrontendChannel* frontendChannel) |
| 41 : m_frontendChannel(frontendChannel) { } |
| 42 |
| 43 DispatcherBase::~DispatcherBase() |
| 44 { |
| 45 clearFrontend(); |
| 46 } |
| 47 |
| 48 // static |
| 49 bool DispatcherBase::getCommandName(const String16& message, String16* result) |
| 50 { |
| 51 std::unique_ptr<protocol::Value> value = parseJSON(message); |
| 52 if (!value) |
| 53 return false; |
| 54 |
| 55 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); |
| 56 if (!object) |
| 57 return false; |
| 58 |
| 59 if (!object->getString("method", result)) |
| 60 return false; |
| 61 |
| 62 return true; |
| 63 } |
| 64 |
| 65 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
, ErrorSupport* errors, std::unique_ptr<protocol::DictionaryValue> result) |
| 66 { |
| 67 if (invocationError.length() || (errors && errors->hasErrors())) { |
| 68 reportProtocolError(callId, ServerError, invocationError, errors); |
| 69 return; |
| 70 } |
| 71 |
| 72 std::unique_ptr<protocol::DictionaryValue> responseMessage = DictionaryValue
::create(); |
| 73 responseMessage->setNumber("id", callId); |
| 74 responseMessage->setObject("result", std::move(result)); |
| 75 if (m_frontendChannel) |
| 76 m_frontendChannel->sendProtocolResponse(callId, responseMessage->toJSONS
tring()); |
| 77 } |
| 78 |
| 79 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
, std::unique_ptr<protocol::DictionaryValue> result) |
| 80 { |
| 81 sendResponse(callId, invocationError, nullptr, std::move(result)); |
| 82 } |
| 83 |
| 84 void DispatcherBase::sendResponse(int callId, const ErrorString& invocationError
) |
| 85 { |
| 86 sendResponse(callId, invocationError, nullptr, DictionaryValue::create()); |
| 87 } |
| 88 |
| 89 static void reportProtocolError(FrontendChannel* frontendChannel, int callId, Di
spatcherBase::CommonErrorCode code, const String16& errorMessage, ErrorSupport*
errors) |
| 90 { |
| 91 std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create()
; |
| 92 error->setNumber("code", code); |
| 93 error->setString("message", errorMessage); |
| 94 DCHECK(error); |
| 95 if (errors && errors->hasErrors()) |
| 96 error->setString("data", errors->errors()); |
| 97 std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create
(); |
| 98 message->setObject("error", std::move(error)); |
| 99 message->setNumber("id", callId); |
| 100 frontendChannel->sendProtocolResponse(callId, message->toJSONString()); |
| 101 } |
| 102 |
| 103 void DispatcherBase::reportProtocolError(int callId, CommonErrorCode code, const
String16& errorMessage, ErrorSupport* errors) |
| 104 { |
| 105 if (m_frontendChannel) |
| 106 ::blink::protocol::reportProtocolError(m_frontendChannel, callId, code,
errorMessage, errors); |
| 107 } |
| 108 |
| 109 void DispatcherBase::clearFrontend() |
| 110 { |
| 111 m_frontendChannel = nullptr; |
| 112 for (auto& weak : m_weakPtrs) |
| 113 weak.first->dispose(); |
| 114 m_weakPtrs.clear(); |
| 115 } |
| 116 |
| 117 std::unique_ptr<DispatcherBase::WeakPtr> DispatcherBase::weakPtr() |
| 118 { |
| 119 std::unique_ptr<DispatcherBase::WeakPtr> weak(new DispatcherBase::WeakPtr(th
is)); |
| 120 m_weakPtrs.add(weak.get()); |
| 121 return weak; |
| 122 } |
| 123 |
| 124 UberDispatcher::UberDispatcher(FrontendChannel* frontendChannel) |
| 125 : m_frontendChannel(frontendChannel) { } |
| 126 |
| 127 void UberDispatcher::registerBackend(const String16& name, std::unique_ptr<proto
col::DispatcherBase> dispatcher) |
| 128 { |
| 129 m_dispatchers.set(name, std::move(dispatcher)); |
| 130 } |
| 131 |
| 132 void UberDispatcher::dispatch(const String16& message) |
| 133 { |
| 134 std::unique_ptr<protocol::Value> parsedMessage = parseJSON(message); |
| 135 if (!parsedMessage) |
| 136 return; |
| 137 std::unique_ptr<protocol::DictionaryValue> messageObject = DictionaryValue::
cast(std::move(parsedMessage)); |
| 138 if (!messageObject) |
| 139 return; |
| 140 |
| 141 int callId = 0; |
| 142 protocol::Value* callIdValue = messageObject->get("id"); |
| 143 bool success = callIdValue->asNumber(&callId); |
| 144 if (!success) |
| 145 return; |
| 146 |
| 147 protocol::Value* methodValue = messageObject->get("method"); |
| 148 String16 method; |
| 149 success = methodValue && methodValue->asString(&method); |
| 150 if (!success) |
| 151 return; |
| 152 |
| 153 size_t dotIndex = method.find("."); |
| 154 if (dotIndex == kNotFound) { |
| 155 reportProtocolError(m_frontendChannel, callId, DispatcherBase::MethodNot
Found, "'" + method + "' wasn't found", nullptr); |
| 156 return; |
| 157 } |
| 158 String16 domain = method.substring(0, dotIndex); |
| 159 auto it = m_dispatchers.find(domain); |
| 160 if (it == m_dispatchers.end()) { |
| 161 reportProtocolError(m_frontendChannel, callId, DispatcherBase::MethodNot
Found, "'" + method + "' wasn't found", nullptr); |
| 162 return; |
| 163 } |
| 164 it->second->dispatch(callId, method, std::move(messageObject)); |
| 165 } |
| 166 |
| 167 UberDispatcher::~UberDispatcher() = default; |
| 168 |
| 169 } // namespace protocol |
| 170 } // namespace blink |
OLD | NEW |