1 // Copyright 2017 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_BROWSER_CHROMEOS_SMB_CLIENT_TEMP_FILE_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_SMB_CLIENT_TEMP_FILE_MANAGER_H_
7 
8 #include <vector>
9 
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 
14 namespace chromeos {
15 namespace smb_client {
16 
17 // Helper class to handle construction and deletion of a temporary directory
18 // containing temporary files.
19 class TempFileManager {
20  public:
21   TempFileManager();
22   ~TempFileManager();
23 
24   // Returns the path of the temporary directory.
25   const base::FilePath& GetTempDirectoryPath() const;
26 
27   // Creates a temporary file in temp_dir_ path and writes in |data|. Before
28   // returning, this seeks to the beginning of the file in preparation for
29   // reading. This also calls unlink() on the created file. Returns a file
30   // descriptor which will be invalid on failure.
31   base::ScopedFD CreateTempFile(const std::vector<uint8_t>& data);
32 
33  private:
34   // Creates a temporary file in temp_dir_ path. This also calls unlink() on the
35   // created file. Returns a file descriptor which will be invalid on failure.
36   base::ScopedFD CreateTempFile();
37 
38   // Scoped class that handles deletion of the temporary directory.
39   base::ScopedTempDir temp_dir_;
40 
41   DISALLOW_COPY_AND_ASSIGN(TempFileManager);
42 };
43 
44 }  // namespace smb_client
45 }  // namespace chromeos
46 
47 #endif  // CHROME_BROWSER_CHROMEOS_SMB_CLIENT_TEMP_FILE_MANAGER_H_
48