1 // Copyright 2013 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.
4
5 #include <string>
6
7 #include "base/android/jni_string.h"
8 #include "base/files/important_file_writer.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "jni/ImportantFileWriterAndroid_jni.h"
11
12 namespace base {
13 namespace android {
14
JNI_ImportantFileWriterAndroid_WriteFileAtomically(JNIEnv * env,const JavaParamRef<jclass> &,const JavaParamRef<jstring> & file_name,const JavaParamRef<jbyteArray> & data)15 static jboolean JNI_ImportantFileWriterAndroid_WriteFileAtomically(
16 JNIEnv* env,
17 const JavaParamRef<jclass>& /* clazz */,
18 const JavaParamRef<jstring>& file_name,
19 const JavaParamRef<jbyteArray>& data) {
20 // This is called on the UI thread during shutdown to save tab data, so
21 // needs to enable IO.
22 base::ThreadRestrictions::ScopedAllowIO allow_io;
23 std::string native_file_name;
24 base::android::ConvertJavaStringToUTF8(env, file_name, &native_file_name);
25 base::FilePath path(native_file_name);
26 int data_length = env->GetArrayLength(data);
27 jbyte* native_data = env->GetByteArrayElements(data, NULL);
28 std::string native_data_string(reinterpret_cast<char *>(native_data),
29 data_length);
30 bool result = base::ImportantFileWriter::WriteFileAtomically(
31 path, native_data_string);
32 env->ReleaseByteArrayElements(data, native_data, JNI_ABORT);
33 return result;
34 }
35
36 } // namespace android
37 } // namespace base
38