1 // Copyright 2014 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.
5 #include "base/android/library_loader/library_loader_hooks.h"
7 #include "base/android/command_line_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/library_loader/library_load_from_apk_status_codes.h"
10 #include "base/at_exit.h"
11 #include "base/metrics/histogram.h"
12 #include "jni/LibraryLoader_jni.h"
19 base::AtExitManager
* g_at_exit_manager
= NULL
;
20 const char* g_library_version_number
= "";
21 LibraryLoadedHook
* g_registration_callback
= NULL
;
23 enum RendererHistogramCode
{
24 // Renderer load at fixed address success, fail, or not attempted.
25 // Renderers do not attempt to load at at fixed address if on a
26 // low-memory device on which browser load at fixed address has already
30 LFA_NOT_ATTEMPTED
= 2,
32 // End sentinel, also used as nothing-pending indicator.
33 MAX_RENDERER_HISTOGRAM_CODE
= 3,
34 NO_PENDING_HISTOGRAM_CODE
= MAX_RENDERER_HISTOGRAM_CODE
37 enum BrowserHistogramCode
{
38 // Non-low-memory random address browser loads.
39 NORMAL_LRA_SUCCESS
= 0,
41 // Low-memory browser loads at fixed address, success or fail.
42 LOW_MEMORY_LFA_SUCCESS
= 1,
43 LOW_MEMORY_LFA_BACKOFF_USED
= 2,
45 MAX_BROWSER_HISTOGRAM_CODE
= 3,
48 RendererHistogramCode g_renderer_histogram_code
= NO_PENDING_HISTOGRAM_CODE
;
52 static void RegisterChromiumAndroidLinkerRendererHistogram(
55 jboolean requested_shared_relro
,
56 jboolean load_at_fixed_address_failed
) {
57 // Note a pending histogram value for later recording.
58 if (requested_shared_relro
) {
59 g_renderer_histogram_code
= load_at_fixed_address_failed
60 ? LFA_BACKOFF_USED
: LFA_SUCCESS
;
62 g_renderer_histogram_code
= LFA_NOT_ATTEMPTED
;
66 void RecordChromiumAndroidLinkerRendererHistogram() {
67 if (g_renderer_histogram_code
== NO_PENDING_HISTOGRAM_CODE
)
69 // Record and release the pending histogram value.
70 UMA_HISTOGRAM_ENUMERATION("ChromiumAndroidLinker.RendererStates",
71 g_renderer_histogram_code
,
72 MAX_RENDERER_HISTOGRAM_CODE
);
73 g_renderer_histogram_code
= NO_PENDING_HISTOGRAM_CODE
;
76 static void RecordChromiumAndroidLinkerBrowserHistogram(
79 jboolean is_using_browser_shared_relros
,
80 jboolean load_at_fixed_address_failed
,
81 jint library_load_from_apk_status
) {
82 // For low-memory devices, record whether or not we successfully loaded the
83 // browser at a fixed address. Otherwise just record a normal invocation.
84 BrowserHistogramCode histogram_code
;
85 if (is_using_browser_shared_relros
) {
86 histogram_code
= load_at_fixed_address_failed
87 ? LOW_MEMORY_LFA_BACKOFF_USED
: LOW_MEMORY_LFA_SUCCESS
;
89 histogram_code
= NORMAL_LRA_SUCCESS
;
91 UMA_HISTOGRAM_ENUMERATION("ChromiumAndroidLinker.BrowserStates",
93 MAX_BROWSER_HISTOGRAM_CODE
);
95 // Record the device support for loading a library directly from the APK file.
96 UMA_HISTOGRAM_ENUMERATION("ChromiumAndroidLinker.LibraryLoadFromApkStatus",
97 library_load_from_apk_status
,
98 LIBRARY_LOAD_FROM_APK_STATUS_CODES_MAX
);
101 void SetLibraryLoadedHook(LibraryLoadedHook
* func
) {
102 g_registration_callback
= func
;
105 static void InitCommandLine(JNIEnv
* env
,
107 jobjectArray init_command_line
) {
108 InitNativeCommandLineFromJavaArray(env
, init_command_line
);
111 static jboolean
LibraryLoaded(JNIEnv
* env
, jobject jcaller
) {
112 if (g_registration_callback
== NULL
) {
115 return g_registration_callback(env
, NULL
);
118 void LibraryLoaderExitHook() {
119 if (g_at_exit_manager
) {
120 delete g_at_exit_manager
;
121 g_at_exit_manager
= NULL
;
125 bool RegisterLibraryLoaderEntryHook(JNIEnv
* env
) {
126 return RegisterNativesImpl(env
);
129 void SetVersionNumber(const char* version_number
) {
130 g_library_version_number
= strdup(version_number
);
133 jstring
GetVersionNumber(JNIEnv
* env
, jobject jcaller
) {
134 return ConvertUTF8ToJavaString(env
, g_library_version_number
).Release();
137 LibraryProcessType
GetLibraryProcessType(JNIEnv
* env
) {
138 return static_cast<LibraryProcessType
>(
139 Java_LibraryLoader_getLibraryProcessType(env
));
142 void InitAtExitManager() {
143 g_at_exit_manager
= new base::AtExitManager();
146 } // namespace android