Chromium Code Reviews| 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 "storage/browser/blob/shareable_file_reference.h" | 5 #include "storage/browser/blob/shareable_file_reference.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/sequence_checker.h" | |
| 12 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
| 13 #include "base/threading/non_thread_safe.h" | |
| 14 | 14 |
| 15 namespace storage { | 15 namespace storage { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // A shareable file map with enforcement of thread checker. | 19 // A shareable file map with enforcement of sequence checker. |
| 20 class ShareableFileMap : public base::NonThreadSafe { | 20 class ShareableFileMap { |
| 21 public: | 21 public: |
| 22 typedef std::map<base::FilePath, ShareableFileReference*> FileMap; | 22 typedef std::map<base::FilePath, ShareableFileReference*> FileMap; |
| 23 typedef FileMap::iterator iterator; | 23 typedef FileMap::iterator iterator; |
| 24 typedef FileMap::key_type key_type; | 24 typedef FileMap::key_type key_type; |
| 25 typedef FileMap::value_type value_type; | 25 typedef FileMap::value_type value_type; |
| 26 | 26 |
| 27 ShareableFileMap() {} | 27 ShareableFileMap() {} |
| 28 | 28 |
| 29 ~ShareableFileMap() { | 29 ~ShareableFileMap() = default; |
| 30 DetachFromThread(); | |
| 31 } | |
| 32 | 30 |
| 33 iterator Find(key_type key) { | 31 iterator Find(key_type key) { |
| 34 DCHECK(CalledOnValidThread()); | 32 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 35 return file_map_.find(key); | 33 return file_map_.find(key); |
| 36 } | 34 } |
| 37 | 35 |
| 38 iterator End() { | 36 iterator End() { |
| 39 DCHECK(CalledOnValidThread()); | 37 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 40 return file_map_.end(); | 38 return file_map_.end(); |
| 41 } | 39 } |
| 42 | 40 |
| 43 std::pair<iterator, bool> Insert(value_type value) { | 41 std::pair<iterator, bool> Insert(value_type value) { |
| 44 DCHECK(CalledOnValidThread()); | 42 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 45 return file_map_.insert(value); | 43 return file_map_.insert(value); |
| 46 } | 44 } |
| 47 | 45 |
| 48 void Erase(key_type key) { | 46 void Erase(key_type key) { |
| 49 DCHECK(CalledOnValidThread()); | 47 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 50 file_map_.erase(key); | 48 file_map_.erase(key); |
| 51 } | 49 } |
| 52 | 50 |
| 51 void AssertCalledOnValidSequence() const { | |
| 52 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 53 } | |
| 54 | |
| 53 private: | 55 private: |
| 54 FileMap file_map_; | 56 FileMap file_map_; |
| 57 | |
| 58 SEQUENCE_CHECKER(sequence_checker_); | |
| 59 | |
| 55 DISALLOW_COPY_AND_ASSIGN(ShareableFileMap); | 60 DISALLOW_COPY_AND_ASSIGN(ShareableFileMap); |
| 56 }; | 61 }; |
| 57 | 62 |
| 58 base::LazyInstance<ShareableFileMap>::DestructorAtExit g_file_map = | 63 base::LazyInstance<ShareableFileMap>::DestructorAtExit g_file_map = |
| 59 LAZY_INSTANCE_INITIALIZER; | 64 LAZY_INSTANCE_INITIALIZER; |
| 60 | 65 |
| 61 } // namespace | 66 } // namespace |
| 62 | 67 |
| 63 // static | 68 // static |
| 64 scoped_refptr<ShareableFileReference> ShareableFileReference::Get( | 69 scoped_refptr<ShareableFileReference> ShareableFileReference::Get( |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 95 | 100 |
| 96 // Wasn't in the map, create a new reference and store the pointer. | 101 // Wasn't in the map, create a new reference and store the pointer. |
| 97 scoped_refptr<ShareableFileReference> reference( | 102 scoped_refptr<ShareableFileReference> reference( |
| 98 new ShareableFileReference(std::move(scoped_file))); | 103 new ShareableFileReference(std::move(scoped_file))); |
| 99 result.first->second = reference.get(); | 104 result.first->second = reference.get(); |
| 100 return reference; | 105 return reference; |
| 101 } | 106 } |
| 102 | 107 |
| 103 void ShareableFileReference::AddFinalReleaseCallback( | 108 void ShareableFileReference::AddFinalReleaseCallback( |
| 104 const FinalReleaseCallback& callback) { | 109 const FinalReleaseCallback& callback) { |
| 105 DCHECK(g_file_map.Get().CalledOnValidThread()); | 110 g_file_map.Get().AssertCalledOnValidSequence(); |
|
jianli
2017/06/01 18:51:05
This will probably add a couple bytes in release c
gab
2017/06/01 19:02:02
The AssertCalledOnValidSequence() call would be co
| |
| 106 scoped_file_.AddScopeOutCallback(callback, NULL); | 111 scoped_file_.AddScopeOutCallback(callback, NULL); |
| 107 } | 112 } |
| 108 | 113 |
| 109 ShareableFileReference::ShareableFileReference(ScopedFile scoped_file) | 114 ShareableFileReference::ShareableFileReference(ScopedFile scoped_file) |
| 110 : scoped_file_(std::move(scoped_file)) { | 115 : scoped_file_(std::move(scoped_file)) { |
| 111 DCHECK(g_file_map.Get().Find(path())->second == NULL); | 116 DCHECK(g_file_map.Get().Find(path())->second == NULL); |
| 112 } | 117 } |
| 113 | 118 |
| 114 ShareableFileReference::~ShareableFileReference() { | 119 ShareableFileReference::~ShareableFileReference() { |
| 115 DCHECK(g_file_map.Get().Find(path())->second == this); | 120 DCHECK(g_file_map.Get().Find(path())->second == this); |
| 116 g_file_map.Get().Erase(path()); | 121 g_file_map.Get().Erase(path()); |
| 117 } | 122 } |
| 118 | 123 |
| 119 } // namespace storage | 124 } // namespace storage |
| OLD | NEW |