Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / android / library_loader / library_loader_hooks.cc
blob18eb7b949f7943ba6addddc3c8747b9183086797
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/android/library_loader/library_prefetcher.h"
11 #include "base/at_exit.h"
12 #include "base/metrics/histogram.h"
13 #include "jni/LibraryLoader_jni.h"
15 namespace base {
16 namespace android {
18 namespace {
20 base::AtExitManager* g_at_exit_manager = NULL;
21 const char* g_library_version_number = "";
22 LibraryLoadedHook* g_registration_callback = NULL;
24 enum RendererHistogramCode {
25 // Renderer load at fixed address success, fail, or not attempted.
26 // Renderers do not attempt to load at at fixed address if on a
27 // low-memory device on which browser load at fixed address has already
28 // failed.
29 LFA_SUCCESS = 0,
30 LFA_BACKOFF_USED = 1,
31 LFA_NOT_ATTEMPTED = 2,
33 // End sentinel, also used as nothing-pending indicator.
34 MAX_RENDERER_HISTOGRAM_CODE = 3,
35 NO_PENDING_HISTOGRAM_CODE = MAX_RENDERER_HISTOGRAM_CODE
38 enum BrowserHistogramCode {
39 // Non-low-memory random address browser loads.
40 NORMAL_LRA_SUCCESS = 0,
42 // Low-memory browser loads at fixed address, success or fail.
43 LOW_MEMORY_LFA_SUCCESS = 1,
44 LOW_MEMORY_LFA_BACKOFF_USED = 2,
46 MAX_BROWSER_HISTOGRAM_CODE = 3,
49 RendererHistogramCode g_renderer_histogram_code = NO_PENDING_HISTOGRAM_CODE;
51 // The amount of time, in milliseconds, that it took to load the shared
52 // libraries in the renderer. Set in
53 // RegisterChromiumAndroidLinkerRendererHistogram.
54 long g_renderer_library_load_time_ms = 0;
56 } // namespace
58 static void RegisterChromiumAndroidLinkerRendererHistogram(
59 JNIEnv* env,
60 jobject jcaller,
61 jboolean requested_shared_relro,
62 jboolean load_at_fixed_address_failed,
63 jlong library_load_time_ms) {
64 // Note a pending histogram value for later recording.
65 if (requested_shared_relro) {
66 g_renderer_histogram_code = load_at_fixed_address_failed
67 ? LFA_BACKOFF_USED : LFA_SUCCESS;
68 } else {
69 g_renderer_histogram_code = LFA_NOT_ATTEMPTED;
72 g_renderer_library_load_time_ms = library_load_time_ms;
75 void RecordChromiumAndroidLinkerRendererHistogram() {
76 if (g_renderer_histogram_code == NO_PENDING_HISTOGRAM_CODE)
77 return;
78 // Record and release the pending histogram value.
79 UMA_HISTOGRAM_ENUMERATION("ChromiumAndroidLinker.RendererStates",
80 g_renderer_histogram_code,
81 MAX_RENDERER_HISTOGRAM_CODE);
82 g_renderer_histogram_code = NO_PENDING_HISTOGRAM_CODE;
84 // Record how long it took to load the shared libraries.
85 UMA_HISTOGRAM_TIMES("ChromiumAndroidLinker.RendererLoadTime",
86 base::TimeDelta::FromMilliseconds(g_renderer_library_load_time_ms));
89 static void RecordChromiumAndroidLinkerBrowserHistogram(
90 JNIEnv* env,
91 jobject jcaller,
92 jboolean is_using_browser_shared_relros,
93 jboolean load_at_fixed_address_failed,
94 jint library_load_from_apk_status,
95 jlong library_load_time_ms) {
96 // For low-memory devices, record whether or not we successfully loaded the
97 // browser at a fixed address. Otherwise just record a normal invocation.
98 BrowserHistogramCode histogram_code;
99 if (is_using_browser_shared_relros) {
100 histogram_code = load_at_fixed_address_failed
101 ? LOW_MEMORY_LFA_BACKOFF_USED : LOW_MEMORY_LFA_SUCCESS;
102 } else {
103 histogram_code = NORMAL_LRA_SUCCESS;
105 UMA_HISTOGRAM_ENUMERATION("ChromiumAndroidLinker.BrowserStates",
106 histogram_code,
107 MAX_BROWSER_HISTOGRAM_CODE);
109 // Record the device support for loading a library directly from the APK file.
110 UMA_HISTOGRAM_ENUMERATION("ChromiumAndroidLinker.LibraryLoadFromApkStatus",
111 library_load_from_apk_status,
112 LIBRARY_LOAD_FROM_APK_STATUS_CODES_MAX);
114 // Record how long it took to load the shared libraries.
115 UMA_HISTOGRAM_TIMES("ChromiumAndroidLinker.BrowserLoadTime",
116 base::TimeDelta::FromMilliseconds(library_load_time_ms));
119 void SetLibraryLoadedHook(LibraryLoadedHook* func) {
120 g_registration_callback = func;
123 static void InitCommandLine(JNIEnv* env,
124 jobject jcaller,
125 jobjectArray init_command_line) {
126 InitNativeCommandLineFromJavaArray(env, init_command_line);
129 static jboolean LibraryLoaded(JNIEnv* env, jobject jcaller) {
130 if (g_registration_callback == NULL) {
131 return true;
133 return g_registration_callback(env, NULL);
136 void LibraryLoaderExitHook() {
137 if (g_at_exit_manager) {
138 delete g_at_exit_manager;
139 g_at_exit_manager = NULL;
143 static jboolean ForkAndPrefetchNativeLibrary(JNIEnv* env, jclass clazz) {
144 return NativeLibraryPrefetcher::ForkAndPrefetchNativeLibrary();
147 bool RegisterLibraryLoaderEntryHook(JNIEnv* env) {
148 return RegisterNativesImpl(env);
151 void SetVersionNumber(const char* version_number) {
152 g_library_version_number = strdup(version_number);
155 ScopedJavaLocalRef<jstring> GetVersionNumber(JNIEnv* env, jobject jcaller) {
156 return ConvertUTF8ToJavaString(env, g_library_version_number);
159 LibraryProcessType GetLibraryProcessType(JNIEnv* env) {
160 return static_cast<LibraryProcessType>(
161 Java_LibraryLoader_getLibraryProcessType(env));
164 void InitAtExitManager() {
165 g_at_exit_manager = new base::AtExitManager();
168 } // namespace android
169 } // namespace base