OLD | NEW |
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 <queue> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
13 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
14 #include "net/disk_cache/simple/simple_entry_format.h" | 15 #include "net/disk_cache/simple/simple_entry_format.h" |
15 #include "net/disk_cache/simple/simple_index.h" | 16 #include "net/disk_cache/simple/simple_index.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 virtual void CancelSparseIO() OVERRIDE; | 86 virtual void CancelSparseIO() OVERRIDE; |
86 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; | 87 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; |
87 | 88 |
88 private: | 89 private: |
89 SimpleEntryImpl(SimpleIndex* index, | 90 SimpleEntryImpl(SimpleIndex* index, |
90 const base::FilePath& path, | 91 const base::FilePath& path, |
91 const std::string& key); | 92 const std::string& key); |
92 | 93 |
93 virtual ~SimpleEntryImpl(); | 94 virtual ~SimpleEntryImpl(); |
94 | 95 |
| 96 // Runs the next operation in the queue, if any and if there is no other |
| 97 // operation running at the moment. Returns true if a operation has run. |
| 98 bool RunNextOperationIfNeeded(); |
| 99 |
| 100 void CloseInternal(); |
| 101 |
| 102 void ReadDataInternal(int index, |
| 103 int offset, |
| 104 scoped_refptr<net::IOBuffer> buf, |
| 105 int buf_len, |
| 106 const CompletionCallback& callback); |
| 107 |
| 108 void WriteDataInternal(int index, |
| 109 int offset, |
| 110 scoped_refptr<net::IOBuffer> buf, |
| 111 int buf_len, |
| 112 const CompletionCallback& callback, |
| 113 bool truncate); |
| 114 |
95 // Called after a SimpleSynchronousEntry has completed CreateEntry() or | 115 // Called after a SimpleSynchronousEntry has completed CreateEntry() or |
96 // OpenEntry(). If |sync_entry| is non-NULL, creation is successful and we | 116 // OpenEntry(). If |sync_entry| is non-NULL, creation is successful and we |
97 // can return |this| SimpleEntryImpl to |*out_entry|. Runs | 117 // can return |this| SimpleEntryImpl to |*out_entry|. Runs |
98 // |completion_callback|. | 118 // |completion_callback|. |
99 void CreationOperationComplete( | 119 void CreationOperationComplete( |
100 Entry** out_entry, | 120 Entry** out_entry, |
101 const CompletionCallback& completion_callback, | 121 const CompletionCallback& completion_callback, |
102 SimpleSynchronousEntry* sync_entry); | 122 SimpleSynchronousEntry* sync_entry); |
103 | 123 |
104 // Called after a SimpleSynchronousEntry has completed an asynchronous IO | 124 // Called after a SimpleSynchronousEntry has completed an asynchronous IO |
105 // operation, such as ReadData() or WriteData(). Calls |completion_callback|. | 125 // operation, such as ReadData() or WriteData(). Calls |completion_callback|. |
106 void EntryOperationComplete( | 126 void EntryOperationComplete( |
107 const CompletionCallback& completion_callback, | 127 const CompletionCallback& completion_callback, |
108 int result); | 128 int result); |
109 | 129 |
110 // Called on initialization and also after the completion of asynchronous IO | 130 // Called on initialization and also after the completion of asynchronous IO |
111 // to initialize the IO thread copies of data returned by synchronous accessor | 131 // to initialize the IO thread copies of data returned by synchronous accessor |
112 // functions. Copies data from |synchronous_entry_| into |this|, so that | 132 // functions. Copies data from |synchronous_entry_| into |this|, so that |
113 // values can be returned during our next IO operation. | 133 // values can be returned during our next IO operation. |
114 void SetSynchronousData(); | 134 void SetSynchronousData(); |
115 | 135 |
116 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 136 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
117 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 137 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
118 base::ThreadChecker io_thread_checker_; | 138 base::ThreadChecker io_thread_checker_; |
119 | 139 |
120 const scoped_refptr<base::SingleThreadTaskRunner> constructor_thread_; | |
121 const base::WeakPtr<SimpleIndex> index_; | 140 const base::WeakPtr<SimpleIndex> index_; |
122 const base::FilePath path_; | 141 const base::FilePath path_; |
123 const std::string key_; | 142 const std::string key_; |
124 | 143 |
125 // |last_used_|, |last_modified_| and |data_size_| are copied from the | 144 // |last_used_|, |last_modified_| and |data_size_| are copied from the |
126 // synchronous entry at the completion of each item of asynchronous IO. | 145 // synchronous entry at the completion of each item of asynchronous IO. |
127 base::Time last_used_; | 146 base::Time last_used_; |
128 base::Time last_modified_; | 147 base::Time last_modified_; |
129 int32 data_size_[kSimpleEntryFileCount]; | 148 int32 data_size_[kSimpleEntryFileCount]; |
130 | 149 |
131 // The |synchronous_entry_| is the worker thread object that performs IO on | 150 // The |synchronous_entry_| is the worker thread object that performs IO on |
132 // entries. It's owned by this SimpleEntryImpl whenever | 151 // entries. It's owned by this SimpleEntryImpl whenever |operation_running_| |
133 // |synchronous_entry_in_use_by_worker_| is false (i.e. when an operation | 152 // is false (i.e. when an operation is not pending on the worker pool). |
134 // is not pending on the worker pool). When an operation is pending on the | |
135 // worker pool, the |synchronous_entry_| is owned by itself. | |
136 SimpleSynchronousEntry* synchronous_entry_; | 153 SimpleSynchronousEntry* synchronous_entry_; |
137 | 154 |
138 // Set to true when a worker operation is posted on the |synchronous_entry_|, | 155 // Set to true when a worker operation is posted on the |synchronous_entry_|, |
139 // and false after. Used to ensure thread safety by not allowing multiple | 156 // and false after. Used to ensure thread safety by not allowing multiple |
140 // threads to access the |synchronous_entry_| simultaneously. | 157 // threads to access the |synchronous_entry_| simultaneously. |
141 bool synchronous_entry_in_use_by_worker_; | 158 bool operation_running_; |
| 159 std::queue<base::Closure> pending_operations_; |
142 }; | 160 }; |
143 | 161 |
144 } // namespace disk_cache | 162 } // namespace disk_cache |
145 | 163 |
146 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 164 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
OLD | NEW |