blob: 94f938b79a6618503451fc21c1f85cff1323c828 [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#ifndef BUFFET_COMMANDS_DBUS_COMMAND_DISPATCHER_H_
6#define BUFFET_COMMANDS_DBUS_COMMAND_DISPATCHER_H_
7
8#include <map>
9#include <string>
10
11#include <base/macros.h>
12#include <base/memory/weak_ptr.h>
13#include <dbus/bus.h>
14
15#include "buffet/commands/command_dispatch_interface.h"
16#include "buffet/commands/dbus_command_proxy.h"
17
18namespace chromeos {
19namespace dbus_utils {
20class ExportedObjectManager;
21} // namespace dbus_utils
22} // namespace chromeos
23
24namespace buffet {
25
26// Implements D-Bus dispatch of commands. When OnCommandAdded is called over
27// CommandDispachInterface, DBusCommandDispacher creates an instance of
28// DBusCommandProxy object and advertises it through ExportedObjectManager on
29// D-Bus. Command handling processes can watch the new D-Bus object appear
30// and communicate with it to update the command handling progress.
31// Once command is handled, DBusCommandProxy::Done() is called and the command
32// is removed from the command queue and D-Bus ExportedObjectManager.
33class DBusCommandDispacher : public CommandDispachInterface {
34 public:
35 DBusCommandDispacher(
36 const scoped_refptr<dbus::Bus>& bus,
37 chromeos::dbus_utils::ExportedObjectManager* object_manager = nullptr);
38 virtual ~DBusCommandDispacher() = default;
39
40 // CommandDispachInterface overrides. Called by CommandQueue.
41 void OnCommandAdded(CommandInstance* command_instance) override;
42 void OnCommandRemoved(CommandInstance* command_instance) override;
43
44 // Finds a D-Bus command proxy for the given command instance.
45 // Returns nullptr if the proxy does not exist.
46 DBusCommandProxy* FindProxy(CommandInstance* command_instance) const;
47
48 protected:
49 virtual std::unique_ptr<DBusCommandProxy> CreateDBusCommandProxy(
50 CommandInstance* command_instance) const;
51
52 private:
53 scoped_refptr<dbus::Bus> bus_;
54 base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager> object_manager_;
55 // This is the map that tracks relationship between CommandInstance and
56 // corresponding DBusCommandProxy objects.
57 std::map<CommandInstance*, std::unique_ptr<DBusCommandProxy>> command_map_;
58
59 // Default constructor is used in special circumstances such as for testing.
60 DBusCommandDispacher() = default;
61
62 friend class DBusCommandDispacherTest;
63 friend class CommandManager;
64 DISALLOW_COPY_AND_ASSIGN(DBusCommandDispacher);
65};
66
67} // namespace buffet
68
69#endif // BUFFET_COMMANDS_DBUS_COMMAND_DISPATCHER_H_