blob: 03f47893e2a1b3fe16b0bbb8ec6cdc101436a804 [file] [log] [blame]
Vitaly Bukaa0305d32015-07-27 16:08:51 -07001// Copyright 2014 The Chromium OS 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
5#include "buffet/dbus_command_dispatcher.h"
6
7#include <chromeos/dbus/exported_object_manager.h>
Vitaly Bukae2713ac2015-08-03 13:50:01 -07008#include <weave/command.h>
Vitaly Bukaa0305d32015-07-27 16:08:51 -07009
10#include "buffet/dbus_command_proxy.h"
11#include "buffet/dbus_constants.h"
Vitaly Bukaa0305d32015-07-27 16:08:51 -070012
13using chromeos::dbus_utils::AsyncEventSequencer;
14using chromeos::dbus_utils::ExportedObjectManager;
15
16namespace buffet {
17
18DBusCommandDispacher::DBusCommandDispacher(
19 const base::WeakPtr<ExportedObjectManager>& object_manager,
20 weave::Commands* command_manager)
21 : object_manager_{object_manager} {
22 command_manager->AddOnCommandAddedCallback(base::Bind(
23 &DBusCommandDispacher::OnCommandAdded, weak_ptr_factory_.GetWeakPtr()));
24}
25
26void DBusCommandDispacher::OnCommandAdded(weave::Command* command) {
27 if (!object_manager_)
28 return;
29 std::unique_ptr<DBusCommandProxy> proxy{new DBusCommandProxy(
30 object_manager_.get(), object_manager_->GetBus(), command,
Robert Gindacf92c662015-08-20 09:30:11 -070031 buffet::dbus_constants::kCommandServicePathPrefix +
32 std::to_string(++next_id_))};
Vitaly Bukaa0305d32015-07-27 16:08:51 -070033 proxy->RegisterAsync(AsyncEventSequencer::GetDefaultCompletionAction());
Vitaly Bukaf6027cb2015-07-31 16:20:48 -070034 // DBusCommandProxy::DBusCommandProxy() subscribe itself to weave::Command
35 // notifications. When weave::Command is being destroyed it sends
36 // ::OnCommandDestroyed() and DBusCommandProxy deletes itself.
37 proxy.release();
Vitaly Bukaa0305d32015-07-27 16:08:51 -070038}
39
40} // namespace buffet