• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIINVOCATION_H_
18 #define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIINVOCATION_H_
19 
20 #include "libnativehelper_api.h"
21 
22 #ifdef __cplusplus
23 
24 // JniInvocation adds a layer of indirection for applications using
25 // the JNI invocation API to allow the JNI implementation to be
26 // selected dynamically. Apps can specify a specific implementation to
27 // be used by calling InitJniInvocation. If this is not done, the
28 // library will chosen based on the value of Android system property
29 // persist.sys.dalvik.vm.lib on the device, and otherwise fall back to
30 // a hard-coded default implementation.
31 class JniInvocation final {
32  public:
JniInvocation()33   JniInvocation() {
34     impl_ = JniInvocationCreate();
35   }
36 
~JniInvocation()37   ~JniInvocation() {
38     JniInvocationDestroy(impl_);
39   }
40 
41   // Initialize JNI invocation API. library should specifiy a valid
42   // shared library for opening via dlopen providing a JNI invocation
43   // implementation, or null to allow defaulting via
44   // persist.sys.dalvik.vm.lib.
Init(const char * library)45   bool Init(const char* library) {
46     return JniInvocationInit(impl_, library) != 0;
47   }
48 
49   // Exposes which library is actually loaded from the given name. The
50   // buffer of size PROPERTY_VALUE_MAX will be used to load the system
51   // property for the default library, if necessary. If no buffer is
52   // provided, the fallback value will be used.
GetLibrary(const char * library,char * buffer)53   static const char* GetLibrary(const char* library, char* buffer) {
54     return JniInvocationGetLibrary(library, buffer);
55   }
56 
57  private:
58   JniInvocation(const JniInvocation&) = delete;
59   JniInvocation& operator=(const JniInvocation&) = delete;
60 
61   static const char* GetLibrary(const char* library, char* buffer, bool (*is_debuggable)(),
62                                 int (*get_library_system_property)(char* buffer));
63 
64   JniInvocationImpl* impl_;
65 
66   friend class JNIInvocation_Debuggable_Test;
67   friend class JNIInvocation_NonDebuggable_Test;
68 };
69 
70 #endif  // __cplusplus
71 
72 #endif  // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIINVOCATION_H_
73