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