OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/shareable_file_reference.h" | 5 #include "webkit/blob/shareable_file_reference.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
| 8 |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/file_util_proxy.h" | 10 #include "base/file_util_proxy.h" |
10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
11 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
| 13 #include "base/threading/thread_checker.h" |
12 | 14 |
13 namespace webkit_blob { | 15 namespace webkit_blob { |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 typedef std::map<FilePath, ShareableFileReference*> ShareableFileMap; | 19 // A shareable file map with enforcement of thread checker. |
| 20 // This map may get deleted on a different thread in AtExitManager at the |
| 21 // very end on the main thread (at the point all other threads must be |
| 22 // terminated), so we use ThreadChecker rather than NonThreadSafe and do not |
| 23 // check thread in the dtor. |
| 24 class ShareableFileMap { |
| 25 public: |
| 26 typedef std::map<FilePath, ShareableFileReference*> FileMap; |
| 27 typedef FileMap::iterator iterator; |
| 28 typedef FileMap::key_type key_type; |
| 29 typedef FileMap::value_type value_type; |
| 30 |
| 31 ShareableFileMap() {} |
| 32 |
| 33 iterator Find(key_type key) { |
| 34 DCHECK(CalledOnValidThread()); |
| 35 return file_map_.find(key); |
| 36 } |
| 37 |
| 38 iterator End() { |
| 39 DCHECK(CalledOnValidThread()); |
| 40 return file_map_.end(); |
| 41 } |
| 42 |
| 43 std::pair<iterator, bool> Insert(value_type value) { |
| 44 DCHECK(CalledOnValidThread()); |
| 45 return file_map_.insert(value); |
| 46 } |
| 47 |
| 48 void Erase(key_type key) { |
| 49 DCHECK(CalledOnValidThread()); |
| 50 file_map_.erase(key); |
| 51 } |
| 52 |
| 53 bool CalledOnValidThread() const { |
| 54 return thread_checker_.CalledOnValidThread(); |
| 55 } |
| 56 |
| 57 private: |
| 58 FileMap file_map_; |
| 59 base::ThreadChecker thread_checker_; |
| 60 DISALLOW_COPY_AND_ASSIGN(ShareableFileMap); |
| 61 }; |
| 62 |
18 base::LazyInstance<ShareableFileMap> g_file_map = LAZY_INSTANCE_INITIALIZER; | 63 base::LazyInstance<ShareableFileMap> g_file_map = LAZY_INSTANCE_INITIALIZER; |
19 | 64 |
20 } // namespace | 65 } // namespace |
21 | 66 |
22 // static | 67 // static |
23 scoped_refptr<ShareableFileReference> ShareableFileReference::Get( | 68 scoped_refptr<ShareableFileReference> ShareableFileReference::Get( |
24 const FilePath& path) { | 69 const FilePath& path) { |
25 ShareableFileMap::iterator found = g_file_map.Get().find(path); | 70 ShareableFileMap::iterator found = g_file_map.Get().Find(path); |
26 ShareableFileReference* reference = | 71 ShareableFileReference* reference = |
27 (found == g_file_map.Get().end()) ? NULL : found->second; | 72 (found == g_file_map.Get().End()) ? NULL : found->second; |
28 return scoped_refptr<ShareableFileReference>(reference); | 73 return scoped_refptr<ShareableFileReference>(reference); |
29 } | 74 } |
30 | 75 |
31 // static | 76 // static |
32 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( | 77 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( |
33 const FilePath& path, FinalReleasePolicy policy, | 78 const FilePath& path, FinalReleasePolicy policy, |
34 base::TaskRunner* file_task_runner) { | 79 base::TaskRunner* file_task_runner) { |
35 DCHECK(file_task_runner); | 80 DCHECK(file_task_runner); |
36 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; | 81 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; |
37 | 82 |
38 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det
ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | 83 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det
ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair |
39 webkit_blob::ShareableFileReference* null_reference = NULL; | 84 webkit_blob::ShareableFileReference* null_reference = NULL; |
40 InsertResult result = g_file_map.Get().insert( | 85 InsertResult result = g_file_map.Get().Insert( |
41 ShareableFileMap::value_type(path, null_reference)); | 86 ShareableFileMap::value_type(path, null_reference)); |
42 if (result.second == false) | 87 if (result.second == false) |
43 return scoped_refptr<ShareableFileReference>(result.first->second); | 88 return scoped_refptr<ShareableFileReference>(result.first->second); |
44 | 89 |
45 // Wasn't in the map, create a new reference and store the pointer. | 90 // Wasn't in the map, create a new reference and store the pointer. |
46 scoped_refptr<ShareableFileReference> reference( | 91 scoped_refptr<ShareableFileReference> reference( |
47 new ShareableFileReference(path, policy, file_task_runner)); | 92 new ShareableFileReference(path, policy, file_task_runner)); |
48 result.first->second = reference.get(); | 93 result.first->second = reference.get(); |
49 return reference; | 94 return reference; |
50 } | 95 } |
51 | 96 |
52 void ShareableFileReference::AddFinalReleaseCallback( | 97 void ShareableFileReference::AddFinalReleaseCallback( |
53 const FinalReleaseCallback& callback) { | 98 const FinalReleaseCallback& callback) { |
| 99 DCHECK(g_file_map.Get().CalledOnValidThread()); |
54 final_release_callbacks_.push_back(callback); | 100 final_release_callbacks_.push_back(callback); |
55 } | 101 } |
56 | 102 |
57 ShareableFileReference::ShareableFileReference( | 103 ShareableFileReference::ShareableFileReference( |
58 const FilePath& path, FinalReleasePolicy policy, | 104 const FilePath& path, FinalReleasePolicy policy, |
59 base::TaskRunner* file_task_runner) | 105 base::TaskRunner* file_task_runner) |
60 : path_(path), | 106 : path_(path), |
61 final_release_policy_(policy), | 107 final_release_policy_(policy), |
62 file_task_runner_(file_task_runner) { | 108 file_task_runner_(file_task_runner) { |
63 DCHECK(g_file_map.Get().find(path_)->second == NULL); | 109 DCHECK(g_file_map.Get().Find(path_)->second == NULL); |
64 } | 110 } |
65 | 111 |
66 ShareableFileReference::~ShareableFileReference() { | 112 ShareableFileReference::~ShareableFileReference() { |
67 DCHECK(g_file_map.Get().find(path_)->second == this); | 113 DCHECK(g_file_map.Get().Find(path_)->second == this); |
68 g_file_map.Get().erase(path_); | 114 g_file_map.Get().Erase(path_); |
69 | 115 |
70 for (size_t i = 0; i < final_release_callbacks_.size(); i++) | 116 for (size_t i = 0; i < final_release_callbacks_.size(); i++) |
71 final_release_callbacks_[i].Run(path_); | 117 final_release_callbacks_[i].Run(path_); |
72 | 118 |
73 if (final_release_policy_ == DELETE_ON_FINAL_RELEASE) { | 119 if (final_release_policy_ == DELETE_ON_FINAL_RELEASE) { |
74 base::FileUtilProxy::Delete(file_task_runner_, path_, false /* recursive */, | 120 base::FileUtilProxy::Delete(file_task_runner_, path_, false /* recursive */, |
75 base::FileUtilProxy::StatusCallback()); | 121 base::FileUtilProxy::StatusCallback()); |
76 } | 122 } |
77 } | 123 } |
78 | 124 |
79 } // namespace webkit_blob | 125 } // namespace webkit_blob |
OLD | NEW |