1 /* 2 * Copyright (C) 2009 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 #define LOG_TAG "FileBackupHelper_native" 18 #include <utils/Log.h> 19 20 #include "JNIHelp.h" 21 #include <android_runtime/AndroidRuntime.h> 22 23 #include <utils/BackupHelpers.h> 24 25 namespace android 26 { 27 28 static jfieldID s_descriptorField = 0; 29 30 static int 31 ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor) 32 { 33 int err; 34 35 int fd = env->GetIntField(fileDescriptor, s_descriptorField); 36 if (fd == -1) { 37 return NULL; 38 } 39 40 return (int)new BackupDataWriter(fd); 41 } 42 43 static void 44 dtor_native(JNIEnv* env, jobject clazz, int w) 45 { 46 delete (BackupDataWriter*)w; 47 } 48 49 static jint 50 writeEntityHeader_native(JNIEnv* env, jobject clazz, int w, jstring key, int dataSize) 51 { 52 int err; 53 BackupDataWriter* writer = (BackupDataWriter*)w; 54 55 const char* keyUTF = env->GetStringUTFChars(key, NULL); 56 if (keyUTF == NULL) { 57 return -1; 58 } 59 60 err = writer->WriteEntityHeader(String8(keyUTF), dataSize); 61 62 env->ReleaseStringUTFChars(key, keyUTF); 63 64 return err; 65 } 66 67 static jint 68 writeEntityData_native(JNIEnv* env, jobject clazz, int w, jbyteArray data, int size) 69 { 70 int err; 71 BackupDataWriter* writer = (BackupDataWriter*)w; 72 73 if (env->GetArrayLength(data) < size) { 74 // size mismatch 75 return -1; 76 } 77 78 jbyte* dataBytes = env->GetByteArrayElements(data, NULL); 79 if (dataBytes == NULL) { 80 return -1; 81 } 82 83 err = writer->WriteEntityData(dataBytes, size); 84 85 env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT); 86 87 return err; 88 } 89 90 static void 91 setKeyPrefix_native(JNIEnv* env, jobject clazz, int w, jstring keyPrefixObj) 92 { 93 int err; 94 BackupDataWriter* writer = (BackupDataWriter*)w; 95 96 const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL); 97 String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : ""); 98 99 writer->SetKeyPrefix(keyPrefix); 100 101 env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF); 102 } 103 104 static const JNINativeMethod g_methods[] = { 105 { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native }, 106 { "dtor", "(I)V", (void*)dtor_native }, 107 { "writeEntityHeader_native", "(ILjava/lang/String;I)I", (void*)writeEntityHeader_native }, 108 { "writeEntityData_native", "(I[BI)I", (void*)writeEntityData_native }, 109 { "setKeyPrefix_native", "(ILjava/lang/String;)V", (void*)setKeyPrefix_native }, 110 }; 111 112 int register_android_backup_BackupDataOutput(JNIEnv* env) 113 { 114 //LOGD("register_android_backup_BackupDataOutput"); 115 116 jclass clazz; 117 118 clazz = env->FindClass("java/io/FileDescriptor"); 119 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); 120 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I"); 121 LOG_FATAL_IF(s_descriptorField == NULL, 122 "Unable to find descriptor field in java.io.FileDescriptor"); 123 124 return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataOutput", 125 g_methods, NELEM(g_methods)); 126 } 127 128 } 129