| OLD | NEW |
| 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 "chrome/browser/android/policy/policy_manager.h" | 5 #include "chrome/browser/android/policy/policy_manager.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/prefs/pref_service.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/browser_process_platform_part.h" |
| 16 #include "chrome/common/pref_names.h" | 10 #include "components/policy/core/browser/android/policy_converter.h" |
| 17 #include "components/policy/core/browser/browser_policy_connector.h" | 11 #include "components/policy/core/browser/browser_policy_connector.h" |
| 18 #include "components/policy/core/common/policy_provider_android.h" | 12 #include "components/policy/core/common/policy_provider_android.h" |
| 13 #include "components/policy/core/common/schema.h" |
| 19 #include "jni/PolicyManager_jni.h" | 14 #include "jni/PolicyManager_jni.h" |
| 20 | 15 |
| 21 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
| 22 using base::android::ConvertJavaStringToUTF8; | 17 using base::android::ScopedJavaLocalRef; |
| 23 | 18 |
| 24 PolicyManager::PolicyManager(JNIEnv* env, jobject obj) | 19 PolicyManager::PolicyManager(JNIEnv* env, jobject obj) |
| 25 : weak_java_policy_manager_(env, obj), | 20 : weak_java_policy_manager_(env, obj) { |
| 26 policy_bundle_(new policy::PolicyBundle) { | |
| 27 policy_provider_ = static_cast<policy::PolicyProviderAndroid*>( | 21 policy_provider_ = static_cast<policy::PolicyProviderAndroid*>( |
| 28 g_browser_process->browser_policy_connector()->GetPlatformProvider()); | 22 g_browser_process->browser_policy_connector()->GetPlatformProvider()); |
| 29 policy_provider_->SetDelegate(this); | 23 policy_provider_->SetDelegate(this); |
| 30 } | 24 } |
| 31 | 25 |
| 32 PolicyManager::~PolicyManager() {} | 26 PolicyManager::~PolicyManager() {} |
| 33 | 27 |
| 28 ScopedJavaLocalRef<jobject> PolicyManager::CreatePolicyConverter(JNIEnv* env, |
| 29 jobject obj) { |
| 30 policy_converter_.reset(new policy::android::PolicyConverter( |
| 31 policy_provider_->GetChromeSchema())); |
| 32 return ScopedJavaLocalRef<jobject>(policy_converter_->GetJavaObject()); |
| 33 } |
| 34 |
| 34 void PolicyManager::RefreshPolicies() { | 35 void PolicyManager::RefreshPolicies() { |
| 35 JNIEnv* env = AttachCurrentThread(); | 36 JNIEnv* env = AttachCurrentThread(); |
| 36 Java_PolicyManager_refreshPolicies( | 37 Java_PolicyManager_refreshPolicies( |
| 37 env, weak_java_policy_manager_.get(env).obj()); | 38 env, weak_java_policy_manager_.get(env).obj()); |
| 38 } | 39 } |
| 39 | 40 |
| 40 void PolicyManager::PolicyProviderShutdown() { | 41 void PolicyManager::PolicyProviderShutdown() { |
| 41 policy_provider_ = NULL; | 42 policy_provider_ = nullptr; |
| 42 } | |
| 43 | |
| 44 void PolicyManager::SetPolicyBoolean(JNIEnv* env, | |
| 45 jobject obj, | |
| 46 jstring policyKey, | |
| 47 jboolean value) { | |
| 48 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 49 new base::FundamentalValue(static_cast<bool>(value))); | |
| 50 } | |
| 51 | |
| 52 void PolicyManager::SetPolicyInteger(JNIEnv* env, | |
| 53 jobject obj, | |
| 54 jstring policyKey, | |
| 55 jint value) { | |
| 56 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 57 new base::FundamentalValue(static_cast<int>(value))); | |
| 58 } | |
| 59 | |
| 60 void PolicyManager::SetPolicyString(JNIEnv* env, | |
| 61 jobject obj, | |
| 62 jstring policyKey, | |
| 63 jstring value) { | |
| 64 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), | |
| 65 new base::StringValue(ConvertJavaStringToUTF8(env, value))); | |
| 66 } | |
| 67 | |
| 68 void PolicyManager::SetPolicyStringArray(JNIEnv* env, | |
| 69 jobject obj, | |
| 70 jstring policyKey, | |
| 71 jobjectArray array) { | |
| 72 std::vector<base::string16> string_vector; | |
| 73 base::android::AppendJavaStringArrayToStringVector(env, array, | |
| 74 &string_vector); | |
| 75 base::ListValue* list_values = new base::ListValue(); | |
| 76 list_values->AppendStrings(string_vector); | |
| 77 SetPolicyValue(ConvertJavaStringToUTF8(env, policyKey), list_values); | |
| 78 } | 43 } |
| 79 | 44 |
| 80 void PolicyManager::FlushPolicies(JNIEnv* env, jobject obj) { | 45 void PolicyManager::FlushPolicies(JNIEnv* env, jobject obj) { |
| 81 if (!policy_provider_) | 46 if (!policy_provider_) |
| 82 return; | 47 return; |
| 83 | 48 |
| 84 policy_provider_->SetPolicies(policy_bundle_.Pass()); | 49 policy_provider_->SetPolicies(policy_converter_->GetPolicyBundle()); |
| 85 policy_bundle_.reset(new policy::PolicyBundle); | |
| 86 } | 50 } |
| 87 | 51 |
| 88 void PolicyManager::Destroy(JNIEnv* env, jobject obj) { | 52 void PolicyManager::Destroy(JNIEnv* env, jobject obj) { |
| 89 if (policy_provider_) | 53 if (policy_provider_) |
| 90 policy_provider_->SetDelegate(NULL); | 54 policy_provider_->SetDelegate(nullptr); |
| 91 | 55 |
| 92 delete this; | 56 delete this; |
| 93 } | 57 } |
| 94 | 58 |
| 95 // static | |
| 96 base::Value* PolicyManager::ConvertValueToSchema(base::Value* raw_value, | |
| 97 const policy::Schema& schema) { | |
| 98 scoped_ptr<base::Value> value(raw_value); | |
| 99 | |
| 100 if (!schema.valid()) | |
| 101 return value.release(); | |
| 102 | |
| 103 switch (schema.type()) { | |
| 104 case base::Value::TYPE_NULL: | |
| 105 return base::Value::CreateNullValue().release(); | |
| 106 | |
| 107 case base::Value::TYPE_BOOLEAN: { | |
| 108 std::string string_value; | |
| 109 if (value->GetAsString(&string_value)) { | |
| 110 if (string_value.compare("true") == 0) | |
| 111 return new base::FundamentalValue(true); | |
| 112 | |
| 113 if (string_value.compare("false") == 0) | |
| 114 return new base::FundamentalValue(false); | |
| 115 | |
| 116 return value.release(); | |
| 117 } | |
| 118 int int_value = 0; | |
| 119 if (value->GetAsInteger(&int_value)) | |
| 120 return new base::FundamentalValue(int_value != 0); | |
| 121 | |
| 122 return value.release(); | |
| 123 } | |
| 124 | |
| 125 case base::Value::TYPE_INTEGER: { | |
| 126 std::string string_value; | |
| 127 if (value->GetAsString(&string_value)) { | |
| 128 int int_value = 0; | |
| 129 if (base::StringToInt(string_value, &int_value)) | |
| 130 return new base::FundamentalValue(int_value); | |
| 131 } | |
| 132 return value.release(); | |
| 133 } | |
| 134 | |
| 135 case base::Value::TYPE_DOUBLE: { | |
| 136 std::string string_value; | |
| 137 if (value->GetAsString(&string_value)) { | |
| 138 double double_value = 0; | |
| 139 if (base::StringToDouble(string_value, &double_value)) | |
| 140 return new base::FundamentalValue(double_value); | |
| 141 } | |
| 142 return value.release(); | |
| 143 } | |
| 144 | |
| 145 // String can't be converted from other types. | |
| 146 case base::Value::TYPE_STRING: { | |
| 147 return value.release(); | |
| 148 } | |
| 149 | |
| 150 // Binary is not a valid schema type. | |
| 151 case base::Value::TYPE_BINARY: { | |
| 152 NOTREACHED(); | |
| 153 return NULL; | |
| 154 } | |
| 155 | |
| 156 // Complex types have to be deserialized from JSON. | |
| 157 case base::Value::TYPE_DICTIONARY: | |
| 158 case base::Value::TYPE_LIST: { | |
| 159 std::string string_value; | |
| 160 if (value->GetAsString(&string_value)) { | |
| 161 scoped_ptr<base::Value> decoded_value = | |
| 162 base::JSONReader::Read(string_value); | |
| 163 if (decoded_value) | |
| 164 return decoded_value.release(); | |
| 165 } | |
| 166 return value.release(); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 NOTREACHED(); | |
| 171 return NULL; | |
| 172 } | |
| 173 | |
| 174 void PolicyManager::SetPolicyValue(const std::string& key, base::Value* value) { | |
| 175 if (!policy_provider_) | |
| 176 return; | |
| 177 | |
| 178 policy::Schema schema = | |
| 179 policy_provider_->GetChromeSchema()->GetKnownProperty(key); | |
| 180 | |
| 181 policy::PolicyNamespace ns(policy::POLICY_DOMAIN_CHROME, std::string()); | |
| 182 policy_bundle_->Get(ns).Set(key, | |
| 183 policy::POLICY_LEVEL_MANDATORY, | |
| 184 policy::POLICY_SCOPE_MACHINE, | |
| 185 ConvertValueToSchema(value, schema), | |
| 186 NULL); | |
| 187 } | |
| 188 | |
| 189 static jlong Init(JNIEnv* env, jobject obj) { | 59 static jlong Init(JNIEnv* env, jobject obj) { |
| 190 return reinterpret_cast<intptr_t>(new PolicyManager(env, obj)); | 60 return reinterpret_cast<intptr_t>(new PolicyManager(env, obj)); |
| 191 } | 61 } |
| 192 | 62 |
| 193 bool RegisterPolicyManager(JNIEnv* env) { | 63 bool RegisterPolicyManager(JNIEnv* env) { |
| 194 return RegisterNativesImpl(env); | 64 return RegisterNativesImpl(env); |
| 195 } | 65 } |
| OLD | NEW |