| Index: chrome/browser/net/net_log_temp_file.cc
|
| ===================================================================
|
| --- chrome/browser/net/net_log_temp_file.cc (revision 0)
|
| +++ chrome/browser/net/net_log_temp_file.cc (revision 0)
|
| @@ -0,0 +1,178 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/net/net_log_temp_file.h"
|
| +
|
| +#include <stdio.h>
|
| +
|
| +#include "base/file_util.h"
|
| +#include "base/values.h"
|
| +#include "chrome/browser/net/chrome_net_log.h"
|
| +#include "chrome/browser/net/net_log_logger.h"
|
| +
|
| +namespace {
|
| +
|
| +bool GetNetExportDir(FilePath* path) {
|
| + // We will try to create chrome-net-export (or make sure chrome-net-export
|
| + // exists) twice in file_util::GetTempDir.
|
| + for (int i = 0; i < 2; ++i) {
|
| + FilePath temp_dir;
|
| + if (!file_util::GetTempDir(&temp_dir))
|
| + continue;
|
| +
|
| + FilePath net_export_dir = temp_dir.Append(
|
| + FILE_PATH_LITERAL("chrome-net-export"));
|
| + if (file_util::PathExists(net_export_dir)) {
|
| + *path = net_export_dir;
|
| + return true;
|
| + }
|
| +
|
| + if (!file_util::CreateDirectory(net_export_dir))
|
| + continue;
|
| +
|
| + *path = net_export_dir;
|
| + return true;
|
| + }
|
| +
|
| + return false;
|
| +}
|
| +
|
| +bool GetNetExportLog(const FilePath::StringType& log_filename, FilePath* path) {
|
| + FilePath net_export_dir;
|
| + if (!GetNetExportDir(&net_export_dir))
|
| + return false;
|
| +
|
| + *path = net_export_dir.Append(log_filename);
|
| + return true;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log)
|
| + : state_(STATE_UNINITIALIZED),
|
| + log_filename_(FILE_PATH_LITERAL("log.json")),
|
| + chrome_net_log_(chrome_net_log) {
|
| +}
|
| +
|
| +NetLogTempFile::~NetLogTempFile() {
|
| + if (net_log_logger_.get())
|
| + net_log_logger_->StopObserving();
|
| +}
|
| +
|
| +void NetLogTempFile::ProcessCommand(Command command) {
|
| + // Determine the state_ if it is not set.
|
| + if (state_ == STATE_UNINITIALIZED)
|
| + Init();
|
| +
|
| + switch (command) {
|
| + case DO_START:
|
| + StartNetLog();
|
| + break;
|
| + case DO_STOP:
|
| + StopNetLog();
|
| + break;
|
| + default:
|
| + break;
|
| + }
|
| +}
|
| +
|
| +DictionaryValue* NetLogTempFile::GetState() {
|
| + // Determine the state_ if it is not set.
|
| + if (state_ == STATE_UNINITIALIZED)
|
| + Init();
|
| +
|
| + base::DictionaryValue* dict = new base::DictionaryValue;
|
| +
|
| +#ifndef NDEBUG
|
| + dict->SetString("file", log_path_.MaybeAsASCII());
|
| +#endif // NDEBUG
|
| +
|
| + switch (state_) {
|
| + case STATE_ALLOW_STOP:
|
| + dict->SetString("state", "ALLOW_STOP");
|
| + break;
|
| + case STATE_ALLOW_START_SEND:
|
| + dict->SetString("state", "ALLOW_START_SEND");
|
| + break;
|
| + default:
|
| + dict->SetString("state", "ALLOW_START");
|
| + break;
|
| + }
|
| + return dict;
|
| +}
|
| +
|
| +void NetLogTempFile::Init() {
|
| + if (state_ != STATE_UNINITIALIZED)
|
| + return;
|
| +
|
| + FilePath net_export_log;
|
| + if (!GetNetExportLog(log_filename_, &net_export_log)) {
|
| + state_ = STATE_ALLOW_START;
|
| + return;
|
| + }
|
| +
|
| + log_path_ = net_export_log;
|
| + if (file_util::PathExists(log_path_))
|
| + state_ = STATE_ALLOW_START_SEND;
|
| + else
|
| + state_ = STATE_ALLOW_START;
|
| +}
|
| +
|
| +void NetLogTempFile::StartNetLog() {
|
| + if (state_ == STATE_ALLOW_STOP)
|
| + return;
|
| +
|
| + // Call GetNetExportLog to make sure "chrome-net-export" is created in
|
| + // file_util::GetTempDir (in case the temporary directory is deleted by the
|
| + // time user clicks on START button).
|
| + FilePath net_export_log;
|
| + if (!GetNetExportLog(log_filename_, &net_export_log))
|
| + return;
|
| +
|
| + // Try to make sure we can create the file.
|
| + FILE* fp = file_util::OpenFile(net_export_log, "w");
|
| + if (!fp) {
|
| + LOG(ERROR) << "Could not open file " << net_export_log.value()
|
| + << " for net logging";
|
| + return;
|
| + } else {
|
| + file_util::CloseFile(fp);
|
| + }
|
| +
|
| + log_path_ = net_export_log;
|
| + net_log_logger_.reset(new NetLogLogger(log_path_));
|
| + net_log_logger_->StartObserving(chrome_net_log_);
|
| + state_ = STATE_ALLOW_STOP;
|
| +}
|
| +
|
| +void NetLogTempFile::StopNetLog() {
|
| + if (state_ != STATE_ALLOW_STOP)
|
| + return;
|
| +
|
| + net_log_logger_->StopObserving();
|
| + net_log_logger_.reset();
|
| + state_ = STATE_ALLOW_START_SEND;
|
| +}
|
| +
|
| +bool NetLogTempFile::GetNetLogToSend(FilePath* path) {
|
| + if (state_ != STATE_ALLOW_START_SEND)
|
| + return false;
|
| +
|
| + FilePath net_export_log;
|
| + if (!GetNetExportLog(log_filename_, &net_export_log))
|
| + return false;
|
| +
|
| + if (!file_util::PathExists(net_export_log))
|
| + return false;
|
| +
|
| +#if defined(OS_POSIX)
|
| + // Users, group and others can read, write and traverse.
|
| + int mode = file_util::FILE_PERMISSION_MASK;
|
| + file_util::SetPosixFilePermissions(net_export_log, mode);
|
| +#endif // defined(OS_POSIX)
|
| +
|
| + *path = net_export_log;
|
| + return true;
|
| +}
|
| +
|
|
|