Chromium Code Reviews| 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" | |
| 12 | 11 |
| 13 namespace chromeos { | 12 namespace chromeos { |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 | 15 |
| 17 class AuthPolicyClientImpl : public AuthPolicyClient { | 16 class AuthPolicyClientImpl : public AuthPolicyClient { |
| 18 public: | 17 public: |
| 19 AuthPolicyClientImpl() : weak_ptr_factory_(this) {} | 18 AuthPolicyClientImpl() : weak_ptr_factory_(this) {} |
| 20 | 19 |
| 21 ~AuthPolicyClientImpl() override {} | 20 ~AuthPolicyClientImpl() override {} |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 callback.Run(false); | 85 callback.Run(false); |
| 87 return; | 86 return; |
| 88 } | 87 } |
| 89 callback.Run(true); | 88 callback.Run(true); |
| 90 } | 89 } |
| 91 | 90 |
| 92 void HandleJoinCallback(const JoinCallback& callback, | 91 void HandleJoinCallback(const JoinCallback& callback, |
| 93 dbus::Response* response) { | 92 dbus::Response* response) { |
| 94 if (!response) { | 93 if (!response) { |
| 95 DLOG(ERROR) << "Join: Couldn't call to authpolicy"; | 94 DLOG(ERROR) << "Join: Couldn't call to authpolicy"; |
| 96 // TODO(rsorokin): make proper call, after defining possible errors codes. | 95 callback.Run(authpolicy::ERROR_DBUS_FAILURE); |
| 97 callback.Run(authpolicy::types::AD_JOIN_ERROR_UNKNOWN); | |
| 98 return; | 96 return; |
| 99 } | 97 } |
| 100 | 98 |
| 101 dbus::MessageReader reader(response); | 99 dbus::MessageReader reader(response); |
| 102 int res = authpolicy::types::AD_JOIN_ERROR_UNKNOWN; | 100 int32_t res = static_cast<int32_t>(authpolicy::ERROR_UNKNOWN); |
| 103 if (!reader.PopInt32(&res)) { | 101 if (!reader.PopInt32(&res)) { |
| 104 DLOG(ERROR) << "Join: Couldn't get an error from the response"; | 102 DLOG(ERROR) << "Join: Couldn't get an error from the response"; |
| 105 // TODO(rsorokin): make proper call, after defining possible errors codes. | 103 callback.Run(authpolicy::ERROR_DBUS_FAILURE); |
| 106 callback.Run(authpolicy::types::AD_JOIN_ERROR_DBUS_FAIL); | |
| 107 return; | 104 return; |
| 108 } | 105 } |
| 106 authpolicy::ErrorType error(authpolicy::ERROR_UNKNOWN); | |
| 107 // Check if result code is out of the enum range. | |
| 108 if (res >= 0 && res < authpolicy::ERROR_COUNT) | |
| 109 error = static_cast<authpolicy::ErrorType>(res); | |
| 109 | 110 |
| 110 callback.Run(res); | 111 callback.Run(error); |
| 111 } | 112 } |
| 112 | 113 |
| 113 void HandleAuthCallback(const AuthCallback& callback, | 114 void HandleAuthCallback(const AuthCallback& callback, |
| 114 dbus::Response* response) { | 115 dbus::Response* response) { |
| 115 std::string user_id; | |
| 116 int32_t res = static_cast<int32_t>(authpolicy::AUTH_USER_ERROR_UNKNOWN); | |
| 117 if (!response) { | 116 if (!response) { |
| 118 DLOG(ERROR) << "Auth: Failed to call to authpolicy"; | 117 DLOG(ERROR) << "Auth: Failed to call to authpolicy"; |
| 119 // TODO(rsorokin): make proper call, after defining possible errors codes. | 118 callback.Run(authpolicy::ERROR_DBUS_FAILURE, std::string()); |
| 120 callback.Run(authpolicy::AUTH_USER_ERROR_DBUS_FAILURE, user_id); | |
| 121 return; | 119 return; |
| 122 } | 120 } |
| 123 dbus::MessageReader reader(response); | 121 dbus::MessageReader reader(response); |
| 122 int32_t res = static_cast<int32_t>(authpolicy::ERROR_UNKNOWN); | |
| 124 if (!reader.PopInt32(&res)) { | 123 if (!reader.PopInt32(&res)) { |
| 125 DLOG(ERROR) << "Auth: Failed to get an error from the response"; | 124 DLOG(ERROR) << "Auth: Failed to get an error from the response"; |
| 126 // TODO(rsorokin): make proper call, after defining possible errors codes. | 125 callback.Run(authpolicy::ERROR_DBUS_FAILURE, std::string()); |
| 127 callback.Run(static_cast<authpolicy::AuthUserErrorType>(res), user_id); | |
| 128 return; | 126 return; |
| 129 } | 127 } |
| 130 if (res < 0 || res >= authpolicy::AUTH_USER_ERROR_COUNT) | 128 authpolicy::ErrorType error(authpolicy::ERROR_UNKNOWN); |
| 131 res = static_cast<int32_t>(authpolicy::AUTH_USER_ERROR_UNKNOWN); | 129 // Check if result code is out of the enum range. |
| 130 if (res >= 0 && res < authpolicy::ERROR_COUNT) | |
| 131 error = static_cast<authpolicy::ErrorType>(res); | |
|
stevenjb
2016/12/27 17:52:26
Rather than duplicate this logic, add a helper fun
Roman Sorokin (ftl)
2016/12/28 11:04:09
Done.
| |
| 132 std::string user_id; | |
| 132 if (!reader.PopString(&user_id)) | 133 if (!reader.PopString(&user_id)) |
| 133 DLOG(ERROR) << "Auth: Failed to get user_id from the response"; | 134 DLOG(ERROR) << "Auth: Failed to get user_id from the response"; |
| 134 callback.Run(static_cast<authpolicy::AuthUserErrorType>(res), user_id); | 135 callback.Run(error, user_id); |
| 135 } | 136 } |
| 136 | 137 |
| 137 dbus::Bus* bus_ = nullptr; | 138 dbus::Bus* bus_ = nullptr; |
| 138 dbus::ObjectProxy* proxy_ = nullptr; | 139 dbus::ObjectProxy* proxy_ = nullptr; |
| 139 | 140 |
| 140 // Note: This should remain the last member so it'll be destroyed and | 141 // Note: This should remain the last member so it'll be destroyed and |
| 141 // invalidate its weak pointers before any other members are destroyed. | 142 // invalidate its weak pointers before any other members are destroyed. |
| 142 base::WeakPtrFactory<AuthPolicyClientImpl> weak_ptr_factory_; | 143 base::WeakPtrFactory<AuthPolicyClientImpl> weak_ptr_factory_; |
| 143 | 144 |
| 144 DISALLOW_COPY_AND_ASSIGN(AuthPolicyClientImpl); | 145 DISALLOW_COPY_AND_ASSIGN(AuthPolicyClientImpl); |
| 145 }; | 146 }; |
| 146 | 147 |
| 147 } // namespace | 148 } // namespace |
| 148 | 149 |
| 149 AuthPolicyClient::AuthPolicyClient() {} | 150 AuthPolicyClient::AuthPolicyClient() {} |
| 150 | 151 |
| 151 AuthPolicyClient::~AuthPolicyClient() {} | 152 AuthPolicyClient::~AuthPolicyClient() {} |
| 152 | 153 |
| 153 // static | 154 // static |
| 154 AuthPolicyClient* AuthPolicyClient::Create() { | 155 AuthPolicyClient* AuthPolicyClient::Create() { |
| 155 return new AuthPolicyClientImpl(); | 156 return new AuthPolicyClientImpl(); |
| 156 } | 157 } |
| 157 | 158 |
| 158 } // namespace chromeos | 159 } // namespace chromeos |
| OLD | NEW |