Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3936)

Unified Diff: chrome/browser/net/net_log_temp_file.cc

Issue 11828036: First cut at UI for saving net_logs data into a temporary file on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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,125 @@
+// 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 "base/file_util.h"
+#include "base/values.h"
+#include "chrome/browser/net/chrome_net_log.h"
+#include "chrome/browser/net/net_log_logger.h"
+
+NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log)
+ : state_(STATE_UNINITIALIZED),
+ log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")),
+ chrome_net_log_(chrome_net_log) {
+}
+
+NetLogTempFile::~NetLogTempFile() {
+ if (net_log_logger_.get())
+ net_log_logger_->StopObserving();
+}
+
+void NetLogTempFile::ProcessCommand(Command command) {
mmenke 2013/01/23 19:01:27 Think you should DCHECK that all of these are call
ramant (doing other things) 2013/01/24 03:23:55 Done.
+ // 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());
mmenke 2013/01/23 19:01:27 nit: Maybe LossyDisplayName() would be better?
ramant (doing other things) 2013/01/24 03:23:55 Done.
+#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;
+
+ if (GetNetExportLog(&log_path_))
+ state_ = STATE_ALLOW_START_SEND;
+ else
+ state_ = STATE_ALLOW_START;
mmenke 2013/01/23 19:01:27 If file_util::GetTempDir fails, we'll end up in ST
ramant (doing other things) 2013/01/24 03:23:55 Done.
+}
+
+void NetLogTempFile::StartNetLog() {
+ if (state_ == STATE_ALLOW_STOP)
+ return;
+
+ // Try to make sure we can create the file.
+ // TODO(rtenneti): Find a better for doing the following.
+ FILE* fp = file_util::OpenFile(log_path_, "w");
+ if (!fp)
+ return;
+ file_util::CloseFile(fp);
+
+ 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(&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;
+}
+
+bool NetLogTempFile::GetNetExportLog(FilePath* path) {
+ FilePath temp_dir;
+ if (!file_util::GetTempDir(&temp_dir))
+ return false;
+
+ *path = temp_dir.Append(log_filename_);
+ return file_util::PathExists(*path);
+}

Powered by Google App Engine
This is Rietveld 408576698