Repository: electron/electron
File: shell/browser/ui/file_dialog_linux_portal.cc (C++)

Code before:
constexpr char kXdgPortalRequiredVersionFlag[] = "xdg-portal-required-version";

bool g_portal_available = false;

struct FileChooserProperties : dbus::PropertySet {
  dbus::Property<uint32_t> version;

  explicit FileChooserProperties(dbus::ObjectProxy* object_proxy)
      : dbus::PropertySet(object_proxy, kFileChooserInterfaceName, {}) {
    RegisterProperty("version", &version);
  }

  ~FileChooserProperties() override = default;
};

base::AtomicFlag* GetAvailabilityTestCompletionFlag() {
  static base::NoDestructor<base::AtomicFlag> flag;
  return flag.get();
}

void CheckPortalAvailabilityOnBusThread() {
  auto* flag = GetAvailabilityTestCompletionFlag();
  if (flag->IsSet())
    return;

  scoped_refptr<dbus::Bus> bus = dbus_thread_linux::GetSharedSessionBus();
  dbus_utils::CheckForServiceAndStart(
      bus, kXdgPortalService,
      base::BindOnce(
          [](scoped_refptr<dbus::Bus> bus, base::AtomicFlag* flag,
             std::optional<bool> name_has_owner) {
            if (name_has_owner.value_or(false)) {
              //
              dbus::ObjectPath portal_path(kXdgPortalObject);
              dbus::ObjectProxy* portal =
                  bus->GetObjectProxy(kXdgPortalService, portal_path);
              FileChooserProperties properties(portal);
              if (!properties.GetAndBlock(&properties.version)) {
                LOG(ERROR) << "Failed to read portal version property";
              } else if (properties.version.value() >=
                         g_required_portal_version) {
                g_portal_available = true;
                g_available_portal_version = properties.version.value();
              }
            }
            VLOG(1) << "File chooser portal available: "
                    << (g_portal_available ? "yes" : "no");
            flag->Set();
            bus->ShutdownAndBlock();
            bus.reset();
          },

Review comment (refactor):
The portal code path cannot use the shared session bus, the upstream implementation refactored the portal code to use shared session bus but it doesn't work for us context in https://github.com/electron/electron/issues/46652#issuecomment-2846088555, hence we explicitly create and mange a bus here.

Code after:
constexpr char kXdgPortalRequiredVersionFlag[] = "xdg-portal-required-version";

bool g_portal_available = false;

struct FileChooserProperties : dbus::PropertySet {
  dbus::Property<uint32_t> version;

  explicit FileChooserProperties(dbus::ObjectProxy* object_proxy)
      : dbus::PropertySet(object_proxy, kFileChooserInterfaceName, {}) {
    RegisterProperty("version", &version);
  }

  ~FileChooserProperties() override = default;
};

base::AtomicFlag* GetAvailabilityTestCompletionFlag() {
  static base::NoDestructor<base::AtomicFlag> flag;
  return flag.get();
}

void CheckPortalAvailabilityOnBusThread() {
  auto* flag = GetAvailabilityTestCompletionFlag();
  if (flag->IsSet())
    return;

  dbus::Bus::Options options;
  options.bus_type = dbus::Bus::SESSION;
  options.connection_type = dbus::Bus::PRIVATE;
  options.dbus_task_runner = dbus_thread_linux::GetTaskRunner();
  scoped_refptr<dbus::Bus> bus = base::MakeRefCounted<dbus::Bus>(options);
  dbus_utils::CheckForServiceAndStart(
      bus, kXdgPortalService,
      base::BindOnce(
          [](scoped_refptr<dbus::Bus> bus, base::AtomicFlag* flag,
             std::optional<bool> name_has_owner) {
            if (name_has_owner.value_or(false)) {
              //
              dbus::ObjectPath portal_path(kXdgPortalObject);
              dbus::ObjectProxy* portal =
                  bus->GetObjectProxy(kXdgPortalService, portal_path);
              FileChooserProperties properties(portal);
              if (!properties.GetAndBlock(&properties.version)) {
                LOG(ERROR) << "Failed to read portal version property";
              } else if (properties.version.value() >=
                         g_required_portal_version) {
                g_portal_available = true;
                g_available_portal_version = properties.version.value();
              }
            }
            VLOG(1) << "File chooser portal available: "
                    << (g_portal_available ? "yes" : "no");