Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: webkit/blob/shareable_file_reference.cc

Issue 10852017: Add thread assertions in ShareableFileReference (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/non_thread_safe.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 class ShareableFileMap : public base::NonThreadSafe {
20 public:
21 typedef std::map<FilePath, ShareableFileReference*> FileMap;
22 typedef FileMap::iterator iterator;
23 typedef FileMap::key_type key_type;
24 typedef FileMap::value_type value_type;
25
26 ShareableFileMap() {}
27
28 iterator Find(key_type key) {
29 DCHECK(CalledOnValidThread());
30 return file_map_.find(key);
31 }
32
33 iterator End() {
34 DCHECK(CalledOnValidThread());
35 return file_map_.end();
36 }
37
38 std::pair<iterator, bool> Insert(value_type value) {
39 DCHECK(CalledOnValidThread());
40 return file_map_.insert(value);
41 }
42
43 void Erase(key_type key) {
44 DCHECK(CalledOnValidThread());
45 file_map_.erase(key);
46 }
47
48 private:
49 FileMap file_map_;
50 DISALLOW_COPY_AND_ASSIGN(ShareableFileMap);
51 };
52
18 base::LazyInstance<ShareableFileMap> g_file_map = LAZY_INSTANCE_INITIALIZER; 53 base::LazyInstance<ShareableFileMap> g_file_map = LAZY_INSTANCE_INITIALIZER;
19 54
20 } // namespace 55 } // namespace
21 56
22 // static 57 // static
23 scoped_refptr<ShareableFileReference> ShareableFileReference::Get( 58 scoped_refptr<ShareableFileReference> ShareableFileReference::Get(
24 const FilePath& path) { 59 const FilePath& path) {
25 ShareableFileMap::iterator found = g_file_map.Get().find(path); 60 ShareableFileMap::iterator found = g_file_map.Get().Find(path);
26 ShareableFileReference* reference = 61 ShareableFileReference* reference =
27 (found == g_file_map.Get().end()) ? NULL : found->second; 62 (found == g_file_map.Get().End()) ? NULL : found->second;
28 return scoped_refptr<ShareableFileReference>(reference); 63 return scoped_refptr<ShareableFileReference>(reference);
29 } 64 }
30 65
31 // static 66 // static
32 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate( 67 scoped_refptr<ShareableFileReference> ShareableFileReference::GetOrCreate(
33 const FilePath& path, FinalReleasePolicy policy, 68 const FilePath& path, FinalReleasePolicy policy,
34 base::TaskRunner* file_task_runner) { 69 base::TaskRunner* file_task_runner) {
35 DCHECK(file_task_runner); 70 DCHECK(file_task_runner);
36 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult; 71 typedef std::pair<ShareableFileMap::iterator, bool> InsertResult;
37 72
38 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair 73 // 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; 74 webkit_blob::ShareableFileReference* null_reference = NULL;
40 InsertResult result = g_file_map.Get().insert( 75 InsertResult result = g_file_map.Get().Insert(
41 ShareableFileMap::value_type(path, null_reference)); 76 ShareableFileMap::value_type(path, null_reference));
42 if (result.second == false) 77 if (result.second == false)
43 return scoped_refptr<ShareableFileReference>(result.first->second); 78 return scoped_refptr<ShareableFileReference>(result.first->second);
44 79
45 // Wasn't in the map, create a new reference and store the pointer. 80 // Wasn't in the map, create a new reference and store the pointer.
46 scoped_refptr<ShareableFileReference> reference( 81 scoped_refptr<ShareableFileReference> reference(
47 new ShareableFileReference(path, policy, file_task_runner)); 82 new ShareableFileReference(path, policy, file_task_runner));
48 result.first->second = reference.get(); 83 result.first->second = reference.get();
49 return reference; 84 return reference;
50 } 85 }
51 86
52 void ShareableFileReference::AddFinalReleaseCallback( 87 void ShareableFileReference::AddFinalReleaseCallback(
53 const FinalReleaseCallback& callback) { 88 const FinalReleaseCallback& callback) {
michaeln 2012/08/06 19:26:01 maybe add a DCHECK(g_file_map.Get().CalledOnValidT
kinuko 2012/08/07 00:57:50 Done.
54 final_release_callbacks_.push_back(callback); 89 final_release_callbacks_.push_back(callback);
55 } 90 }
56 91
57 ShareableFileReference::ShareableFileReference( 92 ShareableFileReference::ShareableFileReference(
58 const FilePath& path, FinalReleasePolicy policy, 93 const FilePath& path, FinalReleasePolicy policy,
59 base::TaskRunner* file_task_runner) 94 base::TaskRunner* file_task_runner)
60 : path_(path), 95 : path_(path),
61 final_release_policy_(policy), 96 final_release_policy_(policy),
62 file_task_runner_(file_task_runner) { 97 file_task_runner_(file_task_runner) {
63 DCHECK(g_file_map.Get().find(path_)->second == NULL); 98 DCHECK(g_file_map.Get().Find(path_)->second == NULL);
64 } 99 }
65 100
66 ShareableFileReference::~ShareableFileReference() { 101 ShareableFileReference::~ShareableFileReference() {
67 DCHECK(g_file_map.Get().find(path_)->second == this); 102 DCHECK(g_file_map.Get().Find(path_)->second == this);
68 g_file_map.Get().erase(path_); 103 g_file_map.Get().Erase(path_);
69 104
70 for (size_t i = 0; i < final_release_callbacks_.size(); i++) 105 for (size_t i = 0; i < final_release_callbacks_.size(); i++)
71 final_release_callbacks_[i].Run(path_); 106 final_release_callbacks_[i].Run(path_);
72 107
73 if (final_release_policy_ == DELETE_ON_FINAL_RELEASE) { 108 if (final_release_policy_ == DELETE_ON_FINAL_RELEASE) {
74 base::FileUtilProxy::Delete(file_task_runner_, path_, false /* recursive */, 109 base::FileUtilProxy::Delete(file_task_runner_, path_, false /* recursive */,
75 base::FileUtilProxy::StatusCallback()); 110 base::FileUtilProxy::StatusCallback());
76 } 111 }
77 } 112 }
78 113
79 } // namespace webkit_blob 114 } // namespace webkit_blob
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698