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

base / power_monitor / battery_level_provider_mac.mm [blame]

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

#include "base/power_monitor/battery_level_provider.h"

#import <Foundation/Foundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/ps/IOPSKeys.h>

#include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/mac/scoped_ioobject.h"

namespace base {
namespace {

// Returns the value corresponding to |key| in the dictionary |description|.
// Returns |default_value| if the dictionary does not contain |key|, the
// corresponding value is nullptr or it could not be converted to SInt64.
std::optional<SInt64> GetValueAsSInt64(CFDictionaryRef description,
                                       CFStringRef key) {
  CFNumberRef number_ref =
      base::apple::GetValueFromDictionary<CFNumberRef>(description, key);

  SInt64 value;
  if (number_ref && CFNumberGetValue(number_ref, kCFNumberSInt64Type, &value))
    return value;

  return std::nullopt;
}

std::optional<bool> GetValueAsBoolean(CFDictionaryRef description,
                                      CFStringRef key) {
  CFBooleanRef boolean =
      base::apple::GetValueFromDictionary<CFBooleanRef>(description, key);
  if (!boolean)
    return std::nullopt;
  return CFBooleanGetValue(boolean);
}

}  // namespace

class BatteryLevelProviderMac : public BatteryLevelProvider {
 public:
  BatteryLevelProviderMac() = default;
  ~BatteryLevelProviderMac() override = default;

  void GetBatteryState(
      base::OnceCallback<void(const std::optional<BatteryState>&)> callback)
      override {
    std::move(callback).Run(GetBatteryStateImpl());
  }

 private:
  std::optional<BatteryState> GetBatteryStateImpl();
};

std::unique_ptr<BatteryLevelProvider> BatteryLevelProvider::Create() {
  return std::make_unique<BatteryLevelProviderMac>();
}

std::optional<BatteryLevelProviderMac::BatteryState>
BatteryLevelProviderMac::GetBatteryStateImpl() {
  const base::mac::ScopedIOObject<io_service_t> service(
      IOServiceGetMatchingService(kIOMasterPortDefault,
                                  IOServiceMatching("IOPMPowerSource")));
  if (!service) {
    // Macs without a battery don't necessarily provide the IOPMPowerSource
    // service (e.g. test bots). Don't report this as an error.
    return MakeBatteryState(/* battery_details=*/{});
  }

  apple::ScopedCFTypeRef<CFMutableDictionaryRef> dict;
  kern_return_t result =
      IORegistryEntryCreateCFProperties(service.get(), dict.InitializeInto(),
                                        /*allocator=*/nullptr, /*options=*/0);

  if (result != KERN_SUCCESS) {
    // Failing to retrieve the dictionary is unexpected.
    return std::nullopt;
  }

  std::optional<bool> battery_installed =
      GetValueAsBoolean(dict.get(), CFSTR("BatteryInstalled"));
  if (!battery_installed.has_value()) {
    // Failing to access the BatteryInstalled property is unexpected.
    return std::nullopt;
  }

  if (!battery_installed.value()) {
    // BatteryInstalled == false means that there is no battery.
    return MakeBatteryState(/* battery_details=*/{});
  }

  std::optional<bool> external_connected =
      GetValueAsBoolean(dict.get(), CFSTR("ExternalConnected"));
  if (!external_connected.has_value()) {
    // Failing to access the ExternalConnected property is unexpected.
    return std::nullopt;
  }

  std::optional<SInt64> current_capacity =
      GetValueAsSInt64(dict.get(), CFSTR("AppleRawCurrentCapacity"));
  if (!current_capacity.has_value()) {
    return std::nullopt;
  }

  std::optional<SInt64> max_capacity =
      GetValueAsSInt64(dict.get(), CFSTR("AppleRawMaxCapacity"));
  if (!max_capacity.has_value()) {
    return std::nullopt;
  }

  std::optional<SInt64> voltage_mv =
      GetValueAsSInt64(dict.get(), CFSTR(kIOPSVoltageKey));
  if (!voltage_mv.has_value()) {
    return std::nullopt;
  }

  DCHECK_GE(*current_capacity, 0);
  DCHECK_GE(*max_capacity, 0);
  DCHECK_GE(*voltage_mv, 0);

  return MakeBatteryState({BatteryDetails{
      .is_external_power_connected = external_connected.value(),
      .current_capacity = static_cast<uint64_t>(current_capacity.value()),
      .full_charged_capacity = static_cast<uint64_t>(max_capacity.value()),
      .voltage_mv = static_cast<uint64_t>(voltage_mv.value()),
      .charge_unit = BatteryLevelUnit::kMAh}});
}

}  // namespace base