| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 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 "components/policy/core/browser/android/policy_converter.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" |
| 12 #include "base/json/json_reader.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/values.h" |
| 17 #include "components/policy/core/common/policy_bundle.h" |
| 18 #include "components/policy/core/common/policy_map.h" |
| 19 #include "components/policy/core/common/policy_namespace.h" |
| 20 #include "components/policy/core/common/policy_types.h" |
| 21 #include "components/policy/core/common/schema.h" |
| 22 #include "jni/PolicyConverter_jni.h" |
| 23 |
| 24 using base::android::ConvertJavaStringToUTF8; |
| 25 |
| 26 namespace policy { |
| 27 namespace android { |
| 28 |
| 29 PolicyConverter::PolicyConverter(const Schema* policy_schema) |
| 30 : policy_schema_(policy_schema), policy_bundle_(new PolicyBundle) { |
| 31 JNIEnv* env = base::android::AttachCurrentThread(); |
| 32 java_obj_.Reset(env, Java_PolicyConverter_create( |
| 33 env, reinterpret_cast<long>(this)).obj()); |
| 34 DCHECK(!java_obj_.is_null()); |
| 35 } |
| 36 |
| 37 PolicyConverter::~PolicyConverter() { |
| 38 Java_PolicyConverter_onNativeDestroyed(base::android::AttachCurrentThread(), |
| 39 java_obj_.obj()); |
| 40 } |
| 41 |
| 42 scoped_ptr<PolicyBundle> PolicyConverter::GetPolicyBundle() { |
| 43 scoped_ptr<PolicyBundle> filled_bundle(policy_bundle_.Pass()); |
| 44 policy_bundle_.reset(new PolicyBundle); |
| 45 return filled_bundle.Pass(); |
| 46 } |
| 47 |
| 48 base::android::ScopedJavaLocalRef<jobject> PolicyConverter::GetJavaObject() { |
| 49 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); |
| 50 } |
| 51 |
| 52 void PolicyConverter::SetPolicyBoolean(JNIEnv* env, |
| 53 jobject obj, |
| 54 jstring policyKey, |
| 55 jboolean value) { |
| 56 SetPolicyValue( |
| 57 ConvertJavaStringToUTF8(env, policyKey), |
| 58 make_scoped_ptr(new base::FundamentalValue(static_cast<bool>(value)))); |
| 59 } |
| 60 |
| 61 void PolicyConverter::SetPolicyInteger(JNIEnv* env, |
| 62 jobject obj, |
| 63 jstring policyKey, |
| 64 jint value) { |
| 65 SetPolicyValue( |
| 66 ConvertJavaStringToUTF8(env, policyKey), |
| 67 make_scoped_ptr(new base::FundamentalValue(static_cast<int>(value)))); |
| 68 } |
| 69 |
| 70 void PolicyConverter::SetPolicyString(JNIEnv* env, |
| 71 jobject obj, |
| 72 jstring policyKey, |
| 73 jstring value) { |
| 74 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), |
| 75 make_scoped_ptr(new base::StringValue( |
| 76 ConvertJavaStringToUTF8(env, value)))); |
| 77 } |
| 78 |
| 79 void PolicyConverter::SetPolicyStringArray(JNIEnv* env, |
| 80 jobject obj, |
| 81 jstring policyKey, |
| 82 jobjectArray array) { |
| 83 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), |
| 84 ConvertJavaStringArrayToListValue(env, array).Pass()); |
| 85 } |
| 86 |
| 87 // static |
| 88 scoped_ptr<base::ListValue> PolicyConverter::ConvertJavaStringArrayToListValue( |
| 89 JNIEnv* env, |
| 90 jobjectArray array) { |
| 91 scoped_ptr<base::ListValue> list_value(new base::ListValue()); |
| 92 |
| 93 if (!array) |
| 94 return list_value.Pass(); |
| 95 |
| 96 // GetArrayLength Could return -1 is array is not a valid Java array |
| 97 size_t len = static_cast<size_t>(std::max(0, env->GetArrayLength(array))); |
| 98 |
| 99 for (size_t i = 0; i < len; ++i) { |
| 100 jstring str = static_cast<jstring>(env->GetObjectArrayElement(array, i)); |
| 101 list_value->AppendString(ConvertJavaStringToUTF8(env, str)); |
| 102 } |
| 103 |
| 104 return list_value.Pass(); |
| 105 } |
| 106 |
| 107 // static |
| 108 scoped_ptr<base::Value> PolicyConverter::ConvertValueToSchema( |
| 109 scoped_ptr<base::Value> value, |
| 110 const Schema& schema) { |
| 111 if (!schema.valid()) |
| 112 return value.Pass(); |
| 113 |
| 114 switch (schema.type()) { |
| 115 case base::Value::TYPE_NULL: |
| 116 return base::Value::CreateNullValue(); |
| 117 |
| 118 case base::Value::TYPE_BOOLEAN: { |
| 119 std::string string_value; |
| 120 if (value->GetAsString(&string_value)) { |
| 121 if (string_value.compare("true") == 0) |
| 122 return make_scoped_ptr(new base::FundamentalValue(true)); |
| 123 |
| 124 if (string_value.compare("false") == 0) |
| 125 return make_scoped_ptr(new base::FundamentalValue(false)); |
| 126 |
| 127 return value.Pass(); |
| 128 } |
| 129 int int_value = 0; |
| 130 if (value->GetAsInteger(&int_value)) |
| 131 return make_scoped_ptr(new base::FundamentalValue(int_value != 0)); |
| 132 |
| 133 return value.Pass(); |
| 134 } |
| 135 |
| 136 case base::Value::TYPE_INTEGER: { |
| 137 std::string string_value; |
| 138 if (value->GetAsString(&string_value)) { |
| 139 int int_value = 0; |
| 140 if (base::StringToInt(string_value, &int_value)) |
| 141 return make_scoped_ptr(new base::FundamentalValue(int_value)); |
| 142 } |
| 143 return value.Pass(); |
| 144 } |
| 145 |
| 146 case base::Value::TYPE_DOUBLE: { |
| 147 std::string string_value; |
| 148 if (value->GetAsString(&string_value)) { |
| 149 double double_value = 0; |
| 150 if (base::StringToDouble(string_value, &double_value)) |
| 151 return make_scoped_ptr(new base::FundamentalValue(double_value)); |
| 152 } |
| 153 return value.Pass(); |
| 154 } |
| 155 |
| 156 // String can't be converted from other types. |
| 157 case base::Value::TYPE_STRING: { |
| 158 return value.Pass(); |
| 159 } |
| 160 |
| 161 // Binary is not a valid schema type. |
| 162 case base::Value::TYPE_BINARY: { |
| 163 NOTREACHED(); |
| 164 return scoped_ptr<base::Value>(); |
| 165 } |
| 166 |
| 167 // Complex types have to be deserialized from JSON. |
| 168 case base::Value::TYPE_DICTIONARY: |
| 169 case base::Value::TYPE_LIST: { |
| 170 std::string string_value; |
| 171 if (value->GetAsString(&string_value)) { |
| 172 scoped_ptr<base::Value> decoded_value = |
| 173 base::JSONReader::Read(string_value); |
| 174 if (decoded_value) |
| 175 return decoded_value.Pass(); |
| 176 } |
| 177 return value.Pass(); |
| 178 } |
| 179 } |
| 180 |
| 181 NOTREACHED(); |
| 182 return scoped_ptr<base::Value>(); |
| 183 } |
| 184 |
| 185 // static |
| 186 bool PolicyConverter::Register(JNIEnv* env) { |
| 187 return RegisterNativesImpl(env); |
| 188 } |
| 189 |
| 190 void PolicyConverter::SetPolicyValue(const std::string& key, |
| 191 scoped_ptr<base::Value> value) { |
| 192 const Schema schema = policy_schema_->GetKnownProperty(key); |
| 193 const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string()); |
| 194 policy_bundle_->Get(ns) |
| 195 .Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
| 196 ConvertValueToSchema(value.Pass(), schema).release(), nullptr); |
| 197 } |
| 198 |
| 199 } // namespace android |
| 200 } // namespace policy |
| OLD | NEW |