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

Side by Side Diff: net/disk_cache/simple/simple_entry_impl.h

Issue 13880016: Make SimpleEntryImpl ref counted. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remediation Created 7 years, 8 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/threading/thread_checker.h" 12 #include "base/threading/thread_checker.h"
13 #include "net/disk_cache/disk_cache.h" 13 #include "net/disk_cache/disk_cache.h"
14 #include "net/disk_cache/simple/simple_entry_format.h" 14 #include "net/disk_cache/simple/simple_entry_format.h"
15 #include "net/disk_cache/simple/simple_index.h" 15 #include "net/disk_cache/simple/simple_index.h"
16 16
17 17
18 namespace net { 18 namespace net {
19 class IOBuffer; 19 class IOBuffer;
20 } 20 }
21 21
22 namespace disk_cache { 22 namespace disk_cache {
23 23
24 class SimpleSynchronousEntry; 24 class SimpleSynchronousEntry;
25 25
26 // SimpleEntryImpl is the IO thread interface to an entry in the very simple 26 // SimpleEntryImpl is the IO thread interface to an entry in the very simple
27 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO 27 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO
28 // on the worker thread. 28 // on the worker thread.
29 class SimpleEntryImpl : public Entry { 29 class SimpleEntryImpl : public Entry,
30 public base::RefCountedThreadSafe<SimpleEntryImpl> {
31 friend class base::RefCountedThreadSafe<SimpleEntryImpl>;
30 public: 32 public:
31 static int OpenEntry(base::WeakPtr<SimpleIndex> index, 33 static int OpenEntry(scoped_refptr<SimpleIndex> index,
Philippe 2013/04/17 10:19:22 I guess passing ref-counted objects around by copy
gavinp 2013/04/17 10:56:28 const scoped_refptr<> seems the right way to go. T
rvargas (doing something else) 2013/04/17 19:56:38 Note that the recommended practice (Google C++ pre
32 const base::FilePath& path, 34 const base::FilePath& path,
33 const std::string& key, 35 const std::string& key,
34 Entry** entry, 36 Entry** entry,
35 const CompletionCallback& callback); 37 const CompletionCallback& callback);
36 38
37 static int CreateEntry(base::WeakPtr<SimpleIndex> index, 39 static int CreateEntry(scoped_refptr<SimpleIndex> index,
38 const base::FilePath& path, 40 const base::FilePath& path,
39 const std::string& key, 41 const std::string& key,
40 Entry** entry, 42 Entry** entry,
41 const CompletionCallback& callback); 43 const CompletionCallback& callback);
42 44
43 static int DoomEntry(base::WeakPtr<SimpleIndex> index, 45 static int DoomEntry(scoped_refptr<SimpleIndex> index,
44 const base::FilePath& path, 46 const base::FilePath& path,
45 const std::string& key, 47 const std::string& key,
46 const CompletionCallback& callback); 48 const CompletionCallback& callback);
47 49
48 // From Entry: 50 // From Entry:
49 virtual void Doom() OVERRIDE; 51 virtual void Doom() OVERRIDE;
50 virtual void Close() OVERRIDE; 52 virtual void Close() OVERRIDE;
51 virtual std::string GetKey() const OVERRIDE; 53 virtual std::string GetKey() const OVERRIDE;
52 virtual base::Time GetLastUsed() const OVERRIDE; 54 virtual base::Time GetLastUsed() const OVERRIDE;
53 virtual base::Time GetLastModified() const OVERRIDE; 55 virtual base::Time GetLastModified() const OVERRIDE;
(...skipping 19 matching lines...) Expand all
73 const CompletionCallback& callback) OVERRIDE; 75 const CompletionCallback& callback) OVERRIDE;
74 virtual int GetAvailableRange(int64 offset, 76 virtual int GetAvailableRange(int64 offset,
75 int len, 77 int len,
76 int64* start, 78 int64* start,
77 const CompletionCallback& callback) OVERRIDE; 79 const CompletionCallback& callback) OVERRIDE;
78 virtual bool CouldBeSparse() const OVERRIDE; 80 virtual bool CouldBeSparse() const OVERRIDE;
79 virtual void CancelSparseIO() OVERRIDE; 81 virtual void CancelSparseIO() OVERRIDE;
80 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; 82 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE;
81 83
82 private: 84 private:
83 SimpleEntryImpl(SimpleSynchronousEntry* synchronous_entry, 85 SimpleEntryImpl(scoped_refptr<SimpleIndex> index,
84 base::WeakPtr<SimpleIndex> index); 86 const base::FilePath& path,
87 const std::string& key);
88
Philippe 2013/04/17 10:19:22 Nit: extra blank line.
gavinp 2013/04/17 10:56:28 Done.
85 89
86 virtual ~SimpleEntryImpl(); 90 virtual ~SimpleEntryImpl();
87 91
88 // Called after a SimpleSynchronousEntry has completed CreateEntry() or 92 // Called after a SimpleSynchronousEntry has completed CreateEntry() or
89 // OpenEntry(). Constructs the new SimpleEntryImpl (if |result| is net::OK) 93 // OpenEntry(). If |sync_entry| is non-NULL, creation is successful and we
90 // and passes it back to the caller via |out_entry|. Also runs 94 // can return |this| SimpleEntryImpl to |*out_entry|. Runs
91 // |completion_callback|. 95 // |completion_callback|.
92 static void CreationOperationComplete( 96 void CreationOperationComplete(
93 base::WeakPtr<SimpleIndex> index, 97 Entry** out_entry,
94 const CompletionCallback& completion_callback, 98 const CompletionCallback& completion_callback,
95 const std::string& key,
96 Entry** out_entry,
97 SimpleSynchronousEntry* sync_entry); 99 SimpleSynchronousEntry* sync_entry);
98 100
99 // Called after a SimpleSynchronousEntry has completed an asynchronous IO 101 // Called after a SimpleSynchronousEntry has completed an asynchronous IO
100 // operation, such as ReadData() or WriteData(). Calls |completion_callback|. 102 // operation, such as ReadData() or WriteData(). Calls |completion_callback|.
101 // If |entry| no longer exists, then it ensures |sync_entry| is closed. 103 void EntryOperationComplete(
102 static void EntryOperationComplete(
103 base::WeakPtr<SimpleIndex> index,
104 const CompletionCallback& completion_callback, 104 const CompletionCallback& completion_callback,
105 base::WeakPtr<SimpleEntryImpl> entry,
106 SimpleSynchronousEntry* sync_entry,
107 int result); 105 int result);
108 106
109 // Called on construction and also after the completion of asynchronous IO to 107 // Called on initialization and also after the completion of asynchronous IO
110 // initialize the IO thread copies of data returned by synchronous accessor 108 // to initialize the IO thread copies of data returned by synchronous accessor
111 // functions. Copies data from |synchronous_entry_| into |this|, so that 109 // functions. Copies data from |synchronous_entry_| into |this|, so that
112 // values can be returned during our next IO operation. 110 // values can be returned during our next IO operation.
113 void SetSynchronousData(); 111 void SetSynchronousData();
114 112
115 // All nonstatic SimpleEntryImpl methods should always be called on the IO 113 // All nonstatic SimpleEntryImpl methods should always be called on the IO
116 // thread, in all cases. |io_thread_checker_| documents and enforces this. 114 // thread, in all cases. |io_thread_checker_| documents and enforces this.
117 base::ThreadChecker io_thread_checker_; 115 base::ThreadChecker io_thread_checker_;
118 116
119 base::WeakPtrFactory<SimpleEntryImpl> weak_ptr_factory_; 117 const scoped_refptr<SimpleIndex> index_;
120
121 // |path_| and |key_| are copied from the synchronous entry on construction,
122 // and never updated as they are const.
123 const base::FilePath path_; 118 const base::FilePath path_;
124 const std::string key_; 119 const std::string key_;
125 120
126 // |last_used_|, |last_modified_| and |data_size_| are copied from the 121 // |last_used_|, |last_modified_| and |data_size_| are copied from the
127 // synchronous entry at the completion of each item of asynchronous IO. 122 // synchronous entry at the completion of each item of asynchronous IO.
128 base::Time last_used_; 123 base::Time last_used_;
129 base::Time last_modified_; 124 base::Time last_modified_;
130 int32 data_size_[kSimpleEntryFileCount]; 125 int32 data_size_[kSimpleEntryFileCount];
131 126
132 // The |synchronous_entry_| is the worker thread object that performs IO on 127 // The |synchronous_entry_| is the worker thread object that performs IO on
133 // entries. It's owned by this SimpleEntryImpl whenever 128 // entries. It's owned by this SimpleEntryImpl whenever
134 // |synchronous_entry_in_use_by_worker_| is false (i.e. when an operation 129 // |synchronous_entry_in_use_by_worker_| is false (i.e. when an operation
135 // is not pending on the worker pool). When an operation is pending on the 130 // is not pending on the worker pool). When an operation is pending on the
136 // worker pool, the |synchronous_entry_| is owned by itself. 131 // worker pool, the |synchronous_entry_| is owned by itself.
137 SimpleSynchronousEntry* synchronous_entry_; 132 SimpleSynchronousEntry* synchronous_entry_;
138 133
139 // Set to true when a worker operation is posted on the |synchronous_entry_|, 134 // Set to true when a worker operation is posted on the |synchronous_entry_|,
140 // and false after. Used to ensure thread safety by not allowing multiple 135 // and false after. Used to ensure thread safety by not allowing multiple
141 // threads to access the |synchronous_entry_| simultaneously. 136 // threads to access the |synchronous_entry_| simultaneously.
142 bool synchronous_entry_in_use_by_worker_; 137 bool synchronous_entry_in_use_by_worker_;
143
144 base::WeakPtr<SimpleIndex> index_;
145 }; 138 };
146 139
147 } // namespace disk_cache 140 } // namespace disk_cache
148 141
149 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ 142 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_backend_impl.cc ('k') | net/disk_cache/simple/simple_entry_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698