Home | History | Annotate | Download | only in android
      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"
     14 #include "jni/BuildInfo_jni.h"
     15 
     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 
     26 namespace base {
     27 namespace android {
     28 
     29 struct BuildInfoSingletonTraits {
     30   static BuildInfo* New() {
     31     return new BuildInfo(AttachCurrentThread());
     32   }
     33 
     34   static void Delete(BuildInfo* x) {
     35     // We're leaking this type, see kRegisterAtExit.
     36     NOTREACHED();
     37   }
     38 
     39   static const bool kRegisterAtExit = false;
     40 #ifndef NDEBUG
     41   static const bool kAllowedToAccessOnNonjoinableThread = true;
     42 #endif
     43 };
     44 
     45 BuildInfo::BuildInfo(JNIEnv* env)
     46     : device_(StrDupJString(Java_BuildInfo_getDevice(env))),
     47       manufacturer_(StrDupJString(Java_BuildInfo_getDeviceManufacturer(env))),
     48       model_(StrDupJString(Java_BuildInfo_getDeviceModel(env))),
     49       brand_(StrDupJString(Java_BuildInfo_getBrand(env))),
     50       android_build_id_(StrDupJString(Java_BuildInfo_getAndroidBuildId(env))),
     51       android_build_fp_(StrDupJString(
     52           Java_BuildInfo_getAndroidBuildFingerprint(env))),
     53       package_version_code_(StrDupJString(Java_BuildInfo_getPackageVersionCode(
     54           env, GetApplicationContext()))),
     55       package_version_name_(StrDupJString(Java_BuildInfo_getPackageVersionName(
     56           env, GetApplicationContext()))),
     57       package_label_(StrDupJString(Java_BuildInfo_getPackageLabel(
     58           env, GetApplicationContext()))),
     59       package_name_(StrDupJString(Java_BuildInfo_getPackageName(
     60           env, GetApplicationContext()))),
     61       build_type_(StrDupJString(Java_BuildInfo_getBuildType(env))),
     62       sdk_int_(Java_BuildInfo_getSdkInt(env)),
     63       java_exception_info_(NULL) {
     64 }
     65 
     66 // static
     67 BuildInfo* BuildInfo::GetInstance() {
     68   return Singleton<BuildInfo, BuildInfoSingletonTraits >::get();
     69 }
     70 
     71 void BuildInfo::set_java_exception_info(const std::string& info) {
     72   DCHECK(!java_exception_info_) << "info should be set only once.";
     73   java_exception_info_ = strndup(info.c_str(), 4096);
     74 }
     75 
     76 // static
     77 bool BuildInfo::RegisterBindings(JNIEnv* env) {
     78   return RegisterNativesImpl(env);
     79 }
     80 
     81 }  // namespace android
     82 }  // namespace base
     83