Use sequenced blocking pool task to create PNaCl's nexe temp files

This isn't necessary in the browser since if there are 2 requests for 
the same translation we don't care which one wins, 
(i.e. which one gets the temp file and does the translation and which one 
blocks to wait) but the unit tests do care because they try to hit particular
corner cases. This is why they were flaky. So just always sequence the temp
file creation, which will have minimal cost in practice.
Also re-enable the disabled tests.
[email protected]
BUG=272492

Review URL: https://chromiumcodereview.appspot.com/23737002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220457 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/nacl_host/pnacl_host.cc b/chrome/browser/nacl_host/pnacl_host.cc
index 86546bd..2c3a082f 100644
--- a/chrome/browser/nacl_host/pnacl_host.cc
+++ b/chrome/browser/nacl_host/pnacl_host.cc
@@ -113,40 +113,41 @@
 
 // Create a temporary file on the blocking pool
 // static
-base::PlatformFile PnaclHost::DoCreateTemporaryFile(base::FilePath temp_dir) {
+void PnaclHost::DoCreateTemporaryFile(base::FilePath temp_dir,
+                                      TempFileCallback cb) {
   DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
 
   base::FilePath file_path;
+  base::PlatformFile file_handle(base::kInvalidPlatformFileValue);
   bool rv = temp_dir.empty()
                 ? file_util::CreateTemporaryFile(&file_path)
                 : file_util::CreateTemporaryFileInDir(temp_dir, &file_path);
   if (!rv) {
     PLOG(ERROR) << "Temp file creation failed.";
-    return base::kInvalidPlatformFileValue;
-  }
-  base::PlatformFileError error;
-  base::PlatformFile file_handle(base::CreatePlatformFile(
-      file_path,
-      base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ |
-          base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_TEMPORARY |
-          base::PLATFORM_FILE_DELETE_ON_CLOSE,
-      NULL,
-      &error));
+  } else {
+    base::PlatformFileError error;
+    file_handle = base::CreatePlatformFile(
+        file_path,
+        base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ |
+            base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_TEMPORARY |
+            base::PLATFORM_FILE_DELETE_ON_CLOSE,
+        NULL,
+        &error);
 
-  if (error != base::PLATFORM_FILE_OK) {
-    PLOG(ERROR) << "Temp file open failed: " << error;
-    return base::kInvalidPlatformFileValue;
+    if (error != base::PLATFORM_FILE_OK) {
+      PLOG(ERROR) << "Temp file open failed: " << error;
+      file_handle = base::kInvalidPlatformFileValue;
+    }
   }
-
-  return file_handle;
+  BrowserThread::PostTask(
+      BrowserThread::IO, FROM_HERE, base::Bind(cb, file_handle));
 }
 
 void PnaclHost::CreateTemporaryFile(TempFileCallback cb) {
-  if (!base::PostTaskAndReplyWithResult(
-          BrowserThread::GetBlockingPool(),
-          FROM_HERE,
-          base::Bind(&PnaclHost::DoCreateTemporaryFile, temp_dir_),
-          cb)) {
+  if (!BrowserThread::PostBlockingPoolSequencedTask(
+           "PnaclHostCreateTempFile",
+           FROM_HERE,
+           base::Bind(&PnaclHost::DoCreateTemporaryFile, temp_dir_, cb))) {
     DCHECK(thread_checker_.CalledOnValidThread());
     cb.Run(base::kInvalidPlatformFileValue);
   }
@@ -311,13 +312,13 @@
   }
 
   if (!base::PostTaskAndReplyWithResult(
-          BrowserThread::GetBlockingPool(),
-          FROM_HERE,
-          base::Bind(
-              &PnaclHost::CopyBufferToFile, pt->nexe_fd, pt->nexe_read_buffer),
-          base::Bind(&PnaclHost::OnBufferCopiedToTempFile,
-                     weak_factory_.GetWeakPtr(),
-                     entry->first))) {
+           BrowserThread::GetBlockingPool(),
+           FROM_HERE,
+           base::Bind(
+               &PnaclHost::CopyBufferToFile, pt->nexe_fd, pt->nexe_read_buffer),
+           base::Bind(&PnaclHost::OnBufferCopiedToTempFile,
+                      weak_factory_.GetWeakPtr(),
+                      entry->first))) {
     pt->callback.Run(base::kInvalidPlatformFileValue, false);
   }
 }
@@ -386,13 +387,13 @@
       !success || entry->second.is_incognito) {
     store_nexe = false;
   } else if (!base::PostTaskAndReplyWithResult(
-                 BrowserThread::GetBlockingPool(),
-                 FROM_HERE,
-                 base::Bind(&PnaclHost::CopyFileToBuffer,
-                            entry->second.nexe_fd),
-                 base::Bind(&PnaclHost::StoreTranslatedNexe,
-                            weak_factory_.GetWeakPtr(),
-                            id))) {
+                  BrowserThread::GetBlockingPool(),
+                  FROM_HERE,
+                  base::Bind(&PnaclHost::CopyFileToBuffer,
+                             entry->second.nexe_fd),
+                  base::Bind(&PnaclHost::StoreTranslatedNexe,
+                             weak_factory_.GetWeakPtr(),
+                             id))) {
     store_nexe = false;
   }
 
diff --git a/chrome/browser/nacl_host/pnacl_host.h b/chrome/browser/nacl_host/pnacl_host.h
index 3c0d2e8..b2f6984 100644
--- a/chrome/browser/nacl_host/pnacl_host.h
+++ b/chrome/browser/nacl_host/pnacl_host.h
@@ -129,7 +129,8 @@
   void InitForTest(base::FilePath temp_dir);
   void OnCacheInitialized(int net_error);
 
-  static base::PlatformFile DoCreateTemporaryFile(base::FilePath temp_dir_);
+  static void DoCreateTemporaryFile(base::FilePath temp_dir_,
+                                    TempFileCallback cb);
 
   // GetNexeFd common steps
   void SendCacheQueryAndTempFileRequest(const std::string& key,
diff --git a/chrome/browser/nacl_host/pnacl_host_unittest.cc b/chrome/browser/nacl_host/pnacl_host_unittest.cc
index e2d529d..0ad9cd5 100644
--- a/chrome/browser/nacl_host/pnacl_host_unittest.cc
+++ b/chrome/browser/nacl_host/pnacl_host_unittest.cc
@@ -195,8 +195,7 @@
   EXPECT_EQ(0U, host_->pending_translations());
 }
 
-// crbug.com/272492; flaky on all platforms.
-TEST_F(PnaclHostTest, DISABLED_OverlappedMissesAfterTempReturn) {
+TEST_F(PnaclHostTest, OverlappedMissesAfterTempReturn) {
   nacl::PnaclCacheInfo info = GetTestCacheInfo();
   GET_NEXE_FD(0, 0, false, info, false);
   FlushQueues();
@@ -216,8 +215,7 @@
   EXPECT_EQ(0U, host_->pending_translations());
 }
 
-// crbug.com/272492; flaky on all platforms.
-TEST_F(PnaclHostTest, DISABLED_OverlappedMissesBeforeTempReturn) {
+TEST_F(PnaclHostTest, OverlappedMissesBeforeTempReturn) {
   nacl::PnaclCacheInfo info = GetTestCacheInfo();
   GET_NEXE_FD(0, 0, false, info, false);
   // Send the 2nd fd request before the first one returns a temp file.
@@ -268,8 +266,7 @@
   EXPECT_EQ(0U, host_->pending_translations());
 }
 
-// crbug.com/272492; flaky on all platforms.
-TEST_F(PnaclHostTest, DISABLED_OverlappedMissesRendererClosing) {
+TEST_F(PnaclHostTest, OverlappedMissesRendererClosing) {
   nacl::PnaclCacheInfo info = GetTestCacheInfo();
   GET_NEXE_FD(0, 0, false, info, false);
   // Send the 2nd fd request from a different renderer.
@@ -307,8 +304,7 @@
   EXPECT_EQ(3, temp_callback_count_);
 }
 
-// crbug.com/272492; flaky on all platforms.
-TEST_F(PnaclHostTest, DISABLED_IncognitoOverlappedMiss) {
+TEST_F(PnaclHostTest, IncognitoOverlappedMiss) {
   nacl::PnaclCacheInfo info = GetTestCacheInfo();
   GET_NEXE_FD(0, 0, true, info, false);
   GET_NEXE_FD(0, 1, false, info, false);
@@ -332,8 +328,7 @@
   host_->RendererClosing(0);
 }
 
-// crbug.com/272492; flaky on all platforms.
-TEST_F(PnaclHostTest, DISABLED_IncognitoSecondOverlappedMiss) {
+TEST_F(PnaclHostTest, IncognitoSecondOverlappedMiss) {
   // If the non-incognito request comes first, it should
   // behave exactly like OverlappedMissBeforeTempReturn
   nacl::PnaclCacheInfo info = GetTestCacheInfo();