scoped_temp_file.cc 871 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // 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. #include "chromecast/base/scoped_temp_file.h"
  5. #include "base/check.h"
  6. #include "base/files/file_util.h"
  7. namespace chromecast {
  8. ScopedTempFile::ScopedTempFile() {
  9. CHECK(base::CreateTemporaryFile(&path_));
  10. }
  11. ScopedTempFile::~ScopedTempFile() {
  12. if (FileExists()) {
  13. CHECK(base::DeleteFile(path_));
  14. }
  15. }
  16. bool ScopedTempFile::FileExists() const {
  17. return base::PathExists(path_);
  18. }
  19. int ScopedTempFile::Write(const std::string& str) {
  20. CHECK(FileExists());
  21. return base::WriteFile(path_, str.c_str(), str.size());
  22. }
  23. std::string ScopedTempFile::Read() const {
  24. CHECK(FileExists());
  25. std::string result;
  26. CHECK(ReadFileToString(path_, &result));
  27. return result;
  28. }
  29. } // namespace chromecast