xref: /aosp_15_r20/external/cronet/crypto/scoped_lacontext.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CRYPTO_SCOPED_LACONTEXT_H_
6 #define CRYPTO_SCOPED_LACONTEXT_H_
7 
8 #if defined(__OBJC__)
9 #import <LocalAuthentication/LocalAuthentication.h>
10 #endif  // defined(__OBJC__)
11 
12 #include <memory>
13 
14 #include "crypto/crypto_export.h"
15 
16 namespace crypto {
17 
18 // ScopedLAContext can hold an `LAContext` and is safe to pass around from C++.
19 // ScopedLAContext functions as a unique pointer. The UI can create one with an
20 // authenticated LAContext, then pass it down to the platform.
21 class CRYPTO_EXPORT ScopedLAContext {
22  public:
23 #if defined(__OBJC__)
24   // Takes ownership of |lacontext|.
25   explicit ScopedLAContext(LAContext* lacontext);
26 #endif  // defined(__OBJC__)
27   ~ScopedLAContext();
28   ScopedLAContext(ScopedLAContext&) = delete;
29   ScopedLAContext(ScopedLAContext&&);
30   ScopedLAContext& operator=(const ScopedLAContext&) = delete;
31   ScopedLAContext& operator=(ScopedLAContext&&);
32 
33 #if defined(__OBJC__)
34   // release returns the last `LAContext` passed on construction and drops its
35   // reference to it. It is invalid to to call release more than once.
36   LAContext* release();
37 #endif  // defined(__OBJC__)
38 
39  private:
40   struct ObjCStorage;
41   std::unique_ptr<ObjCStorage> storage_;
42 };
43 
44 }  // namespace crypto
45 
46 #endif  // CRYPTO_SCOPED_LACONTEXT_H_
47