diff options
author | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-29 23:00:00 +0000 |
---|---|---|
committer | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-29 23:00:00 +0000 |
commit | a5230508efeea42e05005f43f9ffa140fbcc64c0 (patch) | |
tree | 89b4aedc784c9b0b1c93b757b35631e0fd06a884 /webkit/blob/shareable_file_reference.cc | |
parent | 55df9099f9350d906d7b03a4390687ac95269210 (diff) | |
download | chromium_src-a5230508efeea42e05005f43f9ffa140fbcc64c0.zip chromium_src-a5230508efeea42e05005f43f9ffa140fbcc64c0.tar.gz chromium_src-a5230508efeea42e05005f43f9ffa140fbcc64c0.tar.bz2 |
Modify DeletableFileReference to not necessarily delete after all. Renamed the class to ShareableFileReference and fixed up the current callers with the new name and gypi files.
BUG=115603
Review URL: https://chromiumcodereview.appspot.com/9466025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124276 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/blob/shareable_file_reference.cc')
-rw-r--r-- | webkit/blob/shareable_file_reference.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/webkit/blob/shareable_file_reference.cc b/webkit/blob/shareable_file_reference.cc new file mode 100644 index 0000000..b871624 --- /dev/null +++ b/webkit/blob/shareable_file_reference.cc @@ -0,0 +1,77 @@ +// 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 "webkit/blob/shareable_file_reference.h" + +#include <map> +#include "base/file_util.h" +#include "base/file_util_proxy.h" +#include "base/lazy_instance.h" +#include "base/message_loop_proxy.h" + +namespace webkit_blob { + +namespace { + +typedef std::map<FilePath, ShareableFileReference*> ShareableFileMap; +base::LazyInstance<ShareableFileMap> g_file_map = LAZY_INSTANCE_INITIALIZER; + +} // namespace + +// static +scoped_refptr<ShareableFileReference> ShareableFileReference::Get( + const FilePath& path) { + ShareableFileMap::iterator found = g_file_map.Get().find(path); + ShareableFileReference* reference = + (found == g_file_map.Get().end()) ? NULL : found->second; + return scoped_refptr<ShareableFileReference>(reference); +} + +// static +scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( + const FilePath& path, FinalReleasePolicy policy, + base::MessageLoopProxy* file_thread) { + DCHECK(file_thread); + typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; + + // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair + webkit_blob::ShareableFileReference* null_reference = NULL; + InsertResult result = g_file_map.Get().insert( + ShareableFileMap::value_type(path, null_reference)); + if (result.second == false) + return scoped_refptr<ShareableFileReference>(result.first->second); + + // Wasn't in the map, create a new reference and store the pointer. + scoped_refptr<ShareableFileReference> reference( + new ShareableFileReference(path, policy, file_thread)); + result.first->second = reference.get(); + return reference; +} + +void ShareableFileReference::AddFinalReleaseCallback( + const FinalReleaseCallback& callback) { + final_release_callbacks_.push_back(callback); +} + +ShareableFileReference::ShareableFileReference( + const FilePath& path, FinalReleasePolicy policy, + base::MessageLoopProxy* file_thread) + : path_(path), final_release_policy_(policy), file_thread_(file_thread) { + DCHECK(g_file_map.Get().find(path_)->second == NULL); +} + +ShareableFileReference::~ShareableFileReference() { + DCHECK(g_file_map.Get().find(path_)->second == this); + g_file_map.Get().erase(path_); + + for (size_t i = 0; i < final_release_callbacks_.size(); i++) + final_release_callbacks_[i].Run(path_); + + if (final_release_policy_ == DELETE_ON_FINAL_RELEASE) { + base::FileUtilProxy::Delete(file_thread_, path_, false /* recursive */, + base::FileUtilProxy::StatusCallback()); + } +} + +} // namespace webkit_blob |