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" | |
|
Mattias Nissler (ping if slow)
2015/07/13 09:46:05
needed?
dgn
2015/07/14 12:50:26
Nope, removed also some forward declarations in th
| |
| 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 namespace android { | |
| 29 | |
| 30 PolicyConverter::PolicyConverter(const Schema* policy_schema) | |
| 31 : policy_schema_(policy_schema), policy_bundle_(new PolicyBundle) { | |
| 32 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 33 java_obj_.Reset(env, Java_PolicyConverter_create( | |
| 34 env, reinterpret_cast<long>(this)).obj()); | |
| 35 DCHECK(!java_obj_.is_null()); | |
| 36 } | |
| 37 | |
| 38 PolicyConverter::~PolicyConverter() { | |
| 39 Java_PolicyConverter_onNativeDestroyed(base::android::AttachCurrentThread(), | |
| 40 java_obj_.obj()); | |
| 41 } | |
| 42 | |
| 43 scoped_ptr<PolicyBundle> PolicyConverter::GetPolicyBundle() { | |
| 44 scoped_ptr<PolicyBundle> filled_bundle(policy_bundle_.Pass()); | |
| 45 policy_bundle_.reset(new PolicyBundle); | |
| 46 return filled_bundle.Pass(); | |
| 47 } | |
| 48 | |
| 49 base::android::ScopedJavaLocalRef<jobject> PolicyConverter::GetJavaObject() { | |
| 50 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); | |
| 51 } | |
| 52 | |
| 53 void PolicyConverter::SetPolicyBoolean(JNIEnv* env, | |
| 54 jobject obj, | |
| 55 jstring policyKey, | |
| 56 jboolean value) { | |
| 57 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 58 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(ConvertJavaStringToUTF8(env, policyKey), | |
| 66 new base::FundamentalValue(static_cast<int>(value))); | |
| 67 } | |
| 68 | |
| 69 void PolicyConverter::SetPolicyString(JNIEnv* env, | |
| 70 jobject obj, | |
| 71 jstring policyKey, | |
| 72 jstring value) { | |
| 73 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 74 new base::StringValue(ConvertJavaStringToUTF8(env, value))); | |
| 75 } | |
| 76 | |
| 77 void PolicyConverter::SetPolicyStringArray(JNIEnv* env, | |
| 78 jobject obj, | |
| 79 jstring policyKey, | |
| 80 jobjectArray array) { | |
| 81 std::vector<base::string16> string_vector; | |
| 82 base::android::AppendJavaStringArrayToStringVector(env, array, | |
| 83 &string_vector); | |
| 84 base::ListValue* list_values = new base::ListValue(); | |
| 85 list_values->AppendStrings(string_vector); | |
|
Mattias Nissler (ping if slow)
2015/07/13 09:46:05
Double conversion via std::vector seems convoluted
Bernhard Bauer
2015/07/13 10:46:03
Short answer: No :)
Longer answer: We need to con
Mattias Nissler (ping if slow)
2015/07/13 11:27:13
Note that your longer answer specifically doesn't
dgn
2015/07/14 12:50:25
Added it as a separate method. I would have been m
| |
| 86 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), list_values); | |
| 87 } | |
| 88 | |
| 89 // static | |
| 90 scoped_ptr<base::Value> PolicyConverter::ConvertValueToSchema( | |
| 91 scoped_ptr<base::Value> value, | |
| 92 const Schema& schema) { | |
| 93 if (!schema.valid()) | |
| 94 return value.Pass(); | |
| 95 | |
| 96 switch (schema.type()) { | |
| 97 case base::Value::TYPE_NULL: | |
| 98 return base::Value::CreateNullValue(); | |
| 99 | |
| 100 case base::Value::TYPE_BOOLEAN: { | |
| 101 std::string string_value; | |
| 102 if (value->GetAsString(&string_value)) { | |
| 103 if (string_value.compare("true") == 0) | |
| 104 return make_scoped_ptr(new base::FundamentalValue(true)); | |
| 105 | |
| 106 if (string_value.compare("false") == 0) | |
| 107 return make_scoped_ptr(new base::FundamentalValue(false)); | |
| 108 | |
| 109 return value.Pass(); | |
| 110 } | |
| 111 int int_value = 0; | |
| 112 if (value->GetAsInteger(&int_value)) | |
| 113 return make_scoped_ptr(new base::FundamentalValue(int_value != 0)); | |
| 114 | |
| 115 return value.Pass(); | |
| 116 } | |
| 117 | |
| 118 case base::Value::TYPE_INTEGER: { | |
| 119 std::string string_value; | |
| 120 if (value->GetAsString(&string_value)) { | |
| 121 int int_value = 0; | |
| 122 if (base::StringToInt(string_value, &int_value)) | |
| 123 return make_scoped_ptr(new base::FundamentalValue(int_value)); | |
| 124 } | |
| 125 return value.Pass(); | |
| 126 } | |
| 127 | |
| 128 case base::Value::TYPE_DOUBLE: { | |
| 129 std::string string_value; | |
| 130 if (value->GetAsString(&string_value)) { | |
| 131 double double_value = 0; | |
| 132 if (base::StringToDouble(string_value, &double_value)) | |
| 133 return make_scoped_ptr(new base::FundamentalValue(double_value)); | |
| 134 } | |
| 135 return value.Pass(); | |
| 136 } | |
| 137 | |
| 138 // String can't be converted from other types. | |
| 139 case base::Value::TYPE_STRING: { | |
| 140 return value.Pass(); | |
| 141 } | |
| 142 | |
| 143 // Binary is not a valid schema type. | |
| 144 case base::Value::TYPE_BINARY: { | |
| 145 NOTREACHED(); | |
| 146 return scoped_ptr<base::Value>(); | |
| 147 } | |
| 148 | |
| 149 // Complex types have to be deserialized from JSON. | |
| 150 case base::Value::TYPE_DICTIONARY: | |
| 151 case base::Value::TYPE_LIST: { | |
| 152 std::string string_value; | |
| 153 if (value->GetAsString(&string_value)) { | |
| 154 scoped_ptr<base::Value> decoded_value = | |
| 155 base::JSONReader::Read(string_value); | |
| 156 if (decoded_value) | |
| 157 return decoded_value.Pass(); | |
| 158 } | |
| 159 return value.Pass(); | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 NOTREACHED(); | |
| 164 return scoped_ptr<base::Value>(); | |
| 165 } | |
| 166 | |
| 167 // static | |
| 168 bool PolicyConverter::Register(JNIEnv* env) { | |
| 169 return RegisterNativesImpl(env); | |
| 170 } | |
| 171 | |
| 172 void PolicyConverter::SetPolicyValue(const std::string& key, | |
| 173 base::Value* value) { | |
| 174 const Schema schema = policy_schema_->GetKnownProperty(key); | |
| 175 const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string()); | |
| 176 policy_bundle_->Get(ns).Set( | |
| 177 key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
| 178 ConvertValueToSchema(make_scoped_ptr(value), schema).release(), nullptr); | |
| 179 } | |
| 180 | |
| 181 } // namespace android | |
| 182 } // namespace policy | |
| OLD | NEW |