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 "components/arc/arc_bridge_service.h" | 5 #include "components/arc/arc_bridge_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 void ArcBridgeService::AddObserver(Observer* observer) { | 122 void ArcBridgeService::AddObserver(Observer* observer) { |
123 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 123 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
124 observer_list_.AddObserver(observer); | 124 observer_list_.AddObserver(observer); |
125 } | 125 } |
126 | 126 |
127 void ArcBridgeService::RemoveObserver(Observer* observer) { | 127 void ArcBridgeService::RemoveObserver(Observer* observer) { |
128 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 128 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
129 observer_list_.RemoveObserver(observer); | 129 observer_list_.RemoveObserver(observer); |
130 } | 130 } |
131 | 131 |
132 bool ArcBridgeService::RegisterInputDevice(const std::string& name, | 132 base::ScopedFD ArcBridgeService::CreateBridgeInputDevice( |
133 const std::string& device_type, | 133 const std::string& name, |
134 base::ScopedFD fd) { | 134 const std::string& device_type) { |
135 DCHECK(ipc_task_runner_->RunsTasksOnCurrentThread()); | 135 // Create file descriptor pair for communication |
136 if (state_ != State::READY) { | 136 int fd[2]; |
137 LOG(ERROR) << "Called RegisterInputDevice when the service is not ready"; | 137 if (pipe(fd) < 0) { |
138 return false; | 138 PLOG(ERROR) << "Cannot create pipe"; |
Luis Héctor Chávez
2015/11/17 17:51:53
At this point the operation cannot continue. retur
denniskempin
2015/11/19 19:22:09
Done.
| |
139 } | 139 } |
140 return ipc_channel_->Send(new ArcInstanceMsg_RegisterInputDevice( | 140 |
141 name, device_type, base::FileDescriptor(fd.Pass()))); | 141 // The read end is sent to the instance |
142 ipc_channel_->Send(new ArcInstanceMsg_RegisterInputDevice( | |
Luis Héctor Chávez
2015/11/17 17:51:53
What if this fails (returns false)? Shouldn't you
denniskempin
2015/11/19 19:22:09
Done. I'm checking for errors and return -1 as a f
| |
143 name, device_type, base::FileDescriptor(fd[0], true))); | |
144 | |
145 // return the write end | |
146 return base::ScopedFD(fd[1]); | |
147 } | |
148 | |
149 void ArcBridgeService::SetupBridgeInputDevices() { | |
150 bridge_devices_.push_back(new TouchscreenBridgeInputDevice( | |
151 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"))); | |
152 bridge_devices_.push_back(new MouseBridgeInputDevice( | |
153 CreateBridgeInputDevice("ChromeOS Mouse", "mouse"))); | |
154 bridge_devices_.push_back(new KeyboardBridgeInputDevice( | |
155 CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"))); | |
156 } | |
157 | |
158 void ArcBridgeService::SendInputEvent(ui::Event* event) { | |
159 if (bridge_devices_.empty()) { | |
denniskempin
2015/11/16 18:55:49
NOTE: this is temporary until we have a CL that al
denniskempin
2015/11/19 19:22:09
removed.
| |
160 SetupBridgeInputDevices(); | |
161 } | |
162 for (BridgeInputDevice* device : bridge_devices_) { | |
163 device->OnEvent(event); | |
164 } | |
142 } | 165 } |
143 | 166 |
144 void ArcBridgeService::SocketConnect(const base::FilePath& socket_path) { | 167 void ArcBridgeService::SocketConnect(const base::FilePath& socket_path) { |
145 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 168 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
146 if (state_ != State::STOPPED) { | 169 if (state_ != State::STOPPED) { |
147 VLOG(1) << "SocketConnect() called when instance is not stopped"; | 170 VLOG(1) << "SocketConnect() called when instance is not stopped"; |
148 return; | 171 return; |
149 } | 172 } |
150 SetState(State::CONNECTING); | 173 SetState(State::CONNECTING); |
151 base::PostTaskAndReplyWithResult( | 174 base::PostTaskAndReplyWithResult( |
152 file_task_runner_.get(), FROM_HERE, | 175 file_task_runner_.get(), FROM_HERE, |
153 base::Bind(&base::CreateDirectory, socket_path.DirName()), | 176 base::Bind(&base::CreateDirectory, socket_path.DirName()), |
154 base::Bind(&ArcBridgeService::SocketConnectAfterEnsureParentDirectory, | 177 base::Bind(&ArcBridgeService::SocketConnectAfterEnsureParentDirectory, |
155 weak_factory_.GetWeakPtr(), socket_path)); | 178 weak_factory_.GetWeakPtr(), socket_path)); |
156 } | 179 } |
157 | 180 |
158 void ArcBridgeService::SocketConnectAfterEnsureParentDirectory( | 181 void ArcBridgeService::SocketConnectAfterEnsureParentDirectory( |
159 const base::FilePath& socket_path, | 182 const base::FilePath& socket_path, |
160 bool directory_present) { | 183 bool directory_present) { |
161 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 184 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
162 if (state_ != State::CONNECTING) { | 185 if (state_ != State::CONNECTING) { |
163 VLOG(1) << "StopInstance() called while connecting"; | 186 VLOG(1) << "StopInstance() called while connecting"; |
164 return; | 187 return; |
165 } | 188 } |
166 if (!directory_present) { | 189 if (!directory_present) { |
167 LOG(ERROR) << "Error creating directory for " << socket_path.value(); | 190 LOG(ERROR) << "Error creating directory for " << socket_path.value(); |
168 StopInstance(); | 191 StopInstance(); |
169 return; | 192 return; |
170 } | 193 } |
171 | |
172 if (!Connect(IPC::ChannelHandle(socket_path.value()), | 194 if (!Connect(IPC::ChannelHandle(socket_path.value()), |
173 IPC::Channel::MODE_OPEN_NAMED_SERVER)) { | 195 IPC::Channel::MODE_OPEN_NAMED_SERVER)) { |
174 LOG(ERROR) << "Error connecting to " << socket_path.value(); | 196 LOG(ERROR) << "Error connecting to " << socket_path.value(); |
175 StopInstance(); | 197 StopInstance(); |
176 return; | 198 return; |
177 } | 199 } |
178 | 200 |
179 base::PostTaskAndReplyWithResult( | 201 base::PostTaskAndReplyWithResult( |
180 file_task_runner_.get(), FROM_HERE, | 202 file_task_runner_.get(), FROM_HERE, |
181 // TODO(lhchavez): Tighten the security around the socket by tying it to | 203 // TODO(lhchavez): Tighten the security around the socket by tying it to |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 void ArcBridgeService::OnInstanceStopped(bool success) { | 303 void ArcBridgeService::OnInstanceStopped(bool success) { |
282 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 304 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
283 // STOPPING is the only valid state for this function. | 305 // STOPPING is the only valid state for this function. |
284 // DCHECK on enum classes not supported. | 306 // DCHECK on enum classes not supported. |
285 DCHECK(state_ == State::STOPPING); | 307 DCHECK(state_ == State::STOPPING); |
286 ipc_channel_.reset(); | 308 ipc_channel_.reset(); |
287 SetState(State::STOPPED); | 309 SetState(State::STOPPED); |
288 } | 310 } |
289 | 311 |
290 } // namespace arc | 312 } // namespace arc |
OLD | NEW |