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

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

Issue 1550383002: Convert Pass()→std::move() in //components (Android edition) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 <vector> 8 #include <vector>
8 9
9 #include "base/android/jni_android.h" 10 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h" 11 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.h"
12 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
15 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
16 #include "base/values.h" 17 #include "base/values.h"
(...skipping 17 matching lines...) Expand all
34 env, reinterpret_cast<long>(this)).obj()); 35 env, reinterpret_cast<long>(this)).obj());
35 DCHECK(!java_obj_.is_null()); 36 DCHECK(!java_obj_.is_null());
36 } 37 }
37 38
38 PolicyConverter::~PolicyConverter() { 39 PolicyConverter::~PolicyConverter() {
39 Java_PolicyConverter_onNativeDestroyed(base::android::AttachCurrentThread(), 40 Java_PolicyConverter_onNativeDestroyed(base::android::AttachCurrentThread(),
40 java_obj_.obj()); 41 java_obj_.obj());
41 } 42 }
42 43
43 scoped_ptr<PolicyBundle> PolicyConverter::GetPolicyBundle() { 44 scoped_ptr<PolicyBundle> PolicyConverter::GetPolicyBundle() {
44 scoped_ptr<PolicyBundle> filled_bundle(policy_bundle_.Pass()); 45 scoped_ptr<PolicyBundle> filled_bundle(std::move(policy_bundle_));
45 policy_bundle_.reset(new PolicyBundle); 46 policy_bundle_.reset(new PolicyBundle);
46 return filled_bundle.Pass(); 47 return filled_bundle;
47 } 48 }
48 49
49 base::android::ScopedJavaLocalRef<jobject> PolicyConverter::GetJavaObject() { 50 base::android::ScopedJavaLocalRef<jobject> PolicyConverter::GetJavaObject() {
50 return base::android::ScopedJavaLocalRef<jobject>(java_obj_); 51 return base::android::ScopedJavaLocalRef<jobject>(java_obj_);
51 } 52 }
52 53
53 void PolicyConverter::SetPolicyBoolean(JNIEnv* env, 54 void PolicyConverter::SetPolicyBoolean(JNIEnv* env,
54 const JavaRef<jobject>& obj, 55 const JavaRef<jobject>& obj,
55 const JavaRef<jstring>& policyKey, 56 const JavaRef<jstring>& policyKey,
56 jboolean value) { 57 jboolean value) {
(...skipping 18 matching lines...) Expand all
75 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), 76 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey),
76 make_scoped_ptr(new base::StringValue( 77 make_scoped_ptr(new base::StringValue(
77 ConvertJavaStringToUTF8(env, value)))); 78 ConvertJavaStringToUTF8(env, value))));
78 } 79 }
79 80
80 void PolicyConverter::SetPolicyStringArray(JNIEnv* env, 81 void PolicyConverter::SetPolicyStringArray(JNIEnv* env,
81 const JavaRef<jobject>& obj, 82 const JavaRef<jobject>& obj,
82 const JavaRef<jstring>& policyKey, 83 const JavaRef<jstring>& policyKey,
83 const JavaRef<jobjectArray>& array) { 84 const JavaRef<jobjectArray>& array) {
84 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), 85 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey),
85 ConvertJavaStringArrayToListValue(env, array).Pass()); 86 ConvertJavaStringArrayToListValue(env, array));
86 } 87 }
87 88
88 // static 89 // static
89 scoped_ptr<base::ListValue> PolicyConverter::ConvertJavaStringArrayToListValue( 90 scoped_ptr<base::ListValue> PolicyConverter::ConvertJavaStringArrayToListValue(
90 JNIEnv* env, 91 JNIEnv* env,
91 const JavaRef<jobjectArray>& array) { 92 const JavaRef<jobjectArray>& array) {
92 DCHECK(!array.is_null()); 93 DCHECK(!array.is_null());
93 int length = static_cast<int>(env->GetArrayLength(array.obj())); 94 int length = static_cast<int>(env->GetArrayLength(array.obj()));
94 DCHECK_GE(length, 0) << "Invalid array length: " << length; 95 DCHECK_GE(length, 0) << "Invalid array length: " << length;
95 96
96 scoped_ptr<base::ListValue> list_value(new base::ListValue()); 97 scoped_ptr<base::ListValue> list_value(new base::ListValue());
97 for (int i = 0; i < length; ++i) { 98 for (int i = 0; i < length; ++i) {
98 jstring str = 99 jstring str =
99 static_cast<jstring>(env->GetObjectArrayElement(array.obj(), i)); 100 static_cast<jstring>(env->GetObjectArrayElement(array.obj(), i));
100 list_value->AppendString(ConvertJavaStringToUTF8(env, str)); 101 list_value->AppendString(ConvertJavaStringToUTF8(env, str));
101 } 102 }
102 103
103 return list_value.Pass(); 104 return list_value;
104 } 105 }
105 106
106 // static 107 // static
107 scoped_ptr<base::Value> PolicyConverter::ConvertValueToSchema( 108 scoped_ptr<base::Value> PolicyConverter::ConvertValueToSchema(
108 scoped_ptr<base::Value> value, 109 scoped_ptr<base::Value> value,
109 const Schema& schema) { 110 const Schema& schema) {
110 if (!schema.valid()) 111 if (!schema.valid())
111 return value.Pass(); 112 return value;
112 113
113 switch (schema.type()) { 114 switch (schema.type()) {
114 case base::Value::TYPE_NULL: 115 case base::Value::TYPE_NULL:
115 return base::Value::CreateNullValue(); 116 return base::Value::CreateNullValue();
116 117
117 case base::Value::TYPE_BOOLEAN: { 118 case base::Value::TYPE_BOOLEAN: {
118 std::string string_value; 119 std::string string_value;
119 if (value->GetAsString(&string_value)) { 120 if (value->GetAsString(&string_value)) {
120 if (string_value.compare("true") == 0) 121 if (string_value.compare("true") == 0)
121 return make_scoped_ptr(new base::FundamentalValue(true)); 122 return make_scoped_ptr(new base::FundamentalValue(true));
122 123
123 if (string_value.compare("false") == 0) 124 if (string_value.compare("false") == 0)
124 return make_scoped_ptr(new base::FundamentalValue(false)); 125 return make_scoped_ptr(new base::FundamentalValue(false));
125 126
126 return value.Pass(); 127 return value;
127 } 128 }
128 int int_value = 0; 129 int int_value = 0;
129 if (value->GetAsInteger(&int_value)) 130 if (value->GetAsInteger(&int_value))
130 return make_scoped_ptr(new base::FundamentalValue(int_value != 0)); 131 return make_scoped_ptr(new base::FundamentalValue(int_value != 0));
131 132
132 return value.Pass(); 133 return value;
133 } 134 }
134 135
135 case base::Value::TYPE_INTEGER: { 136 case base::Value::TYPE_INTEGER: {
136 std::string string_value; 137 std::string string_value;
137 if (value->GetAsString(&string_value)) { 138 if (value->GetAsString(&string_value)) {
138 int int_value = 0; 139 int int_value = 0;
139 if (base::StringToInt(string_value, &int_value)) 140 if (base::StringToInt(string_value, &int_value))
140 return make_scoped_ptr(new base::FundamentalValue(int_value)); 141 return make_scoped_ptr(new base::FundamentalValue(int_value));
141 } 142 }
142 return value.Pass(); 143 return value;
143 } 144 }
144 145
145 case base::Value::TYPE_DOUBLE: { 146 case base::Value::TYPE_DOUBLE: {
146 std::string string_value; 147 std::string string_value;
147 if (value->GetAsString(&string_value)) { 148 if (value->GetAsString(&string_value)) {
148 double double_value = 0; 149 double double_value = 0;
149 if (base::StringToDouble(string_value, &double_value)) 150 if (base::StringToDouble(string_value, &double_value))
150 return make_scoped_ptr(new base::FundamentalValue(double_value)); 151 return make_scoped_ptr(new base::FundamentalValue(double_value));
151 } 152 }
152 return value.Pass(); 153 return value;
153 } 154 }
154 155
155 // String can't be converted from other types. 156 // String can't be converted from other types.
156 case base::Value::TYPE_STRING: { 157 case base::Value::TYPE_STRING: {
157 return value.Pass(); 158 return value;
158 } 159 }
159 160
160 // Binary is not a valid schema type. 161 // Binary is not a valid schema type.
161 case base::Value::TYPE_BINARY: { 162 case base::Value::TYPE_BINARY: {
162 NOTREACHED(); 163 NOTREACHED();
163 return scoped_ptr<base::Value>(); 164 return scoped_ptr<base::Value>();
164 } 165 }
165 166
166 // Complex types have to be deserialized from JSON. 167 // Complex types have to be deserialized from JSON.
167 case base::Value::TYPE_DICTIONARY: 168 case base::Value::TYPE_DICTIONARY:
168 case base::Value::TYPE_LIST: { 169 case base::Value::TYPE_LIST: {
169 std::string string_value; 170 std::string string_value;
170 if (value->GetAsString(&string_value)) { 171 if (value->GetAsString(&string_value)) {
171 scoped_ptr<base::Value> decoded_value = 172 scoped_ptr<base::Value> decoded_value =
172 base::JSONReader::Read(string_value); 173 base::JSONReader::Read(string_value);
173 if (decoded_value) 174 if (decoded_value)
174 return decoded_value.Pass(); 175 return decoded_value;
175 } 176 }
176 return value.Pass(); 177 return value;
177 } 178 }
178 } 179 }
179 180
180 NOTREACHED(); 181 NOTREACHED();
181 return scoped_ptr<base::Value>(); 182 return scoped_ptr<base::Value>();
182 } 183 }
183 184
184 // static 185 // static
185 bool PolicyConverter::Register(JNIEnv* env) { 186 bool PolicyConverter::Register(JNIEnv* env) {
186 return RegisterNativesImpl(env); 187 return RegisterNativesImpl(env);
187 } 188 }
188 189
189 void PolicyConverter::SetPolicyValue(const std::string& key, 190 void PolicyConverter::SetPolicyValue(const std::string& key,
190 scoped_ptr<base::Value> value) { 191 scoped_ptr<base::Value> value) {
191 const Schema schema = policy_schema_->GetKnownProperty(key); 192 const Schema schema = policy_schema_->GetKnownProperty(key);
192 const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string()); 193 const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string());
193 policy_bundle_->Get(ns) 194 policy_bundle_->Get(ns).Set(
194 .Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 195 key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM,
195 POLICY_SOURCE_PLATFORM, 196 ConvertValueToSchema(std::move(value), schema).release(), nullptr);
196 ConvertValueToSchema(value.Pass(), schema).release(), nullptr);
197 } 197 }
198 198
199 } // namespace android 199 } // namespace android
200 } // namespace policy 200 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698