OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "net/log/file_net_log_observer.h" | 5 #include "net/log/file_net_log_observer.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/json/json_writer.h" | 17 #include "base/json/json_writer.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
22 #include "base/threading/thread.h" | 22 #include "base/task_scheduler/post_task.h" |
23 #include "base/values.h" | 23 #include "base/values.h" |
24 #include "net/log/net_log_capture_mode.h" | 24 #include "net/log/net_log_capture_mode.h" |
25 #include "net/log/net_log_entry.h" | 25 #include "net/log/net_log_entry.h" |
26 #include "net/log/net_log_util.h" | 26 #include "net/log/net_log_util.h" |
27 #include "net/url_request/url_request_context.h" | 27 #include "net/url_request/url_request_context.h" |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 // Number of events that can build up in |write_queue_| before file thread | 31 // Number of events that can build up in |write_queue_| before a task is posted |
32 // is triggered to drain the queue. | 32 // to the file task runner to flush them to disk. |
33 const int kNumWriteQueueEvents = 15; | 33 const int kNumWriteQueueEvents = 15; |
34 | 34 |
| 35 scoped_refptr<base::SequencedTaskRunner> CreateFileTaskRunner() { |
| 36 // The tasks posted to this sequenced task runner do synchronous File I/O for |
| 37 // the purposes of writing NetLog files. |
| 38 // |
| 39 // These intentionally block shutdown to ensure the log file has finished |
| 40 // being written. |
| 41 return base::CreateSequencedTaskRunnerWithTraits( |
| 42 {base::MayBlock(), base::TaskPriority::USER_VISIBLE, |
| 43 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}); |
| 44 } |
| 45 |
35 } // namespace | 46 } // namespace |
36 | 47 |
37 namespace net { | 48 namespace net { |
38 | 49 |
39 // Used to store events to be written to file. | 50 // Used to store events to be written to file. |
40 using EventQueue = std::queue<std::unique_ptr<std::string>>; | 51 using EventQueue = std::queue<std::unique_ptr<std::string>>; |
41 | 52 |
42 // WriteQueue receives events from FileNetLogObserver on the main thread and | 53 // WriteQueue receives events from FileNetLogObserver on the main thread and |
43 // holds them in a queue until they are drained from the queue and written to | 54 // holds them in a queue until they are drained from the queue and written to |
44 // file on the file thread. | 55 // file on the file task runner. |
45 // | 56 // |
46 // WriteQueue contains the resources shared between the main thread and the | 57 // WriteQueue contains the resources shared between the main thread and the |
47 // file thread. |lock_| must be acquired to read or write to |queue_| and | 58 // file task runner. |lock_| must be acquired to read or write to |queue_| and |
48 // |memory_|. | 59 // |memory_|. |
49 // | 60 // |
50 // WriteQueue is refcounted and should be destroyed once all events on the | 61 // WriteQueue is refcounted and should be destroyed once all events on the |
51 // file thread have finished executing. | 62 // file task runner have finished executing. |
52 class FileNetLogObserver::WriteQueue | 63 class FileNetLogObserver::WriteQueue |
53 : public base::RefCountedThreadSafe<WriteQueue> { | 64 : public base::RefCountedThreadSafe<WriteQueue> { |
54 public: | 65 public: |
55 // |memory_max| indicates the maximum amount of memory that the virtual write | 66 // |memory_max| indicates the maximum amount of memory that the virtual write |
56 // queue can use. If |memory_| exceeds |memory_max_|, the |queue_| of events | 67 // queue can use. If |memory_| exceeds |memory_max_|, the |queue_| of events |
57 // is overwritten. | 68 // is overwritten. |
58 explicit WriteQueue(size_t memory_max); | 69 explicit WriteQueue(size_t memory_max); |
59 | 70 |
60 // Adds |event| to |queue_|. Also manages the size of |memory_|; if it | 71 // Adds |event| to |queue_|. Also manages the size of |memory_|; if it |
61 // exceeds |memory_max_|, then old events are dropped from |queue_| without | 72 // exceeds |memory_max_|, then old events are dropped from |queue_| without |
62 // being written to file. | 73 // being written to file. |
63 // | 74 // |
64 // Returns the number of events in the |queue_|. | 75 // Returns the number of events in the |queue_|. |
65 size_t AddEntryToQueue(std::unique_ptr<std::string> event); | 76 size_t AddEntryToQueue(std::unique_ptr<std::string> event); |
66 | 77 |
67 // Swaps |queue_| with |local_queue|. |local_queue| should be empty, so that | 78 // Swaps |queue_| with |local_queue|. |local_queue| should be empty, so that |
68 // |queue_| is emptied. Resets |memory_| to 0. | 79 // |queue_| is emptied. Resets |memory_| to 0. |
69 void SwapQueue(EventQueue* local_queue); | 80 void SwapQueue(EventQueue* local_queue); |
70 | 81 |
71 private: | 82 private: |
72 friend class base::RefCountedThreadSafe<WriteQueue>; | 83 friend class base::RefCountedThreadSafe<WriteQueue>; |
73 | 84 |
74 ~WriteQueue(); | 85 ~WriteQueue(); |
75 | 86 |
76 // Queue of events to be written shared between main thread and file thread. | 87 // Queue of events to be written, shared between main thread and file task |
77 // Main thread adds events to the queue and the file thread drains them and | 88 // runner. Main thread adds events to the queue and the file task runner |
78 // writes the events to file. | 89 // drains them and writes the events to file. |
79 // | 90 // |
80 // |lock_| must be acquired to read or write to this. | 91 // |lock_| must be acquired to read or write to this. |
81 EventQueue queue_; | 92 EventQueue queue_; |
82 | 93 |
83 // Tracks how much memory is being used by the virtual write queue. | 94 // Tracks how much memory is being used by the virtual write queue. |
84 // Incremented in AddEntryToQueue() when events are added to the | 95 // Incremented in AddEntryToQueue() when events are added to the |
85 // buffer, and decremented when SwapQueue() is called and the file thread's | 96 // buffer, and decremented when SwapQueue() is called and the file task |
86 // local queue is swapped with the shared write queue. | 97 // runner's local queue is swapped with the shared write queue. |
87 // | 98 // |
88 // |lock_| must be acquired to read or write to this. | 99 // |lock_| must be acquired to read or write to this. |
89 size_t memory_; | 100 size_t memory_; |
90 | 101 |
91 // Indicates the maximum amount of memory that the |queue_| is allowed to | 102 // Indicates the maximum amount of memory that the |queue_| is allowed to |
92 // use. | 103 // use. |
93 const size_t memory_max_; | 104 const size_t memory_max_; |
94 | 105 |
95 // Protects access to |queue_| and |memory_|. | 106 // Protects access to |queue_| and |memory_|. |
96 // | 107 // |
97 // A lock is necessary because |queue_| and |memory_| are shared between the | 108 // A lock is necessary because |queue_| and |memory_| are shared between the |
98 // file thread and the main thread. NetLog's lock protects OnAddEntry(), | 109 // file task runner and the main thread. NetLog's lock protects OnAddEntry(), |
99 // which calls AddEntryToQueue(), but it does not protect access to the | 110 // which calls AddEntryToQueue(), but it does not protect access to the |
100 // observer's member variables. Thus, a race condition exists if a thread is | 111 // observer's member variables. Thus, a race condition exists if a thread is |
101 // calling OnAddEntry() at the same time that the file thread is accessing | 112 // calling OnAddEntry() at the same time that the file task runner is |
102 // |memory_| and |queue_| to write events to file. The |queue_| and |memory_| | 113 // accessing |memory_| and |queue_| to write events to file. The |queue_| and |
103 // counter are necessary to bound the amount of memory that is used for the | 114 // |memory_| counter are necessary to bound the amount of memory that is used |
104 // queue in the event that the file thread lags significantly behind the main | 115 // for the queue in the event that the file task runner lags significantly |
105 // thread in writing events to file. | 116 // behind the main thread in writing events to file. |
106 base::Lock lock_; | 117 base::Lock lock_; |
107 | 118 |
108 DISALLOW_COPY_AND_ASSIGN(WriteQueue); | 119 DISALLOW_COPY_AND_ASSIGN(WriteQueue); |
109 }; | 120 }; |
110 | 121 |
111 // FileWriter is an interface describing an object that drains events from a | 122 // FileWriter is an interface describing an object that drains events from a |
112 // WriteQueue and writes them to disk. | 123 // WriteQueue and writes them to disk. |
113 class FileNetLogObserver::FileWriter { | 124 class FileNetLogObserver::FileWriter { |
114 public: | 125 public: |
115 virtual ~FileWriter(); | 126 virtual ~FileWriter(); |
(...skipping 15 matching lines...) Expand all Loading... |
131 // FileNetLogObserver after DeleteAllFiles(). | 142 // FileNetLogObserver after DeleteAllFiles(). |
132 virtual void DeleteAllFiles() = 0; | 143 virtual void DeleteAllFiles() = 0; |
133 | 144 |
134 void FlushThenStop(scoped_refptr<WriteQueue> write_queue, | 145 void FlushThenStop(scoped_refptr<WriteQueue> write_queue, |
135 std::unique_ptr<base::Value> polled_data); | 146 std::unique_ptr<base::Value> polled_data); |
136 }; | 147 }; |
137 | 148 |
138 // This implementation of FileWriter is used when the observer is in bounded | 149 // This implementation of FileWriter is used when the observer is in bounded |
139 // mode. | 150 // mode. |
140 // BoundedFileWriter can be constructed on any thread, and afterwards is only | 151 // BoundedFileWriter can be constructed on any thread, and afterwards is only |
141 // accessed on the file thread. | 152 // accessed on the file task runner. |
142 class FileNetLogObserver::BoundedFileWriter | 153 class FileNetLogObserver::BoundedFileWriter |
143 : public FileNetLogObserver::FileWriter { | 154 : public FileNetLogObserver::FileWriter { |
144 public: | 155 public: |
145 BoundedFileWriter(const base::FilePath& directory, | 156 BoundedFileWriter(const base::FilePath& directory, |
146 size_t max_file_size, | 157 size_t max_file_size, |
147 size_t total_num_files, | 158 size_t total_num_files, |
148 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 159 scoped_refptr<base::SequencedTaskRunner> task_runner); |
149 | 160 |
150 ~BoundedFileWriter() override; | 161 ~BoundedFileWriter() override; |
151 | 162 |
152 // FileNetLogObserver::FileWriter implementation | 163 // FileNetLogObserver::FileWriter implementation |
153 void Initialize(std::unique_ptr<base::Value> constants_value) override; | 164 void Initialize(std::unique_ptr<base::Value> constants_value) override; |
154 void Stop(std::unique_ptr<base::Value> polled_data) override; | 165 void Stop(std::unique_ptr<base::Value> polled_data) override; |
155 void Flush(scoped_refptr<WriteQueue> write_queue) override; | 166 void Flush(scoped_refptr<WriteQueue> write_queue) override; |
156 void DeleteAllFiles() override; | 167 void DeleteAllFiles() override; |
157 | 168 |
158 private: | 169 private: |
(...skipping 14 matching lines...) Expand all Loading... |
173 const size_t total_num_files_; | 184 const size_t total_num_files_; |
174 | 185 |
175 // Indicates the index of the file in |event_files_| currently being written | 186 // Indicates the index of the file in |event_files_| currently being written |
176 // into. | 187 // into. |
177 size_t current_file_idx_; | 188 size_t current_file_idx_; |
178 | 189 |
179 // Indicates the maximum size of each individual netlogging file, excluding | 190 // Indicates the maximum size of each individual netlogging file, excluding |
180 // the constant file. | 191 // the constant file. |
181 const size_t max_file_size_; | 192 const size_t max_file_size_; |
182 | 193 |
183 // Task runner from the file thread. | 194 // Task runner for doing file operations. |
184 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 195 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
185 | 196 |
186 DISALLOW_COPY_AND_ASSIGN(BoundedFileWriter); | 197 DISALLOW_COPY_AND_ASSIGN(BoundedFileWriter); |
187 }; | 198 }; |
188 | 199 |
189 // This implementation of FileWriter is used when the observer is in unbounded | 200 // This implementation of FileWriter is used when the observer is in unbounded |
190 // mode. | 201 // mode. |
191 // UnboundedFileWriter can be constructed on any thread, and afterwards is only | 202 // UnboundedFileWriter can be constructed on any thread, and afterwards is only |
192 // accessed on the file thread. | 203 // accessed on the file task runner. |
193 class FileNetLogObserver::UnboundedFileWriter | 204 class FileNetLogObserver::UnboundedFileWriter |
194 : public FileNetLogObserver::FileWriter { | 205 : public FileNetLogObserver::FileWriter { |
195 public: | 206 public: |
196 UnboundedFileWriter(const base::FilePath& path, | 207 UnboundedFileWriter(const base::FilePath& path, |
197 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 208 scoped_refptr<base::SequencedTaskRunner> task_runner); |
198 | 209 |
199 ~UnboundedFileWriter() override; | 210 ~UnboundedFileWriter() override; |
200 | 211 |
201 // FileNetLogObserver::FileWriter implementation | 212 // FileNetLogObserver::FileWriter implementation |
202 void Initialize(std::unique_ptr<base::Value> constants_value) override; | 213 void Initialize(std::unique_ptr<base::Value> constants_value) override; |
203 void Stop(std::unique_ptr<base::Value> polled_data) override; | 214 void Stop(std::unique_ptr<base::Value> polled_data) override; |
204 void Flush(scoped_refptr<WriteQueue> write_queue) override; | 215 void Flush(scoped_refptr<WriteQueue> write_queue) override; |
205 void DeleteAllFiles() override; | 216 void DeleteAllFiles() override; |
206 | 217 |
207 private: | 218 private: |
208 base::FilePath file_path_; | 219 base::FilePath file_path_; |
209 base::ScopedFILE file_; | 220 base::ScopedFILE file_; |
210 | 221 |
211 // Is set to true after the first event is written to the log file. | 222 // Is set to true after the first event is written to the log file. |
212 bool first_event_written_; | 223 bool first_event_written_; |
213 | 224 |
214 // Task runner from the file thread. | 225 // Task runner for doing file operations. |
215 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 226 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
216 | 227 |
217 DISALLOW_COPY_AND_ASSIGN(UnboundedFileWriter); | 228 DISALLOW_COPY_AND_ASSIGN(UnboundedFileWriter); |
218 }; | 229 }; |
219 | 230 |
220 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBounded( | 231 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBounded( |
221 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
222 const base::FilePath& directory, | 232 const base::FilePath& directory, |
223 size_t max_total_size, | 233 size_t max_total_size, |
224 size_t total_num_files, | 234 size_t total_num_files, |
225 std::unique_ptr<base::Value> constants) { | 235 std::unique_ptr<base::Value> constants) { |
226 DCHECK_GT(total_num_files, 0u); | 236 DCHECK_GT(total_num_files, 0u); |
| 237 |
| 238 scoped_refptr<base::SequencedTaskRunner> file_task_runner = |
| 239 CreateFileTaskRunner(); |
| 240 |
227 // The BoundedFileWriter uses a soft limit to write events to file that allows | 241 // The BoundedFileWriter uses a soft limit to write events to file that allows |
228 // the size of the file to exceed the limit, but the WriteQueue uses a hard | 242 // the size of the file to exceed the limit, but the WriteQueue uses a hard |
229 // limit which the size of |WriteQueue::queue_| cannot exceed. Thus, the | 243 // limit which the size of |WriteQueue::queue_| cannot exceed. Thus, the |
230 // BoundedFileWriter may write more events to file than can be contained by | 244 // BoundedFileWriter may write more events to file than can be contained by |
231 // the WriteQueue if they have the same size limit. The maximum size of the | 245 // the WriteQueue if they have the same size limit. The maximum size of the |
232 // WriteQueue is doubled to allow |WriteQueue::queue_| to hold enough events | 246 // WriteQueue is doubled to allow |WriteQueue::queue_| to hold enough events |
233 // for the BoundedFileWriter to fill all files. As long as all events have | 247 // for the BoundedFileWriter to fill all files. As long as all events have |
234 // sizes <= the size of an individual event file, the discrepancy between the | 248 // sizes <= the size of an individual event file, the discrepancy between the |
235 // hard limit and the soft limit will not cause an issue. | 249 // hard limit and the soft limit will not cause an issue. |
236 // TODO(dconnol): Handle the case when the WriteQueue still doesn't | 250 // TODO(dconnol): Handle the case when the WriteQueue still doesn't |
237 // contain enough events to fill all files, because of very large events | 251 // contain enough events to fill all files, because of very large events |
238 // relative to file size. | 252 // relative to file size. |
239 std::unique_ptr<FileWriter> file_writer( | 253 std::unique_ptr<FileWriter> file_writer( |
240 new BoundedFileWriter(directory, max_total_size / total_num_files, | 254 new BoundedFileWriter(directory, max_total_size / total_num_files, |
241 total_num_files, file_task_runner)); | 255 total_num_files, file_task_runner)); |
242 | 256 |
243 scoped_refptr<WriteQueue> write_queue(new WriteQueue(max_total_size * 2)); | 257 scoped_refptr<WriteQueue> write_queue(new WriteQueue(max_total_size * 2)); |
244 | 258 |
245 return std::unique_ptr<FileNetLogObserver>( | 259 return std::unique_ptr<FileNetLogObserver>( |
246 new FileNetLogObserver(file_task_runner, std::move(file_writer), | 260 new FileNetLogObserver(file_task_runner, std::move(file_writer), |
247 std::move(write_queue), std::move(constants))); | 261 std::move(write_queue), std::move(constants))); |
248 } | 262 } |
249 | 263 |
250 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateUnbounded( | 264 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateUnbounded( |
251 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
252 const base::FilePath& log_path, | 265 const base::FilePath& log_path, |
253 std::unique_ptr<base::Value> constants) { | 266 std::unique_ptr<base::Value> constants) { |
| 267 scoped_refptr<base::SequencedTaskRunner> file_task_runner = |
| 268 CreateFileTaskRunner(); |
| 269 |
254 std::unique_ptr<FileWriter> file_writer( | 270 std::unique_ptr<FileWriter> file_writer( |
255 new UnboundedFileWriter(log_path, file_task_runner)); | 271 new UnboundedFileWriter(log_path, file_task_runner)); |
256 | 272 |
257 scoped_refptr<WriteQueue> write_queue( | 273 scoped_refptr<WriteQueue> write_queue( |
258 new WriteQueue(std::numeric_limits<size_t>::max())); | 274 new WriteQueue(std::numeric_limits<size_t>::max())); |
259 | 275 |
260 return std::unique_ptr<FileNetLogObserver>( | 276 return std::unique_ptr<FileNetLogObserver>( |
261 new FileNetLogObserver(file_task_runner, std::move(file_writer), | 277 new FileNetLogObserver(file_task_runner, std::move(file_writer), |
262 std::move(write_queue), std::move(constants))); | 278 std::move(write_queue), std::move(constants))); |
263 } | 279 } |
264 | 280 |
265 FileNetLogObserver::~FileNetLogObserver() { | 281 FileNetLogObserver::~FileNetLogObserver() { |
266 if (net_log()) { | 282 if (net_log()) { |
267 // StopObserving was not called. | 283 // StopObserving was not called. |
268 net_log()->DeprecatedRemoveObserver(this); | 284 net_log()->DeprecatedRemoveObserver(this); |
269 file_task_runner_->PostTask( | 285 file_task_runner_->PostTask( |
270 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, | 286 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, |
271 base::Unretained(file_writer_))); | 287 base::Unretained(file_writer_.get()))); |
272 } | 288 } |
273 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); | 289 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_.release()); |
274 } | 290 } |
275 | 291 |
276 void FileNetLogObserver::StartObserving(NetLog* net_log, | 292 void FileNetLogObserver::StartObserving(NetLog* net_log, |
277 NetLogCaptureMode capture_mode) { | 293 NetLogCaptureMode capture_mode) { |
278 net_log->DeprecatedAddObserver(this, capture_mode); | 294 net_log->DeprecatedAddObserver(this, capture_mode); |
279 } | 295 } |
280 | 296 |
281 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, | 297 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, |
282 const base::Closure& callback) { | 298 const base::Closure& callback) { |
283 net_log()->DeprecatedRemoveObserver(this); | 299 net_log()->DeprecatedRemoveObserver(this); |
284 | 300 |
285 file_task_runner_->PostTaskAndReply( | 301 file_task_runner_->PostTaskAndReply( |
286 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, | 302 FROM_HERE, |
287 base::Unretained(file_writer_), write_queue_, | 303 base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, |
288 base::Passed(&polled_data)), | 304 base::Unretained(file_writer_.get()), write_queue_, |
| 305 base::Passed(&polled_data)), |
289 callback); | 306 callback); |
290 } | 307 } |
291 | 308 |
292 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { | 309 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { |
293 std::unique_ptr<std::string> json(new std::string); | 310 std::unique_ptr<std::string> json(new std::string); |
294 | 311 |
295 // If |entry| cannot be converted to proper JSON, ignore it. | 312 // If |entry| cannot be converted to proper JSON, ignore it. |
296 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) | 313 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) |
297 return; | 314 return; |
298 | 315 |
299 size_t queue_size = write_queue_->AddEntryToQueue(std::move(json)); | 316 size_t queue_size = write_queue_->AddEntryToQueue(std::move(json)); |
300 | 317 |
301 // If events build up in |write_queue_|, trigger the file thread to drain | 318 // If events build up in |write_queue_|, trigger the file task runner to drain |
302 // the queue. Because only 1 item is added to the queue at a time, if | 319 // the queue. Because only 1 item is added to the queue at a time, if |
303 // queue_size > kNumWriteQueueEvents a task has already been posted, or will | 320 // queue_size > kNumWriteQueueEvents a task has already been posted, or will |
304 // be posted. | 321 // be posted. |
305 if (queue_size == kNumWriteQueueEvents) { | 322 if (queue_size == kNumWriteQueueEvents) { |
306 file_task_runner_->PostTask( | 323 file_task_runner_->PostTask( |
307 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, | 324 FROM_HERE, |
308 base::Unretained(file_writer_), write_queue_)); | 325 base::Bind(&FileNetLogObserver::FileWriter::Flush, |
| 326 base::Unretained(file_writer_.get()), write_queue_)); |
309 } | 327 } |
310 } | 328 } |
311 | 329 |
312 FileNetLogObserver::FileNetLogObserver( | 330 FileNetLogObserver::FileNetLogObserver( |
313 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 331 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
314 std::unique_ptr<FileWriter> file_writer, | 332 std::unique_ptr<FileWriter> file_writer, |
315 scoped_refptr<WriteQueue> write_queue, | 333 scoped_refptr<WriteQueue> write_queue, |
316 std::unique_ptr<base::Value> constants) | 334 std::unique_ptr<base::Value> constants) |
317 : file_task_runner_(std::move(file_task_runner)), | 335 : file_task_runner_(std::move(file_task_runner)), |
318 write_queue_(std::move(write_queue)), | 336 write_queue_(std::move(write_queue)), |
319 file_writer_(file_writer.release()) { | 337 file_writer_(std::move(file_writer)) { |
320 if (!constants) | 338 if (!constants) |
321 constants = GetNetConstants(); | 339 constants = GetNetConstants(); |
322 file_task_runner_->PostTask( | 340 file_task_runner_->PostTask( |
323 FROM_HERE, | 341 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Initialize, |
324 base::Bind(&FileNetLogObserver::FileWriter::Initialize, | 342 base::Unretained(file_writer_.get()), |
325 base::Unretained(file_writer_), base::Passed(&constants))); | 343 base::Passed(&constants))); |
326 } | 344 } |
327 | 345 |
328 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) | 346 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) |
329 : memory_(0), memory_max_(memory_max) {} | 347 : memory_(0), memory_max_(memory_max) {} |
330 | 348 |
331 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( | 349 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( |
332 std::unique_ptr<std::string> event) { | 350 std::unique_ptr<std::string> event) { |
333 base::AutoLock lock(lock_); | 351 base::AutoLock lock(lock_); |
334 | 352 |
335 memory_ += event->size(); | 353 memory_ += event->size(); |
(...skipping 24 matching lines...) Expand all Loading... |
360 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue, | 378 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue, |
361 std::unique_ptr<base::Value> polled_data) { | 379 std::unique_ptr<base::Value> polled_data) { |
362 Flush(write_queue); | 380 Flush(write_queue); |
363 Stop(std::move(polled_data)); | 381 Stop(std::move(polled_data)); |
364 } | 382 } |
365 | 383 |
366 FileNetLogObserver::BoundedFileWriter::BoundedFileWriter( | 384 FileNetLogObserver::BoundedFileWriter::BoundedFileWriter( |
367 const base::FilePath& directory, | 385 const base::FilePath& directory, |
368 size_t max_file_size, | 386 size_t max_file_size, |
369 size_t total_num_files, | 387 size_t total_num_files, |
370 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 388 scoped_refptr<base::SequencedTaskRunner> task_runner) |
371 : directory_(directory), | 389 : directory_(directory), |
372 total_num_files_(total_num_files), | 390 total_num_files_(total_num_files), |
373 current_file_idx_(0), | 391 current_file_idx_(0), |
374 max_file_size_(max_file_size), | 392 max_file_size_(max_file_size), |
375 task_runner_(task_runner) { | 393 task_runner_(std::move(task_runner)) { |
376 event_files_.resize(total_num_files_); | 394 event_files_.resize(total_num_files_); |
377 } | 395 } |
378 | 396 |
379 FileNetLogObserver::BoundedFileWriter::~BoundedFileWriter() {} | 397 FileNetLogObserver::BoundedFileWriter::~BoundedFileWriter() {} |
380 | 398 |
381 void FileNetLogObserver::BoundedFileWriter::Initialize( | 399 void FileNetLogObserver::BoundedFileWriter::Initialize( |
382 std::unique_ptr<base::Value> constants_value) { | 400 std::unique_ptr<base::Value> constants_value) { |
383 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 401 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
384 | 402 |
385 event_files_[current_file_idx_] = base::ScopedFILE( | 403 event_files_[current_file_idx_] = base::ScopedFILE( |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 base::DeleteFile(directory_.AppendASCII("end_netlog.json"), false); | 484 base::DeleteFile(directory_.AppendASCII("end_netlog.json"), false); |
467 for (size_t i = 0; i < total_num_files_; i++) { | 485 for (size_t i = 0; i < total_num_files_; i++) { |
468 base::DeleteFile(directory_.AppendASCII("event_file_" + | 486 base::DeleteFile(directory_.AppendASCII("event_file_" + |
469 base::SizeTToString(i) + ".json"), | 487 base::SizeTToString(i) + ".json"), |
470 false); | 488 false); |
471 } | 489 } |
472 } | 490 } |
473 | 491 |
474 FileNetLogObserver::UnboundedFileWriter::UnboundedFileWriter( | 492 FileNetLogObserver::UnboundedFileWriter::UnboundedFileWriter( |
475 const base::FilePath& path, | 493 const base::FilePath& path, |
476 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 494 scoped_refptr<base::SequencedTaskRunner> task_runner) |
477 : file_path_(path), task_runner_(task_runner) {} | 495 : file_path_(path), task_runner_(std::move(task_runner)) {} |
478 | 496 |
479 FileNetLogObserver::UnboundedFileWriter::~UnboundedFileWriter() {} | 497 FileNetLogObserver::UnboundedFileWriter::~UnboundedFileWriter() {} |
480 | 498 |
481 void FileNetLogObserver::UnboundedFileWriter::Initialize( | 499 void FileNetLogObserver::UnboundedFileWriter::Initialize( |
482 std::unique_ptr<base::Value> constants_value) { | 500 std::unique_ptr<base::Value> constants_value) { |
483 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 501 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
484 | 502 |
485 file_.reset(base::OpenFile(file_path_, "w")); | 503 file_.reset(base::OpenFile(file_path_, "w")); |
486 first_event_written_ = false; | 504 first_event_written_ = false; |
487 | 505 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { | 549 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { |
532 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 550 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
533 | 551 |
534 // Reset |file_| to release the file handle so base::DeleteFile can | 552 // Reset |file_| to release the file handle so base::DeleteFile can |
535 // safely access it. | 553 // safely access it. |
536 file_.reset(); | 554 file_.reset(); |
537 base::DeleteFile(file_path_, false); | 555 base::DeleteFile(file_path_, false); |
538 } | 556 } |
539 | 557 |
540 } // namespace net | 558 } // namespace net |
OLD | NEW |