| 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 #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/bind_helpers.h" |
| 7 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 8 #include "components/signin/core/account_id/account_id.h" | 9 #include "components/signin/core/account_id/account_id.h" |
| 9 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
| 10 #include "dbus/message.h" | 11 #include "dbus/message.h" |
| 11 #include "dbus/object_proxy.h" | 12 #include "dbus/object_proxy.h" |
| 12 | 13 |
| 13 namespace chromeos { | 14 namespace chromeos { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 35 class AuthPolicyClientImpl : public AuthPolicyClient { | 36 class AuthPolicyClientImpl : public AuthPolicyClient { |
| 36 public: | 37 public: |
| 37 AuthPolicyClientImpl() : weak_ptr_factory_(this) {} | 38 AuthPolicyClientImpl() : weak_ptr_factory_(this) {} |
| 38 | 39 |
| 39 ~AuthPolicyClientImpl() override {} | 40 ~AuthPolicyClientImpl() override {} |
| 40 | 41 |
| 41 // AuthPolicyClient override. | 42 // AuthPolicyClient override. |
| 42 void JoinAdDomain(const std::string& machine_name, | 43 void JoinAdDomain(const std::string& machine_name, |
| 43 const std::string& user_principal_name, | 44 const std::string& user_principal_name, |
| 44 int password_fd, | 45 int password_fd, |
| 45 const JoinCallback& callback) override { | 46 JoinCallback callback) override { |
| 46 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, | 47 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, |
| 47 authpolicy::kAuthPolicyJoinADDomain); | 48 authpolicy::kAuthPolicyJoinADDomain); |
| 48 dbus::MessageWriter writer(&method_call); | 49 dbus::MessageWriter writer(&method_call); |
| 49 writer.AppendString(machine_name); | 50 writer.AppendString(machine_name); |
| 50 writer.AppendString(user_principal_name); | 51 writer.AppendString(user_principal_name); |
| 51 writer.AppendFileDescriptor(password_fd); | 52 writer.AppendFileDescriptor(password_fd); |
| 52 proxy_->CallMethod(&method_call, kSlowDbusTimeoutMilliseconds, | 53 proxy_->CallMethod( |
| 53 base::Bind(&AuthPolicyClientImpl::HandleJoinCallback, | 54 &method_call, kSlowDbusTimeoutMilliseconds, |
| 54 weak_ptr_factory_.GetWeakPtr(), callback)); | 55 base::Bind(&AuthPolicyClientImpl::HandleJoinCallback, |
| 56 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); |
| 55 } | 57 } |
| 56 | 58 |
| 57 void AuthenticateUser(const std::string& user_principal_name, | 59 void AuthenticateUser(const std::string& user_principal_name, |
| 58 int password_fd, | 60 int password_fd, |
| 59 const AuthCallback& callback) override { | 61 AuthCallback callback) override { |
| 60 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, | 62 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, |
| 61 authpolicy::kAuthPolicyAuthenticateUser); | 63 authpolicy::kAuthPolicyAuthenticateUser); |
| 62 dbus::MessageWriter writer(&method_call); | 64 dbus::MessageWriter writer(&method_call); |
| 63 writer.AppendString(user_principal_name); | 65 writer.AppendString(user_principal_name); |
| 64 writer.AppendFileDescriptor(password_fd); | 66 writer.AppendFileDescriptor(password_fd); |
| 65 proxy_->CallMethod(&method_call, kSlowDbusTimeoutMilliseconds, | 67 proxy_->CallMethod( |
| 66 base::Bind(&AuthPolicyClientImpl::HandleAuthCallback, | 68 &method_call, kSlowDbusTimeoutMilliseconds, |
| 67 weak_ptr_factory_.GetWeakPtr(), callback)); | 69 base::Bind(&AuthPolicyClientImpl::HandleAuthCallback, |
| 70 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); |
| 68 } | 71 } |
| 69 | 72 |
| 70 void RefreshDevicePolicy(const RefreshPolicyCallback& callback) override { | 73 void RefreshDevicePolicy(RefreshPolicyCallback callback) override { |
| 71 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, | 74 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, |
| 72 authpolicy::kAuthPolicyRefreshDevicePolicy); | 75 authpolicy::kAuthPolicyRefreshDevicePolicy); |
| 73 proxy_->CallMethod( | 76 proxy_->CallMethod( |
| 74 &method_call, kSlowDbusTimeoutMilliseconds, | 77 &method_call, kSlowDbusTimeoutMilliseconds, |
| 75 base::Bind(&AuthPolicyClientImpl::HandleRefreshPolicyCallback, | 78 base::Bind(&AuthPolicyClientImpl::HandleRefreshPolicyCallback, |
| 76 weak_ptr_factory_.GetWeakPtr(), callback)); | 79 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); |
| 77 } | 80 } |
| 78 | 81 |
| 79 void RefreshUserPolicy(const AccountId& account_id, | 82 void RefreshUserPolicy(const AccountId& account_id, |
| 80 const RefreshPolicyCallback& callback) override { | 83 RefreshPolicyCallback callback) override { |
| 81 DCHECK(account_id.GetAccountType() == AccountType::ACTIVE_DIRECTORY); | 84 DCHECK(account_id.GetAccountType() == AccountType::ACTIVE_DIRECTORY); |
| 82 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, | 85 dbus::MethodCall method_call(authpolicy::kAuthPolicyInterface, |
| 83 authpolicy::kAuthPolicyRefreshUserPolicy); | 86 authpolicy::kAuthPolicyRefreshUserPolicy); |
| 84 dbus::MessageWriter writer(&method_call); | 87 dbus::MessageWriter writer(&method_call); |
| 85 writer.AppendString(account_id.GetAccountIdKey()); | 88 writer.AppendString(account_id.GetAccountIdKey()); |
| 86 proxy_->CallMethod( | 89 proxy_->CallMethod( |
| 87 &method_call, kSlowDbusTimeoutMilliseconds, | 90 &method_call, kSlowDbusTimeoutMilliseconds, |
| 88 base::Bind(&AuthPolicyClientImpl::HandleRefreshPolicyCallback, | 91 base::Bind(&AuthPolicyClientImpl::HandleRefreshPolicyCallback, |
| 89 weak_ptr_factory_.GetWeakPtr(), callback)); | 92 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); |
| 90 } | 93 } |
| 91 | 94 |
| 92 protected: | 95 protected: |
| 93 void Init(dbus::Bus* bus) override { | 96 void Init(dbus::Bus* bus) override { |
| 94 bus_ = bus; | 97 bus_ = bus; |
| 95 proxy_ = bus_->GetObjectProxy( | 98 proxy_ = bus_->GetObjectProxy( |
| 96 authpolicy::kAuthPolicyServiceName, | 99 authpolicy::kAuthPolicyServiceName, |
| 97 dbus::ObjectPath(authpolicy::kAuthPolicyServicePath)); | 100 dbus::ObjectPath(authpolicy::kAuthPolicyServicePath)); |
| 98 } | 101 } |
| 99 | 102 |
| 100 private: | 103 private: |
| 101 void HandleRefreshPolicyCallback(const RefreshPolicyCallback& callback, | 104 void HandleRefreshPolicyCallback(RefreshPolicyCallback callback, |
| 102 dbus::Response* response) { | 105 dbus::Response* response) { |
| 103 if (!response) { | 106 if (!response) { |
| 104 DLOG(ERROR) << "RefreshDevicePolicy: failed to call to authpolicy"; | 107 DLOG(ERROR) << "RefreshDevicePolicy: failed to call to authpolicy"; |
| 105 callback.Run(false); | 108 std::move(callback).Run(false); |
| 106 return; | 109 return; |
| 107 } | 110 } |
| 108 dbus::MessageReader reader(response); | 111 dbus::MessageReader reader(response); |
| 109 callback.Run(GetErrorFromReader(&reader) == authpolicy::ERROR_NONE); | 112 std::move(callback).Run(GetErrorFromReader(&reader) == |
| 113 authpolicy::ERROR_NONE); |
| 110 } | 114 } |
| 111 | 115 |
| 112 void HandleJoinCallback(const JoinCallback& callback, | 116 void HandleJoinCallback(JoinCallback callback, dbus::Response* response) { |
| 113 dbus::Response* response) { | |
| 114 if (!response) { | 117 if (!response) { |
| 115 DLOG(ERROR) << "Join: Couldn't call to authpolicy"; | 118 DLOG(ERROR) << "Join: Couldn't call to authpolicy"; |
| 116 callback.Run(authpolicy::ERROR_DBUS_FAILURE); | 119 std::move(callback).Run(authpolicy::ERROR_DBUS_FAILURE); |
| 117 return; | 120 return; |
| 118 } | 121 } |
| 119 | 122 |
| 120 dbus::MessageReader reader(response); | 123 dbus::MessageReader reader(response); |
| 121 callback.Run(GetErrorFromReader(&reader)); | 124 std::move(callback).Run(GetErrorFromReader(&reader)); |
| 122 } | 125 } |
| 123 | 126 |
| 124 void HandleAuthCallback(const AuthCallback& callback, | 127 void HandleAuthCallback(AuthCallback callback, dbus::Response* response) { |
| 125 dbus::Response* response) { | |
| 126 authpolicy::ActiveDirectoryAccountData account_data; | 128 authpolicy::ActiveDirectoryAccountData account_data; |
| 127 if (!response) { | 129 if (!response) { |
| 128 DLOG(ERROR) << "Auth: Failed to call to authpolicy"; | 130 DLOG(ERROR) << "Auth: Failed to call to authpolicy"; |
| 129 callback.Run(authpolicy::ERROR_DBUS_FAILURE, account_data); | 131 std::move(callback).Run(authpolicy::ERROR_DBUS_FAILURE, account_data); |
| 130 return; | 132 return; |
| 131 } | 133 } |
| 132 dbus::MessageReader reader(response); | 134 dbus::MessageReader reader(response); |
| 133 const authpolicy::ErrorType error(GetErrorFromReader(&reader)); | 135 const authpolicy::ErrorType error(GetErrorFromReader(&reader)); |
| 134 if (!reader.PopArrayOfBytesAsProto(&account_data)) { | 136 if (!reader.PopArrayOfBytesAsProto(&account_data)) { |
| 135 DLOG(ERROR) << "Failed to parse protobuf."; | 137 DLOG(ERROR) << "Failed to parse protobuf."; |
| 136 callback.Run(authpolicy::ErrorType::ERROR_DBUS_FAILURE, account_data); | 138 std::move(callback).Run(authpolicy::ErrorType::ERROR_DBUS_FAILURE, |
| 139 account_data); |
| 137 return; | 140 return; |
| 138 } | 141 } |
| 139 callback.Run(error, account_data); | 142 std::move(callback).Run(error, account_data); |
| 140 } | 143 } |
| 141 | 144 |
| 142 dbus::Bus* bus_ = nullptr; | 145 dbus::Bus* bus_ = nullptr; |
| 143 dbus::ObjectProxy* proxy_ = nullptr; | 146 dbus::ObjectProxy* proxy_ = nullptr; |
| 144 | 147 |
| 145 // Note: This should remain the last member so it'll be destroyed and | 148 // Note: This should remain the last member so it'll be destroyed and |
| 146 // invalidate its weak pointers before any other members are destroyed. | 149 // invalidate its weak pointers before any other members are destroyed. |
| 147 base::WeakPtrFactory<AuthPolicyClientImpl> weak_ptr_factory_; | 150 base::WeakPtrFactory<AuthPolicyClientImpl> weak_ptr_factory_; |
| 148 | 151 |
| 149 DISALLOW_COPY_AND_ASSIGN(AuthPolicyClientImpl); | 152 DISALLOW_COPY_AND_ASSIGN(AuthPolicyClientImpl); |
| 150 }; | 153 }; |
| 151 | 154 |
| 152 } // namespace | 155 } // namespace |
| 153 | 156 |
| 154 AuthPolicyClient::AuthPolicyClient() {} | 157 AuthPolicyClient::AuthPolicyClient() {} |
| 155 | 158 |
| 156 AuthPolicyClient::~AuthPolicyClient() {} | 159 AuthPolicyClient::~AuthPolicyClient() {} |
| 157 | 160 |
| 158 // static | 161 // static |
| 159 AuthPolicyClient* AuthPolicyClient::Create() { | 162 AuthPolicyClient* AuthPolicyClient::Create() { |
| 160 return new AuthPolicyClientImpl(); | 163 return new AuthPolicyClientImpl(); |
| 161 } | 164 } |
| 162 | 165 |
| 163 } // namespace chromeos | 166 } // namespace chromeos |
| OLD | NEW |