1 // Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright 2 // 2011 The Chromium Authors. All rights reserved. Use of this source code is 3 // governed by a BSD-style license that can be found in the LICENSE file. 4 5 #include "include/wrapper/cef_scoped_temp_dir.h" 6 7 #include "include/base/cef_logging.h" 8 #include "include/cef_file_util.h" 9 CefScopedTempDir()10CefScopedTempDir::CefScopedTempDir() {} 11 ~CefScopedTempDir()12CefScopedTempDir::~CefScopedTempDir() { 13 if (!path_.empty() && !Delete()) 14 DLOG(WARNING) << "Could not delete temp dir in dtor."; 15 } 16 CreateUniqueTempDir()17bool CefScopedTempDir::CreateUniqueTempDir() { 18 if (!path_.empty()) 19 return false; 20 21 // This "scoped_dir" prefix is only used on Windows and serves as a template 22 // for the unique name. 23 if (!CefCreateNewTempDirectory("scoped_dir", path_)) 24 return false; 25 26 return true; 27 } 28 CreateUniqueTempDirUnderPath(const CefString & base_path)29bool CefScopedTempDir::CreateUniqueTempDirUnderPath( 30 const CefString& base_path) { 31 if (!path_.empty()) 32 return false; 33 34 // If |base_path| does not exist, create it. 35 if (!CefCreateDirectory(base_path)) 36 return false; 37 38 // Create a new, uniquely named directory under |base_path|. 39 if (!CefCreateTempDirectoryInDirectory(base_path, "scoped_dir_", path_)) 40 return false; 41 42 return true; 43 } 44 Set(const CefString & path)45bool CefScopedTempDir::Set(const CefString& path) { 46 if (!path_.empty()) 47 return false; 48 49 if (!CefDirectoryExists(path) && !CefCreateDirectory(path)) 50 return false; 51 52 path_ = path; 53 return true; 54 } 55 Delete()56bool CefScopedTempDir::Delete() { 57 if (path_.empty()) 58 return false; 59 60 bool ret = CefDeleteFile(path_, true); 61 if (ret) { 62 // We only clear the path if deleted the directory. 63 path_.clear(); 64 } 65 66 return ret; 67 } 68 Take()69CefString CefScopedTempDir::Take() { 70 CefString ret = path_; 71 path_.clear(); 72 return ret; 73 } 74 GetPath() const75const CefString& CefScopedTempDir::GetPath() const { 76 DCHECK(!path_.empty()) << "Did you call CreateUniqueTempDir* before?"; 77 return path_; 78 } 79 IsEmpty() const80bool CefScopedTempDir::IsEmpty() const { 81 return path_.empty(); 82 } 83 IsValid() const84bool CefScopedTempDir::IsValid() const { 85 return !path_.empty() && CefDirectoryExists(path_); 86 } 87