| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/devtools/devtools_embedder_message_dispatcher.h" | 5 #include "chrome/browser/devtools/devtools_embedder_message_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 template <typename H, typename... As> | 58 template <typename H, typename... As> |
| 59 void Apply(const H& handler, As... args) { | 59 void Apply(const H& handler, As... args) { |
| 60 handler.Run(args...); | 60 handler.Run(args...); |
| 61 } | 61 } |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 template <typename T, typename... Ts> | 64 template <typename T, typename... Ts> |
| 65 struct ParamTuple<T, Ts...> { | 65 struct ParamTuple<T, Ts...> { |
| 66 bool Parse(const base::ListValue& list, | 66 bool Parse(const base::ListValue& list, |
| 67 const base::ListValue::const_iterator& it) { | 67 const base::ListValue::const_iterator& it) { |
| 68 if (it == list.end()) | 68 return it != list.end() && GetValue(*it, &head) && tail.Parse(list, it + 1); |
| 69 return false; | |
| 70 if (!GetValue(*it, &head)) | |
| 71 return false; | |
| 72 return tail.Parse(list, it + 1); | |
| 73 } | 69 } |
| 74 | 70 |
| 75 template <typename H, typename... As> | 71 template <typename H, typename... As> |
| 76 void Apply(const H& handler, As... args) { | 72 void Apply(const H& handler, As... args) { |
| 77 tail.template Apply<H, As..., T>(handler, args..., head); | 73 tail.template Apply<H, As..., T>(handler, args..., head); |
| 78 } | 74 } |
| 79 | 75 |
| 80 typename StorageTraits<T>::StorageType head; | 76 typename StorageTraits<T>::StorageType head; |
| 81 ParamTuple<Ts...> tail; | 77 ParamTuple<Ts...> tail; |
| 82 }; | 78 }; |
| 83 | 79 |
| 84 template<typename... As> | 80 template<typename... As> |
| 85 bool ParseAndHandle(const base::Callback<void(As...)>& handler, | 81 void ParseAndHandle(const base::Callback<void(int, As...)>& handler, |
| 82 int request_id, |
| 86 const base::ListValue& list) { | 83 const base::ListValue& list) { |
| 87 ParamTuple<As...> tuple; | 84 ParamTuple<As...> tuple; |
| 88 if (!tuple.Parse(list, list.begin())) | 85 if (tuple.Parse(list, list.begin())) |
| 89 return false; | 86 tuple.Apply(handler, request_id); |
| 90 tuple.Apply(handler); | |
| 91 return true; | |
| 92 } | 87 } |
| 93 | 88 |
| 94 } // namespace | 89 } // namespace |
| 95 | 90 |
| 96 /** | 91 /** |
| 97 * Dispatcher for messages sent from the frontend running in an | 92 * Dispatcher for messages sent from the frontend running in an |
| 98 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder | 93 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder |
| 99 * in the browser. | 94 * in the browser. |
| 100 * | 95 * |
| 101 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or | 96 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or |
| 102 * chrome.send method accordingly. | 97 * chrome.send method accordingly. |
| 103 */ | 98 */ |
| 104 class DispatcherImpl : public DevToolsEmbedderMessageDispatcher { | 99 class DispatcherImpl : public DevToolsEmbedderMessageDispatcher { |
| 105 public: | 100 public: |
| 106 ~DispatcherImpl() override {} | 101 ~DispatcherImpl() override {} |
| 107 | 102 |
| 108 bool Dispatch(const std::string& method, | 103 void Dispatch(int request_id, |
| 109 const base::ListValue* params, | 104 const std::string& method, |
| 110 std::string* error) override { | 105 const base::ListValue* params) override { |
| 111 HandlerMap::iterator it = handlers_.find(method); | 106 HandlerMap::iterator it = handlers_.find(method); |
| 112 if (it == handlers_.end()) | 107 if (it != handlers_.end()) |
| 113 return false; | 108 it->second.Run(request_id, *params); |
| 114 | |
| 115 if (it->second.Run(*params)) | |
| 116 return true; | |
| 117 | |
| 118 if (error) | |
| 119 *error = "Invalid frontend host message parameters: " + method; | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 123 typedef base::Callback<bool(const base::ListValue&)> Handler; | |
| 124 void RegisterHandler(const std::string& method, const Handler& handler) { | |
| 125 handlers_[method] = handler; | |
| 126 } | 109 } |
| 127 | 110 |
| 128 template<typename T, typename... As> | 111 template<typename T, typename... As> |
| 129 void RegisterHandler(const std::string& method, | 112 void RegisterHandler(const std::string& method, |
| 130 void (T::*handler)(As...), T* delegate) { | 113 void (T::*handler)(int, As...), T* delegate) { |
| 131 handlers_[method] = base::Bind(&ParseAndHandle<As...>, | 114 handlers_[method] = base::Bind(&ParseAndHandle<As...>, |
| 132 base::Bind(handler, | 115 base::Bind(handler, |
| 133 base::Unretained(delegate))); | 116 base::Unretained(delegate))); |
| 134 } | 117 } |
| 135 | 118 |
| 136 private: | 119 private: |
| 137 typedef std::map<std::string, Handler> HandlerMap; | 120 using Handler = base::Callback<void(int, const base::ListValue&)>; |
| 121 using HandlerMap = std::map<std::string, Handler>; |
| 138 HandlerMap handlers_; | 122 HandlerMap handlers_; |
| 139 }; | 123 }; |
| 140 | 124 |
| 141 | 125 // static |
| 142 DevToolsEmbedderMessageDispatcher* | 126 DevToolsEmbedderMessageDispatcher* |
| 143 DevToolsEmbedderMessageDispatcher::createForDevToolsFrontend( | 127 DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend( |
| 144 Delegate* delegate) { | 128 Delegate* delegate) { |
| 145 DispatcherImpl* d = new DispatcherImpl(); | 129 DispatcherImpl* d = new DispatcherImpl(); |
| 146 | 130 |
| 147 d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate); | 131 d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate); |
| 148 d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate); | 132 d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate); |
| 149 d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate); | 133 d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate); |
| 150 d->RegisterHandler("setInspectedPageBounds", | 134 d->RegisterHandler("setInspectedPageBounds", |
| 151 &Delegate::SetInspectedPageBounds, delegate); | 135 &Delegate::SetInspectedPageBounds, delegate); |
| 152 d->RegisterHandler("inspectElementCompleted", | 136 d->RegisterHandler("inspectElementCompleted", |
| 153 &Delegate::InspectElementCompleted, delegate); | 137 &Delegate::InspectElementCompleted, delegate); |
| 154 d->RegisterHandler("inspectedURLChanged", | 138 d->RegisterHandler("inspectedURLChanged", |
| (...skipping 17 matching lines...) Expand all Loading... |
| 172 d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate); | 156 d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate); |
| 173 d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate); | 157 d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate); |
| 174 d->RegisterHandler("openUrlOnRemoteDeviceAndInspect", | 158 d->RegisterHandler("openUrlOnRemoteDeviceAndInspect", |
| 175 &Delegate::OpenUrlOnRemoteDeviceAndInspect, delegate); | 159 &Delegate::OpenUrlOnRemoteDeviceAndInspect, delegate); |
| 176 d->RegisterHandler("setDeviceCountUpdatesEnabled", | 160 d->RegisterHandler("setDeviceCountUpdatesEnabled", |
| 177 &Delegate::SetDeviceCountUpdatesEnabled, delegate); | 161 &Delegate::SetDeviceCountUpdatesEnabled, delegate); |
| 178 d->RegisterHandler("setDevicesUpdatesEnabled", | 162 d->RegisterHandler("setDevicesUpdatesEnabled", |
| 179 &Delegate::SetDevicesUpdatesEnabled, delegate); | 163 &Delegate::SetDevicesUpdatesEnabled, delegate); |
| 180 d->RegisterHandler("sendMessageToBrowser", | 164 d->RegisterHandler("sendMessageToBrowser", |
| 181 &Delegate::SendMessageToBrowser, delegate); | 165 &Delegate::SendMessageToBrowser, delegate); |
| 182 d->RegisterHandler("recordActionUMA", | 166 d->RegisterHandler("recordActionUMA", &Delegate::RecordActionUMA, delegate); |
| 183 &Delegate::RecordActionUMA, delegate); | 167 d->RegisterHandler("sendJsonRequest", &Delegate::SendJsonRequest, delegate); |
| 184 return d; | 168 return d; |
| 185 } | 169 } |
| OLD | NEW |