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.
5 #include "base/android/build_info.h"
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"
14 #include "jni/BuildInfo_jni.h"
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());
29 struct BuildInfoSingletonTraits
{
30 static BuildInfo
* New() {
31 return new BuildInfo(AttachCurrentThread());
34 static void Delete(BuildInfo
* x
) {
35 // We're leaking this type, see kRegisterAtExit.
39 static const bool kRegisterAtExit
= false;
41 static const bool kAllowedToAccessOnNonjoinableThread
= true;
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()))),
56 package_label_(StrDupJString(Java_BuildInfo_getPackageLabel(
57 env
, GetApplicationContext()))),
58 package_name_(StrDupJString(Java_BuildInfo_getPackageName(
59 env
, GetApplicationContext()))),
60 build_type_(StrDupJString(Java_BuildInfo_getBuildType(env
))),
61 sdk_int_(Java_BuildInfo_getSdkInt(env
)),
62 java_exception_info_(NULL
) {
66 BuildInfo
* BuildInfo::GetInstance() {
67 return Singleton
<BuildInfo
, BuildInfoSingletonTraits
>::get();
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(), 4096);
76 bool BuildInfo::RegisterBindings(JNIEnv
* env
) {
77 return RegisterNativesImpl(env
);
80 } // namespace android