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