From 0750b144f8d533628e79d230cc37808d0119aed3 Mon Sep 17 00:00:00 2001
From: "gspencer@chromium.org"
 <gspencer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Tue, 2 Nov 2010 21:59:37 +0000
Subject: [PATCH] This moves log output for ChromeOS to safer locations.

For BWSI (incognito) mode, we want to make sure that we don't leak
private information between sessions, so we need to have chrome write
log output to the tmpfs created for that mode.

Also, the chrome log in "logged in" mode should reside in the "log"
subdir instead of in the user data dir.

BUG=http://crosbug.com/6908
TEST=Ran on device in incognito and regular modes, and watched where
log output ended up.

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=64638

Review URL: http://codereview.chromium.org/4194005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64826 0039d316-1c4b-4281-b951-d872f2087c98
---
 chrome/browser/browser_main.cc               | 12 ++-----
 chrome/browser/chromeos/login/login_utils.cc |  4 +--
 chrome/common/env_vars.cc                    |  3 ++
 chrome/common/env_vars.h                     |  1 +
 chrome/common/logging_chrome.cc              | 38 ++++++++++++++++----
 chrome/common/logging_chrome.h               |  7 ++--
 chrome/test/in_process_browser_test.cc       |  5 +++
 7 files changed, 49 insertions(+), 21 deletions(-)

diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc
index f22a6edf97af4..9e33f689bbf6d 100644
--- a/chrome/browser/browser_main.cc
+++ b/chrome/browser/browser_main.cc
@@ -1223,16 +1223,8 @@ int BrowserMain(const MainFunctionParams& parameters) {
     VLOG(1) << "Relaunching browser for user: " << username;
     chromeos::UserManager::Get()->UserLoggedIn(username);
 
-    // Redirect logs.
-    FilePath user_data_dir;
-    PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
-    ProfileManager* profile_manager = g_browser_process->profile_manager();
-    // The default profile will have been changed because the ProfileManager
-    // will process the notification that the UserManager sends out.
-
-    logging::RedirectChromeLogging(
-        user_data_dir.Append(profile_manager->GetCurrentProfileDir()),
-        *(CommandLine::ForCurrentProcess()));
+    // Redirects Chrome logging to the user data dir.
+    logging::RedirectChromeLogging(parsed_command_line);
   }
 #endif
 
diff --git a/chrome/browser/chromeos/login/login_utils.cc b/chrome/browser/chromeos/login/login_utils.cc
index f872733a2a42f..81366459a7e80 100644
--- a/chrome/browser/chromeos/login/login_utils.cc
+++ b/chrome/browser/chromeos/login/login_utils.cc
@@ -143,9 +143,7 @@ void LoginUtilsImpl::CompleteLogin(
   ProfileManager* profile_manager = g_browser_process->profile_manager();
 
   // Switch log file as soon as possible.
-  logging::RedirectChromeLogging(
-      user_data_dir.Append(profile_manager->GetCurrentProfileDir()),
-      *(CommandLine::ForCurrentProcess()));
+  logging::RedirectChromeLogging(*(CommandLine::ForCurrentProcess()));
   btl->AddLoginTimeMarker("LoggingRedirected", false);
 
   // The default profile will have been changed because the ProfileManager
diff --git a/chrome/common/env_vars.cc b/chrome/common/env_vars.cc
index dc2648cc4b9a2..f63d3e4fbfd2f 100644
--- a/chrome/common/env_vars.cc
+++ b/chrome/common/env_vars.cc
@@ -14,6 +14,9 @@ const char kHeadless[] = "CHROME_HEADLESS";
 // The name of the log file.
 const char kLogFileName[] = "CHROME_LOG_FILE";
 
+// The name of the session log directory when logged in to ChromeOS.
+const char kSessionLogDir[] = "CHROMEOS_SESSION_LOG_DIR";
+
 // If this environment variable is set, Chrome on Windows will log
 // to Event Tracing for Windows.
 const char kEtwLogging[] = "CHROME_ETW_LOGGING";
diff --git a/chrome/common/env_vars.h b/chrome/common/env_vars.h
index e9984d2ac835c..320a0b4456a52 100644
--- a/chrome/common/env_vars.h
+++ b/chrome/common/env_vars.h
@@ -12,6 +12,7 @@ namespace env_vars {
 
 extern const char kHeadless[];
 extern const char kLogFileName[];
+extern const char kSessionLogDir[];
 extern const char kEtwLogging[];
 extern const char kShowRestart[];
 extern const char kRestartInfo[];
diff --git a/chrome/common/logging_chrome.cc b/chrome/common/logging_chrome.cc
index 6106e43d7d251..48bcc37a973e9 100644
--- a/chrome/common/logging_chrome.cc
+++ b/chrome/common/logging_chrome.cc
@@ -158,12 +158,13 @@ FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) {
   // starting a new log, then delete the old symlink and make a new
   // one to a fresh log file.
   FilePath target_path;
-  if (new_log || !file_util::PathExists(symlink_path)) {
+  bool symlink_exists = file_util::PathExists(symlink_path);
+  if (new_log || !symlink_exists) {
     target_path = GenerateTimestampedName(symlink_path, base::Time::Now());
 
     // We don't care if the unlink fails; we're going to continue anyway.
     if (unlink(symlink_path.value().c_str()) == -1) {
-      if (new_log) // only warn if we might expect it to succeed.
+      if (symlink_exists) // only warn if we might expect it to succeed.
         PLOG(WARNING) << "Unable to unlink " << symlink_path.value();
     }
     if (symlink(target_path.value().c_str(),
@@ -193,12 +194,29 @@ void RemoveSymlinkAndLog(const FilePath& link_path,
 
 }  // anonymous namespace
 
-void RedirectChromeLogging(const FilePath& new_log_dir,
-                           const CommandLine& command_line) {
+FilePath GetSessionLogFile(const CommandLine& command_line) {
+  FilePath log_dir;
+  std::string log_dir_str;
+  scoped_ptr<base::Environment> env(base::Environment::Create());
+  if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) &&
+      !log_dir_str.empty()) {
+    log_dir = FilePath(log_dir_str);
+  } else {
+    PathService::Get(chrome::DIR_USER_DATA, &log_dir);
+    FilePath login_profile =
+        command_line.GetSwitchValuePath(switches::kLoginProfile);
+    log_dir = log_dir.Append(login_profile);
+  }
+  return log_dir.Append(GetLogFileName().BaseName());
+}
+
+void RedirectChromeLogging(const CommandLine& command_line) {
   DCHECK(!chrome_logging_redirected_) <<
     "Attempted to redirect logging when it was already initialized.";
-  FilePath orig_log_path = GetLogFileName();
-  FilePath log_path = new_log_dir.Append(orig_log_path.BaseName());
+
+  // Redirect logs to the session log directory, if set.  Otherwise
+  // defaults to the profile dir.
+  FilePath log_path = GetSessionLogFile(command_line);
 
   // Always force a new symlink when redirecting.
   FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
@@ -215,6 +233,8 @@ void RedirectChromeLogging(const FilePath& new_log_dir,
     chrome_logging_redirected_ = true;
   }
 }
+
+
 #endif
 
 void InitChromeLogging(const CommandLine& command_line,
@@ -229,6 +249,12 @@ void InitChromeLogging(const CommandLine& command_line,
   FilePath log_path = GetLogFileName();
 
 #if defined(OS_CHROMEOS)
+  // For BWSI (Incognito) logins, we want to put the logs in the user
+  // profile directory that is created for the temporary session instead
+  // of in the system log directory, for privacy reasons.
+  if (command_line.HasSwitch(switches::kGuestSession))
+    log_path = GetSessionLogFile(command_line);
+
   // On ChromeOS we log to the symlink.  We force creation of a new
   // symlink if we've been asked to delete the old log, since that
   // indicates the start of a new session.
diff --git a/chrome/common/logging_chrome.h b/chrome/common/logging_chrome.h
index c8dac4214ae27..6fc138921c989 100644
--- a/chrome/common/logging_chrome.h
+++ b/chrome/common/logging_chrome.h
@@ -37,8 +37,11 @@ void InitChromeLogging(const CommandLine& command_line,
                        OldFileDeletionState delete_old_log_file);
 
 #if defined(OS_CHROMEOS)
-void RedirectChromeLogging(const FilePath& new_log_dir,
-                           const CommandLine& command_line);
+// Get the log file location.
+FilePath GetSessionLogFile(const CommandLine& command_line);
+
+// Redirects chrome logging to the appropriate session log dir.
+void RedirectChromeLogging(const CommandLine& command_line);
 #endif
 
 // Call when done using logging for Chrome.
diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc
index 8275fd6573997..e8efda869cb0c 100644
--- a/chrome/test/in_process_browser_test.cc
+++ b/chrome/test/in_process_browser_test.cc
@@ -29,6 +29,7 @@
 #include "chrome/common/chrome_constants.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/chrome_switches.h"
+#include "chrome/common/logging_chrome.h"
 #include "chrome/common/main_function_params.h"
 #include "chrome/common/notification_registrar.h"
 #include "chrome/common/notification_type.h"
@@ -190,6 +191,10 @@ void InProcessBrowserTest::SetUp() {
 
 #if defined(OS_CHROMEOS)
   chromeos::CrosLibrary::Get()->GetTestApi()->SetUseStubImpl();
+
+  // Make sure that the log directory exists.
+  FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName();
+  file_util::CreateDirectory(log_dir);
 #endif  // defined(OS_CHROMEOS)
 
   SandboxInitWrapper sandbox_wrapper;
-- 
GitLab