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 JNI_INVOCATION_H_included
18 #define JNI_INVOCATION_H_included
19 
20 #include <jni.h>
21 #include "module_api.h"
22 
23 struct JniInvocationImpl;
24 
25 MODULE_API struct JniInvocationImpl* JniInvocationCreate();
26 MODULE_API void JniInvocationDestroy(struct JniInvocationImpl* instance);
27 MODULE_API int JniInvocationInit(struct JniInvocationImpl* instance, const char* library);
28 MODULE_API const char* JniInvocationGetLibrary(const char* library, char* buffer);
29 
30 #ifdef __cplusplus
31 
32 // JniInvocation adds a layer of indirection for applications using
33 // the JNI invocation API to allow the JNI implementation to be
34 // selected dynamically. Apps can specify a specific implementation to
35 // be used by calling InitJniInvocation. If this is not done, the
36 // library will chosen based on the value of Android system property
37 // persist.sys.dalvik.vm.lib on the device, and otherwise fall back to
38 // a hard-coded default implementation.
39 class JniInvocation final {
40  public:
JniInvocation()41   JniInvocation() {
42     impl_ = JniInvocationCreate();
43   }
44 
~JniInvocation()45   ~JniInvocation() {
46     JniInvocationDestroy(impl_);
47   }
48 
49   // Initialize JNI invocation API. library should specifiy a valid
50   // shared library for opening via dlopen providing a JNI invocation
51   // implementation, or null to allow defaulting via
52   // persist.sys.dalvik.vm.lib.
Init(const char * library)53   bool Init(const char* library) {
54     return JniInvocationInit(impl_, library) != 0;
55   }
56 
57   // Exposes which library is actually loaded from the given name. The
58   // buffer of size PROPERTY_VALUE_MAX will be used to load the system
59   // property for the default library, if necessary. If no buffer is
60   // provided, the fallback value will be used.
GetLibrary(const char * library,char * buffer)61   static const char* GetLibrary(const char* library, char* buffer) {
62     return JniInvocationGetLibrary(library, buffer);
63   }
64 
65  private:
66   JniInvocation(const JniInvocation&) = delete;
67   JniInvocation& operator=(const JniInvocation&) = delete;
68 
69   static const char* GetLibrary(const char* library, char* buffer, bool (*is_debuggable)(),
70                                 int (*get_library_system_property)(char* buffer));
71 
72   JniInvocationImpl* impl_;
73 
74   friend class JNIInvocation_Debuggable_Test;
75   friend class JNIInvocation_NonDebuggable_Test;
76 };
77 
78 #endif  // __cplusplus
79 
80 #endif  // JNI_INVOCATION_H_included
81