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 #include "components/net_log/net_log_file_writer.h" | 5 #include "components/net_log/net_log_file_writer.h" |
6 | 6 |
| 7 #include <set> |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
14 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
16 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
20 #include "components/net_log/chrome_net_log.h" | 21 #include "components/net_log/chrome_net_log.h" |
21 #include "net/log/file_net_log_observer.h" | 22 #include "net/log/file_net_log_observer.h" |
22 #include "net/log/net_log_util.h" | 23 #include "net/log/net_log_util.h" |
23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
24 | 25 |
| 26 namespace net { |
| 27 class URLRequestContext; |
| 28 } |
| 29 |
25 namespace net_log { | 30 namespace net_log { |
26 | 31 |
27 namespace { | 32 namespace { |
28 | 33 |
29 // Path of logs relative to default temporary directory given by | 34 // Path of logs relative to default temporary directory given by |
30 // base::GetTempDir(). Must be kept in sync with | 35 // base::GetTempDir(). Must be kept in sync with |
31 // chrome/android/java/res/xml/file_paths.xml. Only used if not saving log file | 36 // chrome/android/java/res/xml/file_paths.xml. Only used if not saving log file |
32 // to a custom path. | 37 // to a custom path. |
33 const base::FilePath::CharType kLogRelativePath[] = | 38 const base::FilePath::CharType kLogRelativePath[] = |
34 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); | 39 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); |
(...skipping 20 matching lines...) Expand all Loading... |
55 results.default_log_path = default_base_dir.Append(kLogRelativePath); | 60 results.default_log_path = default_base_dir.Append(kLogRelativePath); |
56 if (!base::CreateDirectoryAndGetError(results.default_log_path.DirName(), | 61 if (!base::CreateDirectoryAndGetError(results.default_log_path.DirName(), |
57 nullptr)) | 62 nullptr)) |
58 return results; | 63 return results; |
59 | 64 |
60 results.log_exists = base::PathExists(results.default_log_path); | 65 results.log_exists = base::PathExists(results.default_log_path); |
61 results.default_log_path_success = true; | 66 results.default_log_path_success = true; |
62 return results; | 67 return results; |
63 } | 68 } |
64 | 69 |
| 70 // Generates net log entries for ongoing events from |context_getters| and |
| 71 // adds them to |observer|. |
| 72 void CreateNetLogEntriesForActiveObjects( |
| 73 const NetLogFileWriter::URLRequestContextGetterList& context_getters, |
| 74 net::NetLog::ThreadSafeObserver* observer) { |
| 75 std::set<net::URLRequestContext*> contexts; |
| 76 for (const auto& getter : context_getters) { |
| 77 DCHECK(getter->GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 78 contexts.insert(getter->GetURLRequestContext()); |
| 79 } |
| 80 net::CreateNetLogEntriesForActiveObjects(contexts, observer); |
| 81 } |
| 82 |
65 // Adds net info from net::GetNetInfo() to |polled_data|. | 83 // Adds net info from net::GetNetInfo() to |polled_data|. |
66 std::unique_ptr<base::DictionaryValue> AddNetInfo( | 84 std::unique_ptr<base::DictionaryValue> AddNetInfo( |
67 scoped_refptr<net::URLRequestContextGetter> context_getter, | 85 scoped_refptr<net::URLRequestContextGetter> context_getter, |
68 std::unique_ptr<base::DictionaryValue> polled_data) { | 86 std::unique_ptr<base::DictionaryValue> polled_data) { |
69 DCHECK(context_getter); | 87 DCHECK(context_getter); |
70 std::unique_ptr<base::DictionaryValue> net_info = net::GetNetInfo( | 88 std::unique_ptr<base::DictionaryValue> net_info = net::GetNetInfo( |
71 context_getter->GetURLRequestContext(), net::NET_INFO_ALL_SOURCES); | 89 context_getter->GetURLRequestContext(), net::NET_INFO_ALL_SOURCES); |
72 if (polled_data) | 90 if (polled_data) |
73 net_info->MergeDictionary(polled_data.get()); | 91 net_info->MergeDictionary(polled_data.get()); |
74 return net_info; | 92 return net_info; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 } | 139 } |
122 | 140 |
123 void NetLogFileWriter::Initialize( | 141 void NetLogFileWriter::Initialize( |
124 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 142 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
125 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner) { | 143 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner) { |
126 DCHECK(thread_checker_.CalledOnValidThread()); | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
127 DCHECK(file_task_runner); | 145 DCHECK(file_task_runner); |
128 DCHECK(net_task_runner); | 146 DCHECK(net_task_runner); |
129 | 147 |
130 if (file_task_runner_) | 148 if (file_task_runner_) |
131 DCHECK_EQ(file_task_runner, file_task_runner_); | 149 DCHECK_EQ(file_task_runner_, file_task_runner); |
132 file_task_runner_ = file_task_runner; | 150 file_task_runner_ = file_task_runner; |
133 if (net_task_runner_) | 151 if (net_task_runner_) |
134 DCHECK_EQ(net_task_runner, net_task_runner_); | 152 DCHECK_EQ(net_task_runner_, net_task_runner); |
135 net_task_runner_ = net_task_runner; | 153 net_task_runner_ = net_task_runner; |
136 | 154 |
137 if (state_ != STATE_UNINITIALIZED) | 155 if (state_ != STATE_UNINITIALIZED) |
138 return; | 156 return; |
139 | 157 |
140 state_ = STATE_INITIALIZING; | 158 state_ = STATE_INITIALIZING; |
141 | 159 |
142 NotifyStateObserversAsync(); | 160 NotifyStateObserversAsync(); |
143 | 161 |
144 base::PostTaskAndReplyWithResult( | 162 base::PostTaskAndReplyWithResult( |
145 file_task_runner_.get(), FROM_HERE, | 163 file_task_runner_.get(), FROM_HERE, |
146 base::Bind(&SetUpDefaultLogPath, default_log_base_dir_getter_), | 164 base::Bind(&SetUpDefaultLogPath, default_log_base_dir_getter_), |
147 base::Bind(&NetLogFileWriter::SetStateAfterSetUpDefaultLogPath, | 165 base::Bind(&NetLogFileWriter::SetStateAfterSetUpDefaultLogPath, |
148 weak_ptr_factory_.GetWeakPtr())); | 166 weak_ptr_factory_.GetWeakPtr())); |
149 } | 167 } |
150 | 168 |
151 void NetLogFileWriter::StartNetLog(const base::FilePath& log_path, | 169 void NetLogFileWriter::StartNetLog( |
152 net::NetLogCaptureMode capture_mode) { | 170 const base::FilePath& log_path, |
| 171 net::NetLogCaptureMode capture_mode, |
| 172 const URLRequestContextGetterList& context_getters) { |
153 DCHECK(thread_checker_.CalledOnValidThread()); | 173 DCHECK(thread_checker_.CalledOnValidThread()); |
154 DCHECK(file_task_runner_); | 174 DCHECK(file_task_runner_); |
155 | 175 |
156 if (state_ != STATE_NOT_LOGGING) | 176 if (state_ != STATE_NOT_LOGGING) |
157 return; | 177 return; |
158 | 178 |
159 if (!log_path.empty()) | 179 if (!log_path.empty()) |
160 log_path_ = log_path; | 180 log_path_ = log_path; |
161 | 181 |
162 DCHECK(!log_path_.empty()); | 182 DCHECK(!log_path_.empty()); |
163 | 183 |
| 184 state_ = STATE_STARTING_LOG; |
| 185 |
| 186 NotifyStateObserversAsync(); |
| 187 |
| 188 std::unique_ptr<base::Value> constants( |
| 189 ChromeNetLog::GetConstants(command_line_string_, channel_string_)); |
| 190 // Instantiate a FileNetLogObserver in unbounded mode. |
| 191 file_net_log_observer_ = net::FileNetLogObserver::CreateUnbounded( |
| 192 file_task_runner_, log_path_, std::move(constants)); |
| 193 |
| 194 net_task_runner_->PostTaskAndReply( |
| 195 FROM_HERE, |
| 196 base::Bind(&CreateNetLogEntriesForActiveObjects, context_getters, |
| 197 base::Unretained(file_net_log_observer_.get())), |
| 198 base::Bind( |
| 199 &NetLogFileWriter::StartNetLogAfterCreateEntriesForActiveObjects, |
| 200 weak_ptr_factory_.GetWeakPtr(), capture_mode)); |
| 201 } |
| 202 |
| 203 void NetLogFileWriter::StartNetLogAfterCreateEntriesForActiveObjects( |
| 204 net::NetLogCaptureMode capture_mode) { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 DCHECK_EQ(STATE_STARTING_LOG, state_); |
| 207 |
164 state_ = STATE_LOGGING; | 208 state_ = STATE_LOGGING; |
165 log_exists_ = true; | 209 log_exists_ = true; |
166 log_capture_mode_known_ = true; | 210 log_capture_mode_known_ = true; |
167 log_capture_mode_ = capture_mode; | 211 log_capture_mode_ = capture_mode; |
168 | 212 |
169 NotifyStateObserversAsync(); | 213 NotifyStateObservers(); |
170 | 214 |
171 std::unique_ptr<base::Value> constants( | 215 file_net_log_observer_->StartObserving(chrome_net_log_, capture_mode); |
172 ChromeNetLog::GetConstants(command_line_string_, channel_string_)); | |
173 file_net_log_observer_ = | |
174 base::MakeUnique<net::FileNetLogObserver>(file_task_runner_); | |
175 file_net_log_observer_->StartObservingUnbounded( | |
176 chrome_net_log_, capture_mode, log_path_, std::move(constants), nullptr); | |
177 } | 216 } |
178 | 217 |
179 void NetLogFileWriter::StopNetLog( | 218 void NetLogFileWriter::StopNetLog( |
180 std::unique_ptr<base::DictionaryValue> polled_data, | 219 std::unique_ptr<base::DictionaryValue> polled_data, |
181 scoped_refptr<net::URLRequestContextGetter> context_getter) { | 220 scoped_refptr<net::URLRequestContextGetter> context_getter) { |
182 DCHECK(thread_checker_.CalledOnValidThread()); | 221 DCHECK(thread_checker_.CalledOnValidThread()); |
183 DCHECK(net_task_runner_); | 222 DCHECK(net_task_runner_); |
184 | 223 |
185 if (state_ != STATE_LOGGING) | 224 if (state_ != STATE_LOGGING) |
186 return; | 225 return; |
(...skipping 26 matching lines...) Expand all Loading... |
213 switch (state_) { | 252 switch (state_) { |
214 case STATE_UNINITIALIZED: | 253 case STATE_UNINITIALIZED: |
215 state_string = "UNINITIALIZED"; | 254 state_string = "UNINITIALIZED"; |
216 break; | 255 break; |
217 case STATE_INITIALIZING: | 256 case STATE_INITIALIZING: |
218 state_string = "INITIALIZING"; | 257 state_string = "INITIALIZING"; |
219 break; | 258 break; |
220 case STATE_NOT_LOGGING: | 259 case STATE_NOT_LOGGING: |
221 state_string = "NOT_LOGGING"; | 260 state_string = "NOT_LOGGING"; |
222 break; | 261 break; |
| 262 case STATE_STARTING_LOG: |
| 263 state_string = "STARTING_LOG"; |
| 264 break; |
223 case STATE_LOGGING: | 265 case STATE_LOGGING: |
224 state_string = "LOGGING"; | 266 state_string = "LOGGING"; |
225 break; | 267 break; |
226 case STATE_STOPPING_LOG: | 268 case STATE_STOPPING_LOG: |
227 state_string = "STOPPING_LOG"; | 269 state_string = "STOPPING_LOG"; |
228 break; | 270 break; |
229 } | 271 } |
230 dict->SetString("state", state_string); | 272 dict->SetString("state", state_string); |
231 | 273 |
232 dict->SetBoolean("logExists", log_exists_); | 274 dict->SetBoolean("logExists", log_exists_); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 void NetLogFileWriter::NotifyStateObserversAsync() { | 340 void NetLogFileWriter::NotifyStateObserversAsync() { |
299 DCHECK(thread_checker_.CalledOnValidThread()); | 341 DCHECK(thread_checker_.CalledOnValidThread()); |
300 base::ThreadTaskRunnerHandle::Get()->PostTask( | 342 base::ThreadTaskRunnerHandle::Get()->PostTask( |
301 FROM_HERE, base::Bind(&NetLogFileWriter::NotifyStateObservers, | 343 FROM_HERE, base::Bind(&NetLogFileWriter::NotifyStateObservers, |
302 weak_ptr_factory_.GetWeakPtr())); | 344 weak_ptr_factory_.GetWeakPtr())); |
303 } | 345 } |
304 | 346 |
305 void NetLogFileWriter::SetStateAfterSetUpDefaultLogPath( | 347 void NetLogFileWriter::SetStateAfterSetUpDefaultLogPath( |
306 const DefaultLogPathResults& set_up_default_log_path_results) { | 348 const DefaultLogPathResults& set_up_default_log_path_results) { |
307 DCHECK(thread_checker_.CalledOnValidThread()); | 349 DCHECK(thread_checker_.CalledOnValidThread()); |
308 DCHECK_EQ(state_, STATE_INITIALIZING); | 350 DCHECK_EQ(STATE_INITIALIZING, state_); |
309 | 351 |
310 if (set_up_default_log_path_results.default_log_path_success) { | 352 if (set_up_default_log_path_results.default_log_path_success) { |
311 state_ = STATE_NOT_LOGGING; | 353 state_ = STATE_NOT_LOGGING; |
312 log_path_ = set_up_default_log_path_results.default_log_path; | 354 log_path_ = set_up_default_log_path_results.default_log_path; |
313 log_exists_ = set_up_default_log_path_results.log_exists; | 355 log_exists_ = set_up_default_log_path_results.log_exists; |
314 DCHECK(!log_capture_mode_known_); | 356 DCHECK(!log_capture_mode_known_); |
315 } else { | 357 } else { |
316 state_ = STATE_UNINITIALIZED; | 358 state_ = STATE_UNINITIALIZED; |
317 } | 359 } |
318 NotifyStateObservers(); | 360 NotifyStateObservers(); |
319 } | 361 } |
320 | 362 |
321 void NetLogFileWriter::StopNetLogAfterAddNetInfo( | 363 void NetLogFileWriter::StopNetLogAfterAddNetInfo( |
322 std::unique_ptr<base::DictionaryValue> polled_data) { | 364 std::unique_ptr<base::DictionaryValue> polled_data) { |
323 DCHECK(thread_checker_.CalledOnValidThread()); | 365 DCHECK(thread_checker_.CalledOnValidThread()); |
324 DCHECK_EQ(state_, STATE_STOPPING_LOG); | 366 DCHECK_EQ(STATE_STOPPING_LOG, state_); |
325 | 367 |
326 file_net_log_observer_->StopObserving( | 368 file_net_log_observer_->StopObserving( |
327 std::move(polled_data), | 369 std::move(polled_data), |
328 base::Bind(&NetLogFileWriter::ResetObserverThenSetStateNotLogging, | 370 base::Bind(&NetLogFileWriter::ResetObserverThenSetStateNotLogging, |
329 weak_ptr_factory_.GetWeakPtr())); | 371 weak_ptr_factory_.GetWeakPtr())); |
330 } | 372 } |
331 | 373 |
332 void NetLogFileWriter::ResetObserverThenSetStateNotLogging() { | 374 void NetLogFileWriter::ResetObserverThenSetStateNotLogging() { |
333 DCHECK(thread_checker_.CalledOnValidThread()); | 375 DCHECK(thread_checker_.CalledOnValidThread()); |
334 file_net_log_observer_.reset(); | 376 file_net_log_observer_.reset(); |
335 state_ = STATE_NOT_LOGGING; | 377 state_ = STATE_NOT_LOGGING; |
336 | 378 |
337 NotifyStateObservers(); | 379 NotifyStateObservers(); |
338 } | 380 } |
339 | 381 |
340 } // namespace net_log | 382 } // namespace net_log |
OLD | NEW |