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( |
146 size_t max_file_size, | 157 const base::FilePath& directory, |
147 size_t total_num_files, | 158 size_t max_file_size, |
148 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 159 size_t total_num_files, |
160 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
mmenke
2017/07/07 15:32:59
I think scoped_refptr<base::SequencedTaskRunner> +
eroman
2017/07/07 21:03:08
I switched to const scoped_refptr<> throughout, be
| |
149 | 161 |
150 ~BoundedFileWriter() override; | 162 ~BoundedFileWriter() override; |
151 | 163 |
152 // FileNetLogObserver::FileWriter implementation | 164 // FileNetLogObserver::FileWriter implementation |
153 void Initialize(std::unique_ptr<base::Value> constants_value) override; | 165 void Initialize(std::unique_ptr<base::Value> constants_value) override; |
154 void Stop(std::unique_ptr<base::Value> polled_data) override; | 166 void Stop(std::unique_ptr<base::Value> polled_data) override; |
155 void Flush(scoped_refptr<WriteQueue> write_queue) override; | 167 void Flush(scoped_refptr<WriteQueue> write_queue) override; |
156 void DeleteAllFiles() override; | 168 void DeleteAllFiles() override; |
157 | 169 |
158 private: | 170 private: |
(...skipping 14 matching lines...) Expand all Loading... | |
173 const size_t total_num_files_; | 185 const size_t total_num_files_; |
174 | 186 |
175 // Indicates the index of the file in |event_files_| currently being written | 187 // Indicates the index of the file in |event_files_| currently being written |
176 // into. | 188 // into. |
177 size_t current_file_idx_; | 189 size_t current_file_idx_; |
178 | 190 |
179 // Indicates the maximum size of each individual netlogging file, excluding | 191 // Indicates the maximum size of each individual netlogging file, excluding |
180 // the constant file. | 192 // the constant file. |
181 const size_t max_file_size_; | 193 const size_t max_file_size_; |
182 | 194 |
183 // Task runner from the file thread. | 195 // Task runner for doing file operations. |
184 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 196 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
185 | 197 |
186 DISALLOW_COPY_AND_ASSIGN(BoundedFileWriter); | 198 DISALLOW_COPY_AND_ASSIGN(BoundedFileWriter); |
187 }; | 199 }; |
188 | 200 |
189 // This implementation of FileWriter is used when the observer is in unbounded | 201 // This implementation of FileWriter is used when the observer is in unbounded |
190 // mode. | 202 // mode. |
191 // UnboundedFileWriter can be constructed on any thread, and afterwards is only | 203 // UnboundedFileWriter can be constructed on any thread, and afterwards is only |
192 // accessed on the file thread. | 204 // accessed on the file task runner. |
193 class FileNetLogObserver::UnboundedFileWriter | 205 class FileNetLogObserver::UnboundedFileWriter |
194 : public FileNetLogObserver::FileWriter { | 206 : public FileNetLogObserver::FileWriter { |
195 public: | 207 public: |
196 UnboundedFileWriter(const base::FilePath& path, | 208 UnboundedFileWriter( |
197 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 209 const base::FilePath& path, |
210 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
198 | 211 |
199 ~UnboundedFileWriter() override; | 212 ~UnboundedFileWriter() override; |
200 | 213 |
201 // FileNetLogObserver::FileWriter implementation | 214 // FileNetLogObserver::FileWriter implementation |
202 void Initialize(std::unique_ptr<base::Value> constants_value) override; | 215 void Initialize(std::unique_ptr<base::Value> constants_value) override; |
203 void Stop(std::unique_ptr<base::Value> polled_data) override; | 216 void Stop(std::unique_ptr<base::Value> polled_data) override; |
204 void Flush(scoped_refptr<WriteQueue> write_queue) override; | 217 void Flush(scoped_refptr<WriteQueue> write_queue) override; |
205 void DeleteAllFiles() override; | 218 void DeleteAllFiles() override; |
206 | 219 |
207 private: | 220 private: |
208 base::FilePath file_path_; | 221 base::FilePath file_path_; |
209 base::ScopedFILE file_; | 222 base::ScopedFILE file_; |
210 | 223 |
211 // Is set to true after the first event is written to the log file. | 224 // Is set to true after the first event is written to the log file. |
212 bool first_event_written_; | 225 bool first_event_written_; |
213 | 226 |
214 // Task runner from the file thread. | 227 // Task runner for doing file operations. |
215 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 228 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
216 | 229 |
217 DISALLOW_COPY_AND_ASSIGN(UnboundedFileWriter); | 230 DISALLOW_COPY_AND_ASSIGN(UnboundedFileWriter); |
218 }; | 231 }; |
219 | 232 |
220 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBounded( | 233 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBounded( |
221 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
222 const base::FilePath& directory, | 234 const base::FilePath& directory, |
223 size_t max_total_size, | 235 size_t max_total_size, |
224 size_t total_num_files, | 236 size_t total_num_files, |
225 std::unique_ptr<base::Value> constants) { | 237 std::unique_ptr<base::Value> constants) { |
226 DCHECK_GT(total_num_files, 0u); | 238 DCHECK_GT(total_num_files, 0u); |
239 | |
240 scoped_refptr<base::SequencedTaskRunner> file_task_runner = | |
241 CreateFileTaskRunner(); | |
242 | |
227 // The BoundedFileWriter uses a soft limit to write events to file that allows | 243 // 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 | 244 // 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 | 245 // limit which the size of |WriteQueue::queue_| cannot exceed. Thus, the |
230 // BoundedFileWriter may write more events to file than can be contained by | 246 // 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 | 247 // 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 | 248 // 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 | 249 // 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 | 250 // sizes <= the size of an individual event file, the discrepancy between the |
235 // hard limit and the soft limit will not cause an issue. | 251 // hard limit and the soft limit will not cause an issue. |
236 // TODO(dconnol): Handle the case when the WriteQueue still doesn't | 252 // TODO(dconnol): Handle the case when the WriteQueue still doesn't |
237 // contain enough events to fill all files, because of very large events | 253 // contain enough events to fill all files, because of very large events |
238 // relative to file size. | 254 // relative to file size. |
239 std::unique_ptr<FileWriter> file_writer( | 255 std::unique_ptr<FileWriter> file_writer( |
240 new BoundedFileWriter(directory, max_total_size / total_num_files, | 256 new BoundedFileWriter(directory, max_total_size / total_num_files, |
241 total_num_files, file_task_runner)); | 257 total_num_files, file_task_runner)); |
242 | 258 |
243 scoped_refptr<WriteQueue> write_queue(new WriteQueue(max_total_size * 2)); | 259 scoped_refptr<WriteQueue> write_queue(new WriteQueue(max_total_size * 2)); |
244 | 260 |
245 return std::unique_ptr<FileNetLogObserver>( | 261 return std::unique_ptr<FileNetLogObserver>( |
246 new FileNetLogObserver(file_task_runner, std::move(file_writer), | 262 new FileNetLogObserver(file_task_runner, std::move(file_writer), |
247 std::move(write_queue), std::move(constants))); | 263 std::move(write_queue), std::move(constants))); |
248 } | 264 } |
249 | 265 |
250 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateUnbounded( | 266 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateUnbounded( |
251 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
252 const base::FilePath& log_path, | 267 const base::FilePath& log_path, |
253 std::unique_ptr<base::Value> constants) { | 268 std::unique_ptr<base::Value> constants) { |
269 scoped_refptr<base::SequencedTaskRunner> file_task_runner = | |
270 CreateFileTaskRunner(); | |
271 | |
254 std::unique_ptr<FileWriter> file_writer( | 272 std::unique_ptr<FileWriter> file_writer( |
255 new UnboundedFileWriter(log_path, file_task_runner)); | 273 new UnboundedFileWriter(log_path, file_task_runner)); |
256 | 274 |
257 scoped_refptr<WriteQueue> write_queue( | 275 scoped_refptr<WriteQueue> write_queue( |
258 new WriteQueue(std::numeric_limits<size_t>::max())); | 276 new WriteQueue(std::numeric_limits<size_t>::max())); |
259 | 277 |
260 return std::unique_ptr<FileNetLogObserver>( | 278 return std::unique_ptr<FileNetLogObserver>( |
261 new FileNetLogObserver(file_task_runner, std::move(file_writer), | 279 new FileNetLogObserver(file_task_runner, std::move(file_writer), |
262 std::move(write_queue), std::move(constants))); | 280 std::move(write_queue), std::move(constants))); |
263 } | 281 } |
264 | 282 |
265 FileNetLogObserver::~FileNetLogObserver() { | 283 FileNetLogObserver::~FileNetLogObserver() { |
266 if (net_log()) { | 284 if (net_log()) { |
267 // StopObserving was not called. | 285 // StopObserving was not called. |
268 net_log()->DeprecatedRemoveObserver(this); | 286 net_log()->DeprecatedRemoveObserver(this); |
269 file_task_runner_->PostTask( | 287 file_task_runner_->PostTask( |
270 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, | 288 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, |
271 base::Unretained(file_writer_))); | 289 base::Unretained(file_writer_.get()))); |
272 } | 290 } |
273 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); | 291 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_.release()); |
274 } | 292 } |
275 | 293 |
276 void FileNetLogObserver::StartObserving(NetLog* net_log, | 294 void FileNetLogObserver::StartObserving(NetLog* net_log, |
277 NetLogCaptureMode capture_mode) { | 295 NetLogCaptureMode capture_mode) { |
278 net_log->DeprecatedAddObserver(this, capture_mode); | 296 net_log->DeprecatedAddObserver(this, capture_mode); |
279 } | 297 } |
280 | 298 |
281 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, | 299 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, |
282 const base::Closure& callback) { | 300 const base::Closure& callback) { |
283 net_log()->DeprecatedRemoveObserver(this); | 301 net_log()->DeprecatedRemoveObserver(this); |
284 | 302 |
285 file_task_runner_->PostTaskAndReply( | 303 file_task_runner_->PostTaskAndReply( |
286 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, | 304 FROM_HERE, |
287 base::Unretained(file_writer_), write_queue_, | 305 base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, |
288 base::Passed(&polled_data)), | 306 base::Unretained(file_writer_.get()), write_queue_, |
307 base::Passed(&polled_data)), | |
289 callback); | 308 callback); |
290 } | 309 } |
291 | 310 |
292 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { | 311 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { |
293 std::unique_ptr<std::string> json(new std::string); | 312 std::unique_ptr<std::string> json(new std::string); |
294 | 313 |
295 // If |entry| cannot be converted to proper JSON, ignore it. | 314 // If |entry| cannot be converted to proper JSON, ignore it. |
296 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) | 315 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) |
297 return; | 316 return; |
298 | 317 |
299 size_t queue_size = write_queue_->AddEntryToQueue(std::move(json)); | 318 size_t queue_size = write_queue_->AddEntryToQueue(std::move(json)); |
300 | 319 |
301 // If events build up in |write_queue_|, trigger the file thread to drain | 320 // 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 | 321 // 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 | 322 // queue_size > kNumWriteQueueEvents a task has already been posted, or will |
304 // be posted. | 323 // be posted. |
305 if (queue_size == kNumWriteQueueEvents) { | 324 if (queue_size == kNumWriteQueueEvents) { |
306 file_task_runner_->PostTask( | 325 file_task_runner_->PostTask( |
307 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, | 326 FROM_HERE, |
308 base::Unretained(file_writer_), write_queue_)); | 327 base::Bind(&FileNetLogObserver::FileWriter::Flush, |
328 base::Unretained(file_writer_.get()), write_queue_)); | |
309 } | 329 } |
310 } | 330 } |
311 | 331 |
312 FileNetLogObserver::FileNetLogObserver( | 332 FileNetLogObserver::FileNetLogObserver( |
313 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 333 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
314 std::unique_ptr<FileWriter> file_writer, | 334 std::unique_ptr<FileWriter> file_writer, |
315 scoped_refptr<WriteQueue> write_queue, | 335 scoped_refptr<WriteQueue> write_queue, |
316 std::unique_ptr<base::Value> constants) | 336 std::unique_ptr<base::Value> constants) |
317 : file_task_runner_(std::move(file_task_runner)), | 337 : file_task_runner_(std::move(file_task_runner)), |
318 write_queue_(std::move(write_queue)), | 338 write_queue_(std::move(write_queue)), |
319 file_writer_(file_writer.release()) { | 339 file_writer_(std::move(file_writer)) { |
320 if (!constants) | 340 if (!constants) |
321 constants = GetNetConstants(); | 341 constants = GetNetConstants(); |
322 file_task_runner_->PostTask( | 342 file_task_runner_->PostTask( |
323 FROM_HERE, | 343 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Initialize, |
324 base::Bind(&FileNetLogObserver::FileWriter::Initialize, | 344 base::Unretained(file_writer_.get()), |
325 base::Unretained(file_writer_), base::Passed(&constants))); | 345 base::Passed(&constants))); |
326 } | 346 } |
327 | 347 |
328 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) | 348 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) |
329 : memory_(0), memory_max_(memory_max) {} | 349 : memory_(0), memory_max_(memory_max) {} |
330 | 350 |
331 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( | 351 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( |
332 std::unique_ptr<std::string> event) { | 352 std::unique_ptr<std::string> event) { |
333 base::AutoLock lock(lock_); | 353 base::AutoLock lock(lock_); |
334 | 354 |
335 memory_ += event->size(); | 355 memory_ += event->size(); |
(...skipping 24 matching lines...) Expand all Loading... | |
360 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue, | 380 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue, |
361 std::unique_ptr<base::Value> polled_data) { | 381 std::unique_ptr<base::Value> polled_data) { |
362 Flush(write_queue); | 382 Flush(write_queue); |
363 Stop(std::move(polled_data)); | 383 Stop(std::move(polled_data)); |
364 } | 384 } |
365 | 385 |
366 FileNetLogObserver::BoundedFileWriter::BoundedFileWriter( | 386 FileNetLogObserver::BoundedFileWriter::BoundedFileWriter( |
367 const base::FilePath& directory, | 387 const base::FilePath& directory, |
368 size_t max_file_size, | 388 size_t max_file_size, |
369 size_t total_num_files, | 389 size_t total_num_files, |
370 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 390 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
371 : directory_(directory), | 391 : directory_(directory), |
372 total_num_files_(total_num_files), | 392 total_num_files_(total_num_files), |
373 current_file_idx_(0), | 393 current_file_idx_(0), |
374 max_file_size_(max_file_size), | 394 max_file_size_(max_file_size), |
375 task_runner_(task_runner) { | 395 task_runner_(task_runner) { |
376 event_files_.resize(total_num_files_); | 396 event_files_.resize(total_num_files_); |
377 } | 397 } |
378 | 398 |
379 FileNetLogObserver::BoundedFileWriter::~BoundedFileWriter() {} | 399 FileNetLogObserver::BoundedFileWriter::~BoundedFileWriter() {} |
380 | 400 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 base::DeleteFile(directory_.AppendASCII("end_netlog.json"), false); | 486 base::DeleteFile(directory_.AppendASCII("end_netlog.json"), false); |
467 for (size_t i = 0; i < total_num_files_; i++) { | 487 for (size_t i = 0; i < total_num_files_; i++) { |
468 base::DeleteFile(directory_.AppendASCII("event_file_" + | 488 base::DeleteFile(directory_.AppendASCII("event_file_" + |
469 base::SizeTToString(i) + ".json"), | 489 base::SizeTToString(i) + ".json"), |
470 false); | 490 false); |
471 } | 491 } |
472 } | 492 } |
473 | 493 |
474 FileNetLogObserver::UnboundedFileWriter::UnboundedFileWriter( | 494 FileNetLogObserver::UnboundedFileWriter::UnboundedFileWriter( |
475 const base::FilePath& path, | 495 const base::FilePath& path, |
476 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 496 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
477 : file_path_(path), task_runner_(task_runner) {} | 497 : file_path_(path), task_runner_(task_runner) {} |
478 | 498 |
479 FileNetLogObserver::UnboundedFileWriter::~UnboundedFileWriter() {} | 499 FileNetLogObserver::UnboundedFileWriter::~UnboundedFileWriter() {} |
480 | 500 |
481 void FileNetLogObserver::UnboundedFileWriter::Initialize( | 501 void FileNetLogObserver::UnboundedFileWriter::Initialize( |
482 std::unique_ptr<base::Value> constants_value) { | 502 std::unique_ptr<base::Value> constants_value) { |
483 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 503 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
484 | 504 |
485 file_.reset(base::OpenFile(file_path_, "w")); | 505 file_.reset(base::OpenFile(file_path_, "w")); |
486 first_event_written_ = false; | 506 first_event_written_ = false; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { | 551 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { |
532 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 552 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
533 | 553 |
534 // Reset |file_| to release the file handle so base::DeleteFile can | 554 // Reset |file_| to release the file handle so base::DeleteFile can |
535 // safely access it. | 555 // safely access it. |
536 file_.reset(); | 556 file_.reset(); |
537 base::DeleteFile(file_path_, false); | 557 base::DeleteFile(file_path_, false); |
538 } | 558 } |
539 | 559 |
540 } // namespace net | 560 } // namespace net |
OLD | NEW |