Chromium Code Reviews| 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_provider_android.h" | |
| 21 #include "components/policy/core/common/policy_types.h" | |
| 22 #include "components/policy/core/common/schema.h" | |
| 23 #include "jni/PolicyConverter_jni.h" | |
| 24 | |
| 25 using base::android::ConvertJavaStringToUTF8; | |
| 26 | |
| 27 namespace policy { | |
| 28 | |
| 29 PolicyConverter::PolicyConverter(const Schema* policy_schema) | |
| 30 : policy_schema_(policy_schema), policy_bundle_(new PolicyBundle) { | |
| 31 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 32 weak_java_obj_ = JavaObjectWeakGlobalRef( | |
| 33 env, | |
| 34 Java_PolicyConverter_create(env, reinterpret_cast<long>(this)).obj()); | |
| 35 DCHECK(!weak_java_obj_.is_empty()); | |
| 36 } | |
| 37 | |
| 38 base::android::ScopedJavaLocalRef<jobject> PolicyConverter::GetJavaObject() { | |
| 39 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 40 return base::android::ScopedJavaLocalRef<jobject>(weak_java_obj_.get(env)); | |
| 41 } | |
| 42 | |
| 43 void PolicyConverter::SetPolicyBoolean(JNIEnv* env, | |
| 44 jobject obj, | |
| 45 jstring policyKey, | |
| 46 jboolean value) { | |
| 47 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 48 new base::FundamentalValue(static_cast<bool>(value))); | |
| 49 } | |
| 50 | |
| 51 void PolicyConverter::SetPolicyInteger(JNIEnv* env, | |
| 52 jobject obj, | |
| 53 jstring policyKey, | |
| 54 jint value) { | |
| 55 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 56 new base::FundamentalValue(static_cast<int>(value))); | |
| 57 } | |
| 58 | |
| 59 void PolicyConverter::SetPolicyString(JNIEnv* env, | |
| 60 jobject obj, | |
| 61 jstring policyKey, | |
| 62 jstring value) { | |
| 63 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 64 new base::StringValue(ConvertJavaStringToUTF8(env, value))); | |
| 65 } | |
| 66 | |
| 67 void PolicyConverter::SetPolicyStringArray(JNIEnv* env, | |
| 68 jobject obj, | |
| 69 jstring policyKey, | |
| 70 jobjectArray array) { | |
| 71 std::vector<base::string16> string_vector; | |
| 72 base::android::AppendJavaStringArrayToStringVector(env, array, | |
| 73 &string_vector); | |
| 74 base::ListValue* list_values = new base::ListValue(); | |
| 75 list_values->AppendStrings(string_vector); | |
| 76 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), list_values); | |
| 77 } | |
| 78 | |
| 79 scoped_ptr<PolicyBundle> PolicyConverter::GetPolicyBundle() { | |
| 80 scoped_ptr<PolicyBundle> filled_bundle(policy_bundle_.Pass()); | |
| 81 policy_bundle_.reset(new PolicyBundle); | |
| 82 return filled_bundle.Pass(); | |
| 83 } | |
| 84 | |
| 85 // static | |
| 86 scoped_ptr<base::Value> PolicyConverter::ConvertValueToSchema( | |
| 87 scoped_ptr<base::Value> value, | |
| 88 const Schema& schema) { | |
| 89 if (!schema.valid()) | |
| 90 return value.Pass(); | |
| 91 | |
| 92 switch (schema.type()) { | |
| 93 case base::Value::TYPE_NULL: | |
| 94 return base::Value::CreateNullValue(); | |
| 95 | |
| 96 case base::Value::TYPE_BOOLEAN: { | |
| 97 std::string string_value; | |
| 98 if (value->GetAsString(&string_value)) { | |
| 99 if (string_value.compare("true") == 0) | |
| 100 return scoped_ptr<base::Value>(new base::FundamentalValue(true)); | |
| 101 | |
| 102 if (string_value.compare("false") == 0) | |
| 103 return scoped_ptr<base::Value>(new base::FundamentalValue(false)); | |
| 104 | |
| 105 return value.Pass(); | |
| 106 } | |
| 107 int int_value = 0; | |
| 108 if (value->GetAsInteger(&int_value)) | |
| 109 return scoped_ptr<base::Value>( | |
| 110 new base::FundamentalValue(int_value != 0)); | |
| 111 | |
| 112 return value.Pass(); | |
| 113 } | |
| 114 | |
| 115 case base::Value::TYPE_INTEGER: { | |
| 116 std::string string_value; | |
| 117 if (value->GetAsString(&string_value)) { | |
| 118 int int_value = 0; | |
| 119 if (base::StringToInt(string_value, &int_value)) | |
| 120 return scoped_ptr<base::Value>(new base::FundamentalValue(int_value)); | |
| 121 } | |
| 122 return value.Pass(); | |
| 123 } | |
| 124 | |
| 125 case base::Value::TYPE_DOUBLE: { | |
| 126 std::string string_value; | |
| 127 if (value->GetAsString(&string_value)) { | |
| 128 double double_value = 0; | |
| 129 if (base::StringToDouble(string_value, &double_value)) | |
| 130 return scoped_ptr<base::Value>( | |
| 131 new base::FundamentalValue(double_value)); | |
| 132 } | |
| 133 return value.Pass(); | |
| 134 } | |
| 135 | |
| 136 // String can't be converted from other types. | |
| 137 case base::Value::TYPE_STRING: { | |
| 138 return value.Pass(); | |
| 139 } | |
| 140 | |
| 141 // Binary is not a valid schema type. | |
| 142 case base::Value::TYPE_BINARY: { | |
| 143 NOTREACHED(); | |
| 144 return scoped_ptr<base::Value>(); | |
| 145 } | |
| 146 | |
| 147 // Complex types have to be deserialized from JSON. | |
| 148 case base::Value::TYPE_DICTIONARY: | |
| 149 case base::Value::TYPE_LIST: { | |
| 150 std::string string_value; | |
| 151 if (value->GetAsString(&string_value)) { | |
| 152 scoped_ptr<base::Value> decoded_value = | |
| 153 base::JSONReader::Read(string_value); | |
| 154 if (decoded_value) | |
| 155 return decoded_value.Pass(); | |
| 156 } | |
| 157 return value.Pass(); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 NOTREACHED(); | |
| 162 return scoped_ptr<base::Value>(); | |
| 163 } | |
| 164 | |
| 165 PolicyConverter::~PolicyConverter() { | |
| 166 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 167 Java_PolicyConverter_onNativeDestroyed(env, weak_java_obj_.get(env).obj()); | |
| 168 } | |
| 169 | |
| 170 void PolicyConverter::SetPolicyValue(const std::string& key, | |
| 171 base::Value* value) { | |
| 172 const Schema schema = policy_schema_->GetKnownProperty(key); | |
| 173 const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string()); | |
| 174 policy_bundle_->Get(ns).Set( | |
| 175 key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
| 176 ConvertValueToSchema(scoped_ptr<base::Value>(value), schema).release(), | |
|
Bernhard Bauer
2015/07/09 08:53:58
You can use make_scoped_ptr() here.
dgn
2015/07/09 10:53:41
Done.
| |
| 177 nullptr); | |
| 178 } | |
| 179 | |
| 180 namespace android { | |
| 181 | |
| 182 // static | |
| 183 bool RegisterPolicyConverter(JNIEnv* env) { | |
| 184 return RegisterNativesImpl(env); | |
| 185 } | |
| 186 | |
| 187 } // namespace android | |
| 188 | |
| 189 } // namespace policy | |
| OLD | NEW |