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,180 @@ |
+// 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> |
mmenke
2013/01/18 15:58:00
Is this needed? We use FILE*'s, but no stdio func
ramant (doing other things)
2013/01/18 23:22:33
Done.
|
+ |
+#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(const FilePath::StringType& chrome_net_export, |
+ FilePath* path) { |
+ // We will try to create chrome_net_export (or make sure chrome_net_export |
+ // directory exists) directory 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(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& chrome_net_export, |
+ const FilePath::StringType& log_filename, |
+ FilePath* path) { |
+ FilePath net_export_dir; |
+ if (!GetNetExportDir(chrome_net_export, &net_export_dir)) |
+ return false; |
+ |
+ *path = net_export_dir.Append(log_filename); |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) |
+ : state_(STATE_UNINITIALIZED), |
+ chrome_net_export_(FILE_PATH_LITERAL("chrome-net-export")), |
+ log_filename_(FILE_PATH_LITERAL("log.json")), |
mmenke
2013/01/18 15:58:00
Don't think a pathname + a file name really gets u
ramant (doing other things)
2013/01/18 23:22:33
Done.
|
+ 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(chrome_net_export_, 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(chrome_net_export_, log_filename_, &net_export_log)) |
+ return; |
+ |
+ // Try to make sure we can create the file. |
+ FILE* fp = file_util::OpenFile(net_export_log, "w"); |
mmenke
2013/01/18 15:58:00
Fine for now, but I'd like to look into a better w
ramant (doing other things)
2013/01/18 23:22:33
Added a TODO.
|
+ if (!fp) { |
+ LOG(ERROR) << "Could not open file " << net_export_log.value() |
+ << " for net logging"; |
+ return; |
+ } else { |
+ file_util::CloseFile(fp); |
mmenke
2013/01/18 15:58:00
nit: Fix indent.
ramant (doing other things)
2013/01/18 23:22:33
Done.
|
+ } |
+ |
+ 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::GetFilePath(FilePath* path) { |
+ if (state_ != STATE_ALLOW_START_SEND) |
+ return false; |
+ |
+ FilePath net_export_log; |
+ if (!GetNetExportLog(chrome_net_export_, 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; |
+} |