summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authoraberent@chromium.org <aberent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-01 19:32:27 +0000
committeraberent@chromium.org <aberent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-01 19:32:27 +0000
commitb144fe828c831adedeb1fb31759b495a0ecb6ff1 (patch)
tree6cde14ebaf1ef07b4c654507c3b628e96a337465 /base
parent170369340eb12051b708d1874172e0968bfe70da (diff)
downloadchromium_src-b144fe828c831adedeb1fb31759b495a0ecb6ff1.zip
chromium_src-b144fe828c831adedeb1fb31759b495a0ecb6ff1.tar.gz
chromium_src-b144fe828c831adedeb1fb31759b495a0ecb6ff1.tar.bz2
Create JNI for ImportantFileWriter
Create an interface so that files that must not be corrupted can be written atomically from Java, using the C++ base::ImportantFileWriter class. BUG=148011 TEST=ImportantFileWriterAndroidTest#testAtomicWrite Review URL: https://chromiumcodereview.appspot.com/12382025 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@185569 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/android/base_jni_registrar.cc3
-rw-r--r--base/android/important_file_writer_android.cc38
-rw-r--r--base/android/important_file_writer_android.h18
-rw-r--r--base/android/java/src/org/chromium/base/ImportantFileWriterAndroid.java29
-rw-r--r--base/base.gyp1
-rw-r--r--base/base.gypi2
6 files changed, 91 insertions, 0 deletions
diff --git a/base/android/base_jni_registrar.cc b/base/android/base_jni_registrar.cc
index 2f761ac..6aabd0d 100644
--- a/base/android/base_jni_registrar.cc
+++ b/base/android/base_jni_registrar.cc
@@ -7,6 +7,7 @@
#include "base/basictypes.h"
#include "base/android/build_info.h"
#include "base/android/cpu_features.h"
+#include "base/android/important_file_writer_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_registrar.h"
#include "base/android/locale_utils.h"
@@ -22,6 +23,8 @@ namespace android {
static RegistrationMethod kBaseRegisteredMethods[] = {
{ "BuildInfo", base::android::BuildInfo::RegisterBindings },
{ "CpuFeatures", base::android::RegisterCpuFeatures },
+ { "ImportantFileWriterAndroid",
+ base::android::RegisterImportantFileWriterAndroid },
{ "LocaleUtils", base::android::RegisterLocaleUtils },
{ "PathService", base::android::RegisterPathService },
{ "PathUtils", base::android::RegisterPathUtils },
diff --git a/base/android/important_file_writer_android.cc b/base/android/important_file_writer_android.cc
new file mode 100644
index 0000000..f2275c1
--- /dev/null
+++ b/base/android/important_file_writer_android.cc
@@ -0,0 +1,38 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/android/important_file_writer_android.h"
+
+#include <string>
+
+#include "base/android/jni_string.h"
+#include "base/files/important_file_writer.h"
+#include "jni/ImportantFileWriterAndroid_jni.h"
+
+namespace base {
+namespace android {
+
+static jboolean WriteFileAtomically(JNIEnv* env,
+ jclass /* clazz */,
+ jstring file_name,
+ jbyteArray data) {
+ std::string native_file_name;
+ base::android::ConvertJavaStringToUTF8(env, file_name, &native_file_name);
+ base::FilePath path(native_file_name);
+ int data_length = env->GetArrayLength(data);
+ jbyte* native_data = env->GetByteArrayElements(data, NULL);
+ std::string native_data_string(reinterpret_cast<char *>(native_data),
+ data_length);
+ bool result = base::ImportantFileWriter::WriteFileAtomically(
+ path, native_data_string);
+ env->ReleaseByteArrayElements(data, native_data, JNI_ABORT);
+ return result;
+}
+
+bool RegisterImportantFileWriterAndroid(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace android
+} // namespace base
diff --git a/base/android/important_file_writer_android.h b/base/android/important_file_writer_android.h
new file mode 100644
index 0000000..20956ba
--- /dev/null
+++ b/base/android/important_file_writer_android.h
@@ -0,0 +1,18 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NATIVE_FRAMEWORK_CHROME_IMPORTANT_FILE_WRITE_ANDROID_H_
+#define NATIVE_FRAMEWORK_CHROME_IMPORTANT_FILE_WRITE_ANDROID_H_
+
+#include <jni.h>
+
+namespace base {
+namespace android {
+
+bool RegisterImportantFileWriterAndroid(JNIEnv* env);
+
+} // namespace android
+} // namespace base
+
+#endif // NATIVE_FRAMEWORK_CHROME_IMPORTANT_FILE_WRITE_ANDROID_H_
diff --git a/base/android/java/src/org/chromium/base/ImportantFileWriterAndroid.java b/base/android/java/src/org/chromium/base/ImportantFileWriterAndroid.java
new file mode 100644
index 0000000..1c7c018
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/ImportantFileWriterAndroid.java
@@ -0,0 +1,29 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.base;
+
+/**
+ * This class provides an interface to the native class for writing
+ * important data files without risking data loss.
+ */
+@JNINamespace("base::android")
+public class ImportantFileWriterAndroid {
+
+ /**
+ * Write a binary file atomically.
+ *
+ * This either writes all the data or leaves the file unchanged.
+ *
+ * @param fileName The complete path of the file to be written
+ * @param data The data to be written to the file
+ * @return true if the data was written to the file, false if not.
+ */
+ public static boolean writeFileAtomically(String fileName, byte[] data) {
+ return nativeWriteFileAtomically(fileName, data);
+ }
+
+ private static native boolean nativeWriteFileAtomically(
+ String fileName, byte[] data);
+}
diff --git a/base/base.gyp b/base/base.gyp
index 33c334d..2dd072a 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -1103,6 +1103,7 @@
'sources': [
'android/java/src/org/chromium/base/BuildInfo.java',
'android/java/src/org/chromium/base/CpuFeatures.java',
+ 'android/java/src/org/chromium/base/ImportantFileWriterAndroid.java',
'android/java/src/org/chromium/base/LocaleUtils.java',
'android/java/src/org/chromium/base/PathService.java',
'android/java/src/org/chromium/base/PathUtils.java',
diff --git a/base/base.gypi b/base/base.gypi
index 6e7013f..8f16a34 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -38,6 +38,8 @@
'android/build_info.cc',
'android/build_info.h',
'android/cpu_features.cc',
+ 'android/important_file_writer_android.cc',
+ 'android/important_file_writer_android.h',
'android/scoped_java_ref.cc',
'android/scoped_java_ref.h',
'android/jni_android.cc',