devtools_embedder_message_dispatcher.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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
  3. // found in the LICENSE-CHROMIUM file.
  4. #include "brightray/browser/devtools_embedder_message_dispatcher.h"
  5. #include "base/bind.h"
  6. #include "base/values.h"
  7. namespace brightray {
  8. namespace {
  9. using DispatchCallback = DevToolsEmbedderMessageDispatcher::DispatchCallback;
  10. bool GetValue(const base::Value& value, std::string* result) {
  11. return value.GetAsString(result);
  12. }
  13. bool GetValue(const base::Value& value, int* result) {
  14. return value.GetAsInteger(result);
  15. }
  16. bool GetValue(const base::Value& value, bool* result) {
  17. return value.GetAsBoolean(result);
  18. }
  19. bool GetValue(const base::Value& value, gfx::Rect* rect) {
  20. const base::DictionaryValue* dict;
  21. if (!value.GetAsDictionary(&dict))
  22. return false;
  23. int x = 0;
  24. int y = 0;
  25. int width = 0;
  26. int height = 0;
  27. if (!dict->GetInteger("x", &x) || !dict->GetInteger("y", &y) ||
  28. !dict->GetInteger("width", &width) ||
  29. !dict->GetInteger("height", &height))
  30. return false;
  31. rect->SetRect(x, y, width, height);
  32. return true;
  33. }
  34. template <typename T>
  35. struct StorageTraits {
  36. using StorageType = T;
  37. };
  38. template <typename T>
  39. struct StorageTraits<const T&> {
  40. using StorageType = T;
  41. };
  42. template <typename... Ts>
  43. struct ParamTuple {
  44. bool Parse(const base::ListValue& list,
  45. const base::ListValue::const_iterator& it) {
  46. return it == list.end();
  47. }
  48. template <typename H, typename... As>
  49. void Apply(const H& handler, As... args) {
  50. handler.Run(args...);
  51. }
  52. };
  53. template <typename T, typename... Ts>
  54. struct ParamTuple<T, Ts...> {
  55. bool Parse(const base::ListValue& list,
  56. const base::ListValue::const_iterator& it) {
  57. return it != list.end() && GetValue(*it, &head) && tail.Parse(list, it + 1);
  58. }
  59. template <typename H, typename... As>
  60. void Apply(const H& handler, As... args) {
  61. tail.template Apply<H, As..., T>(handler, args..., head);
  62. }
  63. typename StorageTraits<T>::StorageType head;
  64. ParamTuple<Ts...> tail;
  65. };
  66. template <typename... As>
  67. bool ParseAndHandle(const base::Callback<void(As...)>& handler,
  68. const DispatchCallback& callback,
  69. const base::ListValue& list) {
  70. ParamTuple<As...> tuple;
  71. if (!tuple.Parse(list, list.begin()))
  72. return false;
  73. tuple.Apply(handler);
  74. return true;
  75. }
  76. template <typename... As>
  77. bool ParseAndHandleWithCallback(
  78. const base::Callback<void(const DispatchCallback&, As...)>& handler,
  79. const DispatchCallback& callback,
  80. const base::ListValue& list) {
  81. ParamTuple<As...> tuple;
  82. if (!tuple.Parse(list, list.begin()))
  83. return false;
  84. tuple.Apply(handler, callback);
  85. return true;
  86. }
  87. } // namespace
  88. /**
  89. * Dispatcher for messages sent from the frontend running in an
  90. * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
  91. * in the browser.
  92. *
  93. * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
  94. * chrome.send method accordingly.
  95. */
  96. class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
  97. public:
  98. ~DispatcherImpl() override {}
  99. bool Dispatch(const DispatchCallback& callback,
  100. const std::string& method,
  101. const base::ListValue* params) override {
  102. auto it = handlers_.find(method);
  103. return it != handlers_.end() && it->second.Run(callback, *params);
  104. }
  105. template <typename... As>
  106. void RegisterHandler(const std::string& method,
  107. void (Delegate::*handler)(As...),
  108. Delegate* delegate) {
  109. handlers_[method] =
  110. base::Bind(&ParseAndHandle<As...>,
  111. base::Bind(handler, base::Unretained(delegate)));
  112. }
  113. template <typename... As>
  114. void RegisterHandlerWithCallback(
  115. const std::string& method,
  116. void (Delegate::*handler)(const DispatchCallback&, As...),
  117. Delegate* delegate) {
  118. handlers_[method] =
  119. base::Bind(&ParseAndHandleWithCallback<As...>,
  120. base::Bind(handler, base::Unretained(delegate)));
  121. }
  122. private:
  123. using Handler =
  124. base::Callback<bool(const DispatchCallback&, const base::ListValue&)>;
  125. using HandlerMap = std::map<std::string, Handler>;
  126. HandlerMap handlers_;
  127. };
  128. // static
  129. DevToolsEmbedderMessageDispatcher*
  130. DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
  131. Delegate* delegate) {
  132. auto* d = new DispatcherImpl();
  133. d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate);
  134. d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate);
  135. d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate);
  136. d->RegisterHandler("setInspectedPageBounds",
  137. &Delegate::SetInspectedPageBounds, delegate);
  138. d->RegisterHandler("inspectElementCompleted",
  139. &Delegate::InspectElementCompleted, delegate);
  140. d->RegisterHandler("inspectedURLChanged", &Delegate::InspectedURLChanged,
  141. delegate);
  142. d->RegisterHandlerWithCallback("setIsDocked", &Delegate::SetIsDocked,
  143. delegate);
  144. d->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab, delegate);
  145. d->RegisterHandler("save", &Delegate::SaveToFile, delegate);
  146. d->RegisterHandler("append", &Delegate::AppendToFile, delegate);
  147. d->RegisterHandler("requestFileSystems", &Delegate::RequestFileSystems,
  148. delegate);
  149. d->RegisterHandler("addFileSystem", &Delegate::AddFileSystem, delegate);
  150. d->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem, delegate);
  151. d->RegisterHandler("upgradeDraggedFileSystemPermissions",
  152. &Delegate::UpgradeDraggedFileSystemPermissions, delegate);
  153. d->RegisterHandler("indexPath", &Delegate::IndexPath, delegate);
  154. d->RegisterHandlerWithCallback("loadNetworkResource",
  155. &Delegate::LoadNetworkResource, delegate);
  156. d->RegisterHandler("stopIndexing", &Delegate::StopIndexing, delegate);
  157. d->RegisterHandler("searchInPath", &Delegate::SearchInPath, delegate);
  158. d->RegisterHandler("setWhitelistedShortcuts",
  159. &Delegate::SetWhitelistedShortcuts, delegate);
  160. d->RegisterHandler("zoomIn", &Delegate::ZoomIn, delegate);
  161. d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate);
  162. d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate);
  163. d->RegisterHandler("setDevicesUpdatesEnabled",
  164. &Delegate::SetDevicesUpdatesEnabled, delegate);
  165. d->RegisterHandler("dispatchProtocolMessage",
  166. &Delegate::DispatchProtocolMessageFromDevToolsFrontend,
  167. delegate);
  168. d->RegisterHandlerWithCallback("sendJsonRequest", &Delegate::SendJsonRequest,
  169. delegate);
  170. d->RegisterHandlerWithCallback("getPreferences", &Delegate::GetPreferences,
  171. delegate);
  172. d->RegisterHandler("setPreference", &Delegate::SetPreference, delegate);
  173. d->RegisterHandler("removePreference", &Delegate::RemovePreference, delegate);
  174. d->RegisterHandler("clearPreferences", &Delegate::ClearPreferences, delegate);
  175. d->RegisterHandler("registerExtensionsAPI", &Delegate::RegisterExtensionsAPI,
  176. delegate);
  177. return d;
  178. }
  179. } // namespace brightray