OLD | NEW |
(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 "chrome/browser/android/policy/policy_manager.h" |
| 6 |
| 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" |
| 15 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "components/policy/core/browser/browser_policy_connector.h" |
| 18 #include "components/policy/core/common/policy_provider_android.h" |
| 19 #include "jni/PolicyManager_jni.h" |
| 20 |
| 21 using base::android::AttachCurrentThread; |
| 22 using base::android::ConvertJavaStringToUTF8; |
| 23 |
| 24 PolicyManager::PolicyManager(JNIEnv* env, jobject obj) |
| 25 : weak_java_policy_manager_(env, obj), |
| 26 policy_bundle_(new policy::PolicyBundle) { |
| 27 policy_provider_ = static_cast<policy::PolicyProviderAndroid*>( |
| 28 g_browser_process->browser_policy_connector()->GetPlatformProvider()); |
| 29 policy_provider_->SetDelegate(this); |
| 30 } |
| 31 |
| 32 PolicyManager::~PolicyManager() {} |
| 33 |
| 34 void PolicyManager::RefreshPolicies() { |
| 35 JNIEnv* env = AttachCurrentThread(); |
| 36 Java_PolicyManager_refreshPolicies( |
| 37 env, weak_java_policy_manager_.get(env).obj()); |
| 38 } |
| 39 |
| 40 void PolicyManager::PolicyProviderShutdown() { |
| 41 policy_provider_ = NULL; |
| 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 } |
| 79 |
| 80 void PolicyManager::FlushPolicies(JNIEnv* env, jobject obj) { |
| 81 if (!policy_provider_) |
| 82 return; |
| 83 |
| 84 policy_provider_->SetPolicies(policy_bundle_.Pass()); |
| 85 policy_bundle_.reset(new policy::PolicyBundle); |
| 86 } |
| 87 |
| 88 void PolicyManager::Destroy(JNIEnv* env, jobject obj) { |
| 89 if (policy_provider_) |
| 90 policy_provider_->SetDelegate(NULL); |
| 91 |
| 92 delete this; |
| 93 } |
| 94 |
| 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 base::Value* decoded_value = base::JSONReader::Read(string_value); |
| 162 if (decoded_value) |
| 163 return decoded_value; |
| 164 } |
| 165 return value.release(); |
| 166 } |
| 167 } |
| 168 |
| 169 NOTREACHED(); |
| 170 return NULL; |
| 171 } |
| 172 |
| 173 void PolicyManager::SetPolicyValue(const std::string& key, base::Value* value) { |
| 174 if (!policy_provider_) |
| 175 return; |
| 176 |
| 177 policy::Schema schema = |
| 178 policy_provider_->GetChromeSchema()->GetKnownProperty(key); |
| 179 |
| 180 policy::PolicyNamespace ns(policy::POLICY_DOMAIN_CHROME, std::string()); |
| 181 policy_bundle_->Get(ns).Set(key, |
| 182 policy::POLICY_LEVEL_MANDATORY, |
| 183 policy::POLICY_SCOPE_MACHINE, |
| 184 ConvertValueToSchema(value, schema), |
| 185 NULL); |
| 186 } |
| 187 |
| 188 static jlong Init(JNIEnv* env, jobject obj) { |
| 189 return reinterpret_cast<intptr_t>(new PolicyManager(env, obj)); |
| 190 } |
| 191 |
| 192 bool RegisterPolicyManager(JNIEnv* env) { |
| 193 return RegisterNativesImpl(env); |
| 194 } |
OLD | NEW |