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/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 namespace base { |
| 18 class MessageLoopProxy; |
| 19 } |
17 | 20 |
18 namespace net { | 21 namespace net { |
19 class IOBuffer; | 22 class IOBuffer; |
20 } | 23 } |
21 | 24 |
22 namespace disk_cache { | 25 namespace disk_cache { |
23 | 26 |
24 class SimpleSynchronousEntry; | 27 class SimpleSynchronousEntry; |
25 | 28 |
26 // SimpleEntryImpl is the IO thread interface to an entry in the very simple | 29 // SimpleEntryImpl is the IO thread interface to an entry in the very simple |
27 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO | 30 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO |
28 // on the worker thread. | 31 // on the worker thread. |
29 class SimpleEntryImpl : public Entry { | 32 class SimpleEntryImpl : public Entry, |
| 33 public base::RefCountedThreadSafe<SimpleEntryImpl> { |
| 34 friend class base::RefCountedThreadSafe<SimpleEntryImpl>; |
30 public: | 35 public: |
31 static int OpenEntry(base::WeakPtr<SimpleIndex> index, | 36 static int OpenEntry(const scoped_refptr<SimpleIndex>& index, |
32 const base::FilePath& path, | 37 const base::FilePath& path, |
33 const std::string& key, | 38 const std::string& key, |
34 Entry** entry, | 39 Entry** entry, |
35 const CompletionCallback& callback); | 40 const CompletionCallback& callback); |
36 | 41 |
37 static int CreateEntry(base::WeakPtr<SimpleIndex> index, | 42 static int CreateEntry(const scoped_refptr<SimpleIndex>& index, |
38 const base::FilePath& path, | 43 const base::FilePath& path, |
39 const std::string& key, | 44 const std::string& key, |
40 Entry** entry, | 45 Entry** entry, |
41 const CompletionCallback& callback); | 46 const CompletionCallback& callback); |
42 | 47 |
43 static int DoomEntry(base::WeakPtr<SimpleIndex> index, | 48 static int DoomEntry(const scoped_refptr<SimpleIndex>& index, |
44 const base::FilePath& path, | 49 const base::FilePath& path, |
45 const std::string& key, | 50 const std::string& key, |
46 const CompletionCallback& callback); | 51 const CompletionCallback& callback); |
47 | 52 |
48 // From Entry: | 53 // From Entry: |
49 virtual void Doom() OVERRIDE; | 54 virtual void Doom() OVERRIDE; |
50 virtual void Close() OVERRIDE; | 55 virtual void Close() OVERRIDE; |
51 virtual std::string GetKey() const OVERRIDE; | 56 virtual std::string GetKey() const OVERRIDE; |
52 virtual base::Time GetLastUsed() const OVERRIDE; | 57 virtual base::Time GetLastUsed() const OVERRIDE; |
53 virtual base::Time GetLastModified() const OVERRIDE; | 58 virtual base::Time GetLastModified() const OVERRIDE; |
(...skipping 19 matching lines...) Expand all Loading... |
73 const CompletionCallback& callback) OVERRIDE; | 78 const CompletionCallback& callback) OVERRIDE; |
74 virtual int GetAvailableRange(int64 offset, | 79 virtual int GetAvailableRange(int64 offset, |
75 int len, | 80 int len, |
76 int64* start, | 81 int64* start, |
77 const CompletionCallback& callback) OVERRIDE; | 82 const CompletionCallback& callback) OVERRIDE; |
78 virtual bool CouldBeSparse() const OVERRIDE; | 83 virtual bool CouldBeSparse() const OVERRIDE; |
79 virtual void CancelSparseIO() OVERRIDE; | 84 virtual void CancelSparseIO() OVERRIDE; |
80 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; | 85 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; |
81 | 86 |
82 private: | 87 private: |
83 SimpleEntryImpl(SimpleSynchronousEntry* synchronous_entry, | 88 SimpleEntryImpl(const scoped_refptr<SimpleIndex>& index, |
84 base::WeakPtr<SimpleIndex> index); | 89 const base::FilePath& path, |
| 90 const std::string& key); |
85 | 91 |
86 virtual ~SimpleEntryImpl(); | 92 virtual ~SimpleEntryImpl(); |
87 | 93 |
88 // Called after a SimpleSynchronousEntry has completed CreateEntry() or | 94 // Called after a SimpleSynchronousEntry has completed CreateEntry() or |
89 // OpenEntry(). Constructs the new SimpleEntryImpl (if |result| is net::OK) | 95 // 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 | 96 // can return |this| SimpleEntryImpl to |*out_entry|. Runs |
91 // |completion_callback|. | 97 // |completion_callback|. |
92 static void CreationOperationComplete( | 98 void CreationOperationComplete( |
93 base::WeakPtr<SimpleIndex> index, | 99 Entry** out_entry, |
94 const CompletionCallback& completion_callback, | 100 const CompletionCallback& completion_callback, |
95 const std::string& key, | |
96 Entry** out_entry, | |
97 SimpleSynchronousEntry* sync_entry); | 101 SimpleSynchronousEntry* sync_entry); |
98 | 102 |
99 // Called after a SimpleSynchronousEntry has completed an asynchronous IO | 103 // Called after a SimpleSynchronousEntry has completed an asynchronous IO |
100 // operation, such as ReadData() or WriteData(). Calls |completion_callback|. | 104 // operation, such as ReadData() or WriteData(). Calls |completion_callback|. |
101 // If |entry| no longer exists, then it ensures |sync_entry| is closed. | 105 void EntryOperationComplete( |
102 static void EntryOperationComplete( | |
103 base::WeakPtr<SimpleIndex> index, | |
104 const CompletionCallback& completion_callback, | 106 const CompletionCallback& completion_callback, |
105 base::WeakPtr<SimpleEntryImpl> entry, | |
106 SimpleSynchronousEntry* sync_entry, | |
107 int result); | 107 int result); |
108 | 108 |
109 // Called on construction and also after the completion of asynchronous IO to | 109 // Called on initialization and also after the completion of asynchronous IO |
110 // initialize the IO thread copies of data returned by synchronous accessor | 110 // to initialize the IO thread copies of data returned by synchronous accessor |
111 // functions. Copies data from |synchronous_entry_| into |this|, so that | 111 // functions. Copies data from |synchronous_entry_| into |this|, so that |
112 // values can be returned during our next IO operation. | 112 // values can be returned during our next IO operation. |
113 void SetSynchronousData(); | 113 void SetSynchronousData(); |
114 | 114 |
115 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 115 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
116 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 116 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
117 base::ThreadChecker io_thread_checker_; | 117 base::ThreadChecker io_thread_checker_; |
118 | 118 |
119 base::WeakPtrFactory<SimpleEntryImpl> weak_ptr_factory_; | 119 const scoped_refptr<base::MessageLoopProxy> constructor_thread_; |
120 | 120 const scoped_refptr<SimpleIndex> index_; |
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_; | 121 const base::FilePath path_; |
124 const std::string key_; | 122 const std::string key_; |
125 | 123 |
126 // |last_used_|, |last_modified_| and |data_size_| are copied from the | 124 // |last_used_|, |last_modified_| and |data_size_| are copied from the |
127 // synchronous entry at the completion of each item of asynchronous IO. | 125 // synchronous entry at the completion of each item of asynchronous IO. |
128 base::Time last_used_; | 126 base::Time last_used_; |
129 base::Time last_modified_; | 127 base::Time last_modified_; |
130 int32 data_size_[kSimpleEntryFileCount]; | 128 int32 data_size_[kSimpleEntryFileCount]; |
131 | 129 |
132 // The |synchronous_entry_| is the worker thread object that performs IO on | 130 // The |synchronous_entry_| is the worker thread object that performs IO on |
133 // entries. It's owned by this SimpleEntryImpl whenever | 131 // entries. It's owned by this SimpleEntryImpl whenever |
134 // |synchronous_entry_in_use_by_worker_| is false (i.e. when an operation | 132 // |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 | 133 // is not pending on the worker pool). When an operation is pending on the |
136 // worker pool, the |synchronous_entry_| is owned by itself. | 134 // worker pool, the |synchronous_entry_| is owned by itself. |
137 SimpleSynchronousEntry* synchronous_entry_; | 135 SimpleSynchronousEntry* synchronous_entry_; |
138 | 136 |
139 // Set to true when a worker operation is posted on the |synchronous_entry_|, | 137 // 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 | 138 // and false after. Used to ensure thread safety by not allowing multiple |
141 // threads to access the |synchronous_entry_| simultaneously. | 139 // threads to access the |synchronous_entry_| simultaneously. |
142 bool synchronous_entry_in_use_by_worker_; | 140 bool synchronous_entry_in_use_by_worker_; |
143 | |
144 base::WeakPtr<SimpleIndex> index_; | |
145 }; | 141 }; |
146 | 142 |
147 } // namespace disk_cache | 143 } // namespace disk_cache |
148 | 144 |
149 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 145 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
OLD | NEW |