-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This CL implements the client-side of a new D-Bus service added in CL:3963808. BUG=b:251764421 TEST=Manually with CL:3963808. Change-Id: Ie30da9afedc766920a7bdc778961f6b25a3f1ad6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3932405 Reviewed-by: Satoshi Niwa <niwa@chromium.org> Reviewed-by: Ryo Hashimoto <hashimoto@chromium.org> Commit-Queue: Momoko Hattori <momohatt@chromium.org> Cr-Commit-Position: refs/heads/main@{#1067447}
- Loading branch information
Showing
6 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
chromeos/ash/components/dbus/arc/arcvm_data_migrator_client.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chromeos/ash/components/dbus/arc/arcvm_data_migrator_client.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/check_op.h" | ||
#include "base/logging.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "base/observer_list.h" | ||
#include "chromeos/ash/components/dbus/arc/fake_arcvm_data_migrator_client.h" | ||
#include "dbus/bus.h" | ||
#include "dbus/message.h" | ||
#include "dbus/object_proxy.h" | ||
#include "third_party/cros_system_api/dbus/service_constants.h" | ||
|
||
namespace ash { | ||
|
||
namespace { | ||
|
||
ArcVmDataMigratorClient* g_instance = nullptr; | ||
|
||
void OnSignalConnected(const std::string& interface_name, | ||
const std::string& signal_name, | ||
bool success) { | ||
DCHECK_EQ(interface_name, arc::data_migrator::kArcVmDataMigratorInterface); | ||
LOG_IF(DFATAL, !success) << "Failed to connect to D-Bus signal; interface: " | ||
<< interface_name << "; signal: " << signal_name; | ||
} | ||
|
||
class ArcVmDataMigratorClientImpl : public ArcVmDataMigratorClient { | ||
public: | ||
explicit ArcVmDataMigratorClientImpl(dbus::Bus* bus) | ||
: proxy_(bus->GetObjectProxy( | ||
arc::data_migrator::kArcVmDataMigratorServiceName, | ||
dbus::ObjectPath( | ||
arc::data_migrator::kArcVmDataMigratorServicePath))) { | ||
proxy_->ConnectToSignal( | ||
arc::data_migrator::kArcVmDataMigratorInterface, | ||
arc::data_migrator::kMigrationProgressSignal, | ||
base::BindRepeating(&ArcVmDataMigratorClientImpl::OnMigrationProgress, | ||
weak_ptr_factory_.GetWeakPtr()), | ||
base::BindOnce(&OnSignalConnected)); | ||
} | ||
|
||
~ArcVmDataMigratorClientImpl() override = default; | ||
|
||
ArcVmDataMigratorClientImpl(const ArcVmDataMigratorClientImpl&) = delete; | ||
ArcVmDataMigratorClientImpl& operator=(const ArcVmDataMigratorClientImpl&) = | ||
delete; | ||
|
||
// ArcVmDataMigratorClient overrides: | ||
void StartMigration(const arc::data_migrator::StartMigrationRequest& request, | ||
chromeos::VoidDBusMethodCallback callback) override { | ||
dbus::MethodCall method_call( | ||
arc::data_migrator::kArcVmDataMigratorInterface, | ||
arc::data_migrator::kStartMigrationMethod); | ||
dbus::MessageWriter writer(&method_call); | ||
writer.AppendProtoAsArrayOfBytes(request); | ||
proxy_->CallMethod( | ||
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | ||
base::BindOnce(&ArcVmDataMigratorClientImpl::OnVoidMethod, | ||
weak_ptr_factory_.GetWeakPtr(), std::move(callback))); | ||
} | ||
|
||
void AddObserver(Observer* observer) override { | ||
observers_.AddObserver(observer); | ||
} | ||
|
||
void RemoveObserver(Observer* observer) override { | ||
observers_.RemoveObserver(observer); | ||
} | ||
|
||
private: | ||
void OnMigrationProgress(dbus::Signal* signal) { | ||
dbus::MessageReader reader(signal); | ||
arc::data_migrator::DataMigrationProgress proto; | ||
if (!reader.PopArrayOfBytesAsProto(&proto)) { | ||
LOG(ERROR) << "Failed to parse DataMigrationProgress protobuf from " | ||
"D-Bus signal"; | ||
return; | ||
} | ||
for (Observer& observer : observers_) { | ||
observer.OnDataMigrationProgress(proto); | ||
} | ||
} | ||
|
||
void OnVoidMethod(chromeos::VoidDBusMethodCallback callback, | ||
dbus::Response* response) { | ||
std::move(callback).Run(response); | ||
} | ||
|
||
base::ObserverList<Observer> observers_; | ||
dbus::ObjectProxy* proxy_; | ||
base::WeakPtrFactory<ArcVmDataMigratorClientImpl> weak_ptr_factory_{this}; | ||
}; | ||
|
||
} // namespace | ||
|
||
ArcVmDataMigratorClient::ArcVmDataMigratorClient() { | ||
DCHECK(!g_instance); | ||
g_instance = this; | ||
} | ||
|
||
ArcVmDataMigratorClient::~ArcVmDataMigratorClient() { | ||
DCHECK_EQ(this, g_instance); | ||
g_instance = nullptr; | ||
} | ||
|
||
// static | ||
void ArcVmDataMigratorClient::Initialize(dbus::Bus* bus) { | ||
DCHECK(bus); | ||
new ArcVmDataMigratorClientImpl(bus); | ||
} | ||
|
||
// static | ||
void ArcVmDataMigratorClient::InitializeFake() { | ||
// Do not create a new fake if it was initialized early in a browser test (to | ||
// allow test properties to be set). | ||
if (!FakeArcVmDataMigratorClient::Get()) | ||
new FakeArcVmDataMigratorClient(); | ||
} | ||
|
||
// static | ||
void ArcVmDataMigratorClient::Shutdown() { | ||
DCHECK(g_instance); | ||
delete g_instance; | ||
} | ||
|
||
// static | ||
ArcVmDataMigratorClient* ArcVmDataMigratorClient::Get() { | ||
return g_instance; | ||
} | ||
|
||
} // namespace ash |
58 changes: 58 additions & 0 deletions
58
chromeos/ash/components/dbus/arc/arcvm_data_migrator_client.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROMEOS_ASH_COMPONENTS_DBUS_ARC_ARCVM_DATA_MIGRATOR_CLIENT_H_ | ||
#define CHROMEOS_ASH_COMPONENTS_DBUS_ARC_ARCVM_DATA_MIGRATOR_CLIENT_H_ | ||
|
||
#include "base/component_export.h" | ||
#include "base/observer_list_types.h" | ||
#include "chromeos/ash/components/dbus/arcvm_data_migrator/arcvm_data_migrator.pb.h" | ||
#include "chromeos/dbus/common/dbus_method_call_status.h" | ||
#include "dbus/bus.h" | ||
#include "dbus/message.h" | ||
|
||
namespace ash { | ||
|
||
// ArcVmDataMigratorClient is used to communicate with arcvm-data-migrator. | ||
class COMPONENT_EXPORT(ASH_DBUS_ARC) ArcVmDataMigratorClient { | ||
public: | ||
class Observer : public base::CheckedObserver { | ||
public: | ||
// Called upon receiving a progress signal from arcvm-data-migrator. | ||
virtual void OnDataMigrationProgress( | ||
const arc::data_migrator::DataMigrationProgress& progress) = 0; | ||
}; | ||
|
||
// Creates and initializes the global instance. |bus| must not be null. | ||
static void Initialize(dbus::Bus* bus); | ||
|
||
// Creates and initializes a fake global instance if not already created. | ||
static void InitializeFake(); | ||
|
||
// Destroys the global instance which must have been initialized. | ||
static void Shutdown(); | ||
|
||
// Returns the global instance if initialized. May return null. | ||
static ArcVmDataMigratorClient* Get(); | ||
|
||
// Starts the migration. | ||
virtual void StartMigration( | ||
const arc::data_migrator::StartMigrationRequest& request, | ||
chromeos::VoidDBusMethodCallback callback) = 0; | ||
|
||
virtual void AddObserver(Observer* observer) = 0; | ||
virtual void RemoveObserver(Observer* observer) = 0; | ||
|
||
ArcVmDataMigratorClient(const ArcVmDataMigratorClient&) = delete; | ||
ArcVmDataMigratorClient& operator=(const ArcVmDataMigratorClient&) = delete; | ||
|
||
protected: | ||
// Initialize() should be used instead. | ||
ArcVmDataMigratorClient(); | ||
virtual ~ArcVmDataMigratorClient(); | ||
}; | ||
|
||
} // namespace ash | ||
|
||
#endif // CHROMEOS_ASH_COMPONENTS_DBUS_ARC_ARCVM_DATA_MIGRATOR_CLIENT_H_ |
45 changes: 45 additions & 0 deletions
45
chromeos/ash/components/dbus/arc/fake_arcvm_data_migrator_client.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chromeos/ash/components/dbus/arc/fake_arcvm_data_migrator_client.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/check_op.h" | ||
#include "base/threading/thread_task_runner_handle.h" | ||
|
||
namespace ash { | ||
|
||
namespace { | ||
|
||
// Used to track the fake instance, mirrors the instance in the base class. | ||
FakeArcVmDataMigratorClient* g_instance = nullptr; | ||
|
||
} // namespace | ||
|
||
FakeArcVmDataMigratorClient::FakeArcVmDataMigratorClient() { | ||
DCHECK(!g_instance); | ||
g_instance = this; | ||
} | ||
|
||
FakeArcVmDataMigratorClient::~FakeArcVmDataMigratorClient() { | ||
DCHECK_EQ(this, g_instance); | ||
g_instance = nullptr; | ||
} | ||
|
||
// static | ||
FakeArcVmDataMigratorClient* FakeArcVmDataMigratorClient::Get() { | ||
return g_instance; | ||
} | ||
|
||
void FakeArcVmDataMigratorClient::StartMigration( | ||
const arc::data_migrator::StartMigrationRequest& request, | ||
chromeos::VoidDBusMethodCallback callback) { | ||
base::ThreadTaskRunnerHandle::Get()->PostTask( | ||
FROM_HERE, base::BindOnce(std::move(callback), true)); | ||
} | ||
|
||
void FakeArcVmDataMigratorClient::AddObserver(Observer* observer) {} | ||
void FakeArcVmDataMigratorClient::RemoveObserver(Observer* observer) {} | ||
|
||
} // namespace ash |
39 changes: 39 additions & 0 deletions
39
chromeos/ash/components/dbus/arc/fake_arcvm_data_migrator_client.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROMEOS_ASH_COMPONENTS_DBUS_ARC_FAKE_ARCVM_DATA_MIGRATOR_CLIENT_H_ | ||
#define CHROMEOS_ASH_COMPONENTS_DBUS_ARC_FAKE_ARCVM_DATA_MIGRATOR_CLIENT_H_ | ||
|
||
#include "chromeos/ash/components/dbus/arc/arcvm_data_migrator_client.h" | ||
#include "chromeos/ash/components/dbus/arcvm_data_migrator/arcvm_data_migrator.pb.h" | ||
|
||
namespace ash { | ||
|
||
// Fake implementation of ArcVmDataMigratorClient. | ||
class COMPONENT_EXPORT(ASH_DBUS_ARC) FakeArcVmDataMigratorClient | ||
: public ArcVmDataMigratorClient { | ||
public: | ||
// Returns the fake global instance if initialized. May return null. | ||
static FakeArcVmDataMigratorClient* Get(); | ||
|
||
// ArcVmDataMigratorClient overrides: | ||
void StartMigration(const arc::data_migrator::StartMigrationRequest& request, | ||
chromeos::VoidDBusMethodCallback callback) override; | ||
void AddObserver(Observer* observer) override; | ||
void RemoveObserver(Observer* observer) override; | ||
|
||
FakeArcVmDataMigratorClient(const FakeArcVmDataMigratorClient&) = delete; | ||
FakeArcVmDataMigratorClient& operator=(const FakeArcVmDataMigratorClient&) = | ||
delete; | ||
|
||
protected: | ||
friend class ArcVmDataMigratorClient; | ||
|
||
FakeArcVmDataMigratorClient(); | ||
~FakeArcVmDataMigratorClient() override; | ||
}; | ||
|
||
} // namespace ash | ||
|
||
#endif // CHROMEOS_ASH_COMPONENTS_DBUS_ARC_FAKE_ARCVM_DATA_MIGRATOR_CLIENT_H_ |