xref: /aosp_15_r20/external/cronet/base/android/important_file_writer_android.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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 #include <string>
6 
7 #include "base/android/jni_array.h"
8 #include "base/android/jni_string.h"
9 #include "base/base_jni/ImportantFileWriterAndroid_jni.h"
10 #include "base/files/important_file_writer.h"
11 #include "base/threading/thread_restrictions.h"
12 
13 namespace base {
14 namespace android {
15 
16 class ScopedAllowBlockingForImportantFileWriter
17     : public base::ScopedAllowBlocking {};
18 
JNI_ImportantFileWriterAndroid_WriteFileAtomically(JNIEnv * env,std::string & native_file_name,jni_zero::ByteArrayView & data)19 static jboolean JNI_ImportantFileWriterAndroid_WriteFileAtomically(
20     JNIEnv* env,
21     std::string& native_file_name,
22     jni_zero::ByteArrayView& data) {
23   // This is called on the UI thread during shutdown to save tab data, so
24   // needs to enable IO.
25   ScopedAllowBlockingForImportantFileWriter allow_blocking;
26   base::FilePath path(native_file_name);
27   bool result =
28       base::ImportantFileWriter::WriteFileAtomically(path, data.string_view());
29   return result;
30 }
31 
32 }  // namespace android
33 }  // namespace base
34