[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 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/jni_android.h" |
| 10 | #include "base/android/jni_string.h" |
| 11 | #include "base/android/scoped_java_ref.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/memory/singleton.h" |
[email protected] | e46f6615 | 2012-07-19 20:02:55 | [diff] [blame] | 14 | #include "jni/BuildInfo_jni.h" |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 15 | |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // The caller takes ownership of the returned const char*. |
| 19 | const char* StrDupJString(const base::android::JavaRef<jstring>& java_string) { |
| 20 | std::string str = ConvertJavaStringToUTF8(java_string); |
| 21 | return strdup(str.c_str()); |
| 22 | } |
| 23 | |
| 24 | } // namespace |
| 25 | |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 26 | namespace base { |
| 27 | namespace android { |
| 28 | |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 29 | struct BuildInfoSingletonTraits { |
| 30 | static BuildInfo* New() { |
| 31 | return new BuildInfo(AttachCurrentThread()); |
| 32 | } |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 33 | |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 34 | static void Delete(BuildInfo* x) { |
| 35 | // We're leaking this type, see kRegisterAtExit. |
| 36 | NOTREACHED(); |
| 37 | } |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 38 | |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 39 | static const bool kRegisterAtExit = false; |
[email protected] | 5a8a806 | 2014-03-06 01:11:54 | [diff] [blame] | 40 | #ifndef NDEBUG |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 41 | static const bool kAllowedToAccessOnNonjoinableThread = true; |
[email protected] | 5a8a806 | 2014-03-06 01:11:54 | [diff] [blame] | 42 | #endif |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 43 | }; |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 44 | |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 45 | BuildInfo::BuildInfo(JNIEnv* env) |
| 46 | : device_(StrDupJString(Java_BuildInfo_getDevice(env))), |
| 47 | model_(StrDupJString(Java_BuildInfo_getDeviceModel(env))), |
| 48 | brand_(StrDupJString(Java_BuildInfo_getBrand(env))), |
| 49 | android_build_id_(StrDupJString(Java_BuildInfo_getAndroidBuildId(env))), |
| 50 | android_build_fp_(StrDupJString( |
| 51 | Java_BuildInfo_getAndroidBuildFingerprint(env))), |
| 52 | package_version_code_(StrDupJString(Java_BuildInfo_getPackageVersionCode( |
| 53 | env, GetApplicationContext()))), |
| 54 | package_version_name_(StrDupJString(Java_BuildInfo_getPackageVersionName( |
| 55 | env, GetApplicationContext()))), |
[email protected] | 9be878f | 2012-09-18 01:51:27 | [diff] [blame] | 56 | package_label_(StrDupJString(Java_BuildInfo_getPackageLabel( |
| 57 | env, GetApplicationContext()))), |
[email protected] | 281288d | 2012-11-09 03:03:23 | [diff] [blame] | 58 | package_name_(StrDupJString(Java_BuildInfo_getPackageName( |
| 59 | env, GetApplicationContext()))), |
[email protected] | a588c09 | 2014-02-13 23:26:54 | [diff] [blame] | 60 | build_type_(StrDupJString(Java_BuildInfo_getBuildType(env))), |
[email protected] | 19728484 | 2012-11-20 03:39:01 | [diff] [blame] | 61 | sdk_int_(Java_BuildInfo_getSdkInt(env)), |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 62 | java_exception_info_(NULL) { |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // static |
| 66 | BuildInfo* BuildInfo::GetInstance() { |
[email protected] | 57bf183 | 2012-06-20 16:42:39 | [diff] [blame] | 67 | return Singleton<BuildInfo, BuildInfoSingletonTraits >::get(); |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 1b46a53 | 2012-05-23 05:59:49 | [diff] [blame] | 70 | void BuildInfo::set_java_exception_info(const std::string& info) { |
| 71 | DCHECK(!java_exception_info_) << "info should be set only once."; |
| 72 | java_exception_info_ = strndup(info.c_str(), 1024); |
| 73 | } |
| 74 | |
[email protected] | 4235468 | 2012-07-09 22:12:16 | [diff] [blame] | 75 | // static |
| 76 | bool BuildInfo::RegisterBindings(JNIEnv* env) { |
[email protected] | 6653c19 | 2012-04-10 22:52:44 | [diff] [blame] | 77 | return RegisterNativesImpl(env); |
| 78 | } |
| 79 | |
| 80 | } // namespace android |
| 81 | } // namespace base |