blob: e6041d23da52db28dbbe317ec37673dcdb72cbdb [file] [log] [blame]
Alex Vakulenko95110752014-09-03 16:27:21 -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/commands/dbus_command_dispatcher.h"
6
7#include <chromeos/dbus/exported_object_manager.h>
8
9#include "buffet/commands/command_instance.h"
10
11using chromeos::dbus_utils::AsyncEventSequencer;
12using chromeos::dbus_utils::ExportedObjectManager;
13
14namespace buffet {
15
16DBusCommandDispacher::DBusCommandDispacher(
17 const scoped_refptr<dbus::Bus>& bus,
18 ExportedObjectManager* object_manager)
19 : bus_(bus) {
20 if (object_manager)
21 object_manager_ = object_manager->AsWeakPtr();
22}
23
24void DBusCommandDispacher::OnCommandAdded(CommandInstance* command_instance) {
25 auto proxy = CreateDBusCommandProxy(command_instance);
26 proxy->RegisterAsync(AsyncEventSequencer::GetDefaultCompletionAction());
27 command_instance->SetProxy(proxy.get());
28
29 auto pair = std::make_pair(command_instance, std::move(proxy));
30 CHECK(command_map_.insert(std::move(pair)).second)
31 << "The command instance is already in the dispatcher command map";
32}
33
34void DBusCommandDispacher::OnCommandRemoved(CommandInstance* command_instance) {
35 command_instance->SetProxy(nullptr);
36 CHECK_GT(command_map_.erase(command_instance), 0u)
37 << "The command instance is not in the dispatcher command map";
38}
39
40DBusCommandProxy* DBusCommandDispacher::FindProxy(
41 CommandInstance* command_instance) const {
42 auto p = command_map_.find(command_instance);
43 return p != command_map_.end() ? p->second.get() : nullptr;
44}
45
46std::unique_ptr<DBusCommandProxy> DBusCommandDispacher::CreateDBusCommandProxy(
47 CommandInstance* command_instance) const {
48 return std::unique_ptr<DBusCommandProxy>(
49 new DBusCommandProxy(object_manager_.get(), bus_, command_instance));
50}
51
52} // namespace buffet