Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame^] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 14 | |
| 15 | #include "buffet/dbus_command_dispatcher.h" |
| 16 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 17 | #include <brillo/dbus/exported_object_manager.h> |
Vitaly Buka | e2713ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 18 | #include <weave/command.h> |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 19 | #include <weave/device.h> |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 20 | |
| 21 | #include "buffet/dbus_command_proxy.h" |
| 22 | #include "buffet/dbus_constants.h" |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 23 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 24 | using brillo::dbus_utils::AsyncEventSequencer; |
| 25 | using brillo::dbus_utils::ExportedObjectManager; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 26 | |
| 27 | namespace buffet { |
| 28 | |
| 29 | DBusCommandDispacher::DBusCommandDispacher( |
| 30 | const base::WeakPtr<ExportedObjectManager>& object_manager, |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 31 | weave::Device* device) |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 32 | : object_manager_{object_manager} { |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 33 | device->AddCommandHandler("", |
| 34 | base::Bind(&DBusCommandDispacher::OnCommandAdded, |
| 35 | weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 38 | void DBusCommandDispacher::OnCommandAdded( |
| 39 | const std::weak_ptr<weave::Command>& cmd) { |
| 40 | auto command = cmd.lock(); |
| 41 | if (!object_manager_ || !command) |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 42 | return; |
| 43 | std::unique_ptr<DBusCommandProxy> proxy{new DBusCommandProxy( |
| 44 | object_manager_.get(), object_manager_->GetBus(), command, |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 45 | buffet::dbus_constants::kCommandServicePathPrefix + |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 46 | std::to_string(++next_id_))}; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 47 | proxy->RegisterAsync(AsyncEventSequencer::GetDefaultCompletionAction()); |
Vitaly Buka | f6027cb | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 48 | // DBusCommandProxy::DBusCommandProxy() subscribe itself to weave::Command |
| 49 | // notifications. When weave::Command is being destroyed it sends |
| 50 | // ::OnCommandDestroyed() and DBusCommandProxy deletes itself. |
| 51 | proxy.release(); |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace buffet |