OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "headless/lib/browser/headless_net_log.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/files/scoped_file.h" |
| 13 #include "base/values.h" |
| 14 #include "build/build_config.h" |
| 15 #include "content/public/common/content_switches.h" |
| 16 #include "net/log/net_log_util.h" |
| 17 #include "net/log/write_to_file_net_log_observer.h" |
| 18 |
| 19 namespace headless { |
| 20 namespace { |
| 21 |
| 22 std::unique_ptr<base::Value> GetHeadlessConstants() { |
| 23 std::unique_ptr<base::DictionaryValue> constants_dict = |
| 24 net::GetNetConstants(); |
| 25 |
| 26 // Add a dictionary with client information |
| 27 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 28 |
| 29 dict->SetString("name", "headless"); |
| 30 dict->SetString( |
| 31 "command_line", |
| 32 base::CommandLine::ForCurrentProcess()->GetCommandLineString()); |
| 33 |
| 34 constants_dict->Set("clientInfo", dict); |
| 35 |
| 36 return constants_dict; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 HeadlessNetLog::HeadlessNetLog(const base::FilePath& log_path) { |
| 42 // TODO(mmenke): Other than a different set of constants, this code is |
| 43 // identical to code in ChromeNetLog. Consider merging the code. |
| 44 |
| 45 // Much like logging.h, bypass threading restrictions by using fopen |
| 46 // directly. Have to write on a thread that's shutdown to handle events on |
| 47 // shutdown properly, and posting events to another thread as they occur |
| 48 // would result in an unbounded buffer size, so not much can be gained by |
| 49 // doing this on another thread. It's only used when debugging, so |
| 50 // performance is not a big concern. |
| 51 base::ScopedFILE file; |
| 52 #if defined(OS_WIN) |
| 53 file.reset(_wfopen(log_path.value().c_str(), L"w")); |
| 54 #elif defined(OS_POSIX) |
| 55 file.reset(fopen(log_path.value().c_str(), "w")); |
| 56 #endif |
| 57 |
| 58 if (!file) { |
| 59 LOG(ERROR) << "Could not open file " << log_path.value() |
| 60 << " for net logging"; |
| 61 } else { |
| 62 std::unique_ptr<base::Value> constants(GetHeadlessConstants()); |
| 63 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 64 write_to_file_observer_->StartObserving(this, std::move(file), |
| 65 constants.get(), nullptr); |
| 66 } |
| 67 } |
| 68 |
| 69 HeadlessNetLog::~HeadlessNetLog() { |
| 70 // Remove the observer we own before we're destroyed. |
| 71 if (write_to_file_observer_) |
| 72 write_to_file_observer_->StopObserving(nullptr); |
| 73 } |
| 74 |
| 75 } // namespace headless |
OLD | NEW |