#include "crypto/apple/scoped_keychain_user_interaction_allowed.h"
#include <Security/Security.h>
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#if BUILDFLAG(IS_MAC)
namespace crypto::apple {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
ScopedKeychainUserInteractionAllowed::ScopedKeychainUserInteractionAllowed(
Boolean allowed,
OSStatus* status) {
Boolean was_allowed;
OSStatus local_status = noErr;
absl::Cleanup cleanup = [&status, &local_status] {
if (status) {
*status = local_status;
}
};
local_status = SecKeychainGetUserInteractionAllowed(&was_allowed);
if (local_status != noErr) {
return;
}
local_status = SecKeychainSetUserInteractionAllowed(allowed);
if (local_status != noErr) {
return;
}
was_allowed_ = was_allowed;
}
ScopedKeychainUserInteractionAllowed::~ScopedKeychainUserInteractionAllowed() {
if (was_allowed_.has_value()) {
SecKeychainSetUserInteractionAllowed(was_allowed_.value());
}
}
#pragma clang diagnostic pop
}
#endif