OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #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/lazy_instance.h" |
11 #include "base/message_loop_proxy.h" | 11 #include "base/task_runner.h" |
12 | 12 |
13 namespace webkit_blob { | 13 namespace webkit_blob { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 typedef std::map<FilePath, ShareableFileReference*> ShareableFileMap; | 17 typedef std::map<FilePath, ShareableFileReference*> ShareableFileMap; |
18 base::LazyInstance<ShareableFileMap> g_file_map = LAZY_INSTANCE_INITIALIZER; | 18 base::LazyInstance<ShareableFileMap> g_file_map = LAZY_INSTANCE_INITIALIZER; |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 // static | 22 // static |
23 scoped_refptr<ShareableFileReference> ShareableFileReference::Get( | 23 scoped_refptr<ShareableFileReference> ShareableFileReference::Get( |
24 const FilePath& path) { | 24 const FilePath& path) { |
25 ShareableFileMap::iterator found = g_file_map.Get().find(path); | 25 ShareableFileMap::iterator found = g_file_map.Get().find(path); |
26 ShareableFileReference* reference = | 26 ShareableFileReference* reference = |
27 (found == g_file_map.Get().end()) ? NULL : found->second; | 27 (found == g_file_map.Get().end()) ? NULL : found->second; |
28 return scoped_refptr<ShareableFileReference>(reference); | 28 return scoped_refptr<ShareableFileReference>(reference); |
29 } | 29 } |
30 | 30 |
31 // static | 31 // static |
32 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( | 32 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( |
33 const FilePath& path, FinalReleasePolicy policy, | 33 const FilePath& path, FinalReleasePolicy policy, |
34 base::MessageLoopProxy* file_thread) { | 34 base::TaskRunner* file_task_runner) { |
35 DCHECK(file_thread); | 35 DCHECK(file_task_runner); |
36 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; | 36 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; |
37 | 37 |
38 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det
ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | 38 // 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; | 39 webkit_blob::ShareableFileReference* null_reference = NULL; |
40 InsertResult result = g_file_map.Get().insert( | 40 InsertResult result = g_file_map.Get().insert( |
41 ShareableFileMap::value_type(path, null_reference)); | 41 ShareableFileMap::value_type(path, null_reference)); |
42 if (result.second == false) | 42 if (result.second == false) |
43 return scoped_refptr<ShareableFileReference>(result.first->second); | 43 return scoped_refptr<ShareableFileReference>(result.first->second); |
44 | 44 |
45 // Wasn't in the map, create a new reference and store the pointer. | 45 // Wasn't in the map, create a new reference and store the pointer. |
46 scoped_refptr<ShareableFileReference> reference( | 46 scoped_refptr<ShareableFileReference> reference( |
47 new ShareableFileReference(path, policy, file_thread)); | 47 new ShareableFileReference(path, policy, file_task_runner)); |
48 result.first->second = reference.get(); | 48 result.first->second = reference.get(); |
49 return reference; | 49 return reference; |
50 } | 50 } |
51 | 51 |
52 void ShareableFileReference::AddFinalReleaseCallback( | 52 void ShareableFileReference::AddFinalReleaseCallback( |
53 const FinalReleaseCallback& callback) { | 53 const FinalReleaseCallback& callback) { |
54 final_release_callbacks_.push_back(callback); | 54 final_release_callbacks_.push_back(callback); |
55 } | 55 } |
56 | 56 |
57 ShareableFileReference::ShareableFileReference( | 57 ShareableFileReference::ShareableFileReference( |
58 const FilePath& path, FinalReleasePolicy policy, | 58 const FilePath& path, FinalReleasePolicy policy, |
59 base::MessageLoopProxy* file_thread) | 59 base::TaskRunner* file_task_runner) |
60 : path_(path), final_release_policy_(policy), file_thread_(file_thread) { | 60 : path_(path), |
| 61 final_release_policy_(policy), |
| 62 file_task_runner_(file_task_runner) { |
61 DCHECK(g_file_map.Get().find(path_)->second == NULL); | 63 DCHECK(g_file_map.Get().find(path_)->second == NULL); |
62 } | 64 } |
63 | 65 |
64 ShareableFileReference::~ShareableFileReference() { | 66 ShareableFileReference::~ShareableFileReference() { |
65 DCHECK(g_file_map.Get().find(path_)->second == this); | 67 DCHECK(g_file_map.Get().find(path_)->second == this); |
66 g_file_map.Get().erase(path_); | 68 g_file_map.Get().erase(path_); |
67 | 69 |
68 for (size_t i = 0; i < final_release_callbacks_.size(); i++) | 70 for (size_t i = 0; i < final_release_callbacks_.size(); i++) |
69 final_release_callbacks_[i].Run(path_); | 71 final_release_callbacks_[i].Run(path_); |
70 | 72 |
71 if (final_release_policy_ == DELETE_ON_FINAL_RELEASE) { | 73 if (final_release_policy_ == DELETE_ON_FINAL_RELEASE) { |
72 base::FileUtilProxy::Delete(file_thread_, path_, false /* recursive */, | 74 base::FileUtilProxy::Delete(file_task_runner_, path_, false /* recursive */, |
73 base::FileUtilProxy::StatusCallback()); | 75 base::FileUtilProxy::StatusCallback()); |
74 } | 76 } |
75 } | 77 } |
76 | 78 |
77 } // namespace webkit_blob | 79 } // namespace webkit_blob |
OLD | NEW |