| 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/common/blob/shareable_file_reference.h" | 5 #include "storage/common/blob/shareable_file_reference.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
| 13 | 13 |
| 14 namespace webkit_blob { | 14 namespace storage { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // A shareable file map with enforcement of thread checker. | 18 // A shareable file map with enforcement of thread checker. |
| 19 class ShareableFileMap : public base::NonThreadSafe { | 19 class ShareableFileMap : public base::NonThreadSafe { |
| 20 public: | 20 public: |
| 21 typedef std::map<base::FilePath, ShareableFileReference*> FileMap; | 21 typedef std::map<base::FilePath, ShareableFileReference*> FileMap; |
| 22 typedef FileMap::iterator iterator; | 22 typedef FileMap::iterator iterator; |
| 23 typedef FileMap::key_type key_type; | 23 typedef FileMap::key_type key_type; |
| 24 typedef FileMap::value_type value_type; | 24 typedef FileMap::value_type value_type; |
| 25 | 25 |
| 26 ShareableFileMap() {} | 26 ShareableFileMap() {} |
| 27 | 27 |
| 28 ~ShareableFileMap() { | 28 ~ShareableFileMap() { DetachFromThread(); } |
| 29 DetachFromThread(); | |
| 30 } | |
| 31 | 29 |
| 32 iterator Find(key_type key) { | 30 iterator Find(key_type key) { |
| 33 DCHECK(CalledOnValidThread()); | 31 DCHECK(CalledOnValidThread()); |
| 34 return file_map_.find(key); | 32 return file_map_.find(key); |
| 35 } | 33 } |
| 36 | 34 |
| 37 iterator End() { | 35 iterator End() { |
| 38 DCHECK(CalledOnValidThread()); | 36 DCHECK(CalledOnValidThread()); |
| 39 return file_map_.end(); | 37 return file_map_.end(); |
| 40 } | 38 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 65 ShareableFileReference* reference = | 63 ShareableFileReference* reference = |
| 66 (found == g_file_map.Get().End()) ? NULL : found->second; | 64 (found == g_file_map.Get().End()) ? NULL : found->second; |
| 67 return scoped_refptr<ShareableFileReference>(reference); | 65 return scoped_refptr<ShareableFileReference>(reference); |
| 68 } | 66 } |
| 69 | 67 |
| 70 // static | 68 // static |
| 71 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( | 69 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( |
| 72 const base::FilePath& path, | 70 const base::FilePath& path, |
| 73 FinalReleasePolicy policy, | 71 FinalReleasePolicy policy, |
| 74 base::TaskRunner* file_task_runner) { | 72 base::TaskRunner* file_task_runner) { |
| 75 return GetOrCreate( | 73 return GetOrCreate(ScopedFile( |
| 76 ScopedFile(path, static_cast<ScopedFile::ScopeOutPolicy>(policy), | 74 path, static_cast<ScopedFile::ScopeOutPolicy>(policy), file_task_runner)); |
| 77 file_task_runner)); | |
| 78 } | 75 } |
| 79 | 76 |
| 80 // static | 77 // static |
| 81 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( | 78 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( |
| 82 ScopedFile scoped_file) { | 79 ScopedFile scoped_file) { |
| 83 if (scoped_file.path().empty()) | 80 if (scoped_file.path().empty()) |
| 84 return scoped_refptr<ShareableFileReference>(); | 81 return scoped_refptr<ShareableFileReference>(); |
| 85 | 82 |
| 86 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; | 83 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; |
| 87 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det
ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair | 84 // Required for VS2010: |
| 88 webkit_blob::ShareableFileReference* null_reference = NULL; | 85 // http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-con
verting-from-null-to-a-pointer-type-in-std-pair |
| 86 storage::ShareableFileReference* null_reference = NULL; |
| 89 InsertResult result = g_file_map.Get().Insert( | 87 InsertResult result = g_file_map.Get().Insert( |
| 90 ShareableFileMap::value_type(scoped_file.path(), null_reference)); | 88 ShareableFileMap::value_type(scoped_file.path(), null_reference)); |
| 91 if (result.second == false) { | 89 if (result.second == false) { |
| 92 scoped_file.Release(); | 90 scoped_file.Release(); |
| 93 return scoped_refptr<ShareableFileReference>(result.first->second); | 91 return scoped_refptr<ShareableFileReference>(result.first->second); |
| 94 } | 92 } |
| 95 | 93 |
| 96 // Wasn't in the map, create a new reference and store the pointer. | 94 // Wasn't in the map, create a new reference and store the pointer. |
| 97 scoped_refptr<ShareableFileReference> reference( | 95 scoped_refptr<ShareableFileReference> reference( |
| 98 new ShareableFileReference(scoped_file.Pass())); | 96 new ShareableFileReference(scoped_file.Pass())); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 109 ShareableFileReference::ShareableFileReference(ScopedFile scoped_file) | 107 ShareableFileReference::ShareableFileReference(ScopedFile scoped_file) |
| 110 : scoped_file_(scoped_file.Pass()) { | 108 : scoped_file_(scoped_file.Pass()) { |
| 111 DCHECK(g_file_map.Get().Find(path())->second == NULL); | 109 DCHECK(g_file_map.Get().Find(path())->second == NULL); |
| 112 } | 110 } |
| 113 | 111 |
| 114 ShareableFileReference::~ShareableFileReference() { | 112 ShareableFileReference::~ShareableFileReference() { |
| 115 DCHECK(g_file_map.Get().Find(path())->second == this); | 113 DCHECK(g_file_map.Get().Find(path())->second == this); |
| 116 g_file_map.Get().Erase(path()); | 114 g_file_map.Get().Erase(path()); |
| 117 } | 115 } |
| 118 | 116 |
| 119 } // namespace webkit_blob | 117 } // namespace storage |
| OLD | NEW |