Chromium Code Reviews| Index: components/policy/core/browser/android/app_restrictions_importer.cc |
| diff --git a/components/policy/core/browser/android/app_restrictions_importer.cc b/components/policy/core/browser/android/app_restrictions_importer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a985ee9470ab2f30f9536705e1b12ded04bd36c0 |
| --- /dev/null |
| +++ b/components/policy/core/browser/android/app_restrictions_importer.cc |
| @@ -0,0 +1,193 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/policy/core/browser/android/app_restrictions_importer.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_array.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/logging.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/values.h" |
| +#include "components/policy/core/common/policy_bundle.h" |
| +#include "components/policy/core/common/policy_map.h" |
| +#include "components/policy/core/common/policy_namespace.h" |
| +#include "components/policy/core/common/policy_provider_android.h" |
| +#include "components/policy/core/common/policy_types.h" |
| +#include "components/policy/core/common/schema.h" |
| +#include "jni/AppRestrictionsImporter_jni.h" |
| + |
| +using base::android::ConvertJavaStringToUTF8; |
| + |
| +namespace policy { |
| + |
| +AppRestrictionsImporter::AppRestrictionsImporter( |
| + AppRestrictionsImporterDelegate* delegate) |
| + : delegate_(delegate), policy_bundle_(new PolicyBundle) { |
| +} |
| + |
| +void AppRestrictionsImporter::SetPolicyBoolean(JNIEnv* env, |
| + jobject obj, |
| + jstring policyKey, |
| + jboolean value) { |
| + SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), |
| + new base::FundamentalValue(static_cast<bool>(value))); |
| +} |
| + |
| +void AppRestrictionsImporter::SetPolicyInteger(JNIEnv* env, |
| + jobject obj, |
| + jstring policyKey, |
| + jint value) { |
| + SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), |
| + new base::FundamentalValue(static_cast<int>(value))); |
| +} |
| + |
| +void AppRestrictionsImporter::SetPolicyString(JNIEnv* env, |
| + jobject obj, |
| + jstring policyKey, |
| + jstring value) { |
| + SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), |
| + new base::StringValue(ConvertJavaStringToUTF8(env, value))); |
| +} |
| + |
| +void AppRestrictionsImporter::SetPolicyStringArray(JNIEnv* env, |
| + jobject obj, |
| + jstring policyKey, |
| + jobjectArray array) { |
| + std::vector<base::string16> string_vector; |
| + base::android::AppendJavaStringArrayToStringVector(env, array, |
| + &string_vector); |
| + base::ListValue* list_values = new base::ListValue(); |
| + list_values->AppendStrings(string_vector); |
| + SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), list_values); |
| +} |
| + |
| +void AppRestrictionsImporter::FlushPolicies(JNIEnv* env, jobject obj) { |
| + PolicyProviderAndroid* const policy_provider = delegate_->GetPolicyProvider(); |
| + if (!policy_provider) |
| + return; |
| + |
| + policy_provider->SetPolicies(policy_bundle_.Pass()); |
| + policy_bundle_.reset(new PolicyBundle); |
| +} |
| + |
| +// static |
| +scoped_ptr<base::Value> AppRestrictionsImporter::ConvertValueToSchema( |
| + scoped_ptr<base::Value> value, |
| + const Schema& schema) { |
| + if (!schema.valid()) |
| + return value.Pass(); |
| + |
| + switch (schema.type()) { |
| + case base::Value::TYPE_NULL: |
| + return base::Value::CreateNullValue(); |
| + |
| + case base::Value::TYPE_BOOLEAN: { |
| + std::string string_value; |
| + if (value->GetAsString(&string_value)) { |
| + if (string_value.compare("true") == 0) |
| + return scoped_ptr<base::Value>(new base::FundamentalValue(true)); |
| + |
| + if (string_value.compare("false") == 0) |
| + return scoped_ptr<base::Value>(new base::FundamentalValue(false)); |
| + |
| + return value.Pass(); |
| + } |
| + int int_value = 0; |
| + if (value->GetAsInteger(&int_value)) |
| + return scoped_ptr<base::Value>( |
| + new base::FundamentalValue(int_value != 0)); |
| + |
| + return value.Pass(); |
| + } |
| + |
| + case base::Value::TYPE_INTEGER: { |
| + std::string string_value; |
| + if (value->GetAsString(&string_value)) { |
| + int int_value = 0; |
| + if (base::StringToInt(string_value, &int_value)) |
| + return scoped_ptr<base::Value>(new base::FundamentalValue(int_value)); |
| + } |
| + return value.Pass(); |
| + } |
| + |
| + case base::Value::TYPE_DOUBLE: { |
| + std::string string_value; |
| + if (value->GetAsString(&string_value)) { |
| + double double_value = 0; |
| + if (base::StringToDouble(string_value, &double_value)) |
| + return scoped_ptr<base::Value>( |
| + new base::FundamentalValue(double_value)); |
| + } |
| + return value.Pass(); |
| + } |
| + |
| + // String can't be converted from other types. |
| + case base::Value::TYPE_STRING: { |
| + return value.Pass(); |
| + } |
| + |
| + // Binary is not a valid schema type. |
| + case base::Value::TYPE_BINARY: { |
| + NOTREACHED(); |
| + return scoped_ptr<base::Value>(); |
| + } |
| + |
| + // Complex types have to be deserialized from JSON. |
| + case base::Value::TYPE_DICTIONARY: |
| + case base::Value::TYPE_LIST: { |
| + std::string string_value; |
| + if (value->GetAsString(&string_value)) { |
| + scoped_ptr<base::Value> decoded_value = |
| + base::JSONReader::Read(string_value); |
| + if (decoded_value) |
| + return decoded_value.Pass(); |
| + } |
| + return value.Pass(); |
| + } |
| + } |
| + |
| + NOTREACHED(); |
| + return scoped_ptr<base::Value>(); |
| +} |
| + |
| +AppRestrictionsImporter::~AppRestrictionsImporter() { |
| +} |
| + |
| +void AppRestrictionsImporter::SetPolicyValue(const std::string& key, |
| + base::Value* value) { |
| + const PolicyProviderAndroid* policy_provider = delegate_->GetPolicyProvider(); |
| + if (!policy_provider) |
| + return; |
| + |
| + const Schema schema = |
| + policy_provider->GetChromeSchema()->GetKnownProperty(key); |
| + const PolicyNamespace ns(POLICY_DOMAIN_CHROME, std::string()); |
| + policy_bundle_->Get(ns).Set( |
| + key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
| + ConvertValueToSchema(scoped_ptr<base::Value>(value), schema).release(), |
| + nullptr); |
| +} |
| + |
| +static jlong Init(JNIEnv* env, jobject obj, jlong nativeDelegate) { |
| + AppRestrictionsImporterDelegate* delegate = |
| + reinterpret_cast<AppRestrictionsImporterDelegate*>(nativeDelegate); |
| + DCHECK(delegate); |
| + return reinterpret_cast<intptr_t>(new AppRestrictionsImporter(delegate)); |
|
Bernhard Bauer
2015/07/08 09:54:33
When is this object destroyed?
The whole object l
dgn
2015/07/08 11:25:44
PolicyManager (java) owns AppRestrictionsImporter
dgn
2015/07/08 18:16:01
Now it goes like:
PolicyManager(java) creates Pol
|
| +} |
| + |
| +namespace android { |
| + |
| +// static |
| +bool RegisterAppRestrictionsImporter(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace android |
| + |
| +} // namespace policy |