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 #ifndef BASE_ANDROID_BUILD_INFO_H_
      6 #define BASE_ANDROID_BUILD_INFO_H_
      7 
      8 #include <jni.h>
      9 
     10 #include <string>
     11 
     12 #include "base/base_export.h"
     13 #include "base/macros.h"
     14 #include "base/memory/singleton.h"
     15 
     16 namespace base {
     17 namespace android {
     18 
     19 // This enumeration maps to the values returned by BuildInfo::sdk_int(),
     20 // indicating the Android release associated with a given SDK version.
     21 enum SdkVersion {
     22   SDK_VERSION_JELLY_BEAN = 16,
     23   SDK_VERSION_JELLY_BEAN_MR1 = 17,
     24   SDK_VERSION_JELLY_BEAN_MR2 = 18,
     25   SDK_VERSION_KITKAT = 19,
     26   SDK_VERSION_KITKAT_WEAR = 20,
     27   SDK_VERSION_LOLLIPOP = 21,
     28   SDK_VERSION_LOLLIPOP_MR1 = 22,
     29   SDK_VERSION_MARSHMALLOW = 23
     30 };
     31 
     32 // BuildInfo is a singleton class that stores android build and device
     33 // information. It will be called from Android specific code and gets used
     34 // primarily in crash reporting.
     35 
     36 // It is also used to store the last java exception seen during JNI.
     37 // TODO(nileshagrawal): Find a better place to store this info.
     38 class BASE_EXPORT BuildInfo {
     39  public:
     40 
     41   ~BuildInfo() {}
     42 
     43   // Static factory method for getting the singleton BuildInfo instance.
     44   // Note that ownership is not conferred on the caller and the BuildInfo in
     45   // question isn't actually freed until shutdown. This is ok because there
     46   // should only be one instance of BuildInfo ever created.
     47   static BuildInfo* GetInstance();
     48 
     49   // Const char* is used instead of std::strings because these values must be
     50   // available even if the process is in a crash state. Sadly
     51   // std::string.c_str() doesn't guarantee that memory won't be allocated when
     52   // it is called.
     53   const char* device() const {
     54     return device_;
     55   }
     56 
     57   const char* manufacturer() const {
     58     return manufacturer_;
     59   }
     60 
     61   const char* model() const {
     62     return model_;
     63   }
     64 
     65   const char* brand() const {
     66     return brand_;
     67   }
     68 
     69   const char* android_build_id() const {
     70     return android_build_id_;
     71   }
     72 
     73   const char* android_build_fp() const {
     74     return android_build_fp_;
     75   }
     76 
     77   const char* gms_version_code() const {
     78     return gms_version_code_;
     79   }
     80 
     81   const char* package_version_code() const {
     82     return package_version_code_;
     83   }
     84 
     85   const char* package_version_name() const {
     86     return package_version_name_;
     87   }
     88 
     89   const char* package_label() const {
     90     return package_label_;
     91   }
     92 
     93   const char* package_name() const {
     94     return package_name_;
     95   }
     96 
     97   const char* build_type() const {
     98     return build_type_;
     99   }
    100 
    101   int sdk_int() const {
    102     return sdk_int_;
    103   }
    104 
    105   const char* java_exception_info() const {
    106     return java_exception_info_;
    107   }
    108 
    109   void SetJavaExceptionInfo(const std::string& info);
    110 
    111   void ClearJavaExceptionInfo();
    112 
    113   static bool RegisterBindings(JNIEnv* env);
    114 
    115  private:
    116   friend struct BuildInfoSingletonTraits;
    117 
    118   explicit BuildInfo(JNIEnv* env);
    119 
    120   // Const char* is used instead of std::strings because these values must be
    121   // available even if the process is in a crash state. Sadly
    122   // std::string.c_str() doesn't guarantee that memory won't be allocated when
    123   // it is called.
    124   const char* const device_;
    125   const char* const manufacturer_;
    126   const char* const model_;
    127   const char* const brand_;
    128   const char* const android_build_id_;
    129   const char* const android_build_fp_;
    130   const char* const gms_version_code_;
    131   const char* const package_version_code_;
    132   const char* const package_version_name_;
    133   const char* const package_label_;
    134   const char* const package_name_;
    135   const char* const build_type_;
    136   const int sdk_int_;
    137   // This is set via set_java_exception_info, not at constructor time.
    138   const char* java_exception_info_;
    139 
    140   DISALLOW_COPY_AND_ASSIGN(BuildInfo);
    141 };
    142 
    143 }  // namespace android
    144 }  // namespace base
    145 
    146 #endif  // BASE_ANDROID_BUILD_INFO_H_
    147