Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / net / chrome_net_log.cc
blob373fc7f46e5d163955fa376b32377008549bd9d8
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/net/chrome_net_log.h"
7 #include <stdio.h>
9 #include "base/command_line.h"
10 #include "base/files/scoped_file.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/values.h"
15 #include "chrome/browser/net/net_log_temp_file.h"
16 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "content/public/common/content_switches.h"
19 #include "net/log/trace_net_log_observer.h"
20 #include "net/log/write_to_file_net_log_observer.h"
22 namespace {
24 net::NetLogCaptureMode GetCaptureModeFromCommandLine(
25 const base::CommandLine& command_line) {
26 if (command_line.HasSwitch(switches::kNetLogCaptureMode)) {
27 std::string capture_mode_string =
28 command_line.GetSwitchValueASCII(switches::kNetLogCaptureMode);
30 if (capture_mode_string == "Default")
31 return net::NetLogCaptureMode::Default();
32 if (capture_mode_string == "IncludeCookiesAndCredentials")
33 return net::NetLogCaptureMode::IncludeCookiesAndCredentials();
34 if (capture_mode_string == "IncludeSocketBytes")
35 return net::NetLogCaptureMode::IncludeSocketBytes();
37 LOG(ERROR) << "Unrecognized value for --" << switches::kNetLogCaptureMode;
40 return net::NetLogCaptureMode::Default();
43 } // namespace
45 ChromeNetLog::ChromeNetLog()
46 : net_log_temp_file_(new NetLogTempFile(this)) {
47 const base::CommandLine& command_line =
48 *base::CommandLine::ForCurrentProcess();
50 if (command_line.HasSwitch(switches::kLogNetLog)) {
51 base::FilePath log_path =
52 command_line.GetSwitchValuePath(switches::kLogNetLog);
53 // Much like logging.h, bypass threading restrictions by using fopen
54 // directly. Have to write on a thread that's shutdown to handle events on
55 // shutdown properly, and posting events to another thread as they occur
56 // would result in an unbounded buffer size, so not much can be gained by
57 // doing this on another thread. It's only used when debugging Chrome, so
58 // performance is not a big concern.
59 base::ScopedFILE file;
60 #if defined(OS_WIN)
61 file.reset(_wfopen(log_path.value().c_str(), L"w"));
62 #elif defined(OS_POSIX)
63 file.reset(fopen(log_path.value().c_str(), "w"));
64 #endif
66 if (!file) {
67 LOG(ERROR) << "Could not open file " << log_path.value()
68 << " for net logging";
69 } else {
70 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants());
71 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
73 net::NetLogCaptureMode capture_mode =
74 GetCaptureModeFromCommandLine(command_line);
75 write_to_file_observer_->set_capture_mode(capture_mode);
77 write_to_file_observer_->StartObserving(this, file.Pass(),
78 constants.get(), nullptr);
82 trace_net_log_observer_.reset(new net::TraceNetLogObserver());
83 trace_net_log_observer_->WatchForTraceStart(this);
86 ChromeNetLog::~ChromeNetLog() {
87 net_log_temp_file_.reset();
88 // Remove the observers we own before we're destroyed.
89 if (write_to_file_observer_)
90 write_to_file_observer_->StopObserving(nullptr);
91 if (trace_net_log_observer_)
92 trace_net_log_observer_->StopWatchForTraceStart();