OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "services/ui/input_manager/input_associate.h" | 5 #include "services/ui/input_manager/input_associate.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "mojo/public/cpp/bindings/interface_request.h" | 11 #include "mojo/services/ui/views/cpp/formatting.h" |
12 | 12 |
13 namespace input_manager { | 13 namespace input_manager { |
| 14 namespace { |
| 15 std::ostream& operator<<(std::ostream& os, const mojo::Event& value) { |
| 16 os << "{action=" << value.action; |
| 17 if (value.pointer_data) |
| 18 os << ", x=" << value.pointer_data->x << ", y=" << value.pointer_data->y; |
| 19 if (value.key_data) |
| 20 os << ", key_code=" << value.key_data->key_code; |
| 21 return os << "}"; |
| 22 } |
| 23 } // namespace |
14 | 24 |
15 InputAssociate::InputAssociate() {} | 25 InputAssociate::InputAssociate() {} |
16 | 26 |
17 InputAssociate::~InputAssociate() {} | 27 InputAssociate::~InputAssociate() {} |
18 | 28 |
19 void InputAssociate::Connect( | 29 void InputAssociate::Connect( |
20 mojo::InterfaceHandle<mojo::ui::ViewInspector> inspector, | 30 mojo::InterfaceHandle<mojo::ui::ViewInspector> inspector, |
21 const ConnectCallback& callback) { | 31 const ConnectCallback& callback) { |
22 DCHECK(inspector); // checked by mojom | 32 DCHECK(inspector); // checked by mojom |
23 | 33 |
| 34 input_connections_by_view_token_.clear(); |
| 35 input_dispatchers_by_view_tree_token_.clear(); |
| 36 inspector_ = new mojo::ui::ViewInspectorClient( |
| 37 mojo::ui::ViewInspectorPtr::Create(std::move(inspector))); |
| 38 |
24 auto info = mojo::ui::ViewAssociateInfo::New(); | 39 auto info = mojo::ui::ViewAssociateInfo::New(); |
25 info->view_service_names.push_back(mojo::ui::InputConnection::Name_); | 40 info->view_service_names.push_back(mojo::ui::InputConnection::Name_); |
26 info->view_tree_service_names.push_back(mojo::ui::InputDispatcher::Name_); | 41 info->view_tree_service_names.push_back(mojo::ui::InputDispatcher::Name_); |
27 callback.Run(info.Pass()); | 42 callback.Run(info.Pass()); |
28 } | 43 } |
29 | 44 |
30 void InputAssociate::ConnectToViewService( | 45 void InputAssociate::ConnectToViewService( |
31 mojo::ui::ViewTokenPtr view_token, | 46 mojo::ui::ViewTokenPtr view_token, |
32 const mojo::String& service_name, | 47 const mojo::String& service_name, |
33 mojo::ScopedMessagePipeHandle client_handle) { | 48 mojo::ScopedMessagePipeHandle client_handle) { |
34 DCHECK(view_token); // checked by mojom | 49 DCHECK(view_token); // checked by mojom |
35 | 50 |
36 if (service_name == mojo::ui::InputConnection::Name_) { | 51 if (service_name == mojo::ui::InputConnection::Name_) { |
37 input_connections_.AddBinding( | 52 CreateInputConnection( |
38 new InputConnectionImpl(this, view_token.Pass()), | 53 view_token.Pass(), |
39 mojo::MakeRequest<mojo::ui::InputConnection>(client_handle.Pass())); | 54 mojo::MakeRequest<mojo::ui::InputConnection>(client_handle.Pass())); |
40 } | 55 } |
41 } | 56 } |
42 | 57 |
43 void InputAssociate::ConnectToViewTreeService( | 58 void InputAssociate::ConnectToViewTreeService( |
44 mojo::ui::ViewTreeTokenPtr view_tree_token, | 59 mojo::ui::ViewTreeTokenPtr view_tree_token, |
45 const mojo::String& service_name, | 60 const mojo::String& service_name, |
46 mojo::ScopedMessagePipeHandle client_handle) { | 61 mojo::ScopedMessagePipeHandle client_handle) { |
47 DCHECK(view_tree_token); // checked by mojom | 62 DCHECK(view_tree_token); // checked by mojom |
48 | 63 |
49 if (service_name == mojo::ui::InputDispatcher::Name_) { | 64 if (service_name == mojo::ui::InputDispatcher::Name_) { |
50 input_dispatchers_.AddBinding( | 65 CreateInputDispatcher( |
51 new InputDispatcherImpl(this, view_tree_token.Pass()), | 66 view_tree_token.Pass(), |
52 mojo::MakeRequest<mojo::ui::InputDispatcher>(client_handle.Pass())); | 67 mojo::MakeRequest<mojo::ui::InputDispatcher>(client_handle.Pass())); |
53 } | 68 } |
54 } | 69 } |
55 | 70 |
56 void InputAssociate::SetListener( | 71 void InputAssociate::CreateInputConnection( |
57 mojo::ui::ViewToken* view_token, | 72 mojo::ui::ViewTokenPtr view_token, |
58 mojo::InterfaceHandle<mojo::ui::InputListener> listener) { | 73 mojo::InterfaceRequest<mojo::ui::InputConnection> request) { |
59 // TODO(jeffbrown): This simple hack just hooks up the first listener | 74 DCHECK(view_token); |
60 // ever seen. | 75 DCHECK(request.is_pending()); |
61 if (!listener_) | 76 DVLOG(1) << "CreateInputConnection: view_token=" << view_token; |
62 listener_ = mojo::ui::InputListenerPtr::Create(std::move(listener)); | 77 |
| 78 const uint32_t view_token_value = view_token->value; |
| 79 input_connections_by_view_token_.emplace( |
| 80 view_token_value, |
| 81 std::unique_ptr<InputConnectionImpl>( |
| 82 new InputConnectionImpl(this, view_token.Pass(), request.Pass()))); |
63 } | 83 } |
64 | 84 |
65 void InputAssociate::DispatchEvent(mojo::ui::ViewTreeToken* view_tree_token, | 85 void InputAssociate::OnInputConnectionDied(InputConnectionImpl* connection) { |
66 mojo::EventPtr event) { | 86 DCHECK(connection); |
67 if (listener_) | 87 auto it = |
68 listener_->OnEvent( | 88 input_connections_by_view_token_.find(connection->view_token()->value); |
69 event.Pass(), | 89 DCHECK(it != input_connections_by_view_token_.end()); |
70 base::Bind(&InputAssociate::OnEventFinished, base::Unretained(this))); | 90 DCHECK(it->second.get() == connection); |
| 91 DVLOG(1) << "OnInputConnectionDied: view_token=" << connection->view_token(); |
| 92 |
| 93 input_connections_by_view_token_.erase(it); |
71 } | 94 } |
72 | 95 |
73 void InputAssociate::OnEventFinished(bool handled) { | 96 void InputAssociate::CreateInputDispatcher( |
74 // TODO: detect ANRs | 97 mojo::ui::ViewTreeTokenPtr view_tree_token, |
| 98 mojo::InterfaceRequest<mojo::ui::InputDispatcher> request) { |
| 99 DCHECK(view_tree_token); |
| 100 DCHECK(request.is_pending()); |
| 101 DVLOG(1) << "CreateInputDispatcher: view_tree_token=" << view_tree_token; |
| 102 |
| 103 const uint32_t view_tree_token_value = view_tree_token->value; |
| 104 input_dispatchers_by_view_tree_token_.emplace( |
| 105 view_tree_token_value, |
| 106 std::unique_ptr<InputDispatcherImpl>(new InputDispatcherImpl( |
| 107 this, view_tree_token.Pass(), request.Pass()))); |
75 } | 108 } |
76 | 109 |
77 InputAssociate::InputConnectionImpl::InputConnectionImpl( | 110 void InputAssociate::OnInputDispatcherDied(InputDispatcherImpl* dispatcher) { |
78 InputAssociate* associate, | 111 DCHECK(dispatcher); |
79 mojo::ui::ViewTokenPtr view_token) | 112 DVLOG(1) << "OnInputDispatcherDied: view_tree_token=" |
80 : associate_(associate), view_token_(view_token.Pass()) { | 113 << dispatcher->view_tree_token(); |
81 DCHECK(associate_); | 114 |
82 DCHECK(view_token_); | 115 auto it = input_dispatchers_by_view_tree_token_.find( |
| 116 dispatcher->view_tree_token()->value); |
| 117 DCHECK(it != input_dispatchers_by_view_tree_token_.end()); |
| 118 DCHECK(it->second.get() == dispatcher); |
| 119 |
| 120 input_dispatchers_by_view_tree_token_.erase(it); |
83 } | 121 } |
84 | 122 |
85 InputAssociate::InputConnectionImpl::~InputConnectionImpl() {} | 123 void InputAssociate::DeliverEvent(const mojo::ui::ViewToken* view_token, |
| 124 mojo::EventPtr event) { |
| 125 DCHECK(view_token); |
| 126 DCHECK(event); |
| 127 DVLOG(1) << "DeliverEvent: view_token=" << *view_token |
| 128 << ", event=" << *event; |
86 | 129 |
87 void InputAssociate::InputConnectionImpl::SetListener( | 130 auto it = input_connections_by_view_token_.find(view_token->value); |
88 mojo::InterfaceHandle<mojo::ui::InputListener> listener) { | 131 if (it == input_connections_by_view_token_.end()) { |
89 associate_->SetListener(view_token_.get(), std::move(listener)); | 132 DVLOG(1) << "DeliverEvent: dropped because there was no input connection"; |
90 } | 133 return; |
| 134 } |
91 | 135 |
92 InputAssociate::InputDispatcherImpl::InputDispatcherImpl( | 136 it->second->DeliverEvent(event.Pass()); |
93 InputAssociate* associate, | |
94 mojo::ui::ViewTreeTokenPtr view_tree_token) | |
95 : associate_(associate), view_tree_token_(view_tree_token.Pass()) { | |
96 DCHECK(associate_); | |
97 DCHECK(view_tree_token_); | |
98 } | |
99 | |
100 InputAssociate::InputDispatcherImpl::~InputDispatcherImpl() {} | |
101 | |
102 void InputAssociate::InputDispatcherImpl::DispatchEvent(mojo::EventPtr event) { | |
103 associate_->DispatchEvent(view_tree_token_.get(), event.Pass()); | |
104 } | 137 } |
105 | 138 |
106 } // namespace input_manager | 139 } // namespace input_manager |
OLD | NEW |