Chromium Code Reviews| 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" | |
| 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" | 7 #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" | 8 #include "components/policy/core/browser/browser_policy_connector.h" |
| 18 #include "components/policy/core/common/policy_provider_android.h" | 9 #include "components/policy/core/common/policy_provider_android.h" |
| 19 #include "jni/PolicyManager_jni.h" | 10 #include "jni/PolicyManager_jni.h" |
| 20 | 11 |
| 21 using base::android::AttachCurrentThread; | 12 using base::android::AttachCurrentThread; |
| 22 using base::android::ConvertJavaStringToUTF8; | |
| 23 | 13 |
| 24 PolicyManager::PolicyManager(JNIEnv* env, jobject obj) | 14 PolicyManager::PolicyManager(JNIEnv* env, jobject obj) |
| 25 : weak_java_policy_manager_(env, obj), | 15 : weak_java_policy_manager_(env, obj) { |
| 26 policy_bundle_(new policy::PolicyBundle) { | |
| 27 policy_provider_ = static_cast<policy::PolicyProviderAndroid*>( | 16 policy_provider_ = static_cast<policy::PolicyProviderAndroid*>( |
| 28 g_browser_process->browser_policy_connector()->GetPlatformProvider()); | 17 g_browser_process->browser_policy_connector()->GetPlatformProvider()); |
|
bartfab (slow)
2015/07/03 16:55:47
Nit: #include "chrome/browser/browser_process_plat
dgn
2015/07/06 17:34:02
Done. I don't understand why we need that though.
| |
| 29 policy_provider_->SetDelegate(this); | 18 policy_provider_->SetDelegate(this); |
| 30 } | 19 } |
| 31 | 20 |
| 32 PolicyManager::~PolicyManager() {} | 21 PolicyManager::~PolicyManager() {} |
| 33 | 22 |
| 23 policy::Schema PolicyManager::GetPolicySchema(const std::string& key) { | |
| 24 return policy_provider_->GetChromeSchema()->GetKnownProperty(key); | |
|
bartfab (slow)
2015/07/03 16:55:47
Nit: #include "components/policy/core/common/schem
dgn
2015/07/06 17:34:02
Done.
| |
| 25 } | |
| 26 | |
| 27 policy::PolicyProviderAndroid* PolicyManager::GetPolicyProvider() { | |
| 28 return policy_provider_; | |
| 29 } | |
| 30 | |
| 34 void PolicyManager::RefreshPolicies() { | 31 void PolicyManager::RefreshPolicies() { |
| 35 JNIEnv* env = AttachCurrentThread(); | 32 JNIEnv* env = AttachCurrentThread(); |
|
bartfab (slow)
2015/07/03 16:55:47
Nit: #include "base/android/jni_android.h"
dgn
2015/07/06 17:34:02
Done.
| |
| 36 Java_PolicyManager_refreshPolicies( | 33 Java_PolicyManager_refreshPolicies( |
| 37 env, weak_java_policy_manager_.get(env).obj()); | 34 env, weak_java_policy_manager_.get(env).obj()); |
| 38 } | 35 } |
| 39 | 36 |
| 40 void PolicyManager::PolicyProviderShutdown() { | 37 void PolicyManager::PolicyProviderShutdown() { |
| 41 policy_provider_ = NULL; | 38 policy_provider_ = NULL; |
| 42 } | 39 } |
| 43 | 40 |
| 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) { | 41 void PolicyManager::Destroy(JNIEnv* env, jobject obj) { |
| 89 if (policy_provider_) | 42 if (policy_provider_) |
| 90 policy_provider_->SetDelegate(NULL); | 43 policy_provider_->SetDelegate(NULL); |
| 91 | 44 |
| 92 delete this; | 45 delete this; |
| 93 } | 46 } |
| 94 | 47 |
| 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) { | 48 static jlong Init(JNIEnv* env, jobject obj) { |
| 190 return reinterpret_cast<intptr_t>(new PolicyManager(env, obj)); | 49 return reinterpret_cast<intptr_t>(new PolicyManager(env, obj)); |
| 191 } | 50 } |
| 192 | 51 |
| 193 bool RegisterPolicyManager(JNIEnv* env) { | 52 bool RegisterPolicyManager(JNIEnv* env) { |
| 194 return RegisterNativesImpl(env); | 53 return RegisterNativesImpl(env); |
| 195 } | 54 } |
| OLD | NEW |