summaryrefslogtreecommitdiffstats
path: root/chromium/components/policy/core/common/android/policy_converter.cc
blob: 42ed096dfe1f92d0a2ae33729003cf4a058a2453 (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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2015 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/android/policy_converter.h"

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

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/check_op.h"
#include "base/json/json_reader.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "components/policy/android/jni_headers/PolicyConverter_jni.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/core/common/schema.h"

using base::android::ConvertJavaStringToUTF8;
using base::android::JavaRef;

namespace policy {
namespace android {

PolicyConverter::PolicyConverter(const Schema* policy_schema)
    : policy_schema_(policy_schema), policy_bundle_(new PolicyBundle) {
  JNIEnv* env = base::android::AttachCurrentThread();
  java_obj_.Reset(
      env,
      Java_PolicyConverter_create(env, reinterpret_cast<intptr_t>(this)).obj());
  DCHECK(!java_obj_.is_null());
}

PolicyConverter::~PolicyConverter() {
  Java_PolicyConverter_onNativeDestroyed(base::android::AttachCurrentThread(),
                                         java_obj_);
}

std::unique_ptr<PolicyBundle> PolicyConverter::GetPolicyBundle() {
  std::unique_ptr<PolicyBundle> filled_bundle(std::move(policy_bundle_));
  policy_bundle_ = std::make_unique<PolicyBundle>();
  return filled_bundle;
}

base::android::ScopedJavaLocalRef<jobject> PolicyConverter::GetJavaObject() {
  return base::android::ScopedJavaLocalRef<jobject>(java_obj_);
}

void PolicyConverter::SetPolicyBoolean(JNIEnv* env,
                                       const JavaRef<jobject>& obj,
                                       const JavaRef<jstring>& policyKey,
                                       jboolean value) {
  SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey),
                 base::Value(static_cast<bool>(value)));
}

void PolicyConverter::SetPolicyInteger(JNIEnv* env,
                                       const JavaRef<jobject>& obj,
                                       const JavaRef<jstring>& policyKey,
                                       jint value) {
  SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey),
                 base::Value(static_cast<int>(value)));
}

void PolicyConverter::SetPolicyString(JNIEnv* env,
                                      const JavaRef<jobject>& obj,
                                      const JavaRef<jstring>& policyKey,
                                      const JavaRef<jstring>& value) {
  SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey),
                 base::Value(ConvertJavaStringToUTF8(env, value)));
}

void PolicyConverter::SetPolicyStringArray(JNIEnv* env,
                                           const JavaRef<jobject>& obj,
                                           const JavaRef<jstring>& policyKey,
                                           const JavaRef<jobjectArray>& array) {
  SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey),
                 ConvertJavaStringArrayToListValue(env, array));
}

// static
base::Value PolicyConverter::ConvertJavaStringArrayToListValue(
    JNIEnv* env,
    const JavaRef<jobjectArray>& array) {
  DCHECK(!array.is_null());
  base::android::JavaObjectArrayReader<jstring> array_reader(array);
  DCHECK_GE(array_reader.size(), 0)
      << "Invalid array length: " << array_reader.size();

  base::Value list_value(base::Value::Type::LIST);
  for (auto j_str : array_reader)
    list_value.Append(ConvertJavaStringToUTF8(env, j_str));

  return list_value;
}

// static
absl::optional<base::Value> PolicyConverter::ConvertValueToSchema(
    base::Value value,
    const Schema& schema) {
  if (!schema.valid())
    return value;

  switch (schema.type()) {
    case base::Value::Type::NONE:
      return base::Value();

    case base::Value::Type::BOOLEAN: {
      std::string string_value;
      if (value.is_string()) {
        const std::string& string_value = value.GetString();
        if (string_value.compare("true") == 0)
          return base::Value(true);

        if (string_value.compare("false") == 0)
          return base::Value(false);

        return value;
      }
      if (value.is_int())
        return base::Value(value.GetInt() != 0);

      return value;
    }

    case base::Value::Type::INTEGER: {
      if (value.is_string()) {
        const std::string& string_value = value.GetString();
        int int_value = 0;
        if (base::StringToInt(string_value, &int_value))
          return base::Value(int_value);
      }
      return value;
    }

    case base::Value::Type::DOUBLE: {
      if (value.is_string()) {
        const std::string& string_value = value.GetString();
        double double_value = 0;
        if (base::StringToDouble(string_value, &double_value))
          return base::Value(double_value);
      }
      return value;
    }

    // String can't be converted from other types.
    case base::Value::Type::STRING: {
      return value;
    }

    // Binary is not a valid schema type.
    case base::Value::Type::BINARY: {
      NOTREACHED();
      return base::Value();
    }

    // Complex types have to be deserialized from JSON.
    case base::Value::Type::DICTIONARY:
    case base::Value::Type::LIST: {
      if (value.is_string()) {
        const std::string str_value = value.GetString();
        // Do not try to convert empty string to list/dictionaries, since most
        // likely the value was not simply not set by the UEM.
        if (str_value.empty())
          return absl::nullopt;
        absl::optional<base::Value> decoded_value = base::JSONReader::Read(
            str_value, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
        if (decoded_value.has_value())
          return decoded_value;
      }
      return value;
    }
  }

  NOTREACHED();
  return absl::nullopt;
}

void PolicyConverter::SetPolicyValueForTesting(const std::string& key,
                                               base::Value value) {
  PolicyConverter::SetPolicyValue(key, std::move(value));
}

void PolicyConverter::SetPolicyValue(const std::string& key,
                                     base::Value value) {
  const Schema schema = policy_schema_->GetKnownProperty(key);
  const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string());
  absl::optional<base::Value> converted_value =
      ConvertValueToSchema(std::move(value), schema);
  if (converted_value) {
    // Do not set list/dictionary policies that are sent as empty strings from
    // the UEM. This is common on Android when the UEM pushes the policy with
    // managed configurations.
    policy_bundle_->Get(ns).Set(key, POLICY_LEVEL_MANDATORY,
                                POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM,
                                std::move(converted_value), nullptr);
  }
}

}  // namespace android
}  // namespace policy