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 #include <vector>
12 
13 #include "base/base_export.h"
14 #include "base/macros.h"
15 #include "base/memory/singleton.h"
16 
17 namespace base {
18 namespace android {
19 
20 // This enumeration maps to the values returned by BuildInfo::sdk_int(),
21 // indicating the Android release associated with a given SDK version.
22 enum SdkVersion {
23   SDK_VERSION_JELLY_BEAN = 16,
24   SDK_VERSION_JELLY_BEAN_MR1 = 17,
25   SDK_VERSION_JELLY_BEAN_MR2 = 18,
26   SDK_VERSION_KITKAT = 19,
27   SDK_VERSION_KITKAT_WEAR = 20,
28   SDK_VERSION_LOLLIPOP = 21,
29   SDK_VERSION_LOLLIPOP_MR1 = 22,
30   SDK_VERSION_MARSHMALLOW = 23,
31   SDK_VERSION_NOUGAT = 24,
32   SDK_VERSION_NOUGAT_MR1 = 25,
33   SDK_VERSION_OREO = 26,
34   SDK_VERSION_O_MR1 = 27,
35   SDK_VERSION_P = 28,
36   SDK_VERSION_Q = 29,
37   SDK_VERSION_R = 30,
38 };
39 
40 // BuildInfo is a singleton class that stores android build and device
41 // information. It will be called from Android specific code and gets used
42 // primarily in crash reporting.
43 class BASE_EXPORT BuildInfo {
44  public:
45 
~BuildInfo()46   ~BuildInfo() {}
47 
48   // Static factory method for getting the singleton BuildInfo instance.
49   // Note that ownership is not conferred on the caller and the BuildInfo in
50   // question isn't actually freed until shutdown. This is ok because there
51   // should only be one instance of BuildInfo ever created.
52   static BuildInfo* GetInstance();
53 
54   // Const char* is used instead of std::strings because these values must be
55   // available even if the process is in a crash state. Sadly
56   // std::string.c_str() doesn't guarantee that memory won't be allocated when
57   // it is called.
device()58   const char* device() const {
59     return device_;
60   }
61 
manufacturer()62   const char* manufacturer() const {
63     return manufacturer_;
64   }
65 
model()66   const char* model() const {
67     return model_;
68   }
69 
brand()70   const char* brand() const {
71     return brand_;
72   }
73 
android_build_id()74   const char* android_build_id() const {
75     return android_build_id_;
76   }
77 
android_build_fp()78   const char* android_build_fp() const {
79     return android_build_fp_;
80   }
81 
gms_version_code()82   const char* gms_version_code() const {
83     return gms_version_code_;
84   }
85 
host_package_name()86   const char* host_package_name() const { return host_package_name_; }
87 
host_version_code()88   const char* host_version_code() const { return host_version_code_; }
89 
host_package_label()90   const char* host_package_label() const { return host_package_label_; }
91 
package_version_code()92   const char* package_version_code() const {
93     return package_version_code_;
94   }
95 
package_version_name()96   const char* package_version_name() const {
97     return package_version_name_;
98   }
99 
package_name()100   const char* package_name() const {
101     return package_name_;
102   }
103 
104   // Will be empty string if no app id is assigned.
firebase_app_id()105   const char* firebase_app_id() const { return firebase_app_id_; }
106 
custom_themes()107   const char* custom_themes() const { return custom_themes_; }
108 
resources_version()109   const char* resources_version() const { return resources_version_; }
110 
build_type()111   const char* build_type() const {
112     return build_type_;
113   }
114 
board()115   const char* board() const { return board_; }
116 
installer_package_name()117   const char* installer_package_name() const { return installer_package_name_; }
118 
abi_name()119   const char* abi_name() const { return abi_name_; }
120 
extracted_file_suffix()121   std::string extracted_file_suffix() const { return extracted_file_suffix_; }
122 
sdk_int()123   int sdk_int() const {
124     return sdk_int_;
125   }
126 
is_at_least_q()127   bool is_at_least_q() const { return is_at_least_q_; }
128 
targets_at_least_r()129   bool targets_at_least_r() const { return targets_at_least_r_; }
130 
is_debug_android()131   bool is_debug_android() const { return is_debug_android_; }
132 
133  private:
134   friend struct BuildInfoSingletonTraits;
135 
136   explicit BuildInfo(const std::vector<std::string>& params);
137 
138   // Const char* is used instead of std::strings because these values must be
139   // available even if the process is in a crash state. Sadly
140   // std::string.c_str() doesn't guarantee that memory won't be allocated when
141   // it is called.
142   const char* const brand_;
143   const char* const device_;
144   const char* const android_build_id_;
145   const char* const manufacturer_;
146   const char* const model_;
147   const int sdk_int_;
148   const char* const build_type_;
149   const char* const board_;
150   const char* const host_package_name_;
151   const char* const host_version_code_;
152   const char* const host_package_label_;
153   const char* const package_name_;
154   const char* const package_version_code_;
155   const char* const package_version_name_;
156   const char* const android_build_fp_;
157   const char* const gms_version_code_;
158   const char* const installer_package_name_;
159   const char* const abi_name_;
160   const char* const firebase_app_id_;
161   const char* const custom_themes_;
162   const char* const resources_version_;
163   // Not needed by breakpad.
164   const std::string extracted_file_suffix_;
165   const bool is_at_least_q_;
166   const bool targets_at_least_r_;
167   const bool is_debug_android_;
168 
169   DISALLOW_COPY_AND_ASSIGN(BuildInfo);
170 };
171 
172 }  // namespace android
173 }  // namespace base
174 
175 #endif  // BASE_ANDROID_BUILD_INFO_H_
176