Chromium Code Reviews| 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 COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ | 5 #ifndef COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ |
| 6 #define COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ | 6 #define COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | |
| 11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "net/log/net_log_capture_mode.h" | |
| 16 | 20 |
| 17 namespace base { | 21 namespace base { |
| 18 class DictionaryValue; | 22 class DictionaryValue; |
| 23 class SingleThreadTaskRunner; | |
| 24 class Value; | |
| 19 } | 25 } |
| 20 | 26 |
| 21 namespace net { | 27 namespace net { |
| 22 class NetLogCaptureMode; | 28 class FileNetLogObserver; |
| 23 class WriteToFileNetLogObserver; | 29 class URLRequestContextGetter; |
| 24 } | 30 } |
| 25 | 31 |
| 26 namespace net_log { | 32 namespace net_log { |
| 27 | 33 |
| 28 class ChromeNetLog; | 34 class ChromeNetLog; |
| 29 | 35 |
| 30 // NetLogFileWriter logs all the NetLog entries into a specified file. | 36 // NetLogFileWriter logs all the NetLog entries into a specified file. |
|
eroman
2017/01/06 21:46:23
Please add a description for the threading model:
wangyix1
2017/01/10 22:20:15
Done.
| |
| 31 // | 37 // |
| 32 // NetLogFileWriter maintains the current logging state (state_) and log file | 38 // NetLogFileWriter maintains the current logging state (state_) and log file |
| 33 // type (log_type_) of the logging into a chrome-net-export-log.json file. | 39 // state (log_exists_, log_capture_mode_known_, and log_capture_mode_). |
| 34 // | 40 // |
| 35 // The following are the possible states | 41 // The following are the possible states after initialization: |
| 36 // a) Only Start is allowed (STATE_NOT_LOGGING, LOG_TYPE_NONE). | 42 // a) Only Start is allowed (STATE_NOT_LOGGING, log_exists_ is false). |
| 37 // b) Only Stop is allowed (STATE_LOGGING). | 43 // b) Only Stop is allowed (STATE_LOGGING). |
| 38 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, anything but | 44 // c) Either Send or Start is allowed (STATE_NOT_LOGGING, log_exists_ is true). |
| 39 // LOG_TYPE_NONE). | 45 class NetLogFileWriter : public base::SupportsWeakPtr<NetLogFileWriter> { |
|
eroman
2017/01/06 21:46:23
Why is SupportsWeakPtr<> needed? (the weak_ptr_fa
wangyix1
2017/01/10 22:20:16
Done.
| |
| 40 // | |
| 41 // This is created/destroyed on the main thread, but all other function calls | |
| 42 // occur on a background thread. | |
| 43 // | |
| 44 // This relies on the UI thread outlasting all other threads for thread safety. | |
| 45 class NetLogFileWriter { | |
| 46 public: | 46 public: |
| 47 // This enum lists the UI button commands it could receive. | |
| 48 enum Command { | |
| 49 DO_START_LOG_BYTES, // Call StartNetLog logging all bytes received. | |
| 50 DO_START, // Call StartNetLog. | |
| 51 DO_START_STRIP_PRIVATE_DATA, // Call StartNetLog stripping private data. | |
| 52 DO_STOP, // Call StopNetLog. | |
| 53 }; | |
| 54 | 47 |
| 55 virtual ~NetLogFileWriter(); | 48 virtual ~NetLogFileWriter(); |
|
eroman
2017/01/06 21:46:23
Does this class still need a vtable? The only virt
wangyix1
2017/01/10 22:20:15
Done.
| |
| 56 | 49 |
| 57 // Accepts the button command and executes it. | 50 typedef base::Callback<void(std::unique_ptr<base::DictionaryValue>)> |
|
eroman
2017/01/06 21:46:23
For new code we prefer "using" over typedef.
wangyix1
2017/01/10 22:20:16
Done.
| |
| 58 void ProcessCommand(Command command); | 51 StateCallback; |
| 59 | 52 |
| 60 // Returns true and the path to the file. If there is no file to | 53 // Start collecting NetLog data into chrome-net-export-log.json file in |
|
eroman
2017/01/06 21:46:23
Can you update this to read "Starts" rather than "
wangyix1
2017/01/10 22:20:15
Done.
| |
| 61 // send, then it returns false. It also returns false when actively logging to | 54 // a directory, using the specified capture mode. It is a no-op if we are |
| 62 // the file. | 55 // already collecting data into a file, and |capture_mode| is ignored. |
| 63 bool GetFilePath(base::FilePath* path); | 56 // ignored. |
| 57 // TODO(mmenke): That's rather weird behavior, think about improving it. | |
| 58 void StartNetLog(net::NetLogCaptureMode capture_mode, | |
| 59 StateCallback state_callback); | |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 64 | 60 |
| 65 // Creates a Value summary of the state of the NetLogFileWriter. The caller is | 61 // Stop collecting NetLog data into the file. It is a no-op if we |
| 66 // responsible for deleting the returned value. | 62 // are not collecting data into a file. |
| 67 base::DictionaryValue* GetState(); | 63 void StopNetLog(std::unique_ptr<base::DictionaryValue> polled_data, |
| 64 scoped_refptr<net::URLRequestContextGetter> context_getter, | |
| 65 StateCallback state_callback); | |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 66 | |
| 67 // Creates a Value summary of the state of the NetLogFileWriter and calls | |
| 68 // |state_callback| with the state as the param. | |
| 69 void GetState(StateCallback state_callback); | |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 70 | |
| 71 typedef base::Callback<void(const base::FilePath&)> FilePathCallback; | |
|
eroman
2017/01/06 21:46:23
For new code we prefer "using" over typedef.
wangyix1
2017/01/10 22:20:15
Done.
| |
| 72 | |
| 73 // Gets the log filepath; if it's valid and the file writer is not actively | |
| 74 // logging, |success_callback| is called with the log filepath as the param. | |
|
eroman
2017/01/06 21:46:23
Please update the comment to describer what happen
wangyix1
2017/01/10 22:20:15
Done.
| |
| 75 void GetFilePath(FilePathCallback success_callback); | |
| 68 | 76 |
| 69 // Updates |log_path_| to the |custom_path|. | 77 // Updates |log_path_| to the |custom_path|. |
| 70 void SetUpNetExportLogPath(const base::FilePath& custom_path); | 78 void SetUpNetExportLogPath(const base::FilePath& custom_path); |
| 71 | 79 |
| 80 void SetFileTaskRunner( | |
|
eroman
2017/01/06 21:46:23
Please add some explanation for this mechanism (ei
wangyix1
2017/01/10 22:20:15
Done.
| |
| 81 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | |
| 82 | |
| 83 void SetNetTaskRunner( | |
| 84 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner); | |
| 85 | |
| 86 scoped_refptr<base::SingleThreadTaskRunner> GetFileTaskRunner() const; | |
| 87 | |
| 88 scoped_refptr<base::SingleThreadTaskRunner> GetNetTaskRunner() const; | |
| 89 | |
| 72 protected: | 90 protected: |
| 73 // Constructs a NetLogFileWriter. Only one instance is created in browser | 91 // Constructs a NetLogFileWriter. Only one instance is created in browser |
| 74 // process. | 92 // process. |
| 75 NetLogFileWriter(ChromeNetLog* chrome_net_log, | 93 NetLogFileWriter(ChromeNetLog* chrome_net_log, |
| 76 const base::CommandLine::StringType& command_line_string, | 94 const base::CommandLine::StringType& command_line_string, |
| 77 const std::string& channel_string); | 95 const std::string& channel_string); |
| 78 | 96 |
| 79 // Returns path name to base::GetTempDir() directory. Returns false if | |
| 80 // base::GetTempDir() fails. | |
| 81 virtual bool GetNetExportLogBaseDirectory(base::FilePath* path) const; | |
| 82 | |
| 83 private: | 97 private: |
| 84 friend class ChromeNetLog; | 98 friend class ChromeNetLog; |
| 85 friend class NetLogFileWriterTest; | 99 friend class NetLogFileWriterTest; |
| 86 | 100 |
| 87 // Allow tests to access our innards for testing purposes. | 101 // Allow tests to access our innards for testing purposes. |
| 88 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitFailure); | 102 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitFailure); |
| 89 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitAllowStart); | 103 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitAllowStart); |
| 90 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitAllowStartOrSend); | 104 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, EnsureInitAllowStartOrSend); |
| 91 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, ProcessCommandDoStartAndStop); | 105 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, ProcessCommandDoStartAndStop); |
| 92 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, DoStartClearsFile); | 106 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, DoStartClearsFile); |
| 93 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, CheckAddEvent); | 107 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, CheckAddEvent); |
| 94 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, CheckAddEventWithCustomPath); | 108 FRIEND_TEST_ALL_PREFIXES(NetLogFileWriterTest, CheckAddEventWithCustomPath); |
| 95 | 109 |
| 96 // This enum lists the possible state NetLogFileWriter could be in. It is used | 110 // This enum lists the possible state NetLogFileWriter could be in. It is used |
| 97 // to enable/disable "Start", "Stop" and "Send" (email) UI actions. | 111 // to enable/disable "Start", "Stop" and "Send" (email) UI actions. |
| 98 enum State { | 112 enum State { |
| 99 STATE_UNINITIALIZED, | 113 STATE_UNINITIALIZED, |
| 100 // Not currently logging to file. | 114 // Not currently logging to file. |
| 101 STATE_NOT_LOGGING, | 115 STATE_NOT_LOGGING, |
| 102 // Currently logging to file. | 116 // Currently logging to file. |
| 103 STATE_LOGGING, | 117 STATE_LOGGING, |
| 104 }; | 118 }; |
| 105 | 119 |
| 106 // The type of the current log file on disk. | 120 // Performs file-related initialization tasks on the file thread, and runs |
| 107 enum LogType { | 121 // |success_callback| if the initialization tasks succeeded. |
| 108 // There is no current log file. | 122 void InitAndCallback(base::Closure success_callback); |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 109 LOG_TYPE_NONE, | 123 |
| 110 // The file predates this session. May or may not have private data. | 124 // Struct used to store the results of SetUpDefaultLogPath. Will be passed to |
| 111 // TODO(davidben): This state is kind of silly. | 125 // InitStateAndCallback() |
| 112 LOG_TYPE_UNKNOWN, | 126 struct DefaultLogPathResults { |
| 113 // The log includes raw bytes. | 127 bool default_log_path_success; |
| 114 LOG_TYPE_LOG_BYTES, | 128 base::FilePath default_log_path; |
| 115 // The file includes all data. | 129 bool log_exists; |
| 116 LOG_TYPE_NORMAL, | |
| 117 // The file has credentials and cookies stripped. | |
| 118 LOG_TYPE_STRIP_PRIVATE_DATA, | |
| 119 }; | 130 }; |
| 120 | 131 |
| 121 // Returns the NetLog::CaptureMode corresponding to a LogType. | 132 // File-related initialization tasks |
| 122 static net::NetLogCaptureMode GetCaptureModeForLogType(LogType log_type); | 133 static DefaultLogPathResults SetUpDefaultLogPath(); |
| 123 | 134 |
| 124 // Initializes the |state_| to STATE_NOT_LOGGING and |log_type_| to | 135 // Initializes |state_| and |log_path_| using results from |
| 125 // LOG_TYPE_NONE (if there is no file from earlier run) or | 136 // SetUpDefaultLogPath(), and runs |success_callback| if |
| 126 // LOG_TYPE_UNKNOWN (if there is a file from earlier run). Returns | 137 // |default_log_path_success| is true. |
| 127 // false if initialization of |log_path_| fails. | 138 void InitStateAndCallback(base::Closure success_callback, |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 128 bool EnsureInit(); | 139 const DefaultLogPathResults& results); |
| 129 | 140 |
| 130 // Start collecting NetLog data into chrome-net-export-log.json file in | 141 // Only called during state transition from STATE_NOT_LOGGING to STATE_LOGGING |
| 131 // a directory, using the specified capture mode. It is a no-op if we are | 142 void StartLogging(net::NetLogCaptureMode capture_mode, |
| 132 // already collecting data into a file, and |capture_mode| is ignored. | 143 StateCallback state_callback); |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 133 // ignored. | |
| 134 // TODO(mmenke): That's rather weird behavior, think about improving it. | |
| 135 void StartNetLog(LogType log_type); | |
| 136 | 144 |
| 137 // Stop collecting NetLog data into the file. It is a no-op if we | 145 // Only called during state transition from STATE_LOGGING to STATE_NOT_LOGGING |
| 138 // are not collecting data into a file. | 146 static std::unique_ptr<base::DictionaryValue> AddNetInfo( |
| 139 void StopNetLog(); | 147 scoped_refptr<net::URLRequestContextGetter> context_getter, |
| 148 std::unique_ptr<base::DictionaryValue> polled_data); | |
| 140 | 149 |
| 141 // Updates |log_path_| to be the base::FilePath to use for log files, which | 150 // Only called during state transition from STATE_LOGGING to STATE_NOT_LOGGING |
| 142 // will be inside the base::GetTempDir() directory. Returns false if | 151 void StopLogging(StateCallback state_callback, |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 143 // base::GetTempDir() fails, or unable to create a subdirectory for logging | 152 std::unique_ptr<base::DictionaryValue> polled_data); |
| 144 // within that directory. | |
| 145 bool SetUpDefaultNetExportLogPath(); | |
| 146 | 153 |
| 147 // Returns true if a file exists at |log_path_|. | 154 // Creates a Value summary of the state of the NetLogFileWriter. The caller is |
| 148 bool NetExportLogExists() const; | 155 // responsible for deleting the returned value. |
|
eroman
2017/01/06 21:46:23
Can this return an std::unique_ptr<> instead?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 156 base::DictionaryValue* GetState(); | |
| 157 | |
| 158 // Gets the state and runs state_callback with the state as a param. | |
| 159 void RunStateCallback(StateCallback state_callback); | |
|
eroman
2017/01/06 21:46:23
Can callback can be a const-ref?
wangyix1
2017/01/10 22:20:15
Done.
| |
| 160 | |
| 161 // If running on a POSIX OS, this sets all the permission flags of the file at | |
| 162 // |path| to 1. Returns false if file does not exist. | |
| 163 static bool SetPosixFilePermissionsAll(const base::FilePath& path); | |
| 164 | |
| 165 static void RunFilePathCallback(FilePathCallback path_callback, | |
| 166 const base::FilePath& path, bool runCallback); | |
| 167 | |
| 168 // Helper function for unit tests. | |
| 169 State state() const { return state_; } | |
| 149 | 170 |
| 150 base::ThreadChecker thread_checker_; | 171 base::ThreadChecker thread_checker_; |
| 151 | 172 |
| 152 // Helper function for unit tests. | 173 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
|
eroman
2017/01/06 21:46:23
Please add a comment for the data members below ex
wangyix1
2017/01/10 22:20:15
Done.
| |
| 153 State state() const { return state_; } | 174 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner_; |
| 154 LogType log_type() const { return log_type_; } | |
| 155 | 175 |
| 156 State state_; // Current state of NetLogFileWriter. | 176 State state_; // Current state of NetLogFileWriter. |
| 157 LogType log_type_; // Type of current log file on disk. | 177 |
| 178 bool log_exists_; // Whether or not the log exists on disk | |
| 179 bool log_capture_mode_known_; | |
| 180 net::NetLogCaptureMode log_capture_mode_; | |
| 158 | 181 |
| 159 base::FilePath log_path_; // base::FilePath to the NetLog file. | 182 base::FilePath log_path_; // base::FilePath to the NetLog file. |
| 160 | 183 |
| 161 // |write_to_file_observer_| watches the NetLog event stream, and | 184 // |write_to_file_observer_| watches the NetLog event stream, and |
| 162 // sends all entries to the file created in StartNetLog(). | 185 // sends all entries to the file created in StartNetLog(). |
| 163 std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; | 186 std::unique_ptr<net::FileNetLogObserver> write_to_file_observer_; |
| 164 | 187 |
| 165 // The |chrome_net_log_| is owned by the browser process, cached here to avoid | 188 // The |chrome_net_log_| is owned by the browser process, cached here to avoid |
| 166 // using global (g_browser_process). | 189 // using global (g_browser_process). |
| 167 ChromeNetLog* chrome_net_log_; | 190 ChromeNetLog* chrome_net_log_; |
| 168 | 191 |
| 169 const base::CommandLine::StringType command_line_string_; | 192 const base::CommandLine::StringType command_line_string_; |
| 170 const std::string channel_string_; | 193 const std::string channel_string_; |
| 171 | 194 |
| 195 base::WeakPtrFactory<NetLogFileWriter> weak_ptr_factory_; | |
| 196 | |
| 172 DISALLOW_COPY_AND_ASSIGN(NetLogFileWriter); | 197 DISALLOW_COPY_AND_ASSIGN(NetLogFileWriter); |
| 173 }; | 198 }; |
| 174 | 199 |
| 175 } // namespace net_log | 200 } // namespace net_log |
| 176 | 201 |
| 177 #endif // COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ | 202 #endif // COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ |
| OLD | NEW |