net_log.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "brightray/browser/net_log.h"
  5. #include <utility>
  6. #include "base/callback.h"
  7. #include "base/command_line.h"
  8. #include "base/files/file_path.h"
  9. #include "base/values.h"
  10. #include "content/public/common/content_switches.h"
  11. #include "net/log/file_net_log_observer.h"
  12. #include "net/log/net_log_util.h"
  13. namespace brightray {
  14. namespace {
  15. std::unique_ptr<base::DictionaryValue> GetConstants() {
  16. std::unique_ptr<base::DictionaryValue> constants = net::GetNetConstants();
  17. // Adding client information to constants dictionary.
  18. auto client_info = std::make_unique<base::DictionaryValue>();
  19. client_info->SetString(
  20. "command_line",
  21. base::CommandLine::ForCurrentProcess()->GetCommandLineString());
  22. constants->Set("clientInfo", std::move(client_info));
  23. return constants;
  24. }
  25. } // namespace
  26. NetLog::NetLog() {}
  27. NetLog::~NetLog() {
  28. if (file_net_log_observer_) {
  29. file_net_log_observer_->StopObserving(nullptr, base::Closure());
  30. file_net_log_observer_.reset();
  31. }
  32. }
  33. void NetLog::StartLogging() {
  34. auto* command_line = base::CommandLine::ForCurrentProcess();
  35. if (!command_line->HasSwitch(switches::kLogNetLog))
  36. return;
  37. base::FilePath log_path =
  38. command_line->GetSwitchValuePath(switches::kLogNetLog);
  39. std::unique_ptr<base::Value> constants(GetConstants());
  40. net::NetLogCaptureMode capture_mode = net::NetLogCaptureMode::Default();
  41. file_net_log_observer_ =
  42. net::FileNetLogObserver::CreateUnbounded(log_path, std::move(constants));
  43. file_net_log_observer_->StartObserving(this, capture_mode);
  44. }
  45. } // namespace brightray