gesture_router.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2021 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. #include "chromecast/browser/gesture_router.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "chromecast/base/chromecast_switches.h"
  9. namespace chromecast {
  10. GestureRouter::GestureRouter()
  11. : gesture_source_receiver_(this),
  12. handler_(nullptr),
  13. can_go_back_(false),
  14. can_top_drag_(false),
  15. can_right_drag_(false),
  16. managed_mode_(GetSwitchValueBoolean(switches::kManagedMode, false)),
  17. weak_factory_(this) {}
  18. GestureRouter::~GestureRouter() = default;
  19. bool GestureRouter::CanHandleGesture(GestureType gesture_type) {
  20. switch (gesture_type) {
  21. case GestureType::GO_BACK:
  22. return CanGoBack();
  23. case GestureType::TAP:
  24. return true;
  25. case GestureType::TAP_DOWN:
  26. return true;
  27. case GestureType::TOP_DRAG:
  28. return CanTopDrag();
  29. case GestureType::RIGHT_DRAG:
  30. return CanRightDrag();
  31. default:
  32. return false;
  33. }
  34. }
  35. void GestureRouter::GestureProgress(GestureType gesture_type,
  36. const gfx::Point& touch_location) {
  37. switch (gesture_type) {
  38. case GestureType::GO_BACK:
  39. if (!CanGoBack())
  40. return;
  41. SendBackGestureProgress(touch_location);
  42. break;
  43. case GestureType::TOP_DRAG:
  44. if (!CanTopDrag())
  45. return;
  46. SendTopDragGestureProgress(touch_location);
  47. break;
  48. case GestureType::RIGHT_DRAG:
  49. if (!CanRightDrag())
  50. return;
  51. SendRightDragGestureProgress(touch_location);
  52. break;
  53. default:
  54. return;
  55. }
  56. }
  57. void GestureRouter::CancelGesture(GestureType gesture_type) {
  58. switch (gesture_type) {
  59. case GestureType::GO_BACK:
  60. if (!CanGoBack())
  61. return;
  62. SendBackGestureCancel();
  63. break;
  64. default:
  65. return;
  66. }
  67. }
  68. void GestureRouter::ConsumeGesture(GestureType gesture_type,
  69. GestureHandledCallback handled_callback) {
  70. switch (gesture_type) {
  71. case GestureType::NO_GESTURE:
  72. std::move(handled_callback).Run(false);
  73. break;
  74. case GestureType::GO_BACK:
  75. if (!CanGoBack()) {
  76. std::move(handled_callback).Run(false);
  77. return;
  78. }
  79. SendBackGesture(std::move(handled_callback));
  80. break;
  81. case GestureType::TAP:
  82. SendTapGesture();
  83. std::move(handled_callback).Run(true);
  84. break;
  85. case GestureType::TAP_DOWN:
  86. SendTapDownGesture();
  87. std::move(handled_callback).Run(true);
  88. break;
  89. case GestureType::TOP_DRAG:
  90. SendTopDragGestureDone();
  91. std::move(handled_callback).Run(true);
  92. break;
  93. case GestureType::RIGHT_DRAG:
  94. SendRightDragGestureDone();
  95. std::move(handled_callback).Run(true);
  96. break;
  97. default:
  98. std::move(handled_callback).Run(false);
  99. }
  100. }
  101. void GestureRouter::SetConsumeGestureCallback(ConsumerCallback callback) {
  102. consumer_callback_ = callback;
  103. }
  104. void GestureRouter::SetHandler(mojom::GestureHandler* handler) {
  105. handler_ = handler;
  106. }
  107. void GestureRouter::Subscribe(
  108. mojo::PendingRemote<mojom::GestureHandler> pending_handler_remote) {
  109. if (handler_remote_.is_bound()) {
  110. handler_remote_.reset();
  111. }
  112. handler_remote_.Bind(std::move(pending_handler_remote));
  113. SetHandler(handler_remote_.get());
  114. }
  115. void GestureRouter::SetCanGoBack(bool can_go_back) {
  116. can_go_back_ = can_go_back;
  117. if (delegate_)
  118. delegate_->SetCanGoBack(can_go_back_);
  119. }
  120. bool GestureRouter::CanGoBack() const {
  121. return can_go_back_ && (consumer_callback_ || handler_);
  122. }
  123. void GestureRouter::SendBackGesture(
  124. base::OnceCallback<void(bool)> was_handled_callback) {
  125. if (consumer_callback_) {
  126. consumer_callback_.Run(GestureType::GO_BACK,
  127. std::move(was_handled_callback));
  128. return;
  129. }
  130. if (!handler_)
  131. return;
  132. handler_->OnBackGesture(std::move(was_handled_callback));
  133. }
  134. void GestureRouter::SendBackGestureProgress(const gfx::Point& touch_location) {
  135. if (!handler_)
  136. return;
  137. handler_->OnBackGestureProgress(touch_location);
  138. }
  139. void GestureRouter::SendBackGestureCancel() {
  140. if (!handler_)
  141. return;
  142. handler_->OnBackGestureCancel();
  143. }
  144. void GestureRouter::SetCanTopDrag(bool can_top_drag) {
  145. can_top_drag_ = can_top_drag;
  146. }
  147. bool GestureRouter::CanTopDrag() const {
  148. return handler_ && can_top_drag_ && !managed_mode_;
  149. }
  150. void GestureRouter::SendTopDragGestureDone() {
  151. if (!handler_)
  152. return;
  153. handler_->OnTopDragGestureDone();
  154. }
  155. void GestureRouter::SendTopDragGestureProgress(
  156. const gfx::Point& touch_location) {
  157. if (!handler_)
  158. return;
  159. handler_->OnTopDragGestureProgress(touch_location);
  160. }
  161. void GestureRouter::SetCanRightDrag(bool can_right_drag) {
  162. can_right_drag_ = can_right_drag;
  163. }
  164. bool GestureRouter::CanRightDrag() const {
  165. return handler_ && can_right_drag_;
  166. }
  167. void GestureRouter::SendRightDragGestureDone() {
  168. if (!handler_)
  169. return;
  170. handler_->OnRightDragGestureDone();
  171. }
  172. void GestureRouter::SendRightDragGestureProgress(
  173. const gfx::Point& touch_location) {
  174. if (!handler_)
  175. return;
  176. handler_->OnRightDragGestureProgress(touch_location);
  177. }
  178. void GestureRouter::SendTapGesture() {
  179. if (!handler_)
  180. return;
  181. handler_->OnTapGesture();
  182. }
  183. void GestureRouter::SendTapDownGesture() {
  184. if (!handler_)
  185. return;
  186. handler_->OnTapDownGesture();
  187. }
  188. void GestureRouter::SetBackGestureDelegate(Delegate* delegate) {
  189. delegate_ = delegate;
  190. if (delegate_)
  191. delegate_->SetCanGoBack(can_go_back_);
  192. }
  193. base::RepeatingCallback<void(mojo::PendingReceiver<mojom::GestureSource>)>
  194. GestureRouter::GetBinder() {
  195. return base::BindRepeating(&GestureRouter::BindGestureSource,
  196. weak_factory_.GetWeakPtr());
  197. }
  198. void GestureRouter::BindGestureSource(
  199. mojo::PendingReceiver<mojom::GestureSource> request) {
  200. if (gesture_source_receiver_.is_bound()) {
  201. LOG(WARNING) << "Gesture router is being re-bound.";
  202. gesture_source_receiver_.reset();
  203. }
  204. gesture_source_receiver_.Bind(std::move(request));
  205. }
  206. } // namespace chromecast