blob: 9bbb55e64b1889aaca6dbfeaf79939e3ab1348e4 [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>
8
9#include "buffet/dbus_command_proxy.h"
10#include "buffet/dbus_constants.h"
11#include "weave/command.h"
12
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,
31 buffet::kCommandServicePathPrefix + std::to_string(++next_id_))};
32 proxy->RegisterAsync(AsyncEventSequencer::GetDefaultCompletionAction());
Vitaly Bukaf6027cb2015-07-31 16:20:48 -070033 // DBusCommandProxy::DBusCommandProxy() subscribe itself to weave::Command
34 // notifications. When weave::Command is being destroyed it sends
35 // ::OnCommandDestroyed() and DBusCommandProxy deletes itself.
36 proxy.release();
Vitaly Bukaa0305d32015-07-27 16:08:51 -070037}
38
39} // namespace buffet