| 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 #ifndef NET_LOG_FILE_NET_LOG_OBSERVER_H_ | 5 #ifndef NET_LOG_FILE_NET_LOG_OBSERVER_H_ |
| 6 #define NET_LOG_FILE_NET_LOG_OBSERVER_H_ | 6 #define NET_LOG_FILE_NET_LOG_OBSERVER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
| 13 #include "net/log/net_log.h" | 13 #include "net/log/net_log.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class Value; | 16 class Value; |
| 17 class FilePath; | 17 class FilePath; |
| 18 class SingleThreadTaskRunner; | 18 class SingleThreadTaskRunner; |
| 19 } // namespace base | 19 } // namespace base |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 class NetLogCaptureMode; | 23 class NetLogCaptureMode; |
| 24 class URLRequestContext; | |
| 25 | 24 |
| 26 // FileNetLogObserver watches the NetLog event stream and sends all entries to | 25 // FileNetLogObserver watches the NetLog event stream and sends all entries to |
| 27 // either a group of files in a directory (bounded mode) or to a single file | 26 // either a group of files in a directory (bounded mode) or to a single file |
| 28 // (unbounded mode). | 27 // (unbounded mode). |
| 29 // | 28 // |
| 30 // Bounded mode: | 29 // Bounded mode: |
| 31 // The events are written to a single JSON object that is split across the | 30 // The events are written to a single JSON object that is split across the |
| 32 // files, and the files must be stitched together once the observation period | 31 // files, and the files must be stitched together once the observation period |
| 33 // is over. The first file is constants.json, followed by a consumer-specified | 32 // is over. The first file is constants.json, followed by a consumer-specified |
| 34 // number of event files named event_file_<index>.json, and the last file is | 33 // number of event files named event_file_<index>.json, and the last file is |
| 35 // end_netlog.json. | 34 // end_netlog.json. |
| 36 // | 35 // |
| 37 // The user is able to specify an approximate maximum cumulative size for the | 36 // The user is able to specify an approximate maximum cumulative size for the |
| 38 // netlog files and the observer overwrites old events when the maximum file | 37 // netlog files and the observer overwrites old events when the maximum file |
| 39 // size is reached. | 38 // size is reached. |
| 40 // | 39 // |
| 41 // Unbounded mode: | 40 // Unbounded mode: |
| 42 // The entire JSON object is put into one file. There is no size limit to how | 41 // The entire JSON object is put into one file. There is no size limit to how |
| 43 // large this file can grow; all events added will be written to the file. | 42 // large this file can grow; all events added will be written to the file. |
| 44 // | 43 // |
| 45 // The consumer must call StartObservingBounded/StartObservingUnbounded before | 44 // The consumer must call StartObserving before calling StopObserving, and must |
| 46 // calling StopObserving, and must call each method exactly once in the lifetime | 45 // call each method exactly once in the lifetime of the observer. StartObserving |
| 47 // of the observer. StartObservingBounded/StartObservingUnbounded and | 46 // and StopObserving must be called on the same thread, but there is no |
| 48 // StopObserving must be called on the same thread, but there is no restriction | 47 // restriction on which thread is used. |
| 49 // on which thread is used. | |
| 50 class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { | 48 class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { |
| 51 public: | 49 public: |
| 50 // Creates a FileNetLogObserver in bounded mode. |
| 51 // |
| 52 // |file_task_runner| indicates the task runner that should be used to post | 52 // |file_task_runner| indicates the task runner that should be used to post |
| 53 // tasks from the main thread to the file thread. | 53 // tasks from the main thread to the file thread. |
| 54 explicit FileNetLogObserver( | 54 // |
| 55 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 55 // |directory| is the directory where the log files will be. |
| 56 | |
| 57 ~FileNetLogObserver() override; | |
| 58 | |
| 59 // Starts observing |net_log| in bounded mode and writes output files to | |
| 60 // |directory|. | |
| 61 // May only be called once in the lifetime of the object. | |
| 62 // | 56 // |
| 63 // |max_total_size| is the approximate limit on the cumulative size of all | 57 // |max_total_size| is the approximate limit on the cumulative size of all |
| 64 // netlog files. | 58 // netlog files. |
| 65 // | 59 // |
| 66 // |total_num_files| sets the total number of event files that are used to | 60 // |total_num_files| sets the total number of event files that are used to |
| 67 // write the events. It must be greater than 0. | 61 // write the events. It must be greater than 0. |
| 68 // | 62 // |
| 69 // |constants| is an optional legend for decoding constant values used in | 63 // |constants| is an optional legend for decoding constant values used in |
| 70 // the log. It should generally be a modified version of GetNetConstants(). | 64 // the log. It should generally be a modified version of GetNetConstants(). |
| 71 // If not present, the output of GetNetConstants() will be used. | 65 // If not present, the output of GetNetConstants() will be used. |
| 66 static std::unique_ptr<FileNetLogObserver> CreateBounded( |
| 67 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 68 const base::FilePath& directory, |
| 69 size_t max_total_size, |
| 70 size_t total_num_files, |
| 71 std::unique_ptr<base::Value> constants); |
| 72 |
| 73 // Creates a FileNetLogObserver in unbounded mode. |
| 72 // | 74 // |
| 73 // |url_request_context| is an optional URLRequestContext that will be used | 75 // |file_task_runner| indicates the task runner that should be used to post |
| 74 // to pre-populate the log with information about in-progress events. If the | 76 // tasks from the main thread to the file thread. |
| 75 // context is non-NULL, StartObservingBounded() must be called on the | 77 // |
| 76 // context's thread. | 78 // |log_path| is where the log file will be. |
| 77 void StartObservingBounded(NetLog* net_log, | |
| 78 NetLogCaptureMode capture_mode, | |
| 79 const base::FilePath& directory, | |
| 80 std::unique_ptr<base::Value> constants, | |
| 81 URLRequestContext* url_request_context, | |
| 82 size_t max_total_size, | |
| 83 size_t total_num_files); | |
| 84 | |
| 85 // Starts observing |net_log| in unbounded mode and writes to the file at | |
| 86 // |filepath|. | |
| 87 // May only be called once in the lifetime of the object. | |
| 88 // | 79 // |
| 89 // |constants| is an optional legend for decoding constant values used in | 80 // |constants| is an optional legend for decoding constant values used in |
| 90 // the log. It should generally be a modified version of GetNetConstants(). | 81 // the log. It should generally be a modified version of GetNetConstants(). |
| 91 // If not present, the output of GetNetConstants() will be used. | 82 // If not present, the output of GetNetConstants() will be used. |
| 92 // | 83 static std::unique_ptr<FileNetLogObserver> CreateUnbounded( |
| 93 // |url_request_context| is an optional URLRequestContext that will be used | 84 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 94 // to pre-populate the log with information about in-progress events. If the | 85 const base::FilePath& log_path, |
| 95 // context is non-NULL, StartObservingUnbounded() must be called on | 86 std::unique_ptr<base::Value> constants); |
| 96 // the context's thread. | |
| 97 void StartObservingUnbounded(NetLog* net_log, | |
| 98 NetLogCaptureMode capture_mode, | |
| 99 const base::FilePath& filepath, | |
| 100 std::unique_ptr<base::Value> constants, | |
| 101 URLRequestContext* url_request_context); | |
| 102 | 87 |
| 103 // Stops observing net_log(). Must be called after StartObservingBounded/ | 88 ~FileNetLogObserver() override; |
| 104 // StartObservingUnbounded. Should be called before destruction of the | 89 |
| 90 // Attaches this observer to |net_log| and begins observing events. |
| 91 void StartObserving(NetLog* net_log, NetLogCaptureMode capture_mode); |
| 92 |
| 93 // Stops observing net_log() and closes the output file(s). Must be called |
| 94 // after StartObserving. Should be called before destruction of the |
| 105 // FileNetLogObserver and the NetLog, or the NetLog files will be deleted when | 95 // FileNetLogObserver and the NetLog, or the NetLog files will be deleted when |
| 106 // the observer is destroyed. | 96 // the observer is destroyed. |
| 107 // | 97 // |
| 98 // |polled_data| is an optional argument used to add additional network stack |
| 99 // state to the log. |
| 100 // |
| 108 // |callback| will be run on whichever thread StopObserving() was called on | 101 // |callback| will be run on whichever thread StopObserving() was called on |
| 109 // once all file writing is complete and the netlog files can be accessed | 102 // once all file writing is complete and the netlog files can be accessed |
| 110 // safely. | 103 // safely. |
| 111 // | |
| 112 // |polled_data| is an optional argument used to add additional network stack | |
| 113 // state to the log. | |
| 114 void StopObserving(std::unique_ptr<base::Value> polled_data, | 104 void StopObserving(std::unique_ptr<base::Value> polled_data, |
| 115 const base::Closure& callback); | 105 const base::Closure& callback); |
| 116 | 106 |
| 117 // NetLog::ThreadSafeObserver | 107 // NetLog::ThreadSafeObserver |
| 118 void OnAddEntry(const NetLogEntry& entry) override; | 108 void OnAddEntry(const NetLogEntry& entry) override; |
| 119 | 109 |
| 120 private: | 110 private: |
| 121 class WriteQueue; | 111 class WriteQueue; |
| 122 class FileWriter; | 112 class FileWriter; |
| 123 class BoundedFileWriter; | 113 class BoundedFileWriter; |
| 124 class UnboundedFileWriter; | 114 class UnboundedFileWriter; |
| 125 | 115 |
| 126 // Performs tasks common to both StartObservingBounded() and | 116 FileNetLogObserver( |
| 127 // StartObservingUnbounded(). | 117 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 128 void StartObservingHelper(NetLog* net_log, | 118 std::unique_ptr<FileWriter> file_writer, |
| 129 NetLogCaptureMode capture_mode, | 119 scoped_refptr<WriteQueue> write_queue, |
| 130 std::unique_ptr<base::Value> constants, | 120 std::unique_ptr<base::Value> constants); |
| 131 URLRequestContext* url_request_context); | |
| 132 | 121 |
| 133 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 122 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 134 | 123 |
| 135 // The |write_queue_| object is shared between the file thread and the main | 124 // The |write_queue_| object is shared between the file thread and the main |
| 136 // thread, and should be alive for the entirety of the observer's lifetime. | 125 // thread, and should be alive for the entirety of the observer's lifetime. |
| 137 // It should be destroyed once both the observer has been destroyed and all | 126 // It should be destroyed once both the observer has been destroyed and all |
| 138 // tasks posted to the file thread have completed. | 127 // tasks posted to the file thread have completed. |
| 139 scoped_refptr<WriteQueue> write_queue_; | 128 scoped_refptr<WriteQueue> write_queue_; |
| 140 | 129 |
| 141 // This is the owning reference to a file thread object. The observer is | 130 // This is the owning reference to a file thread object. The observer is |
| 142 // responsible for destroying the file thread object by posting a task from | 131 // responsible for destroying the file thread object by posting a task from |
| 143 // the main thread to the file thread to destroy the FileWriter when the | 132 // the main thread to the file thread to destroy the FileWriter when the |
| 144 // observer is destroyed. | 133 // observer is destroyed. |
| 145 // | 134 // |
| 146 // The use of base::Unretained with |file_writer_| to post tasks to the file | 135 // The use of base::Unretained with |file_writer_| to post tasks to the file |
| 147 // thread is safe because the FileWriter object will be alive until the | 136 // thread is safe because the FileWriter object will be alive until the |
| 148 // observer's destruction. | 137 // observer's destruction. |
| 149 FileWriter* file_writer_; | 138 FileWriter* file_writer_; |
| 150 | 139 |
| 151 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); | 140 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); |
| 152 }; | 141 }; |
| 153 | 142 |
| 154 } // namespace net | 143 } // namespace net |
| 155 | 144 |
| 156 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_ | 145 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_ |
| OLD | NEW |