// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DBUS_PROPERTIES_DBUS_PROPERTIES_H_
#define COMPONENTS_DBUS_PROPERTIES_DBUS_PROPERTIES_H_

#include "base/component_export.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/dbus/utils/signature.h"
#include "components/dbus/utils/variant.h"
#include "dbus/bus.h"
#include "dbus/exported_object.h"

// https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties
class COMPONENT_EXPORT(COMPONENTS_DBUS) DbusProperties {
 public:
  using InitializedCallback = base::OnceCallback<void(bool success)>;

  // Registers method handlers for the properties interface.  The handlers will
  // not be removed until the bus is shut down.
  DbusProperties(dbus::ExportedObject* exported_object,
                 InitializedCallback callback);

  DbusProperties(const DbusProperties&) = delete;
  DbusProperties& operator=(const DbusProperties&) = delete;

  ~DbusProperties();

  void RegisterInterface(const std::string& interface);

  template <dbus_utils::SignatureLiteral Signature>
  void SetProperty(const std::string& interface,
                   const std::string& name,
                   dbus_utils::internal::ParseDBusSignature<Signature> value,
                   bool emit_signal = true) {
    auto interface_it = properties_.find(interface);
    DCHECK(interface_it != properties_.end());
    auto& properties = interface_it->second;
    auto property_it = properties.find(name);
    auto new_value = dbus_utils::Variant::Wrap<Signature>(std::move(value));
    const bool value_changed =
        property_it == properties.end() || property_it->second != new_value;
    properties[name] = std::move(new_value);
    if (emit_signal && value_changed) {
      PropertyUpdated(interface, name, /*deleted=*/false);
    }
  }

  // Deletes the property if it exists.  Returns the deleted property value, or
  // std::nullopt if the property did not exist.
  std::optional<dbus_utils::Variant> DeleteProperty(
      const std::string& interface,
      const std::string& property_name,
      bool emit_signal);

 private:
  void PropertyUpdated(const std::string& interface,
                       const std::string& property_name,
                       bool deleted);

  void OnExported(const std::string& interface_name,
                  const std::string& method_name,
                  bool success);

  void OnGetAllProperties(dbus::MethodCall* method_call,
                          dbus::ExportedObject::ResponseSender response_sender);
  void OnGetProperty(dbus::MethodCall* method_call,
                     dbus::ExportedObject::ResponseSender response_sender);
  void OnSetProperty(dbus::MethodCall* method_call,
                     dbus::ExportedObject::ResponseSender response_sender);

  bool initialized_ = false;

  raw_ptr<dbus::ExportedObject> exported_object_ = nullptr;

  base::RepeatingCallback<void(bool)> barrier_;

  // A map from interface name to a map of properties.  The properties map is
  // from property name to property value.
  std::map<std::string, std::map<std::string, dbus_utils::Variant>> properties_;

  base::WeakPtrFactory<DbusProperties> weak_factory_{this};
};

#endif  // COMPONENTS_DBUS_PROPERTIES_DBUS_PROPERTIES_H_
