summaryrefslogtreecommitdiffstats
path: root/chromium/components/policy/core/common/mac_util.cc
blob: c94255aaa7d6af0fbe21467c63ad52aa3d133b69 (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/core/common/mac_util.h"

#include <memory>
#include <string>
#include <utility>

#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"

using base::mac::CFCast;

namespace policy {

namespace {

// Callback function for CFDictionaryApplyFunction. |key| and |value| are an
// entry of the CFDictionary that should be converted into an equivalent entry
// in the DictionaryValue in |context|.
void DictionaryEntryToValue(const void* key, const void* value, void* context) {
  if (CFStringRef cf_key = CFCast<CFStringRef>(key)) {
    std::unique_ptr<base::Value> converted =
        PropertyToValue(static_cast<CFPropertyListRef>(value));
    if (converted) {
      const std::string string = base::SysCFStringRefToUTF8(cf_key);
      static_cast<base::DictionaryValue*>(context)->Set(string,
                                                        std::move(converted));
    }
  }
}

// Callback function for CFArrayApplyFunction. |value| is an entry of the
// CFArray that should be converted into an equivalent entry in the ListValue
// in |context|.
void ArrayEntryToValue(const void* value, void* context) {
  std::unique_ptr<base::Value> converted =
      PropertyToValue(static_cast<CFPropertyListRef>(value));
  if (converted)
    static_cast<base::ListValue*>(context)->Append(std::move(converted));
}

}  // namespace

std::unique_ptr<base::Value> PropertyToValue(CFPropertyListRef property) {
  if (CFCast<CFNullRef>(property))
    return std::make_unique<base::Value>();

  if (CFBooleanRef boolean = CFCast<CFBooleanRef>(property)) {
    return std::make_unique<base::Value>(
        static_cast<bool>(CFBooleanGetValue(boolean)));
  }

  if (CFNumberRef number = CFCast<CFNumberRef>(property)) {
    // CFNumberGetValue() converts values implicitly when the conversion is not
    // lossy. Check the type before trying to convert.
    if (CFNumberIsFloatType(number)) {
      double double_value = 0.0;
      if (CFNumberGetValue(number, kCFNumberDoubleType, &double_value)) {
        return std::make_unique<base::Value>(double_value);
      }
    } else {
      int int_value = 0;
      if (CFNumberGetValue(number, kCFNumberIntType, &int_value)) {
        return std::make_unique<base::Value>(int_value);
      }
    }
  }

  if (CFStringRef string = CFCast<CFStringRef>(property)) {
    return std::make_unique<base::Value>(base::SysCFStringRefToUTF8(string));
  }

  if (CFDictionaryRef dict = CFCast<CFDictionaryRef>(property)) {
    std::unique_ptr<base::DictionaryValue> dict_value(
        new base::DictionaryValue());
    CFDictionaryApplyFunction(dict, DictionaryEntryToValue, dict_value.get());
    return std::move(dict_value);
  }

  if (CFArrayRef array = CFCast<CFArrayRef>(property)) {
    std::unique_ptr<base::ListValue> list_value(new base::ListValue());
    CFArrayApplyFunction(array,
                         CFRangeMake(0, CFArrayGetCount(array)),
                         ArrayEntryToValue,
                         list_value.get());
    return std::move(list_value);
  }

  return nullptr;
}

}  // namespace policy