xref: /aosp_15_r20/external/cronet/base/mac/login_util.cc (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 #include "base/mac/login_util.h"
6 
7 #include <dlfcn.h>
8 
9 #include <optional>
10 
11 // NB: Other possible useful functions for the future (if needed):
12 //
13 // OSStatus SACLockScreenImmediate()
14 // Boolean SACScreenSaverIsRunning()
15 // OSStatus SACScreenSaverStartNow()
16 // OSStatus SACScreenSaverStopNow()
17 
18 namespace base::mac {
19 
20 namespace {
21 
GetLoginFramework()22 void* GetLoginFramework() {
23   static void* login_framework = dlopen(
24       "/System/Library/PrivateFrameworks/login.framework/Versions/A/login",
25       RTLD_LAZY | RTLD_LOCAL);
26   return login_framework;
27 }
28 
29 }  // namespace
30 
IsScreenLockEnabled()31 std::optional<bool> IsScreenLockEnabled() {
32   if (!GetLoginFramework()) {
33     return std::nullopt;
34   }
35   using SACScreenLockEnabledType = Boolean (*)();
36   static auto func = reinterpret_cast<SACScreenLockEnabledType>(
37       dlsym(GetLoginFramework(), "SACScreenLockEnabled"));
38   if (!func) {
39     return std::nullopt;
40   }
41   return func();
42 }
43 
SwitchToLoginWindow()44 std::optional<OSStatus> SwitchToLoginWindow() {
45   if (!GetLoginFramework()) {
46     return std::nullopt;
47   }
48   using SACSwitchToLoginWindowType = OSStatus (*)();
49   static auto func = reinterpret_cast<SACSwitchToLoginWindowType>(
50       dlsym(GetLoginFramework(), "SACSwitchToLoginWindow"));
51   if (!func) {
52     return std::nullopt;
53   }
54   return func();
55 }
56 
57 }  // namespace base::mac
58