blob: 35d258bfb367f283be6f01edc35ab39f579d8259 [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
Alex Vakulenko95110752014-09-03 16:27:21 -070044 protected:
45 virtual std::unique_ptr<DBusCommandProxy> CreateDBusCommandProxy(
Anton Muhin5191e812014-10-30 17:49:48 +040046 CommandInstance* command_instance);
Alex Vakulenko95110752014-09-03 16:27:21 -070047
48 private:
49 scoped_refptr<dbus::Bus> bus_;
50 base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager> object_manager_;
Anton Muhin5191e812014-10-30 17:49:48 +040051 int next_id_;
Alex Vakulenko95110752014-09-03 16:27:21 -070052
53 // Default constructor is used in special circumstances such as for testing.
54 DBusCommandDispacher() = default;
55
56 friend class DBusCommandDispacherTest;
57 friend class CommandManager;
58 DISALLOW_COPY_AND_ASSIGN(DBusCommandDispacher);
59};
60
61} // namespace buffet
62
63#endif // BUFFET_COMMANDS_DBUS_COMMAND_DISPATCHER_H_