self_cleaning_temp_dir.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_INSTALLER_UTIL_SELF_CLEANING_TEMP_DIR_H_ 6#define CHROME_INSTALLER_UTIL_SELF_CLEANING_TEMP_DIR_H_ 7 8#include "base/basictypes.h" 9#include "base/file_path.h" 10#include "base/gtest_prod_util.h" 11 12namespace installer { 13 14// A helper class for managing a temporary directory. In relation to 15// ScopedTempDir, this class additionally cleans up all non-empty parent 16// directories of the temporary directory that are created by an instance. 17class SelfCleaningTempDir { 18 public: 19 typedef FilePath::StringType StringType; 20 21 SelfCleaningTempDir(); 22 23 // Performs a Delete(). 24 ~SelfCleaningTempDir(); 25 26 // Creates a temporary directory named |temp_name| under |parent_dir|, 27 // creating intermediate directories as needed. 28 bool Initialize(const FilePath& parent_dir, const StringType& temp_name); 29 30 // Returns the temporary directory created in Initialize(). 31 const FilePath& path() const { return temp_dir_; } 32 33 // Deletes the temporary directory created in Initialize() and all of its 34 // contents, as well as all empty intermediate directories. Any of these that 35 // cannot be deleted immediately are scheduled for deletion upon reboot. 36 bool Delete(); 37 38 private: 39 static void GetTopDirToCreate(const FilePath& temp_parent_dir, 40 FilePath* base_dir); 41 42 // The topmost directory created. 43 FilePath base_dir_; 44 45 // The temporary directory. 46 FilePath temp_dir_; 47 48 FRIEND_TEST_ALL_PREFIXES(SelfCleaningTempDirTest, TopLevel); 49 FRIEND_TEST_ALL_PREFIXES(SelfCleaningTempDirTest, TopLevelPlusOne); 50 DISALLOW_COPY_AND_ASSIGN(SelfCleaningTempDir); 51}; 52 53} // namespace installer 54 55#endif // CHROME_INSTALLER_UTIL_SELF_CLEANING_TEMP_DIR_H_ 56