Android handler: Fix name collision.

The android handler was using the android framework to create temporary
files. This creates predictible names and ends up creating 2 libraries
with the same name. This is then an issue with ldopen that will not open
those independently.

This CL changes the implementation to create names that will not be
predictible.

R=blundell@chromium.org

Review URL: https://codereview.chromium.org/1133283002
diff --git a/shell/android/android_handler.cc b/shell/android/android_handler.cc
index 9d3181b..555efa8 100644
--- a/shell/android/android_handler.cc
+++ b/shell/android/android_handler.cc
@@ -4,13 +4,17 @@
 
 #include "shell/android/android_handler.h"
 
+#include <fcntl.h>
+
 #include "base/android/jni_android.h"
 #include "base/android/jni_string.h"
 #include "base/files/file_path.h"
 #include "base/logging.h"
 #include "base/message_loop/message_loop.h"
+#include "base/rand_util.h"
 #include "base/run_loop.h"
 #include "base/scoped_native_library.h"
+#include "base/strings/string_number_conversions.h"
 #include "base/trace_event/trace_event.h"
 #include "jni/AndroidHandler_jni.h"
 #include "mojo/common/data_pipe_utils.h"
@@ -154,6 +158,28 @@
       });
 }
 
+jstring CreateTemporaryFile(JNIEnv* env,
+                            jclass jcaller,
+                            jstring j_directory,
+                            jstring j_basename,
+                            jstring j_extension) {
+  std::string basename(ConvertJavaStringToUTF8(env, j_basename));
+  std::string extension(ConvertJavaStringToUTF8(env, j_extension));
+  base::FilePath directory(ConvertJavaStringToUTF8(env, j_directory));
+
+  for (;;) {
+    std::string random = base::RandBytesAsString(16);
+    std::string filename =
+        basename + base::HexEncode(random.data(), random.size()) + extension;
+    base::FilePath temporary_file = directory.Append(filename);
+    int fd = open(temporary_file.value().c_str(), O_CREAT | O_EXCL, 0600);
+    if (fd != -1) {
+      close(fd);
+      return ConvertUTF8ToJavaString(env, temporary_file.value()).Release();
+    }
+  }
+}
+
 bool RegisterAndroidHandlerJni(JNIEnv* env) {
   return RegisterNativesImpl(env);
 }