1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157

base / mac / mac_util.h [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_MAC_MAC_UTIL_H_
#define BASE_MAC_MAC_UTIL_H_

#include <AvailabilityMacros.h>
#import <CoreGraphics/CoreGraphics.h>
#include <stdint.h>

#include <string>
#include <string_view>
#include <vector>

#include "base/base_export.h"

namespace base {
class FilePath;
}

namespace base::mac {

// Returns an sRGB color space.  The return value is a static value; do not
// release it!
BASE_EXPORT CGColorSpaceRef GetSRGBColorSpace();

// Adds the specified application to the set of Login Items with specified
// "hide" flag. This has the same effect as adding/removing the application in
// SystemPreferences->Accounts->LoginItems or marking Application in the Dock
// as "Options->Open on Login".
// Does nothing if the application is already set up as Login Item with
// specified hide flag.
BASE_EXPORT void AddToLoginItems(const FilePath& app_bundle_file_path,
                                 bool hide_on_startup);

// Removes the specified application from the list of Login Items.
BASE_EXPORT void RemoveFromLoginItems(const FilePath& app_bundle_file_path);

// Returns true if the current process was automatically launched as a
// 'Login Item' or via Lion's Resume. Used to suppress opening windows.
BASE_EXPORT bool WasLaunchedAsLoginOrResumeItem();

// Returns true if the current process was automatically launched as a
// 'Login Item' or via Resume, and the 'Reopen windows when logging back in'
// checkbox was selected by the user.  This indicates that the previous
// session should be restored.
BASE_EXPORT bool WasLaunchedAsLoginItemRestoreState();

// Returns true if the current process was automatically launched as a
// 'Login Item' with 'hide on startup' flag. Used to suppress opening windows.
BASE_EXPORT bool WasLaunchedAsHiddenLoginItem();

// Remove the quarantine xattr from the given file. Returns false if there was
// an error, or true otherwise.
BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);

// Sets the tags on a given file or folder.
BASE_EXPORT void SetFileTags(const FilePath& file_path,
                             const std::vector<std::string>& file_tags);

// The following two functions return the version of the macOS currently
// running. MacOSVersion() returns the full trio of version numbers, packed into
// one int (e.g. macOS 12.6.5 returns 12'06'05), and MacOSMajorVersion() returns
// only the major version number (e.g. macOS 12.6.5 returns 12). Use for runtime
// OS version checking. Prefer to use @available in Objective-C files. Note that
// this does not include any Rapid Security Response (RSR) suffixes (the "(a)"
// at the end of version numbers.)
BASE_EXPORT __attribute__((const)) int MacOSVersion();
inline __attribute__((const)) int MacOSMajorVersion() {
  return MacOSVersion() / 1'00'00;
}

enum class CPUType {
  kIntel,
  kTranslatedIntel,  // Rosetta
  kArm,
};

// Returns the type of CPU this is being executed on.
BASE_EXPORT CPUType GetCPUType();

// Returns an OS name + version string. e.g.:
//
//   "macOS Version 10.14.3 (Build 18D109)"
//
// Parts of this string change based on OS locale, so it's only useful for
// displaying to the user.
BASE_EXPORT std::string GetOSDisplayName();

// Returns the serial number of the macOS device.
BASE_EXPORT std::string GetPlatformSerialNumber();

// System Settings (née System Preferences) pane or subpanes to open via
// `OpenSystemSettingsPane()`, below. The naming is based on the naming in the
// System Settings app in the latest macOS release, macOS 13 Ventura.
enum class SystemSettingsPane {
  // Accessibility > Captions
  kAccessibility_Captions,

  // Date & Time
  kDateTime,

  // Network > Proxies
  kNetwork_Proxies,

  // Notifications; optionally pass a bundle identifier as `id_param` to
  // directly open the notification settings page for the given app.
  kNotifications,

  // Printers & Scanners
  kPrintersScanners,

  // Privacy & Security
  kPrivacySecurity,

  // Privacy & Security > Accessibility
  kPrivacySecurity_Accessibility,

  // Privacy & Security > Bluetooth
  kPrivacySecurity_Bluetooth,

  // Privacy & Security > Camera
  kPrivacySecurity_Camera,

  // Privacy & Security > Extensions > Sharing
  kPrivacySecurity_Extensions_Sharing,

  // Privacy & Security > Location Services
  kPrivacySecurity_LocationServices,

  // Privacy & Security > Microphone
  kPrivacySecurity_Microphone,

  // Privacy & Security > Screen Recording
  kPrivacySecurity_ScreenRecording,

  // Trackpad
  kTrackpad,
};

// Opens the specified System Settings pane. If the specified subpane does not
// exist on the release of macOS that is running, the parent pane will open
// instead. For some panes, `id_param` can be used to specify a subpane. See the
// various SystemSettingsPane values for details.
BASE_EXPORT void OpenSystemSettingsPane(SystemSettingsPane pane,
                                        const std::string& id_param = "");

// ------- For testing --------

// An implementation detail of `MacOSVersion()` above, exposed for testing.
BASE_EXPORT int ParseOSProductVersionForTesting(
    const std::string_view& version);

}  // namespace base::mac

#endif  // BASE_MAC_MAC_UTIL_H_