blob: 57703e59e3d9ac4ab715d801777f8f623ffe3382 [file] [log] [blame]
slanef83eab2015-12-09 00:59:311// Copyright 2015 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 CHROMECAST_BASE_SCOPED_TEMP_FILE_H_
6#define CHROMECAST_BASE_SCOPED_TEMP_FILE_H_
7
8#include <string>
9
10#include "base/files/file_path.h"
11#include "base/macros.h"
12
13namespace chromecast {
14
15// Creates a temporary file that is deleted when this object is destroyed,
16// unless the underlying file has been moved or deleted.
17// Warning: This class uses CHECKs, and should only be used for testing.
18class ScopedTempFile {
19 public:
20 ScopedTempFile();
21 ~ScopedTempFile();
22
23 // Return the path to the temporary file. Note that if the underlying file has
24 // been moved or deleted, this will still return the original path.
25 base::FilePath path() const { return path_; }
26
27 // Returns true if the underlying file exists, false otherwise. This will
28 // return false, for example, if the file has been moved or deleted.
29 bool FileExists() const;
30
31 // Write the contents of |str| to the file. Return the number of characters
32 // written, or -1 on error. CHECKs that FileExists() returns true.
33 int Write(const std::string& str);
34
35 // Read the file and return the contents. CHECKs that FileExists() returns
36 // true.
37 std::string Read() const;
38
39 private:
40 base::FilePath path_;
41
42 DISALLOW_COPY_AND_ASSIGN(ScopedTempFile);
43};
44
45} // namespace chromecast
46
47#endif // CHROMECAST_BASE_SCOPED_TEMP_FILE_H_