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

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: Add DCHECKS to fail loudly in debug builds 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 DCHECK(array);
92
93 // GetArrayLength could return -1 if array is not a valid Java array
94 int length = static_cast<int>(env->GetArrayLength(array));
95 if (length < 0) {
96 NOTREACHED() << "Invalid array length: " << length;
Bernhard Bauer 2015/07/14 17:57:28 Just DCHECK it. If you have a for-loop below, you'
dgn 2015/07/14 19:15:15 thanks, understood.
97 return scoped_ptr<base::ListValue>();
98 }
99
100 scoped_ptr<base::ListValue> list_value(new base::ListValue());
101 for (int i = 0; i < length; ++i) {
102 jstring str = static_cast<jstring>(env->GetObjectArrayElement(array, i));
103 list_value->AppendString(ConvertJavaStringToUTF8(env, str));
104 }
105
106 return list_value.Pass();
107 }
108
109 // static
110 scoped_ptr<base::Value> PolicyConverter::ConvertValueToSchema(
111 scoped_ptr<base::Value> value,
112 const Schema& schema) {
113 if (!schema.valid())
114 return value.Pass();
115
116 switch (schema.type()) {
117 case base::Value::TYPE_NULL:
118 return base::Value::CreateNullValue();
119
120 case base::Value::TYPE_BOOLEAN: {
121 std::string string_value;
122 if (value->GetAsString(&string_value)) {
123 if (string_value.compare("true") == 0)
124 return make_scoped_ptr(new base::FundamentalValue(true));
125
126 if (string_value.compare("false") == 0)
127 return make_scoped_ptr(new base::FundamentalValue(false));
128
129 return value.Pass();
130 }
131 int int_value = 0;
132 if (value->GetAsInteger(&int_value))
133 return make_scoped_ptr(new base::FundamentalValue(int_value != 0));
134
135 return value.Pass();
136 }
137
138 case base::Value::TYPE_INTEGER: {
139 std::string string_value;
140 if (value->GetAsString(&string_value)) {
141 int int_value = 0;
142 if (base::StringToInt(string_value, &int_value))
143 return make_scoped_ptr(new base::FundamentalValue(int_value));
144 }
145 return value.Pass();
146 }
147
148 case base::Value::TYPE_DOUBLE: {
149 std::string string_value;
150 if (value->GetAsString(&string_value)) {
151 double double_value = 0;
152 if (base::StringToDouble(string_value, &double_value))
153 return make_scoped_ptr(new base::FundamentalValue(double_value));
154 }
155 return value.Pass();
156 }
157
158 // String can't be converted from other types.
159 case base::Value::TYPE_STRING: {
160 return value.Pass();
161 }
162
163 // Binary is not a valid schema type.
164 case base::Value::TYPE_BINARY: {
165 NOTREACHED();
166 return scoped_ptr<base::Value>();
167 }
168
169 // Complex types have to be deserialized from JSON.
170 case base::Value::TYPE_DICTIONARY:
171 case base::Value::TYPE_LIST: {
172 std::string string_value;
173 if (value->GetAsString(&string_value)) {
174 scoped_ptr<base::Value> decoded_value =
175 base::JSONReader::Read(string_value);
176 if (decoded_value)
177 return decoded_value.Pass();
178 }
179 return value.Pass();
180 }
181 }
182
183 NOTREACHED();
184 return scoped_ptr<base::Value>();
185 }
186
187 // static
188 bool PolicyConverter::Register(JNIEnv* env) {
189 return RegisterNativesImpl(env);
190 }
191
192 void PolicyConverter::SetPolicyValue(const std::string& key,
193 scoped_ptr<base::Value> value) {
194 const Schema schema = policy_schema_->GetKnownProperty(key);
195 const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string());
196 policy_bundle_->Get(ns)
197 .Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
198 ConvertValueToSchema(value.Pass(), schema).release(), nullptr);
199 }
200
201 } // namespace android
202 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698