2015-06-05 20:24:38 +05:30
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-05-18 15:58:12 -07:00
|
|
|
#include "brightray/browser/net_log.h"
|
2015-06-05 20:24:38 +05:30
|
|
|
|
2017-08-12 20:27:48 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2015-06-05 20:24:38 +05:30
|
|
|
#include "base/command_line.h"
|
2015-08-11 18:29:55 +08:00
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/values.h"
|
2017-08-24 15:08:05 +02:00
|
|
|
#include "net/log/file_net_log_observer.h"
|
2015-06-05 20:24:38 +05:30
|
|
|
#include "net/log/net_log_util.h"
|
2018-04-08 23:02:24 +05:30
|
|
|
#include "services/network/public/cpp/network_switches.h"
|
2015-08-11 18:29:55 +08:00
|
|
|
|
|
|
|
namespace brightray {
|
2015-06-05 20:24:38 +05:30
|
|
|
|
2015-06-06 14:33:07 +05:30
|
|
|
namespace {
|
|
|
|
|
2016-05-23 10:59:07 +09:00
|
|
|
std::unique_ptr<base::DictionaryValue> GetConstants() {
|
|
|
|
std::unique_ptr<base::DictionaryValue> constants = net::GetNetConstants();
|
2015-06-06 14:33:07 +05:30
|
|
|
|
|
|
|
// Adding client information to constants dictionary.
|
2018-04-12 08:48:32 -04:00
|
|
|
auto client_info = std::make_unique<base::DictionaryValue>();
|
2015-06-09 09:51:38 +08:00
|
|
|
client_info->SetString(
|
|
|
|
"command_line",
|
2015-06-06 14:33:07 +05:30
|
|
|
base::CommandLine::ForCurrentProcess()->GetCommandLineString());
|
|
|
|
|
2017-08-12 20:27:48 +03:00
|
|
|
constants->Set("clientInfo", std::move(client_info));
|
2015-06-09 09:51:38 +08:00
|
|
|
return constants;
|
2015-06-06 14:33:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-17 21:56:12 -04:00
|
|
|
NetLog::NetLog() {}
|
2015-09-07 22:44:13 +05:30
|
|
|
|
|
|
|
NetLog::~NetLog() {
|
2018-06-18 18:45:58 -07:00
|
|
|
StopDynamicLogging();
|
|
|
|
StopLogging();
|
2015-09-07 22:44:13 +05:30
|
|
|
}
|
|
|
|
|
2017-08-24 15:08:05 +02:00
|
|
|
void NetLog::StartLogging() {
|
2018-04-17 15:41:47 -07:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
2018-04-08 23:02:24 +05:30
|
|
|
if (!command_line->HasSwitch(network::switches::kLogNetLog))
|
2015-08-11 18:29:55 +08:00
|
|
|
return;
|
|
|
|
|
2018-06-19 11:52:17 +10:00
|
|
|
base::FilePath log_path =
|
|
|
|
command_line->GetSwitchValuePath(network::switches::kLogNetLog);
|
2018-06-18 18:45:58 -07:00
|
|
|
if (log_path.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<base::Value> constants(GetConstants()); // Net constants
|
2018-05-24 14:46:54 -07:00
|
|
|
net::NetLogCaptureMode capture_mode = net::NetLogCaptureMode::Default();
|
2017-08-24 15:08:05 +02:00
|
|
|
|
2017-11-02 19:30:16 +05:30
|
|
|
file_net_log_observer_ =
|
2017-08-24 15:08:05 +02:00
|
|
|
net::FileNetLogObserver::CreateUnbounded(log_path, std::move(constants));
|
|
|
|
file_net_log_observer_->StartObserving(this, capture_mode);
|
2015-06-05 20:24:38 +05:30
|
|
|
}
|
|
|
|
|
2018-06-18 18:45:58 -07:00
|
|
|
void NetLog::StopLogging() {
|
|
|
|
if (!file_net_log_observer_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
file_net_log_observer_->StopObserving(nullptr, base::OnceClosure());
|
|
|
|
file_net_log_observer_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetLog::StartDynamicLogging(const base::FilePath& log_path) {
|
|
|
|
if (dynamic_file_net_log_observer_ || log_path.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
dynamic_file_net_log_path_ = log_path;
|
|
|
|
|
|
|
|
std::unique_ptr<base::Value> constants(GetConstants()); // Net constants
|
|
|
|
net::NetLogCaptureMode capture_mode = net::NetLogCaptureMode::Default();
|
|
|
|
|
|
|
|
dynamic_file_net_log_observer_ = net::FileNetLogObserver::CreateUnbounded(
|
|
|
|
dynamic_file_net_log_path_, std::move(constants));
|
|
|
|
dynamic_file_net_log_observer_->StartObserving(this, capture_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetLog::IsDynamicLogging() {
|
|
|
|
return !!dynamic_file_net_log_observer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::FilePath NetLog::GetDynamicLoggingPath() {
|
|
|
|
return dynamic_file_net_log_path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetLog::StopDynamicLogging(base::OnceClosure callback) {
|
|
|
|
if (!dynamic_file_net_log_observer_) {
|
|
|
|
if (callback)
|
|
|
|
std::move(callback).Run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic_file_net_log_observer_->StopObserving(nullptr, std::move(callback));
|
|
|
|
dynamic_file_net_log_observer_.reset();
|
|
|
|
|
|
|
|
dynamic_file_net_log_path_ = base::FilePath();
|
|
|
|
}
|
|
|
|
|
2015-06-05 20:24:38 +05:30
|
|
|
} // namespace brightray
|