+// Initializes the user data dir. Must be called before InitializeLocalState().
+void InitializeUserDataDir() {
+ CommandLine* command_line = CommandLine::ForCurrentProcess();
+ base::FilePath user_data_dir =
+ command_line->GetSwitchValuePath(switches::kUserDataDir);
+ std::string process_type =
+ command_line->GetSwitchValueASCII(switches::kProcessType);
+
+#if defined(OS_LINUX)
+ // On Linux, Chrome does not support running multiple copies under different
+ // DISPLAYs, so the profile directory can be specified in the environment to
+ // support the virtual desktop use-case.
+ if (user_data_dir.empty()) {
+ std::string user_data_dir_string;
+ scoped_ptr<base::Environment> environment(base::Environment::Create());
+ if (environment->GetVar("CHROME_USER_DATA_DIR", &user_data_dir_string) &&
+ IsStringUTF8(user_data_dir_string)) {
+ user_data_dir = base::FilePath::FromUTF8Unsafe(user_data_dir_string);
+ }
+ }
+#endif
+#if defined(OS_MACOSX) || defined(OS_WIN)
+ policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
+#endif
+
+ const bool specified_directory_was_invalid = !user_data_dir.empty() &&
+ !PathService::OverrideAndCreateIfNeeded(chrome::DIR_USER_DATA,
+ user_data_dir, chrome::ProcessNeedsProfileDir(process_type));
+ // Save inaccessible or invalid paths so the user may be prompted later.
+ if (specified_directory_was_invalid)
+ chrome::SetInvalidSpecifiedUserDataDir(user_data_dir);
+
+ // Getting the user data directory can fail if the directory isn't creatable.
+ // ProcessSingleton needs a real user data directory on Mac/Linux, so it's
+ // better to fail here than fail mysteriously elsewhere.
+ CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
+ << "Must be able to get user data directory!";
+
+ // Append the fallback user data directory to the commandline. Otherwise,
+ // child or service processes will attempt to use the invalid directory.
+ if (specified_directory_was_invalid)
+ command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
+}
+