Commit a6bdb289 authored by Chris Morin's avatar Chris Morin Committed by Commit Bot

Refactor UpstartClient and allow starting arbitrary jobs

Factor out common code in UpstartClient, and allow UpstartClient to
start arbitrary jobs instead of a fixed set of them.

BUG=b:115779632
TEST=./out/Debug/unit_tests
TEST=./out/Debug/component_unittests
TEST=Ensure dummy job can be started with UpstartClient

Change-Id: I362ad288b93615753cd778e823173d24a9ac16df
Reviewed-on: https://chromium-review.googlesource.com/c/1307238Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Reviewed-by: default avatarAchuith Bhandarkar <achuith@chromium.org>
Commit-Queue: Christopher Morin <cmtm@google.com>
Cr-Commit-Position: refs/heads/master@{#604095}
parent 4f6cf809
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
#include "chromeos/cryptohome/async_method_caller.h" #include "chromeos/cryptohome/async_method_caller.h"
#include "chromeos/cryptohome/cryptohome_util.h" #include "chromeos/cryptohome/cryptohome_util.h"
#include "chromeos/dbus/cryptohome/rpc.pb.h" #include "chromeos/dbus/cryptohome/rpc.pb.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/upstart_client.h" #include "chromeos/dbus/upstart_client.h"
#include "chromeos/login/login_state.h" #include "chromeos/login/login_state.h"
...@@ -121,6 +122,8 @@ const char kDeviceLocalAccountPendingDataRemoval[] = ...@@ -121,6 +122,8 @@ const char kDeviceLocalAccountPendingDataRemoval[] =
constexpr char kGoogleDotCom[] = "@google.com"; constexpr char kGoogleDotCom[] = "@google.com";
constexpr char kBluetoothLoggingUpstartJob[] = "bluetoothlog";
bool FakeOwnership() { bool FakeOwnership() {
return base::CommandLine::ForCurrentProcess()->HasSwitch( return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kStubCrosSettings); switches::kStubCrosSettings);
...@@ -204,9 +207,8 @@ void MaybeStartBluetoothLogging(const AccountId& account_id) { ...@@ -204,9 +207,8 @@ void MaybeStartBluetoothLogging(const AccountId& account_id) {
const std::string board_name = board[0]; const std::string board_name = board[0];
if (board_name != "eve" && board_name != "nocturne") if (board_name != "eve" && board_name != "nocturne")
return; return;
chromeos::DBusThreadManager::Get() chromeos::DBusThreadManager::Get()->GetUpstartClient()->StartJob(
->GetUpstartClient() kBluetoothLoggingUpstartJob, {}, EmptyVoidDBusMethodCallback());
->StartBluetoothLogging();
} }
} // namespace } // namespace
......
...@@ -17,6 +17,13 @@ FakeUpstartClient::~FakeUpstartClient() = default; ...@@ -17,6 +17,13 @@ FakeUpstartClient::~FakeUpstartClient() = default;
void FakeUpstartClient::Init(dbus::Bus* bus) {} void FakeUpstartClient::Init(dbus::Bus* bus) {}
void FakeUpstartClient::StartJob(const std::string& job,
const std::vector<std::string>& upstart_env,
VoidDBusMethodCallback callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), true));
}
void FakeUpstartClient::StartAuthPolicyService() { void FakeUpstartClient::StartAuthPolicyService() {
static_cast<FakeAuthPolicyClient*>( static_cast<FakeAuthPolicyClient*>(
DBusThreadManager::Get()->GetAuthPolicyClient()) DBusThreadManager::Get()->GetAuthPolicyClient())
...@@ -73,8 +80,4 @@ void FakeUpstartClient::StopMediaAnalytics(VoidDBusMethodCallback callback) { ...@@ -73,8 +80,4 @@ void FakeUpstartClient::StopMediaAnalytics(VoidDBusMethodCallback callback) {
FROM_HERE, base::BindOnce(std::move(callback), true)); FROM_HERE, base::BindOnce(std::move(callback), true));
} }
void FakeUpstartClient::StartBluetoothLogging() {
NOTIMPLEMENTED();
}
} // namespace chromeos } // namespace chromeos
...@@ -19,6 +19,9 @@ class CHROMEOS_EXPORT FakeUpstartClient : public UpstartClient { ...@@ -19,6 +19,9 @@ class CHROMEOS_EXPORT FakeUpstartClient : public UpstartClient {
// DBusClient overrides. // DBusClient overrides.
void Init(dbus::Bus* bus) override; void Init(dbus::Bus* bus) override;
void StartJob(const std::string& job,
const std::vector<std::string>& upstart_env,
VoidDBusMethodCallback callback) override;
// UpstartClient overrides. // UpstartClient overrides.
void StartAuthPolicyService() override; void StartAuthPolicyService() override;
void RestartAuthPolicyService() override; void RestartAuthPolicyService() override;
...@@ -27,7 +30,6 @@ class CHROMEOS_EXPORT FakeUpstartClient : public UpstartClient { ...@@ -27,7 +30,6 @@ class CHROMEOS_EXPORT FakeUpstartClient : public UpstartClient {
void RestartMediaAnalytics(VoidDBusMethodCallback callback) override; void RestartMediaAnalytics(VoidDBusMethodCallback callback) override;
void StopMediaAnalytics() override; void StopMediaAnalytics() override;
void StopMediaAnalytics(VoidDBusMethodCallback callback) override; void StopMediaAnalytics(VoidDBusMethodCallback callback) override;
void StartBluetoothLogging() override;
private: private:
DISALLOW_COPY_AND_ASSIGN(FakeUpstartClient); DISALLOW_COPY_AND_ASSIGN(FakeUpstartClient);
......
...@@ -14,17 +14,15 @@ namespace chromeos { ...@@ -14,17 +14,15 @@ namespace chromeos {
namespace { namespace {
const char kUpstartServiceName[] = "com.ubuntu.Upstart"; constexpr char kUpstartServiceName[] = "com.ubuntu.Upstart";
const char kUpstartJobInterface[] = "com.ubuntu.Upstart0_6.Job"; constexpr char kUpstartJobInterface[] = "com.ubuntu.Upstart0_6.Job";
const char kUpstartStartMethod[] = "Start"; constexpr char kStartMethod[] = "Start";
const char kUpstartRestartMethod[] = "Restart"; constexpr char kRestartMethod[] = "Restart";
const char kUpstartStopMethod[] = "Stop"; constexpr char kStopMethod[] = "Stop";
const char kUpstartAuthPolicyPath[] = "/com/ubuntu/Upstart/jobs/authpolicyd"; constexpr char kUpstartJobsPath[] = "/com/ubuntu/Upstart/jobs/";
const char kUpstartMediaAnalyticsPath[] = constexpr char kAuthPolicyJob[] = "authpolicyd";
"/com/ubuntu/Upstart/jobs/rtanalytics"; constexpr char kMediaAnalyticsJob[] = "rtanalytics";
const char kUpstartBluetoothLoggingPath[] =
"/com/ubuntu/Upstart/jobs/bluetoothlog";
class UpstartClientImpl : public UpstartClient { class UpstartClientImpl : public UpstartClient {
public: public:
...@@ -32,118 +30,66 @@ class UpstartClientImpl : public UpstartClient { ...@@ -32,118 +30,66 @@ class UpstartClientImpl : public UpstartClient {
~UpstartClientImpl() override = default; ~UpstartClientImpl() override = default;
void StartJob(const std::string& job,
const std::vector<std::string>& upstart_env,
VoidDBusMethodCallback callback) override {
CallJobMethod(job, kStartMethod, upstart_env, std::move(callback));
}
// UpstartClient override. // UpstartClient override.
void StartAuthPolicyService() override { void StartAuthPolicyService() override {
dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStartMethod); StartJob(kAuthPolicyJob, {}, EmptyVoidDBusMethodCallback());
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfStrings(std::vector<std::string>());
writer.AppendBool(true /* wait for response */);
auth_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::HandleAuthResponse,
weak_ptr_factory_.GetWeakPtr()));
} }
void RestartAuthPolicyService() override { void RestartAuthPolicyService() override {
dbus::MethodCall method_call(kUpstartJobInterface, kUpstartRestartMethod); CallJobMethod(kAuthPolicyJob, kRestartMethod, {},
dbus::MessageWriter writer(&method_call); EmptyVoidDBusMethodCallback());
writer.AppendArrayOfStrings(std::vector<std::string>());
writer.AppendBool(true /* wait for response */);
auth_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::HandleAuthResponse,
weak_ptr_factory_.GetWeakPtr()));
} }
void StartMediaAnalytics(const std::vector<std::string>& upstart_env, void StartMediaAnalytics(const std::vector<std::string>& upstart_env,
VoidDBusMethodCallback callback) override { VoidDBusMethodCallback callback) override {
dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStartMethod); StartJob(kMediaAnalyticsJob, upstart_env, std::move(callback));
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfStrings(upstart_env);
writer.AppendBool(true /* wait for response */);
ma_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::OnVoidMethod,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
void RestartMediaAnalytics(VoidDBusMethodCallback callback) override { void RestartMediaAnalytics(VoidDBusMethodCallback callback) override {
dbus::MethodCall method_call(kUpstartJobInterface, kUpstartRestartMethod); CallJobMethod(kMediaAnalyticsJob, kRestartMethod, {}, std::move(callback));
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfStrings(std::vector<std::string>());
writer.AppendBool(true /* wait for response */);
ma_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::OnVoidMethod,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
void StopMediaAnalytics() override { void StopMediaAnalytics() override {
dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStopMethod); CallJobMethod(kMediaAnalyticsJob, kStopMethod, {},
dbus::MessageWriter writer(&method_call); EmptyVoidDBusMethodCallback());
writer.AppendArrayOfStrings(std::vector<std::string>());
writer.AppendBool(true /* wait for response */);
ma_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::HandleStopMediaAnalyticsResponse,
weak_ptr_factory_.GetWeakPtr()));
} }
void StopMediaAnalytics(VoidDBusMethodCallback callback) override { void StopMediaAnalytics(VoidDBusMethodCallback callback) override {
dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStopMethod); CallJobMethod(kMediaAnalyticsJob, kStopMethod, {}, std::move(callback));
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfStrings(std::vector<std::string>());
writer.AppendBool(true /* wait for response */);
ma_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::OnVoidMethod,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void StartBluetoothLogging() override {
dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStartMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfStrings(std::vector<std::string>());
writer.AppendBool(true /* wait for response */);
bluetooth_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::HandleBluetoothLoggingResponse,
weak_ptr_factory_.GetWeakPtr()));
} }
protected: protected:
void Init(dbus::Bus* bus) override { void Init(dbus::Bus* bus) override {
bus_ = bus; bus_ = bus;
auth_proxy_ = bus_->GetObjectProxy(
kUpstartServiceName, dbus::ObjectPath(kUpstartAuthPolicyPath));
ma_proxy_ = bus_->GetObjectProxy(
kUpstartServiceName, dbus::ObjectPath(kUpstartMediaAnalyticsPath));
bluetooth_proxy_ = bus_->GetObjectProxy(
kUpstartServiceName, dbus::ObjectPath(kUpstartBluetoothLoggingPath));
} }
private: private:
void HandleAuthResponse(dbus::Response* response) { void CallJobMethod(const std::string& job,
LOG_IF(ERROR, !response) << "Failed to signal Upstart, response is null"; const std::string& method,
const std::vector<std::string>& upstart_env,
VoidDBusMethodCallback callback) {
dbus::ObjectProxy* job_proxy = bus_->GetObjectProxy(
kUpstartServiceName, dbus::ObjectPath(kUpstartJobsPath + job));
dbus::MethodCall method_call(kUpstartJobInterface, method);
dbus::MessageWriter writer(&method_call);
writer.AppendArrayOfStrings(upstart_env);
writer.AppendBool(true /* wait for response */);
job_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&UpstartClientImpl::OnVoidMethod,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
void OnVoidMethod(VoidDBusMethodCallback callback, dbus::Response* response) { void OnVoidMethod(VoidDBusMethodCallback callback, dbus::Response* response) {
std::move(callback).Run(response); std::move(callback).Run(response);
} }
void HandleStopMediaAnalyticsResponse(dbus::Response* response) {
LOG_IF(ERROR, !response) << "Failed to signal Upstart, response is null";
}
void HandleBluetoothLoggingResponse(dbus::Response* response) {
LOG_IF(ERROR, !response)
<< "Failed to start bluetooth logging service, response is null";
}
dbus::Bus* bus_ = nullptr; dbus::Bus* bus_ = nullptr;
dbus::ObjectProxy* auth_proxy_ = nullptr;
dbus::ObjectProxy* ma_proxy_ = nullptr;
dbus::ObjectProxy* bluetooth_proxy_ = nullptr;
// Note: This should remain the last member so it'll be destroyed and // Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed. // invalidate its weak pointers before any other members are destroyed.
......
...@@ -27,6 +27,14 @@ class CHROMEOS_EXPORT UpstartClient : public DBusClient { ...@@ -27,6 +27,14 @@ class CHROMEOS_EXPORT UpstartClient : public DBusClient {
// For normal usage, access the singleton via DBusThreadManager::Get(). // For normal usage, access the singleton via DBusThreadManager::Get().
static UpstartClient* Create(); static UpstartClient* Create();
// Starts an Upstart job.
// |job|: Name of Upstart job.
// |upstart_env|: List of upstart environment variables to be passed to the
// upstart service.
virtual void StartJob(const std::string& job,
const std::vector<std::string>& upstart_env,
VoidDBusMethodCallback callback) = 0;
// Starts authpolicyd. // Starts authpolicyd.
virtual void StartAuthPolicyService() = 0; virtual void StartAuthPolicyService() = 0;
...@@ -47,10 +55,6 @@ class CHROMEOS_EXPORT UpstartClient : public DBusClient { ...@@ -47,10 +55,6 @@ class CHROMEOS_EXPORT UpstartClient : public DBusClient {
// Provides an interface for stopping the media analytics process. // Provides an interface for stopping the media analytics process.
virtual void StopMediaAnalytics(VoidDBusMethodCallback callback) = 0; virtual void StopMediaAnalytics(VoidDBusMethodCallback callback) = 0;
// Starts the bluetooth logging service.
virtual void StartBluetoothLogging() = 0;
protected: protected:
// Create() should be used instead. // Create() should be used instead.
UpstartClient(); UpstartClient();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment