File: apk_info.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 3,926 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/apk_info.h"

#include <string>
#include <variant>

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/compiler_specific.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "base/build_info_jni/ApkInfo_jni.h"

#if __ANDROID_API__ >= 29
// .aidl based NDK generation is only available when our min SDK level is 29 or
// higher.
#include "aidl/org/chromium/base/IApkInfo.h"
using aidl::org::chromium::base::IApkInfo;
#endif

namespace base::android::apk_info {

namespace {
#if __ANDROID_API__ < 29
struct IApkInfo {
  const std::string hostPackageLabel;
  const std::string hostPackageName;
  const std::string hostVersionCode;
  const std::string installerPackageName;
  bool isDebugApp;
  const std::string packageName;
  const std::string packageVersionCode;
  const std::string packageVersionName;
  const std::string resourcesVersion;
  int targetSdkVersion;
};
#endif

static std::optional<IApkInfo>& get_holder() {
  static base::NoDestructor<std::optional<IApkInfo>> holder;
  return *holder;
}

const IApkInfo& get_apk_info() {
  const std::optional<IApkInfo>& holder = get_holder();
  if (!holder.has_value()) {
    Java_ApkInfo_nativeReadyForFields(AttachCurrentThread());
  }
  return *holder;
}

}  // namespace

static void JNI_ApkInfo_FillFields(JNIEnv* env,
                                   std::string& hostPackageName,
                                   std::string& hostVersionCode,
                                   std::string& hostPackageLabel,
                                   std::string& packageVersionCode,
                                   std::string& packageVersionName,
                                   std::string& packageName,
                                   std::string& resourcesVersion,
                                   std::string& installerPackageName,
                                   jboolean isDebugApp,
                                   jint targetSdkVersion) {
  std::optional<IApkInfo>& holder = get_holder();
  DCHECK(!holder.has_value());
  holder.emplace(IApkInfo{.hostPackageLabel = hostPackageLabel,
                          .hostPackageName = hostPackageName,
                          .hostVersionCode = hostVersionCode,
                          .installerPackageName = installerPackageName,
                          .isDebugApp = static_cast<bool>(isDebugApp),
                          .packageName = packageName,
                          .packageVersionCode = packageVersionCode,
                          .packageVersionName = packageVersionName,
                          .resourcesVersion = resourcesVersion,
                          .targetSdkVersion = targetSdkVersion});
}

const std::string& host_package_name() {
  return get_apk_info().hostPackageName;
}

const std::string& host_version_code() {
  return get_apk_info().hostVersionCode;
}

const std::string& host_package_label() {
  return get_apk_info().hostPackageLabel;
}

const std::string& package_version_code() {
  return get_apk_info().packageVersionCode;
}

const std::string& package_version_name() {
  return get_apk_info().packageVersionName;
}

const std::string& package_name() {
  return get_apk_info().packageName;
}

const std::string& resources_version() {
  return get_apk_info().resourcesVersion;
}

const std::string& installer_package_name() {
  return get_apk_info().installerPackageName;
}

bool is_debug_app() {
  return get_apk_info().isDebugApp;
}

int target_sdk_version() {
  return get_apk_info().targetSdkVersion;
}

}  // namespace base::android::apk_info