1//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef APMANAGER_DBUS_MANAGER_DBUS_ADAPTOR_H_
18#define APMANAGER_DBUS_MANAGER_DBUS_ADAPTOR_H_
19
20#include <map>
21
22#include <base/macros.h>
23#include <brillo/dbus/dbus_service_watcher.h>
24#include <dbus_bindings/org.chromium.apmanager.Manager.h>
25
26#include "apmanager/manager_adaptor_interface.h"
27
28namespace apmanager {
29
30class Manager;
31class Service;
32
33class ManagerDBusAdaptor : public org::chromium::apmanager::ManagerInterface,
34                           public ManagerAdaptorInterface {
35 public:
36  ManagerDBusAdaptor(const scoped_refptr<dbus::Bus>& bus,
37                     brillo::dbus_utils::ExportedObjectManager* object_manager,
38                     Manager* manager);
39  ~ManagerDBusAdaptor() override;
40
41  // Implementation of org::chromium::apmanager::ManagerInterface.
42  bool CreateService(brillo::ErrorPtr* dbus_error,
43                     dbus::Message* message,
44                     dbus::ObjectPath* out_service) override;
45  bool RemoveService(brillo::ErrorPtr* dbus_error,
46                     dbus::Message* message,
47                     const dbus::ObjectPath& in_service) override;
48
49  // Implementation of ManagerAdaptorInterface.
50  void RegisterAsync(
51      const base::Callback<void(bool)>& completion_callback) override;
52
53 private:
54  using DBusServiceWatcher = brillo::dbus_utils::DBusServiceWatcher;
55  // Context for service owner watcher.
56  struct ServiceOwnerWatcherContext {
57    ServiceOwnerWatcherContext() {}
58    ServiceOwnerWatcherContext(const scoped_refptr<Service>& in_service,
59                               std::unique_ptr<DBusServiceWatcher> in_watcher)
60        : service(in_service),
61          watcher(std::move(in_watcher)) {}
62    scoped_refptr<Service> service;
63    std::unique_ptr<DBusServiceWatcher> watcher;
64  };
65
66  // Invoked when the owner of a Service vanished.
67  void OnServiceOwnerVanished(const dbus::ObjectPath& service_path);
68
69  org::chromium::apmanager::ManagerAdaptor adaptor_;
70  brillo::dbus_utils::DBusObject dbus_object_;
71  scoped_refptr<dbus::Bus> bus_;
72  Manager* manager_;
73  // Map of service path to owner monitor context.
74  std::map<dbus::ObjectPath, ServiceOwnerWatcherContext>
75      service_owner_watchers_;
76
77  DISALLOW_COPY_AND_ASSIGN(ManagerDBusAdaptor);
78};
79
80}  // namespace apmanager
81
82#endif  // APMANAGER_DBUS_MANAGER_DBUS_ADAPTOR_H_
83