| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "chromeos/dbus/auth_policy_client.h" | 4 #include "chromeos/dbus/auth_policy_client.h" |
| 5 | 5 |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 8 #include "dbus/bus.h" | 8 #include "dbus/bus.h" |
| 9 #include "dbus/message.h" | 9 #include "dbus/message.h" |
| 10 #include "dbus/object_proxy.h" | 10 #include "dbus/object_proxy.h" |
| 11 #include "third_party/cros_system_api/dbus/service_constants.h" | 11 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 12 | 12 |
| 13 namespace chromeos { | 13 namespace chromeos { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 class AuthPolicyClientImpl : public AuthPolicyClient { | 17 class AuthPolicyClientImpl : public AuthPolicyClient { |
| 18 public: | 18 public: |
| 19 AuthPolicyClientImpl() : weak_ptr_factory_(this) {} | 19 AuthPolicyClientImpl() : weak_ptr_factory_(this) {} |
| 20 | 20 |
| 21 ~AuthPolicyClientImpl() override {} | 21 ~AuthPolicyClientImpl() override {} |
| 22 | 22 |
| 23 // AuthPolicyClient override. | 23 // AuthPolicyClient override. |
| 24 void JoinAdDomain(const std::string& machine_name, | 24 void JoinAdDomain(const std::string& machine_name, |
| 25 const std::string& user, | 25 const std::string& user, |
| 26 int password_fd, | 26 int password_fd, |
| 27 const JoinCallback& callback) override { | 27 const JoinCallback& callback) override { |
| 28 dbus::ObjectPath objectPath(authpolicy::kAuthPolicyServicePath); | |
| 29 dbus::ObjectProxy* proxy = | |
| 30 bus_->GetObjectProxy(authpolicy::kAuthPolicyServiceName, objectPath); | |
| 31 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, | 28 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, |
| 32 authpolicy::kAuthPolicyJoinADDomain); | 29 authpolicy::kAuthPolicyJoinADDomain); |
| 33 dbus::MessageWriter writer(&method_call); | 30 dbus::MessageWriter writer(&method_call); |
| 34 writer.AppendString(machine_name); | 31 writer.AppendString(machine_name); |
| 35 writer.AppendString(user); | 32 writer.AppendString(user); |
| 36 writer.AppendFileDescriptor(password_fd); | 33 writer.AppendFileDescriptor(password_fd); |
| 37 proxy->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 34 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 38 base::Bind(&AuthPolicyClientImpl::HandleJoinCallback, | 35 base::Bind(&AuthPolicyClientImpl::HandleJoinCallback, |
| 39 weak_ptr_factory_.GetWeakPtr(), callback)); | 36 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 37 } |
| 38 |
| 39 void RefreshDevicePolicy(const RefreshPolicyCallback& callback) override { |
| 40 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, |
| 41 authpolicy::kAuthPolicyRefreshDevicePolicy); |
| 42 proxy_->CallMethod( |
| 43 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 44 base::Bind(&AuthPolicyClientImpl::HandleRefreshPolicyCallback, |
| 45 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 46 } |
| 47 |
| 48 void RefreshUserPolicy(const std::string& account_id, |
| 49 const RefreshPolicyCallback& callback) override { |
| 50 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, |
| 51 authpolicy::kAuthPolicyRefreshUserPolicy); |
| 52 dbus::MessageWriter writer(&method_call); |
| 53 writer.AppendString(account_id); |
| 54 proxy_->CallMethod( |
| 55 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 56 base::Bind(&AuthPolicyClientImpl::HandleRefreshPolicyCallback, |
| 57 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 40 } | 58 } |
| 41 | 59 |
| 42 protected: | 60 protected: |
| 43 void Init(dbus::Bus* bus) override { bus_ = bus; } | 61 void Init(dbus::Bus* bus) override { |
| 62 bus_ = bus; |
| 63 proxy_ = bus_->GetObjectProxy( |
| 64 authpolicy::kAuthPolicyServiceName, |
| 65 dbus::ObjectPath(authpolicy::kAuthPolicyServicePath)); |
| 66 } |
| 44 | 67 |
| 45 private: | 68 private: |
| 69 void HandleRefreshPolicyCallback(const RefreshPolicyCallback& callback, |
| 70 dbus::Response* response) { |
| 71 if (!response) { |
| 72 LOG(ERROR) << "RefreshDevicePolicy: failed to call to authpolicy"; |
| 73 callback.Run(false); |
| 74 return; |
| 75 } |
| 76 callback.Run(true); |
| 77 } |
| 78 |
| 46 void HandleJoinCallback(const JoinCallback& callback, | 79 void HandleJoinCallback(const JoinCallback& callback, |
| 47 dbus::Response* response) { | 80 dbus::Response* response) { |
| 48 if (!response) { | 81 if (!response) { |
| 49 LOG(ERROR) << "Join: Couldn't call to authpolicy"; | 82 LOG(ERROR) << "Join: Couldn't call to authpolicy"; |
| 50 // TODO(rsorokin): make proper call, after defining possible errors codes. | 83 // TODO(rsorokin): make proper call, after defining possible errors codes. |
| 51 callback.Run(authpolicy::AD_JOIN_ERROR_UNKNOWN); | 84 callback.Run(authpolicy::AD_JOIN_ERROR_UNKNOWN); |
| 52 return; | 85 return; |
| 53 } | 86 } |
| 54 | 87 |
| 55 dbus::MessageReader reader(response); | 88 dbus::MessageReader reader(response); |
| 56 int res = authpolicy::AD_JOIN_ERROR_UNKNOWN; | 89 int res = authpolicy::AD_JOIN_ERROR_UNKNOWN; |
| 57 if (!reader.PopInt32(&res)) { | 90 if (!reader.PopInt32(&res)) { |
| 58 LOG(ERROR) << "Join: Couldn't get an error from the response"; | 91 LOG(ERROR) << "Join: Couldn't get an error from the response"; |
| 59 // TODO(rsorokin): make proper call, after defining possible errors codes. | 92 // TODO(rsorokin): make proper call, after defining possible errors codes. |
| 60 callback.Run(authpolicy::AD_JOIN_ERROR_DBUS_FAIL); | 93 callback.Run(authpolicy::AD_JOIN_ERROR_DBUS_FAIL); |
| 61 return; | 94 return; |
| 62 } | 95 } |
| 63 | 96 |
| 64 callback.Run(res); | 97 callback.Run(res); |
| 65 } | 98 } |
| 66 | 99 |
| 67 dbus::Bus* bus_ = nullptr; | 100 dbus::Bus* bus_ = nullptr; |
| 101 dbus::ObjectProxy* proxy_ = nullptr; |
| 68 | 102 |
| 69 // Note: This should remain the last member so it'll be destroyed and | 103 // Note: This should remain the last member so it'll be destroyed and |
| 70 // invalidate its weak pointers before any other members are destroyed. | 104 // invalidate its weak pointers before any other members are destroyed. |
| 71 base::WeakPtrFactory<AuthPolicyClientImpl> weak_ptr_factory_; | 105 base::WeakPtrFactory<AuthPolicyClientImpl> weak_ptr_factory_; |
| 72 | 106 |
| 73 DISALLOW_COPY_AND_ASSIGN(AuthPolicyClientImpl); | 107 DISALLOW_COPY_AND_ASSIGN(AuthPolicyClientImpl); |
| 74 }; | 108 }; |
| 75 | 109 |
| 76 } // namespace | 110 } // namespace |
| 77 | 111 |
| 78 AuthPolicyClient::AuthPolicyClient() {} | 112 AuthPolicyClient::AuthPolicyClient() {} |
| 79 | 113 |
| 80 AuthPolicyClient::~AuthPolicyClient() {} | 114 AuthPolicyClient::~AuthPolicyClient() {} |
| 81 | 115 |
| 82 // static | 116 // static |
| 83 AuthPolicyClient* AuthPolicyClient::Create() { | 117 AuthPolicyClient* AuthPolicyClient::Create() { |
| 84 return new AuthPolicyClientImpl(); | 118 return new AuthPolicyClientImpl(); |
| 85 } | 119 } |
| 86 | 120 |
| 87 } // namespace chromeos | 121 } // namespace chromeos |
| OLD | NEW |