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