Unpack extensions inside chrome's profile directory.

Other users of the temp directory will be altered in a subsequent CL.

BUG=13044
TEST=SandboxedExtensionUnpackerTest.*, ScopedTempDir.UniqueTempDirUnderPath, FileUtilTest.CreateNewTempDirInDirTest, manual testing on win, linux, mac.

Review URL: http://codereview.chromium.org/1582022

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46078 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/file_util.h b/base/file_util.h
index 96891e6..467b4041 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -249,6 +249,13 @@
 bool CreateTemporaryFileInDir(const FilePath& dir,
                               FilePath* temp_file);
 
+// Create a directory within another directory.
+// Extra characters will be appended to |name_tmpl| to ensure that the
+// new directory does not have the same name as an existing directory.
+bool CreateTemporaryDirInDir(const FilePath& base_dir,
+                             const FilePath::StringType& prefix,
+                             FilePath* new_dir);
+
 // Create a new directory under TempPath. If prefix is provided, the new
 // directory name is in the format of prefixyyyy.
 // NOTE: prefix is ignored in the POSIX implementation.
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 4d4e572..d9cbe09 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -401,20 +401,39 @@
   return ((fd >= 0) && !close(fd));
 }
 
+static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
+                                        const FilePath::StringType& name_tmpl,
+                                        FilePath* new_dir) {
+  CHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
+    << "Directory name template must contain \"XXXXXX\".";
+
+  FilePath sub_dir = base_dir.Append(name_tmpl);
+  std::string sub_dir_string = sub_dir.value();
+
+  // this should be OK since mkdtemp just replaces characters in place
+  char* buffer = const_cast<char*>(sub_dir_string.c_str());
+  char* dtemp = mkdtemp(buffer);
+  if (!dtemp)
+    return false;
+  *new_dir = FilePath(dtemp);
+  return true;
+}
+
+bool CreateTemporaryDirInDir(const FilePath& base_dir,
+                             const FilePath::StringType& prefix,
+                             FilePath* new_dir) {
+  FilePath::StringType mkdtemp_template = prefix;
+  mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX"));
+  return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir);
+}
+
 bool CreateNewTempDirectory(const FilePath::StringType& prefix,
                             FilePath* new_temp_path) {
   FilePath tmpdir;
   if (!GetTempDir(&tmpdir))
     return false;
-  tmpdir = tmpdir.Append(kTempFileName);
-  std::string tmpdir_string = tmpdir.value();
-  // this should be OK since mkdtemp just replaces characters in place
-  char* buffer = const_cast<char*>(tmpdir_string.c_str());
-  char* dtemp = mkdtemp(buffer);
-  if (!dtemp)
-    return false;
-  *new_temp_path = FilePath(dtemp);
-  return true;
+
+  return CreateTemporaryDirInDirImpl(tmpdir, kTempFileName, new_temp_path);
 }
 
 bool CreateDirectory(const FilePath& full_path) {
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index c46c39f..25b93e1 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -1141,6 +1141,17 @@
   EXPECT_TRUE(file_util::Delete(temp_dir, false));
 }
 
+TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
+  FilePath new_dir;
+  ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
+                  test_dir_,
+                  FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
+                  &new_dir));
+  EXPECT_TRUE(file_util::PathExists(new_dir));
+  EXPECT_TRUE(test_dir_.IsParent(new_dir));
+  EXPECT_TRUE(file_util::Delete(new_dir, false));
+}
+
 TEST_F(FileUtilTest, GetShmemTempDirTest) {
   FilePath dir;
   EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index d1cdc6ce..445f82e 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -500,12 +500,9 @@
   return true;
 }
 
-bool CreateNewTempDirectory(const FilePath::StringType& prefix,
-                            FilePath* new_temp_path) {
-  FilePath system_temp_dir;
-  if (!GetTempDir(&system_temp_dir))
-    return false;
-
+bool CreateTemporaryDirInDir(const FilePath& base_dir,
+                             const FilePath::StringType& prefix,
+                             FilePath* new_dir) {
   FilePath path_to_create;
   srand(static_cast<uint32>(time(NULL)));
 
@@ -513,12 +510,13 @@
   while (count < 50) {
     // Try create a new temporary directory with random generated name. If
     // the one exists, keep trying another path name until we reach some limit.
-    path_to_create = system_temp_dir;
+    path_to_create = base_dir;
+
     std::wstring new_dir_name;
     new_dir_name.assign(prefix);
     new_dir_name.append(IntToWString(rand() % kint16max));
-    path_to_create = path_to_create.Append(new_dir_name);
 
+    path_to_create = path_to_create.Append(new_dir_name);
     if (::CreateDirectory(path_to_create.value().c_str(), NULL))
       break;
     count++;
@@ -528,10 +526,19 @@
     return false;
   }
 
-  *new_temp_path = path_to_create;
+  *new_dir = path_to_create;
   return true;
 }
 
+bool CreateNewTempDirectory(const FilePath::StringType& prefix,
+                            FilePath* new_temp_path) {
+  FilePath system_temp_dir;
+  if (!GetTempDir(&system_temp_dir))
+    return false;
+
+  return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path);
+}
+
 bool CreateDirectory(const FilePath& full_path) {
   // If the path exists, we've succeeded if it's a directory, failed otherwise.
   const wchar_t* full_path_str = full_path.value().c_str();
diff --git a/base/scoped_temp_dir.cc b/base/scoped_temp_dir.cc
index 07fcfb7..c8ed9c6 100644
--- a/base/scoped_temp_dir.cc
+++ b/base/scoped_temp_dir.cc
@@ -26,6 +26,21 @@
   return true;
 }
 
+bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) {
+  // If |path| does not exist, create it.
+  if (!file_util::CreateDirectory(base_path))
+    return false;
+
+  // Create a new, uniquly named directory under |base_path|.
+  if (!file_util::CreateTemporaryDirInDir(
+          base_path,
+          FILE_PATH_LITERAL("scoped_dir_"),
+          &path_)) {
+    return false;
+  }
+  return true;
+}
+
 bool ScopedTempDir::Set(const FilePath& path) {
   DCHECK(path_.empty());
   if (!file_util::DirectoryExists(path) &&
diff --git a/base/scoped_temp_dir.h b/base/scoped_temp_dir.h
index e9d45b9..a5dca1e 100644
--- a/base/scoped_temp_dir.h
+++ b/base/scoped_temp_dir.h
@@ -25,6 +25,9 @@
   // See file_util::CreateNewTemporaryDirectory.
   bool CreateUniqueTempDir();
 
+  // Creates a unique directory under a given path, and takes ownership of it.
+  bool CreateUniqueTempDirUnderPath(const FilePath& path);
+
   // Takes ownership of directory at |path|, creating it if necessary.
   // Don't call multiple times unless Take() has been called first.
   bool Set(const FilePath& path);
diff --git a/base/scoped_temp_dir_unittest.cc b/base/scoped_temp_dir_unittest.cc
index 72f4d8ce..4be0d07 100644
--- a/base/scoped_temp_dir_unittest.cc
+++ b/base/scoped_temp_dir_unittest.cc
@@ -55,3 +55,21 @@
   }
   EXPECT_FALSE(file_util::DirectoryExists(test_path));
 }
+
+TEST(ScopedTempDir, UniqueTempDirUnderPath) {
+  // Create a path which will contain a unique temp path.
+  FilePath base_path;
+  file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("base_dir"),
+                                    &base_path);
+
+  FilePath test_path;
+  {
+    ScopedTempDir dir;
+    EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
+    test_path = dir.path();
+    EXPECT_TRUE(file_util::DirectoryExists(test_path));
+    EXPECT_TRUE(base_path.IsParent(test_path));
+    EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos);
+  }
+  EXPECT_FALSE(file_util::DirectoryExists(test_path));
+}