| 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 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 // Struct used to store the results of setting up the default log directory | 65 // Struct used to store the results of setting up the default log directory |
| 66 // and log path. | 66 // and log path. |
| 67 struct DefaultLogPathResults { | 67 struct DefaultLogPathResults { |
| 68 bool default_log_path_success; | 68 bool default_log_path_success; |
| 69 base::FilePath default_log_path; | 69 base::FilePath default_log_path; |
| 70 bool log_exists; | 70 bool log_exists; |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 using FilePathCallback = base::Callback<void(const base::FilePath&)>; | 73 using FilePathCallback = base::Callback<void(const base::FilePath&)>; |
| 74 | |
| 75 using DirectoryGetter = base::Callback<bool(base::FilePath*)>; | 74 using DirectoryGetter = base::Callback<bool(base::FilePath*)>; |
| 75 using URLRequestContextGetterList = |
| 76 std::vector<scoped_refptr<net::URLRequestContextGetter>>; |
| 76 | 77 |
| 77 ~NetLogFileWriter(); | 78 ~NetLogFileWriter(); |
| 78 | 79 |
| 79 // Attaches a StateObserver. |observer| will be notified of state changes to | 80 // Attaches a StateObserver. |observer| will be notified of state changes to |
| 80 // NetLogFileWriter. State changes may occur in response to Initiailze(), | 81 // NetLogFileWriter. State changes may occur in response to Initiailze(), |
| 81 // StartNetLog(), or StopNetLog(). StateObserver::OnNewState() will be called | 82 // StartNetLog(), or StopNetLog(). StateObserver::OnNewState() will be called |
| 82 // asynchronously relative to the command that caused the state change. | 83 // asynchronously relative to the command that caused the state change. |
| 83 // |observer| must remain alive until RemoveObserver() is called. | 84 // |observer| must remain alive until RemoveObserver() is called. |
| 84 void AddObserver(StateObserver* observer); | 85 void AddObserver(StateObserver* observer); |
| 85 | 86 |
| 86 // Detaches a StateObserver. | 87 // Detaches a StateObserver. |
| 87 void RemoveObserver(StateObserver* observer); | 88 void RemoveObserver(StateObserver* observer); |
| 88 | 89 |
| 89 // Initializes NetLogFileWriter if not initialized. | 90 // Initializes NetLogFileWriter if not initialized. |
| 90 // | 91 // |
| 91 // Also sets the task runners used by NetLogFileWriter for doing file I/O and | 92 // Also sets the task runners used by NetLogFileWriter for doing file I/O and |
| 92 // network I/O respectively. The task runners must not be changed once set. | 93 // network I/O respectively. The task runners must not be changed once set. |
| 93 // However, calling this function again with the same parameters is OK. | 94 // However, calling this function again with the same parameters is OK. |
| 94 void Initialize(scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 95 void Initialize(scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 95 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner); | 96 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner); |
| 96 | 97 |
| 97 // Starts collecting NetLog data into the file at |log_path|. If |log_path| is | 98 // Starts collecting NetLog data into the file at |log_path|. If |log_path| is |
| 98 // empty, the default log path is used. If NetLogFileWriter is already | 99 // empty, the default log path is used. If NetLogFileWriter is already |
| 99 // logging, this is a no-op and |capture_mode| is ignored. | 100 // logging, this is a no-op and |capture_mode| is ignored. |
| 101 // |
| 102 // |context_getters| is an optional list of URLRequestContextGetters used only |
| 103 // to add log entries for ongoing events when logging starts. They are not |
| 104 // used for retrieving polled data. All the contexts must be bound to the same |
| 105 // thread. |
| 100 void StartNetLog(const base::FilePath& log_path, | 106 void StartNetLog(const base::FilePath& log_path, |
| 101 net::NetLogCaptureMode capture_mode); | 107 net::NetLogCaptureMode capture_mode, |
| 108 const URLRequestContextGetterList& context_getters); |
| 102 | 109 |
| 103 // Stops collecting NetLog data into the file. It is a no-op if | 110 // Stops collecting NetLog data into the file. It is a no-op if |
| 104 // NetLogFileWriter is currently not logging. | 111 // NetLogFileWriter is currently not logging. |
| 105 // | 112 // |
| 106 // |polled_data| is a JSON dictionary that will be appended to the end of the | 113 // |polled_data| is a JSON dictionary that will be appended to the end of the |
| 107 // log; it's for adding additional info to the log that aren't events. | 114 // log; it's for adding additional info to the log that aren't events. |
| 108 // If |context_getter| is not null, then StopNetLog() will automatically | 115 // If |context_getter| is not null, then StopNetLog() will automatically |
| 109 // append net info (from net::GetNetInfo() retrieved using |context_getter|) | 116 // append net info (from net::GetNetInfo() retrieved using |context_getter|) |
| 110 // to |polled_data|. | 117 // to |polled_data|. |
| 118 // Note that StopNetLog() accepts (optionally) only one context getter for |
| 119 // retrieving net polled data as opposed to StartNetLog() which accepts zero |
| 120 // or more context getters for retrieving ongoing net events. |
| 111 void StopNetLog(std::unique_ptr<base::DictionaryValue> polled_data, | 121 void StopNetLog(std::unique_ptr<base::DictionaryValue> polled_data, |
| 112 scoped_refptr<net::URLRequestContextGetter> context_getter); | 122 scoped_refptr<net::URLRequestContextGetter> context_getter); |
| 113 | 123 |
| 114 // Creates a DictionaryValue summary of the state of the NetLogFileWriter | 124 // Creates a DictionaryValue summary of the state of the NetLogFileWriter |
| 115 std::unique_ptr<base::DictionaryValue> GetState() const; | 125 std::unique_ptr<base::DictionaryValue> GetState() const; |
| 116 | 126 |
| 117 // Gets the log filepath. |path_callback| will be used to notify the caller | 127 // Gets the log filepath. |path_callback| will be used to notify the caller |
| 118 // when the filepath is retrieved. |path_callback| will be executed with an | 128 // when the filepath is retrieved. |path_callback| will be executed with an |
| 119 // empty filepath if any of the following occurs: | 129 // empty filepath if any of the following occurs: |
| 120 // (1) The NetLogFileWriter is not initialized. | 130 // (1) The NetLogFileWriter is not initialized. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 147 friend class ChromeNetLog; | 157 friend class ChromeNetLog; |
| 148 friend class NetLogFileWriterTest; | 158 friend class NetLogFileWriterTest; |
| 149 | 159 |
| 150 // The possible logging states of NetLogFileWriter. | 160 // The possible logging states of NetLogFileWriter. |
| 151 enum State { | 161 enum State { |
| 152 STATE_UNINITIALIZED, | 162 STATE_UNINITIALIZED, |
| 153 // Currently in the process of initializing. | 163 // Currently in the process of initializing. |
| 154 STATE_INITIALIZING, | 164 STATE_INITIALIZING, |
| 155 // Not currently logging to file. | 165 // Not currently logging to file. |
| 156 STATE_NOT_LOGGING, | 166 STATE_NOT_LOGGING, |
| 167 // Currently in the process of starting the log. |
| 168 STATE_STARTING_LOG, |
| 157 // Currently logging to file. | 169 // Currently logging to file. |
| 158 STATE_LOGGING, | 170 STATE_LOGGING, |
| 159 // Currently in the process of stopping the log. | 171 // Currently in the process of stopping the log. |
| 160 STATE_STOPPING_LOG, | 172 STATE_STOPPING_LOG, |
| 161 }; | 173 }; |
| 162 | 174 |
| 163 void NotifyStateObservers(); | 175 void NotifyStateObservers(); |
| 164 | 176 |
| 165 // Posts NotifyStateObservers() to the current thread. | 177 // Posts NotifyStateObservers() to the current thread. |
| 166 void NotifyStateObserversAsync(); | 178 void NotifyStateObserversAsync(); |
| 167 | 179 |
| 168 // Called internally by Initialize(). Will initialize NetLogFileWriter's state | 180 // Called internally by Initialize(). Will initialize NetLogFileWriter's state |
| 169 // variables after the default log directory is set up and the default log | 181 // variables after the default log directory is set up and the default log |
| 170 // path is determined. | 182 // path is determined on the |file_task_runner_|. |
| 171 void SetStateAfterSetUpDefaultLogPath( | 183 void SetStateAfterSetUpDefaultLogPath( |
| 172 const DefaultLogPathResults& set_up_default_log_path_results); | 184 const DefaultLogPathResults& set_up_default_log_path_results); |
| 173 | 185 |
| 174 // Called internally by StopNetLog(). Does the actual work needed by | 186 // Called internally by StartNetLog(). Contains tasks to be done to start |
| 175 // StopNetLog() outside of retrieving the net info. | 187 // logging after net log entries for ongoing events are added to the log from |
| 188 // the |net_task_runner_|. |
| 189 void StartNetLogAfterCreateEntriesForActiveObjects( |
| 190 net::NetLogCaptureMode capture_mode); |
| 191 |
| 192 // Called internally by StopNetLog(). Contains tasks to be done to stop |
| 193 // logging after net-thread polled data is retrieved on the |
| 194 // |net_task_runner_|. |
| 176 void StopNetLogAfterAddNetInfo( | 195 void StopNetLogAfterAddNetInfo( |
| 177 std::unique_ptr<base::DictionaryValue> polled_data); | 196 std::unique_ptr<base::DictionaryValue> polled_data); |
| 178 | 197 |
| 179 // Contains tasks to be done after |file_net_log_observer_| has completely | 198 // Contains tasks to be done after |file_net_log_observer_| has completely |
| 180 // stopped writing. | 199 // stopped writing. |
| 181 void ResetObserverThenSetStateNotLogging(); | 200 void ResetObserverThenSetStateNotLogging(); |
| 182 | 201 |
| 183 // All members are accessed solely from the main thread (the thread that | 202 // All members are accessed solely from the main thread (the thread that |
| 184 // |thread_checker_| is bound to). | 203 // |thread_checker_| is bound to). |
| 185 | 204 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 DirectoryGetter default_log_base_dir_getter_; | 236 DirectoryGetter default_log_base_dir_getter_; |
| 218 | 237 |
| 219 base::WeakPtrFactory<NetLogFileWriter> weak_ptr_factory_; | 238 base::WeakPtrFactory<NetLogFileWriter> weak_ptr_factory_; |
| 220 | 239 |
| 221 DISALLOW_COPY_AND_ASSIGN(NetLogFileWriter); | 240 DISALLOW_COPY_AND_ASSIGN(NetLogFileWriter); |
| 222 }; | 241 }; |
| 223 | 242 |
| 224 } // namespace net_log | 243 } // namespace net_log |
| 225 | 244 |
| 226 #endif // COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ | 245 #endif // COMPONENTS_NET_LOG_NET_LOG_FILE_WRITER_H_ |
| OLD | NEW |