blob: d23d071eacf073605d98874f0ce2708357919eac [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// 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 Bukaa0305d32015-07-27 16:08:51 -070014
15#include "buffet/dbus_command_dispatcher.h"
16
Alex Vakulenko41705852015-10-13 10:12:06 -070017#include <brillo/dbus/exported_object_manager.h>
Vitaly Bukae2713ac2015-08-03 13:50:01 -070018#include <weave/command.h>
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070019#include <weave/device.h>
Vitaly Bukaa0305d32015-07-27 16:08:51 -070020
21#include "buffet/dbus_command_proxy.h"
22#include "buffet/dbus_constants.h"
Vitaly Bukaa0305d32015-07-27 16:08:51 -070023
Alex Vakulenko41705852015-10-13 10:12:06 -070024using brillo::dbus_utils::AsyncEventSequencer;
25using brillo::dbus_utils::ExportedObjectManager;
Vitaly Bukaa0305d32015-07-27 16:08:51 -070026
27namespace buffet {
28
29DBusCommandDispacher::DBusCommandDispacher(
30 const base::WeakPtr<ExportedObjectManager>& object_manager,
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070031 weave::Device* device)
Vitaly Bukaa0305d32015-07-27 16:08:51 -070032 : object_manager_{object_manager} {
Alex Vakulenko14b3e6e2015-12-10 10:01:36 -080033 device->AddCommandHandler("", "",
Alex Vakulenko297114c2015-10-12 13:45:43 -070034 base::Bind(&DBusCommandDispacher::OnCommandAdded,
35 weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukaa0305d32015-07-27 16:08:51 -070036}
37
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070038void DBusCommandDispacher::OnCommandAdded(
39 const std::weak_ptr<weave::Command>& cmd) {
40 auto command = cmd.lock();
41 if (!object_manager_ || !command)
Vitaly Bukaa0305d32015-07-27 16:08:51 -070042 return;
43 std::unique_ptr<DBusCommandProxy> proxy{new DBusCommandProxy(
44 object_manager_.get(), object_manager_->GetBus(), command,
Robert Gindacf92c662015-08-20 09:30:11 -070045 buffet::dbus_constants::kCommandServicePathPrefix +
Alex Vakulenko297114c2015-10-12 13:45:43 -070046 std::to_string(++next_id_))};
Vitaly Bukaa0305d32015-07-27 16:08:51 -070047 proxy->RegisterAsync(AsyncEventSequencer::GetDefaultCompletionAction());
Vitaly Bukaf6027cb2015-07-31 16:20:48 -070048 // 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 Bukaa0305d32015-07-27 16:08:51 -070052}
53
54} // namespace buffet