Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: components/policy/core/browser/android/policy_converter.cc

Issue 1220683008: Move AppRestriction to Policy code out of //chrome (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Return null instead of empty when input is not an array Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 if (!array)
92 return scoped_ptr<base::ListValue>();
93
94 // GetArrayLength Could return -1 is array is not a valid Java array
dgn 2015/07/14 17:15:46 TODO at next patch: s/Could return -1 is array/cou
95 int length = static_cast<int>(env->GetArrayLength(array));
96 if (length < 0)
97 return scoped_ptr<base::ListValue>();
98
99 scoped_ptr<base::ListValue> list_value(new base::ListValue());
100 for (int i = 0; i < length; ++i) {
101 jstring str = static_cast<jstring>(env->GetObjectArrayElement(array, i));
102 list_value->AppendString(ConvertJavaStringToUTF8(env, str));
103 }
104
105 return list_value.Pass();
106 }
107
108 // static
109 scoped_ptr<base::Value> PolicyConverter::ConvertValueToSchema(
110 scoped_ptr<base::Value> value,
111 const Schema& schema) {
112 if (!schema.valid())
113 return value.Pass();
114
115 switch (schema.type()) {
116 case base::Value::TYPE_NULL:
117 return base::Value::CreateNullValue();
118
119 case base::Value::TYPE_BOOLEAN: {
120 std::string string_value;
121 if (value->GetAsString(&string_value)) {
122 if (string_value.compare("true") == 0)
123 return make_scoped_ptr(new base::FundamentalValue(true));
124
125 if (string_value.compare("false") == 0)
126 return make_scoped_ptr(new base::FundamentalValue(false));
127
128 return value.Pass();
129 }
130 int int_value = 0;
131 if (value->GetAsInteger(&int_value))
132 return make_scoped_ptr(new base::FundamentalValue(int_value != 0));
133
134 return value.Pass();
135 }
136
137 case base::Value::TYPE_INTEGER: {
138 std::string string_value;
139 if (value->GetAsString(&string_value)) {
140 int int_value = 0;
141 if (base::StringToInt(string_value, &int_value))
142 return make_scoped_ptr(new base::FundamentalValue(int_value));
143 }
144 return value.Pass();
145 }
146
147 case base::Value::TYPE_DOUBLE: {
148 std::string string_value;
149 if (value->GetAsString(&string_value)) {
150 double double_value = 0;
151 if (base::StringToDouble(string_value, &double_value))
152 return make_scoped_ptr(new base::FundamentalValue(double_value));
153 }
154 return value.Pass();
155 }
156
157 // String can't be converted from other types.
158 case base::Value::TYPE_STRING: {
159 return value.Pass();
160 }
161
162 // Binary is not a valid schema type.
163 case base::Value::TYPE_BINARY: {
164 NOTREACHED();
165 return scoped_ptr<base::Value>();
166 }
167
168 // Complex types have to be deserialized from JSON.
169 case base::Value::TYPE_DICTIONARY:
170 case base::Value::TYPE_LIST: {
171 std::string string_value;
172 if (value->GetAsString(&string_value)) {
173 scoped_ptr<base::Value> decoded_value =
174 base::JSONReader::Read(string_value);
175 if (decoded_value)
176 return decoded_value.Pass();
177 }
178 return value.Pass();
179 }
180 }
181
182 NOTREACHED();
183 return scoped_ptr<base::Value>();
184 }
185
186 // static
187 bool PolicyConverter::Register(JNIEnv* env) {
188 return RegisterNativesImpl(env);
189 }
190
191 void PolicyConverter::SetPolicyValue(const std::string& key,
192 scoped_ptr<base::Value> value) {
193 const Schema schema = policy_schema_->GetKnownProperty(key);
194 const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string());
195 policy_bundle_->Get(ns)
196 .Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
197 ConvertValueToSchema(value.Pass(), schema).release(), nullptr);
198 }
199
200 } // namespace android
201 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698