diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-19 23:34:17 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-19 23:34:17 +0000 |
commit | e078590661444968a2c49a3e5464413714471dca (patch) | |
tree | 13a4dbe4dedccc8057361bd86c89e065390247fc /base/memory | |
parent | d9f76627b14170b95639619089139b6f0493ae8c (diff) | |
download | chromium_src-e078590661444968a2c49a3e5464413714471dca.zip chromium_src-e078590661444968a2c49a3e5464413714471dca.tar.gz chromium_src-e078590661444968a2c49a3e5464413714471dca.tar.bz2 |
Move scoped_temp_dir and scoped_native_library back from base/memory to base.
It looks like they got moved accidentally in http://codereview.chromium.org/6714032
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/7048007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86010 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/memory')
-rw-r--r-- | base/memory/scoped_native_library.cc | 44 | ||||
-rw-r--r-- | base/memory/scoped_native_library.h | 53 | ||||
-rw-r--r-- | base/memory/scoped_native_library_unittest.cc | 33 | ||||
-rw-r--r-- | base/memory/scoped_temp_dir.cc | 84 | ||||
-rw-r--r-- | base/memory/scoped_temp_dir.h | 59 | ||||
-rw-r--r-- | base/memory/scoped_temp_dir_unittest.cc | 110 |
6 files changed, 0 insertions, 383 deletions
diff --git a/base/memory/scoped_native_library.cc b/base/memory/scoped_native_library.cc deleted file mode 100644 index c1e6afc..0000000 --- a/base/memory/scoped_native_library.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/memory/scoped_native_library.h" - -namespace base { - -ScopedNativeLibrary::ScopedNativeLibrary() : library_(NULL) { -} - -ScopedNativeLibrary::ScopedNativeLibrary(NativeLibrary library) - : library_(library) { -} - -ScopedNativeLibrary::ScopedNativeLibrary(const FilePath& library_path) { - library_ = base::LoadNativeLibrary(library_path, NULL); -} - -ScopedNativeLibrary::~ScopedNativeLibrary() { - if (library_) - base::UnloadNativeLibrary(library_); -} - -void* ScopedNativeLibrary::GetFunctionPointer( - const char* function_name) const { - if (!library_) - return NULL; - return base::GetFunctionPointerFromNativeLibrary(library_, function_name); -} - -void ScopedNativeLibrary::Reset(NativeLibrary library) { - if (library_) - base::UnloadNativeLibrary(library_); - library_ = library; -} - -NativeLibrary ScopedNativeLibrary::Release() { - NativeLibrary result = library_; - library_ = NULL; - return result; -} - -} // namespace base diff --git a/base/memory/scoped_native_library.h b/base/memory/scoped_native_library.h deleted file mode 100644 index 56116b9..0000000 --- a/base/memory/scoped_native_library.h +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BASE_MEMORY_SCOPED_NATIVE_LIBRARY_H_ -#define BASE_MEMORY_SCOPED_NATIVE_LIBRARY_H_ -#pragma once - -#include "base/base_api.h" -#include "base/native_library.h" - -class FilePath; - -namespace base { - -// A class which encapsulates a base::NativeLibrary object available only in a -// scope. -// This class automatically unloads the loaded library in its destructor. -class BASE_API ScopedNativeLibrary { - public: - // Initializes with a NULL library. - ScopedNativeLibrary(); - - // Takes ownership of the given library handle. - explicit ScopedNativeLibrary(NativeLibrary library); - - // Opens the given library and manages its lifetime. - explicit ScopedNativeLibrary(const FilePath& library_path); - - ~ScopedNativeLibrary(); - - // Returns true if there's a valid library loaded. - bool is_valid() const { return !!library_; } - - void* GetFunctionPointer(const char* function_name) const; - - // Takes ownership of the given library handle. Any existing handle will - // be freed. - void Reset(NativeLibrary library); - - // Returns the native library handle and removes it from this object. The - // caller must manage the lifetime of the handle. - NativeLibrary Release(); - - private: - NativeLibrary library_; - - DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary); -}; - -} // namespace base - -#endif // BASE_MEMORY_SCOPED_NATIVE_LIBRARY_H_ diff --git a/base/memory/scoped_native_library_unittest.cc b/base/memory/scoped_native_library_unittest.cc deleted file mode 100644 index 0cc60e2..0000000 --- a/base/memory/scoped_native_library_unittest.cc +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/memory/scoped_native_library.h" -#if defined(OS_WIN) -#include "base/file_path.h" -#endif - -#include "testing/gtest/include/gtest/gtest.h" - -// Tests whether or not a function pointer retrieved via ScopedNativeLibrary -// is available only in a scope. -TEST(ScopedNativeLibrary, Basic) { -#if defined(OS_WIN) - // Get the pointer to DirectDrawCreate() from "ddraw.dll" and verify it - // is valid only in this scope. - // FreeLibrary() doesn't actually unload a DLL until its reference count - // becomes zero, i.e. this function pointer is still valid if the DLL used - // in this test is also used by another part of this executable. - // So, this test uses "ddraw.dll", which is not used by Chrome at all but - // installed on all versions of Windows. - FARPROC test_function; - { - FilePath path(base::GetNativeLibraryName(L"ddraw")); - base::ScopedNativeLibrary library(path); - test_function = reinterpret_cast<FARPROC>( - library.GetFunctionPointer("DirectDrawCreate")); - EXPECT_EQ(0, IsBadCodePtr(test_function)); - } - EXPECT_NE(0, IsBadCodePtr(test_function)); -#endif -} diff --git a/base/memory/scoped_temp_dir.cc b/base/memory/scoped_temp_dir.cc deleted file mode 100644 index f7db15d..0000000 --- a/base/memory/scoped_temp_dir.cc +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/memory/scoped_temp_dir.h" - -#include "base/file_util.h" -#include "base/logging.h" - -ScopedTempDir::ScopedTempDir() { -} - -ScopedTempDir::~ScopedTempDir() { - if (!path_.empty() && !Delete()) - LOG(WARNING) << "Could not delete temp dir in dtor."; -} - -bool ScopedTempDir::CreateUniqueTempDir() { - if (!path_.empty()) - return false; - - // This "scoped_dir" prefix is only used on Windows and serves as a template - // for the unique name. - if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), - &path_)) - return false; - - return true; -} - -bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) { - if (!path_.empty()) - return false; - - // If |base_path| does not exist, create it. - if (!file_util::CreateDirectory(base_path)) - return false; - - // Create a new, uniquely named directory under |base_path|. - if (!file_util::CreateTemporaryDirInDir( - base_path, - FILE_PATH_LITERAL("scoped_dir_"), - &path_)) - return false; - - return true; -} - -bool ScopedTempDir::Set(const FilePath& path) { - if (!path_.empty()) - return false; - - if (!file_util::DirectoryExists(path) && - !file_util::CreateDirectory(path)) - return false; - - path_ = path; - return true; -} - -bool ScopedTempDir::Delete() { - if (path_.empty()) - return false; - - bool ret = file_util::Delete(path_, true); - if (ret) { - // We only clear the path if deleted the directory. - path_.clear(); - } else { - LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value(); - } - - return ret; -} - -FilePath ScopedTempDir::Take() { - FilePath ret = path_; - path_ = FilePath(); - return ret; -} - -bool ScopedTempDir::IsValid() const { - return !path_.empty() && file_util::DirectoryExists(path_); -} diff --git a/base/memory/scoped_temp_dir.h b/base/memory/scoped_temp_dir.h deleted file mode 100644 index 4c0a73f..0000000 --- a/base/memory/scoped_temp_dir.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BASE_MEMORY_SCOPED_TEMP_DIR_H_ -#define BASE_MEMORY_SCOPED_TEMP_DIR_H_ -#pragma once - -// An object representing a temporary / scratch directory that should be cleaned -// up (recursively) when this object goes out of scope. Note that since -// deletion occurs during the destructor, no further error handling is possible -// if the directory fails to be deleted. As a result, deletion is not -// guaranteed by this class. -// -// Multiple calls to the methods which establish a temporary directory -// (CreateUniqueTempDir, CreateUniqueTempDirUnderPath, and Set) must have -// intervening calls to Delete or Take, or the calls will fail. - -#include "base/base_api.h" -#include "base/file_path.h" - -class BASE_API ScopedTempDir { - public: - // No directory is owned/created initially. - ScopedTempDir(); - - // Recursively delete path. - ~ScopedTempDir(); - - // Creates a unique directory in TempPath, and takes ownership of it. - // See file_util::CreateNewTemporaryDirectory. - bool CreateUniqueTempDir() WARN_UNUSED_RESULT; - - // Creates a unique directory under a given path, and takes ownership of it. - bool CreateUniqueTempDirUnderPath(const FilePath& path) WARN_UNUSED_RESULT; - - // Takes ownership of directory at |path|, creating it if necessary. - // Don't call multiple times unless Take() has been called first. - bool Set(const FilePath& path) WARN_UNUSED_RESULT; - - // Deletes the temporary directory wrapped by this object. - bool Delete() WARN_UNUSED_RESULT; - - // Caller takes ownership of the temporary directory so it won't be destroyed - // when this object goes out of scope. - FilePath Take(); - - const FilePath& path() const { return path_; } - - // Returns true if path_ is non-empty and exists. - bool IsValid() const; - - private: - FilePath path_; - - DISALLOW_COPY_AND_ASSIGN(ScopedTempDir); -}; - -#endif // BASE_MEMORY_SCOPED_TEMP_DIR_H_ diff --git a/base/memory/scoped_temp_dir_unittest.cc b/base/memory/scoped_temp_dir_unittest.cc deleted file mode 100644 index a83856f..0000000 --- a/base/memory/scoped_temp_dir_unittest.cc +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/file_util.h" -#include "base/memory/scoped_temp_dir.h" -#include "base/platform_file.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(ScopedTempDir, FullPath) { - FilePath test_path; - file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"), - &test_path); - - // Against an existing dir, it should get destroyed when leaving scope. - EXPECT_TRUE(file_util::DirectoryExists(test_path)); - { - ScopedTempDir dir; - EXPECT_TRUE(dir.Set(test_path)); - EXPECT_TRUE(dir.IsValid()); - } - EXPECT_FALSE(file_util::DirectoryExists(test_path)); - - { - ScopedTempDir dir; - EXPECT_TRUE(dir.Set(test_path)); - // Now the dir doesn't exist, so ensure that it gets created. - EXPECT_TRUE(file_util::DirectoryExists(test_path)); - // When we call Release(), it shouldn't get destroyed when leaving scope. - FilePath path = dir.Take(); - EXPECT_EQ(path.value(), test_path.value()); - EXPECT_FALSE(dir.IsValid()); - } - EXPECT_TRUE(file_util::DirectoryExists(test_path)); - - // Clean up. - { - ScopedTempDir dir; - EXPECT_TRUE(dir.Set(test_path)); - } - EXPECT_FALSE(file_util::DirectoryExists(test_path)); -} - -TEST(ScopedTempDir, TempDir) { - // In this case, just verify that a directory was created and that it's a - // child of TempDir. - FilePath test_path; - { - ScopedTempDir dir; - EXPECT_TRUE(dir.CreateUniqueTempDir()); - test_path = dir.path(); - EXPECT_TRUE(file_util::DirectoryExists(test_path)); - FilePath tmp_dir; - EXPECT_TRUE(file_util::GetTempDir(&tmp_dir)); - EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos); - } - EXPECT_FALSE(file_util::DirectoryExists(test_path)); -} - -TEST(ScopedTempDir, UniqueTempDirUnderPath) { - // Create a path which will contain a unique temp path. - FilePath base_path; - file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("base_dir"), - &base_path); - - FilePath test_path; - { - ScopedTempDir dir; - EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path)); - test_path = dir.path(); - EXPECT_TRUE(file_util::DirectoryExists(test_path)); - EXPECT_TRUE(base_path.IsParent(test_path)); - EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos); - } - EXPECT_FALSE(file_util::DirectoryExists(test_path)); -} - -TEST(ScopedTempDir, MultipleInvocations) { - ScopedTempDir dir; - EXPECT_TRUE(dir.CreateUniqueTempDir()); - EXPECT_FALSE(dir.CreateUniqueTempDir()); - EXPECT_TRUE(dir.Delete()); - EXPECT_TRUE(dir.CreateUniqueTempDir()); - EXPECT_FALSE(dir.CreateUniqueTempDir()); - ScopedTempDir other_dir; - EXPECT_TRUE(other_dir.Set(dir.Take())); - EXPECT_TRUE(dir.CreateUniqueTempDir()); - EXPECT_FALSE(dir.CreateUniqueTempDir()); - EXPECT_FALSE(other_dir.CreateUniqueTempDir()); -} - -#if defined(OS_WIN) -TEST(ScopedTempDir, LockedTempDir) { - ScopedTempDir dir; - EXPECT_TRUE(dir.CreateUniqueTempDir()); - int file_flags = base::PLATFORM_FILE_CREATE_ALWAYS | - base::PLATFORM_FILE_WRITE; - base::PlatformFileError error_code = base::PLATFORM_FILE_OK; - FilePath file_path(dir.path().Append(FILE_PATH_LITERAL("temp"))); - base::PlatformFile file = base::CreatePlatformFile(file_path, file_flags, - NULL, &error_code); - EXPECT_NE(base::kInvalidPlatformFileValue, file); - EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); - EXPECT_FALSE(dir.Delete()); // We should not be able to delete. - EXPECT_FALSE(dir.path().empty()); // We should still have a valid path. - EXPECT_TRUE(base::ClosePlatformFile(file)); - // Now, we should be able to delete. - EXPECT_TRUE(dir.Delete()); -} -#endif // defined(OS_WIN) |