Remove all npapi plugins from windows metro chrome
[chromium-blink-merge.git] / base / scoped_temp_dir.cc
blob9f4bcf40c6db07f5174675087b51916c09246f52
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.
5 #include "base/scoped_temp_dir.h"
7 #include "base/file_util.h"
8 #include "base/logging.h"
10 ScopedTempDir::ScopedTempDir() {
13 ScopedTempDir::~ScopedTempDir() {
14 if (!path_.empty() && !Delete())
15 DLOG(WARNING) << "Could not delete temp dir in dtor.";
18 bool ScopedTempDir::CreateUniqueTempDir() {
19 if (!path_.empty())
20 return false;
22 // This "scoped_dir" prefix is only used on Windows and serves as a template
23 // for the unique name.
24 if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"),
25 &path_))
26 return false;
28 return true;
31 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) {
32 if (!path_.empty())
33 return false;
35 // If |base_path| does not exist, create it.
36 if (!file_util::CreateDirectory(base_path))
37 return false;
39 // Create a new, uniquely named directory under |base_path|.
40 if (!file_util::CreateTemporaryDirInDir(
41 base_path,
42 FILE_PATH_LITERAL("scoped_dir_"),
43 &path_))
44 return false;
46 return true;
49 bool ScopedTempDir::Set(const FilePath& path) {
50 if (!path_.empty())
51 return false;
53 if (!file_util::DirectoryExists(path) &&
54 !file_util::CreateDirectory(path))
55 return false;
57 path_ = path;
58 return true;
61 bool ScopedTempDir::Delete() {
62 if (path_.empty())
63 return false;
65 bool ret = file_util::Delete(path_, true);
66 if (ret) {
67 // We only clear the path if deleted the directory.
68 path_.clear();
71 return ret;
74 FilePath ScopedTempDir::Take() {
75 FilePath ret = path_;
76 path_ = FilePath();
77 return ret;
80 bool ScopedTempDir::IsValid() const {
81 return !path_.empty() && file_util::DirectoryExists(path_);