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_array.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/logging.h"
13 #include "base/memory/singleton.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "jni/BuildInfo_jni.h"
16 
17 namespace base {
18 namespace android {
19 
20 namespace {
21 
22 // We are leaking these strings.
StrDupParam(const std::vector<std::string> & params,int index)23 const char* StrDupParam(const std::vector<std::string>& params, int index) {
24   return strdup(params[index].c_str());
25 }
26 
GetIntParam(const std::vector<std::string> & params,int index)27 int GetIntParam(const std::vector<std::string>& params, int index) {
28   int ret = 0;
29   bool success = StringToInt(params[index], &ret);
30   DCHECK(success);
31   return ret;
32 }
33 
34 }  // namespace
35 
36 struct BuildInfoSingletonTraits {
Newbase::android::BuildInfoSingletonTraits37   static BuildInfo* New() {
38     JNIEnv* env = AttachCurrentThread();
39     ScopedJavaLocalRef<jobjectArray> params_objs = Java_BuildInfo_getAll(env);
40     std::vector<std::string> params;
41     AppendJavaStringArrayToStringVector(env, params_objs.obj(), &params);
42     return new BuildInfo(params);
43   }
44 
Deletebase::android::BuildInfoSingletonTraits45   static void Delete(BuildInfo* x) {
46     // We're leaking this type, see kRegisterAtExit.
47     NOTREACHED();
48   }
49 
50   static const bool kRegisterAtExit = false;
51 #if DCHECK_IS_ON()
52   static const bool kAllowedToAccessOnNonjoinableThread = true;
53 #endif
54 };
55 
BuildInfo(const std::vector<std::string> & params)56 BuildInfo::BuildInfo(const std::vector<std::string>& params)
57     : brand_(StrDupParam(params, 0)),
58       device_(StrDupParam(params, 1)),
59       android_build_id_(StrDupParam(params, 2)),
60       manufacturer_(StrDupParam(params, 3)),
61       model_(StrDupParam(params, 4)),
62       sdk_int_(GetIntParam(params, 5)),
63       build_type_(StrDupParam(params, 6)),
64       board_(StrDupParam(params, 7)),
65       host_package_name_(StrDupParam(params, 8)),
66       host_version_code_(StrDupParam(params, 9)),
67       host_package_label_(StrDupParam(params, 10)),
68       package_name_(StrDupParam(params, 11)),
69       package_version_code_(StrDupParam(params, 12)),
70       package_version_name_(StrDupParam(params, 13)),
71       android_build_fp_(StrDupParam(params, 14)),
72       gms_version_code_(StrDupParam(params, 15)),
73       installer_package_name_(StrDupParam(params, 16)),
74       abi_name_(StrDupParam(params, 17)),
75       firebase_app_id_(StrDupParam(params, 18)),
76       custom_themes_(StrDupParam(params, 19)),
77       resources_version_(StrDupParam(params, 20)),
78       extracted_file_suffix_(params[21]),
79       is_at_least_p_(GetIntParam(params, 22)) {}
80 
81 // static
GetInstance()82 BuildInfo* BuildInfo::GetInstance() {
83   return Singleton<BuildInfo, BuildInfoSingletonTraits >::get();
84 }
85 
86 }  // namespace android
87 }  // namespace base
88