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

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

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

Powered by Google App Engine
This is Rietveld 408576698