Skip to content

Commit

Permalink
# This is a combination of 8 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

Fix cr114 changes

# This is the commit message #2:

Begin work on GeoLocationClientObject

# This is the commit message #3:

Add ScopedBlockingCall

# This is the commit message #4:

Use AccuracyLevel

# This is the commit message #5:

WIP locationupdated signal

# This is the commit message #6:

Formatting

# This is the commit message #7:

Move initialization into the GeoClueClientObject

# This is the commit message #8:

Revert to CHECK
  • Loading branch information
fallaciousreasoning committed Jun 14, 2023
1 parent a511211 commit f91c07b
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 180 deletions.
2 changes: 2 additions & 0 deletions services/device/geolocation/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ component("geolocation") {
defines = [ "IS_BRAVE_GEOLOCATION_IMPL" ]

sources = [
"geoclue_client_object.cc",
"geoclue_client_object.h",
"geoclue_location_provider.cc",
"geoclue_location_provider.h",
]
Expand Down
137 changes: 137 additions & 0 deletions services/device/geolocation/geoclue_client_object.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/services/device/geolocation/geoclue_client_object.h"

#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"

GeoClueClientObject::LocationProperties::LocationProperties(
dbus::ObjectProxy* proxy)
: dbus::PropertySet(proxy, kLocationInterfaceName, base::NullCallback()) {
RegisterProperty("Latitude", &latitude);
RegisterProperty("Longitude", &longitude);
RegisterProperty("Accuracy", &accuracy);
RegisterProperty("Altitude", &altitude);
RegisterProperty("Speed", &speed);
RegisterProperty("Heading", &heading);
}

GeoClueClientObject::LocationProperties::~LocationProperties() = default;

void GeoClueClientObject::LocationProperties::GetAll(
base::OnceCallback<void()> on_got_all) {
// We only support this one at a time. It's fine for now.
DCHECK(!on_got_all_);
on_got_all_ = std::move(on_got_all);
dbus::PropertySet::GetAll();
}

// dbus::PropertySet:
void GeoClueClientObject::LocationProperties::OnGetAll(
dbus::Response* response) {
dbus::PropertySet::OnGetAll(response);

if (on_got_all_) {
std::move(on_got_all_).Run();
}
}

GeoClueClientObject::Properties::Properties(dbus::ObjectProxy* proxy)
: dbus::PropertySet(proxy,
GeoClueClientObject::kInterfaceName,
base::NullCallback()) {
RegisterProperty("DesktopId", &desktop_id);
RegisterProperty("RequestedAccuracyLevel", &requested_accuracy_level);
}

GeoClueClientObject::Properties::~Properties() = default;

using GetClientCallback =
base::OnceCallback<void(std::unique_ptr<GeoClueClientObject>)>;
// static
void GeoClueClientObject::GetClient(scoped_refptr<dbus::Bus> bus,
GetClientCallback callback) {
auto* manager_proxy =
bus->GetObjectProxy(kServiceName, dbus::ObjectPath(kManagerObjectPath));
dbus::MethodCall get_client(kManagerInterfaceName, "GetClient");
manager_proxy->CallMethod(
&get_client, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(
[](scoped_refptr<dbus::Bus> bus, GetClientCallback callback,
dbus::Response* response) {
if (!response) {
std::move(callback).Run(nullptr);
return;
}

dbus::MessageReader reader(response);
dbus::ObjectPath client_path;
if (!reader.PopObjectPath(&client_path)) {
std::move(callback).Run(nullptr);
return;
}

std::move(callback).Run(std::unique_ptr<GeoClueClientObject>(
new GeoClueClientObject(bus, client_path)));
},
bus, std::move(callback)));
}

GeoClueClientObject::GeoClueClientObject(scoped_refptr<dbus::Bus> bus,
const dbus::ObjectPath& object_path)
: bus_(bus) {
proxy_ = bus_->GetObjectProxy(kServiceName, object_path);
properties_ = std::make_unique<Properties>(proxy_.get());
}

GeoClueClientObject::~GeoClueClientObject() = default;

void GeoClueClientObject::Start(dbus::ObjectProxy::ResponseCallback callback) {
dbus::MethodCall method(kInterfaceName, "Start");
proxy_->CallMethod(&method, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
std::move(callback));
}

void GeoClueClientObject::Stop(dbus::ObjectProxy::ResponseCallback callback) {
dbus::MethodCall method(kInterfaceName, "Stop");
proxy_->CallMethod(&method, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
std::move(callback));
}

using LocationChangedCallback = base::RepeatingCallback<void(
std::unique_ptr<GeoClueClientObject::LocationProperties>)>;
void GeoClueClientObject::ConnectToLocationUpdatedSignal(
LocationChangedCallback callback,
dbus::ObjectProxy::OnConnectedCallback on_connected) {
proxy_->ConnectToSignal(
kInterfaceName, "LocationUpdated",
base::BindRepeating(
[](scoped_refptr<dbus::Bus> bus, LocationChangedCallback callback,
dbus::Signal* signal) {
dbus::MessageReader reader(signal);
dbus::ObjectPath old_location;
dbus::ObjectPath new_location;
if (!reader.PopObjectPath(&old_location) ||
!reader.PopObjectPath(&new_location)) {
callback.Run(nullptr);
return;
}
auto properties = std::make_unique<LocationProperties>(
bus->GetObjectProxy(kServiceName, new_location));
properties->GetAll(base::BindOnce(callback, std::move(properties)));
},
bus_, std::move(callback)),
std::move(on_connected));
}
97 changes: 97 additions & 0 deletions services/device/geolocation/geoclue_client_object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_SERVICES_DEVICE_GEOLOCATION_GEOCLUE_CLIENT_OBJECT_H_
#define BRAVE_SERVICES_DEVICE_GEOLOCATION_GEOCLUE_CLIENT_OBJECT_H_

#include <memory>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "dbus/property.h"

class GeoClueClientObject {
public:
static constexpr char kServiceName[] = "org.freedesktop.GeoClue2";
static constexpr char kManagerInterfaceName[] =
"org.freedesktop.GeoClue2.Manager";
static constexpr char kManagerObjectPath[] =
"/org/freedesktop/GeoClue2/Manager";
static constexpr char kInterfaceName[] = "org.freedesktop.GeoClue2.Client";
static constexpr char kLocationInterfaceName[] =
"org.freedesktop.GeoClue2.Location";

enum AccuracyLevel : uint32_t {
kNone = 0,
kCountry = 1,
kCity = 4,
kNeighborhood = 5,
kStreet = 6,
kExact = 8,
};

struct LocationProperties : public dbus::PropertySet {
dbus::Property<double> latitude;
dbus::Property<double> longitude;
dbus::Property<double> accuracy;
dbus::Property<double> altitude;
dbus::Property<double> speed;
dbus::Property<double> heading;

explicit LocationProperties(dbus::ObjectProxy* proxy);
~LocationProperties() override;

using dbus::PropertySet::GetAll;
void GetAll(base::OnceCallback<void()> on_got_all);

// dbus::PropertySet:
void OnGetAll(dbus::Response* response) override;

private:
base::OnceCallback<void()> on_got_all_;
};

struct Properties : public dbus::PropertySet {
dbus::Property<std::string> desktop_id;
dbus::Property<uint32_t> requested_accuracy_level;

explicit Properties(dbus::ObjectProxy* proxy);
~Properties() override;
};

static void GetClient(
scoped_refptr<dbus::Bus> bus,
base::OnceCallback<void(std::unique_ptr<GeoClueClientObject>)> callback);

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

~GeoClueClientObject();

void Start(dbus::ObjectProxy::ResponseCallback callback = base::DoNothing());
void Stop(dbus::ObjectProxy::ResponseCallback callback = base::DoNothing());

void ConnectToLocationUpdatedSignal(
base::RepeatingCallback<
void(std::unique_ptr<LocationProperties> location)> callback,
dbus::ObjectProxy::OnConnectedCallback on_connected);

Properties* properties() { return properties_.get(); }

private:
GeoClueClientObject(scoped_refptr<dbus::Bus> bus,
const dbus::ObjectPath& object_path);

scoped_refptr<dbus::Bus> bus_;
scoped_refptr<dbus::ObjectProxy> proxy_;
std::unique_ptr<Properties> properties_;
};

#endif // BRAVE_SERVICES_DEVICE_GEOLOCATION_GEOCLUE_CLIENT_OBJECT_H_
Loading

0 comments on commit f91c07b

Please sign in to comment.