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 <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.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_disk_format.h" | 15 #include "net/disk_cache/simple/simple_disk_format.h" |
15 #include "net/disk_cache/simple/simple_index.h" | 16 #include "net/disk_cache/simple/simple_index.h" |
16 | 17 |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 class IOBuffer; | 20 class IOBuffer; |
20 } | 21 } |
21 | 22 |
22 namespace disk_cache { | 23 namespace disk_cache { |
23 | 24 |
24 class SimpleSynchronousEntry; | 25 class SimpleSynchronousEntry; |
25 | 26 |
26 // SimpleEntryImpl is the IO thread interface to an entry in the very simple | 27 // SimpleEntryImpl is the IO thread interface to an entry in the very simple |
27 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO | 28 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO |
28 // on the worker thread. | 29 // on the worker thread. |
29 class SimpleEntryImpl : public Entry { | 30 class SimpleEntryImpl : public Entry, public base::RefCounted<SimpleEntryImpl> { |
31 friend class base::RefCounted<SimpleEntryImpl>; | |
30 public: | 32 public: |
31 static int OpenEntry(base::WeakPtr<SimpleIndex> index, | 33 static int OpenEntry(base::WeakPtr<SimpleIndex> index, |
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(base::WeakPtr<SimpleIndex> index, |
38 const base::FilePath& path, | 40 const base::FilePath& path, |
39 const std::string& key, | 41 const std::string& key, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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(base::WeakPtr<SimpleIndex> index, |
84 base::WeakPtr<SimpleIndex> index); | 86 const base::FilePath& path, |
87 const std::string& key); | |
88 | |
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(). Constructs the new SimpleEntryImpl (if |result| is net::OK) |
90 // and passes it back to the caller via |out_entry|. Also runs | 94 // and passes it back to the caller via |out_entry|. Also runs |
91 // |completion_callback|. | 95 // |completion_callback|. |
92 static void CreationOperationComplete( | 96 void CreationOperationComplete( |
93 base::WeakPtr<SimpleIndex> index, | |
94 const CompletionCallback& completion_callback, | 97 const CompletionCallback& completion_callback, |
95 const std::string& key, | |
96 Entry** out_entry, | |
97 SimpleSynchronousEntry* sync_entry); | 98 SimpleSynchronousEntry* sync_entry); |
98 | 99 |
99 // Called after a SimpleSynchronousEntry has completed an asynchronous IO | 100 // Called after a SimpleSynchronousEntry has completed an asynchronous IO |
100 // operation, such as ReadData() or WriteData(). Calls |completion_callback|. | 101 // operation, such as ReadData() or WriteData(). Calls |completion_callback|. |
101 // If |entry| no longer exists, then it ensures |sync_entry| is closed. | 102 void EntryOperationComplete( |
102 static void EntryOperationComplete( | |
103 base::WeakPtr<SimpleIndex> index, | |
104 const CompletionCallback& completion_callback, | 103 const CompletionCallback& completion_callback, |
105 base::WeakPtr<SimpleEntryImpl> entry, | |
106 SimpleSynchronousEntry* sync_entry, | |
107 int result); | 104 int result); |
108 | 105 |
106 // Called after a SimpleSynchronousEntry has been successfully created for | |
107 // this entry. After this call, the SimpleEntryImpl claims |sync_entry| and | |
108 // is responsible for closing it. | |
109 void Initialize(SimpleSynchronousEntry* sync_entry); | |
110 | |
109 // Called on construction and also after the completion of asynchronous IO to | 111 // Called on construction and also after the completion of asynchronous IO to |
110 // initialize the IO thread copies of data returned by synchronous accessor | 112 // initialize the IO thread copies of data returned by synchronous accessor |
111 // functions. Copies data from |synchronous_entry_| into |this|, so that | 113 // functions. Copies data from |synchronous_entry_| into |this|, so that |
112 // values can be returned during our next IO operation. | 114 // values can be returned during our next IO operation. |
113 void SetSynchronousData(); | 115 void SetSynchronousData(); |
114 | 116 |
117 // The disk_cache::Entry API requires a self delete on Close(). At the same | |
118 // time, we use ref counting to keep the SimpleEntryImpl alive while IO | |
119 // operations are pending. This |self_| reference is set to |this| at | |
120 // initialization and holds the implicit reference built in to the API. On | |
121 // Close(), we call |self_.reset()| so the Entry will self release after | |
felipeg
2013/04/12 15:52:19
in the actual code you do
self_ = NULL;
change one
gavinp
2013/04/13 08:54:35
Done.
| |
122 // the last pending IO operation completes. | |
123 scoped_refptr<SimpleEntryImpl> self_; | |
124 | |
115 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 125 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
116 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 126 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
117 base::ThreadChecker io_thread_checker_; | 127 base::ThreadChecker io_thread_checker_; |
118 | 128 |
119 base::WeakPtrFactory<SimpleEntryImpl> weak_ptr_factory_; | 129 const base::WeakPtr<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_; | 130 const base::FilePath path_; |
124 const std::string key_; | 131 const std::string key_; |
125 | 132 |
126 // |last_used_|, |last_modified_| and |data_size_| are copied from the | 133 // |last_used_|, |last_modified_| and |data_size_| are copied from the |
127 // synchronous entry at the completion of each item of asynchronous IO. | 134 // synchronous entry at the completion of each item of asynchronous IO. |
128 base::Time last_used_; | 135 base::Time last_used_; |
129 base::Time last_modified_; | 136 base::Time last_modified_; |
130 int32 data_size_[kSimpleEntryFileCount]; | 137 int32 data_size_[kSimpleEntryFileCount]; |
131 | 138 |
132 // The |synchronous_entry_| is the worker thread object that performs IO on | 139 // The |synchronous_entry_| is the worker thread object that performs IO on |
133 // entries. It's owned by this SimpleEntryImpl whenever | 140 // entries. It's owned by this SimpleEntryImpl whenever |
134 // |synchronous_entry_in_use_by_worker_| is false (i.e. when an operation | 141 // |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 | 142 // is not pending on the worker pool). When an operation is pending on the |
136 // worker pool, the |synchronous_entry_| is owned by itself. | 143 // worker pool, the |synchronous_entry_| is owned by itself. |
137 SimpleSynchronousEntry* synchronous_entry_; | 144 SimpleSynchronousEntry* synchronous_entry_; |
138 | 145 |
139 // Set to true when a worker operation is posted on the |synchronous_entry_|, | 146 // 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 | 147 // and false after. Used to ensure thread safety by not allowing multiple |
141 // threads to access the |synchronous_entry_| simultaneously. | 148 // threads to access the |synchronous_entry_| simultaneously. |
142 bool synchronous_entry_in_use_by_worker_; | 149 bool synchronous_entry_in_use_by_worker_; |
143 | |
144 base::WeakPtr<SimpleIndex> index_; | |
145 }; | 150 }; |
146 | 151 |
147 } // namespace disk_cache | 152 } // namespace disk_cache |
148 | 153 |
149 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 154 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
OLD | NEW |