| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/upstart_client.h" | 5 #include "chromeos/dbus/upstart_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "dbus/bus.h" | 9 #include "dbus/bus.h" |
| 10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
| 11 #include "dbus/object_proxy.h" | 11 #include "dbus/object_proxy.h" |
| 12 | 12 |
| 13 namespace chromeos { | 13 namespace chromeos { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const char kUpstartServiceName[] = "com.ubuntu.Upstart"; | 17 const char kUpstartServiceName[] = "com.ubuntu.Upstart"; |
| 18 const char kUpstartJobInterface[] = "com.ubuntu.Upstart0_6.Job"; | 18 const char kUpstartJobInterface[] = "com.ubuntu.Upstart0_6.Job"; |
| 19 const char kUpstartStartMethod[] = "Start"; | 19 const char kUpstartStartMethod[] = "Start"; |
| 20 const char kUpstartRestartMethod[] = "Restart"; |
| 20 | 21 |
| 21 const char kUpstartAuthPolicyPath[] = "/com/ubuntu/Upstart/jobs/authpolicyd"; | 22 const char kUpstartAuthPolicyPath[] = "/com/ubuntu/Upstart/jobs/authpolicyd"; |
| 22 | 23 |
| 23 class UpstartClientImpl : public UpstartClient { | 24 class UpstartClientImpl : public UpstartClient { |
| 24 public: | 25 public: |
| 25 UpstartClientImpl() : weak_ptr_factory_(this) {} | 26 UpstartClientImpl() : weak_ptr_factory_(this) {} |
| 26 | 27 |
| 27 ~UpstartClientImpl() override {} | 28 ~UpstartClientImpl() override {} |
| 28 | 29 |
| 29 // UpstartClient override. | 30 // UpstartClient override. |
| 30 void StartAuthPolicyService() override { | 31 void StartAuthPolicyService() override { |
| 31 dbus::ObjectPath objectPath(kUpstartAuthPolicyPath); | |
| 32 dbus::ObjectProxy* proxy = | |
| 33 bus_->GetObjectProxy(kUpstartServiceName, objectPath); | |
| 34 dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStartMethod); | 32 dbus::MethodCall method_call(kUpstartJobInterface, kUpstartStartMethod); |
| 35 dbus::MessageWriter writer(&method_call); | 33 dbus::MessageWriter writer(&method_call); |
| 36 writer.AppendArrayOfStrings(std::vector<std::string>()); | 34 writer.AppendArrayOfStrings(std::vector<std::string>()); |
| 37 writer.AppendBool(true); // Wait for response. | 35 writer.AppendBool(true); // Wait for response. |
| 38 proxy->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 36 auth_proxy_->CallMethod(&method_call, |
| 39 base::Bind(&UpstartClientImpl::HandleStartResponse, | 37 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 40 weak_ptr_factory_.GetWeakPtr())); | 38 base::Bind(&UpstartClientImpl::HandleAuthResponse, |
| 39 weak_ptr_factory_.GetWeakPtr())); |
| 40 } |
| 41 |
| 42 void RestartAuthPolicyService() override { |
| 43 dbus::MethodCall method_call(kUpstartJobInterface, kUpstartRestartMethod); |
| 44 dbus::MessageWriter writer(&method_call); |
| 45 writer.AppendArrayOfStrings(std::vector<std::string>()); |
| 46 writer.AppendBool(true); // Wait for response. |
| 47 auth_proxy_->CallMethod(&method_call, |
| 48 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 49 base::Bind(&UpstartClientImpl::HandleAuthResponse, |
| 50 weak_ptr_factory_.GetWeakPtr())); |
| 41 } | 51 } |
| 42 | 52 |
| 43 protected: | 53 protected: |
| 44 void Init(dbus::Bus* bus) override { bus_ = bus; } | 54 void Init(dbus::Bus* bus) override { |
| 55 bus_ = bus; |
| 56 auth_proxy_ = bus_->GetObjectProxy( |
| 57 kUpstartServiceName, dbus::ObjectPath(kUpstartAuthPolicyPath)); |
| 58 } |
| 45 | 59 |
| 46 private: | 60 private: |
| 47 void HandleStartResponse(dbus::Response* response) { | 61 void HandleAuthResponse(dbus::Response* response) { |
| 48 LOG_IF(ERROR, !response) << "Failed to signal Upstart, response is null"; | 62 LOG_IF(ERROR, !response) << "Failed to signal Upstart, response is null"; |
| 49 } | 63 } |
| 50 | 64 |
| 51 dbus::Bus* bus_ = nullptr; | 65 dbus::Bus* bus_ = nullptr; |
| 66 dbus::ObjectProxy* auth_proxy_ = nullptr; |
| 52 | 67 |
| 53 // Note: This should remain the last member so it'll be destroyed and | 68 // Note: This should remain the last member so it'll be destroyed and |
| 54 // invalidate its weak pointers before any other members are destroyed. | 69 // invalidate its weak pointers before any other members are destroyed. |
| 55 base::WeakPtrFactory<UpstartClientImpl> weak_ptr_factory_; | 70 base::WeakPtrFactory<UpstartClientImpl> weak_ptr_factory_; |
| 56 | 71 |
| 57 DISALLOW_COPY_AND_ASSIGN(UpstartClientImpl); | 72 DISALLOW_COPY_AND_ASSIGN(UpstartClientImpl); |
| 58 }; | 73 }; |
| 59 | 74 |
| 60 } // namespace | 75 } // namespace |
| 61 | 76 |
| 62 UpstartClient::UpstartClient() {} | 77 UpstartClient::UpstartClient() {} |
| 63 | 78 |
| 64 UpstartClient::~UpstartClient() {} | 79 UpstartClient::~UpstartClient() {} |
| 65 | 80 |
| 66 // static | 81 // static |
| 67 UpstartClient* UpstartClient::Create() { | 82 UpstartClient* UpstartClient::Create() { |
| 68 return new UpstartClientImpl(); | 83 return new UpstartClientImpl(); |
| 69 } | 84 } |
| 70 | 85 |
| 71 } // namespace chromeos | 86 } // namespace chromeos |
| OLD | NEW |