Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 1 | // 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 | |
| 18 | namespace chromeos { |
| 19 | namespace dbus_utils { |
| 20 | class ExportedObjectManager; |
| 21 | } // namespace dbus_utils |
| 22 | } // namespace chromeos |
| 23 | |
| 24 | namespace 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. |
| 33 | class 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 Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 44 | protected: |
| 45 | virtual std::unique_ptr<DBusCommandProxy> CreateDBusCommandProxy( |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 46 | CommandInstance* command_instance); |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | scoped_refptr<dbus::Bus> bus_; |
| 50 | base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager> object_manager_; |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 51 | int next_id_; |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 52 | |
| 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_ |