OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 |
| 5 #include "webkit/blob/deletable_file_reference.h" |
| 6 |
| 7 #include <map> |
| 8 #include "base/file_util.h" |
| 9 #include "base/file_util_proxy.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/singleton.h" |
| 12 |
| 13 namespace webkit_blob { |
| 14 |
| 15 namespace { |
| 16 |
| 17 typedef std::map<FilePath, DeletableFileReference*> DeleteableFileMap; |
| 18 |
| 19 DeleteableFileMap* map() { |
| 20 return Singleton<DeleteableFileMap>::get(); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 // static |
| 26 scoped_refptr<DeletableFileReference> DeletableFileReference::Get( |
| 27 const FilePath& path) { |
| 28 DeleteableFileMap::iterator found = map()->find(path); |
| 29 DeletableFileReference* reference = |
| 30 (found == map()->end()) ? NULL : found->second; |
| 31 return scoped_refptr<DeletableFileReference>(reference); |
| 32 } |
| 33 |
| 34 // static |
| 35 scoped_refptr<DeletableFileReference> DeletableFileReference::GetOrCreate( |
| 36 const FilePath& path, base::MessageLoopProxy* file_thread) { |
| 37 DCHECK(file_thread); |
| 38 typedef std::pair<DeleteableFileMap::iterator, bool> InsertResult; |
| 39 InsertResult result = map()->insert( |
| 40 DeleteableFileMap::value_type(path, NULL)); |
| 41 if (result.second == false) |
| 42 return scoped_refptr<DeletableFileReference>(result.first->second); |
| 43 |
| 44 // Wasn't in the map, create a new reference and store the pointer. |
| 45 scoped_refptr<DeletableFileReference> reference = |
| 46 new DeletableFileReference(path, file_thread); |
| 47 result.first->second = reference.get(); |
| 48 return reference; |
| 49 } |
| 50 |
| 51 DeletableFileReference::DeletableFileReference( |
| 52 const FilePath& path, base::MessageLoopProxy* file_thread) |
| 53 : path_(path), file_thread_(file_thread) { |
| 54 DCHECK(map()->find(path_)->second == NULL); |
| 55 } |
| 56 |
| 57 DeletableFileReference::~DeletableFileReference() { |
| 58 DCHECK(map()->find(path_)->second == this); |
| 59 map()->erase(path_); |
| 60 base::FileUtilProxy::Delete(file_thread_, path_, NULL); |
| 61 } |
| 62 |
| 63 } // namespace webkit_blob |
OLD | NEW |