xref: /aosp_15_r20/external/cronet/base/android/library_loader/library_loader_hooks.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
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_LIBRARY_LOADER_LIBRARY_LOADER_HOOKS_H_
6 #define BASE_ANDROID_LIBRARY_LOADER_LIBRARY_LOADER_HOOKS_H_
7 
8 #include <jni.h>
9 
10 #include "base/base_export.h"
11 #include "base/command_line.h"
12 #include "base/functional/callback.h"
13 #include "base/metrics/field_trial.h"
14 
15 namespace base {
16 
17 namespace android {
18 
19 // The process the shared library is loaded in.
20 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base.library_loader
21 enum LibraryProcessType {
22   // The LibraryLoad has not been initialized.
23   PROCESS_UNINITIALIZED = 0,
24   // Shared library is running in browser process.
25   PROCESS_BROWSER = 1,
26   // Shared library is running in child process.
27   PROCESS_CHILD = 2,
28   // Shared library is running in the app that uses webview.
29   PROCESS_WEBVIEW = 3,
30   // Shared library is running in child process as part of webview.
31   PROCESS_WEBVIEW_CHILD = 4,
32   // Shared library is running in a non-embedded WebView process.
33   PROCESS_WEBVIEW_NONEMBEDDED = 5,
34 };
35 
36 // Returns the library process type this library was loaded for.
37 BASE_EXPORT LibraryProcessType GetLibraryProcessType();
38 
39 // Whether fewer code should be prefetched, and no-readahead should be set.
40 // Returns true on low-end devices, where this speeds up startup, and false
41 // elsewhere, where it slows it down. See
42 // https://bugs.chromium.org/p/chromium/issues/detail?id=758566#c71 for details.
43 BASE_EXPORT bool IsUsingOrderfileOptimization();
44 
45 typedef bool NativeInitializationHook(LibraryProcessType library_process_type);
46 
47 BASE_EXPORT void SetNativeInitializationHook(
48     NativeInitializationHook native_initialization_hook);
49 
50 // Record any pending renderer histogram value as histograms.  Pending values
51 // are set by
52 // JNI_LibraryLoader_RegisterChromiumAndroidLinkerRendererHistogram().
53 BASE_EXPORT void RecordLibraryLoaderRendererHistograms();
54 
55 // Typedef for hook function to be called (indirectly from Java) once the
56 // libraries are loaded. The hook function should register the JNI bindings
57 // required to start the application. It should return true for success and
58 // false for failure.
59 // Note: this can't use base::{Once, Repeating}Callback because there is no
60 // way of initializing the default callback without using static objects, which
61 // we forbid.
62 typedef bool LibraryLoadedHook(JNIEnv* env,
63                                jclass clazz,
64                                LibraryProcessType library_process_type);
65 
66 // Set the hook function to be called (from Java) once the libraries are loaded.
67 // SetLibraryLoadedHook may only be called from JNI_OnLoad. The hook function
68 // should register the JNI bindings required to start the application.
69 
70 BASE_EXPORT void SetLibraryLoadedHook(LibraryLoadedHook* func);
71 
72 // Call on exit to delete the AtExitManager which OnLibraryLoadedOnUIThread
73 // created.
74 BASE_EXPORT void LibraryLoaderExitHook();
75 
76 // Initialize AtExitManager, this must be done at the begining of loading
77 // shared library.
78 void InitAtExitManager();
79 
80 }  // namespace android
81 }  // namespace base
82 
83 #endif  // BASE_ANDROID_LIBRARY_LOADER_LIBRARY_LOADER_HOOKS_H_
84