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