| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/android/build_info.h" | 5 #include "base/android/build_info.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "jni/BuildInfo_jni.h" | 15 #include "jni/BuildInfo_jni.h" |
| 15 | 16 |
| 17 namespace base { |
| 18 namespace android { |
| 19 |
| 16 namespace { | 20 namespace { |
| 17 | 21 |
| 18 // We are leaking these strings. | 22 // We are leaking these strings. |
| 19 const char* StrDupJString(const base::android::JavaRef<jstring>& java_string) { | 23 const char* StrDupParam(const std::vector<std::string>& params, int index) { |
| 20 std::string str = ConvertJavaStringToUTF8(java_string); | 24 return strdup(params[index].c_str()); |
| 21 return strdup(str.c_str()); | 25 } |
| 26 |
| 27 int SdkIntParam(const std::vector<std::string>& params, int index) { |
| 28 int ret = 0; |
| 29 bool success = StringToInt(params[index], &ret); |
| 30 DCHECK(success); |
| 31 return ret; |
| 22 } | 32 } |
| 23 | 33 |
| 24 } // namespace | 34 } // namespace |
| 25 | 35 |
| 26 namespace base { | |
| 27 namespace android { | |
| 28 | |
| 29 struct BuildInfoSingletonTraits { | 36 struct BuildInfoSingletonTraits { |
| 30 static BuildInfo* New() { | 37 static BuildInfo* New() { |
| 31 return new BuildInfo(AttachCurrentThread()); | 38 JNIEnv* env = AttachCurrentThread(); |
| 39 ScopedJavaLocalRef<jobjectArray> params_objs = Java_BuildInfo_getAll(env); |
| 40 std::vector<std::string> params; |
| 41 AppendJavaStringArrayToStringVector(env, params_objs.obj(), ¶ms); |
| 42 return new BuildInfo(params); |
| 32 } | 43 } |
| 33 | 44 |
| 34 static void Delete(BuildInfo* x) { | 45 static void Delete(BuildInfo* x) { |
| 35 // We're leaking this type, see kRegisterAtExit. | 46 // We're leaking this type, see kRegisterAtExit. |
| 36 NOTREACHED(); | 47 NOTREACHED(); |
| 37 } | 48 } |
| 38 | 49 |
| 39 static const bool kRegisterAtExit = false; | 50 static const bool kRegisterAtExit = false; |
| 40 #if DCHECK_IS_ON() | 51 #if DCHECK_IS_ON() |
| 41 static const bool kAllowedToAccessOnNonjoinableThread = true; | 52 static const bool kAllowedToAccessOnNonjoinableThread = true; |
| 42 #endif | 53 #endif |
| 43 }; | 54 }; |
| 44 | 55 |
| 45 BuildInfo::BuildInfo(JNIEnv* env) | 56 BuildInfo::BuildInfo(const std::vector<std::string>& params) |
| 46 : device_(StrDupJString(Java_BuildInfo_getDevice(env))), | 57 : brand_(StrDupParam(params, 0)), |
| 47 manufacturer_(StrDupJString(Java_BuildInfo_getDeviceManufacturer(env))), | 58 device_(StrDupParam(params, 1)), |
| 48 model_(StrDupJString(Java_BuildInfo_getDeviceModel(env))), | 59 android_build_id_(StrDupParam(params, 2)), |
| 49 brand_(StrDupJString(Java_BuildInfo_getBrand(env))), | 60 manufacturer_(StrDupParam(params, 3)), |
| 50 android_build_id_(StrDupJString(Java_BuildInfo_getAndroidBuildId(env))), | 61 model_(StrDupParam(params, 4)), |
| 51 android_build_fp_( | 62 sdk_int_(SdkIntParam(params, 5)), |
| 52 StrDupJString(Java_BuildInfo_getAndroidBuildFingerprint(env))), | 63 build_type_(StrDupParam(params, 6)), |
| 53 gms_version_code_(StrDupJString(Java_BuildInfo_getGMSVersionCode(env))), | 64 package_label_(StrDupParam(params, 7)), |
| 54 package_version_code_( | 65 package_name_(StrDupParam(params, 8)), |
| 55 StrDupJString(Java_BuildInfo_getPackageVersionCode(env))), | 66 package_version_code_(StrDupParam(params, 9)), |
| 56 package_version_name_( | 67 package_version_name_(StrDupParam(params, 10)), |
| 57 StrDupJString(Java_BuildInfo_getPackageVersionName(env))), | 68 android_build_fp_(StrDupParam(params, 11)), |
| 58 package_label_(StrDupJString(Java_BuildInfo_getPackageLabel(env))), | 69 gms_version_code_(StrDupParam(params, 12)), |
| 59 package_name_(StrDupJString(Java_BuildInfo_getPackageName(env))), | 70 extracted_file_suffix_(params[13]), |
| 60 build_type_(StrDupJString(Java_BuildInfo_getBuildType(env))), | |
| 61 extracted_file_suffix_( | |
| 62 ConvertJavaStringToUTF8(Java_BuildInfo_getExtractedFileSuffix(env))), | |
| 63 sdk_int_(Java_BuildInfo_getSdkInt(env)), | |
| 64 java_exception_info_(NULL) {} | 71 java_exception_info_(NULL) {} |
| 65 | 72 |
| 66 // static | 73 // static |
| 67 BuildInfo* BuildInfo::GetInstance() { | 74 BuildInfo* BuildInfo::GetInstance() { |
| 68 return Singleton<BuildInfo, BuildInfoSingletonTraits >::get(); | 75 return Singleton<BuildInfo, BuildInfoSingletonTraits >::get(); |
| 69 } | 76 } |
| 70 | 77 |
| 71 void BuildInfo::SetJavaExceptionInfo(const std::string& info) { | 78 void BuildInfo::SetJavaExceptionInfo(const std::string& info) { |
| 72 DCHECK(!java_exception_info_) << "info should be set only once."; | 79 DCHECK(!java_exception_info_) << "info should be set only once."; |
| 73 java_exception_info_ = strndup(info.c_str(), 4096); | 80 java_exception_info_ = strndup(info.c_str(), 4096); |
| 74 } | 81 } |
| 75 | 82 |
| 76 void BuildInfo::ClearJavaExceptionInfo() { | 83 void BuildInfo::ClearJavaExceptionInfo() { |
| 77 delete java_exception_info_; | 84 delete java_exception_info_; |
| 78 java_exception_info_ = nullptr; | 85 java_exception_info_ = nullptr; |
| 79 } | 86 } |
| 80 | 87 |
| 81 } // namespace android | 88 } // namespace android |
| 82 } // namespace base | 89 } // namespace base |
| OLD | NEW |