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

Side by Side Diff: chrome/browser/user_data_dir_extractor.cc

Issue 174253002: Initialize chrome::DIR_USER_DATA early on for service processes, etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use dynamic_annotations_win64 for common_constants_win64. Created 6 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/user_data_dir_extractor.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/path_service.h"
11 #include "chrome/browser/ui/simple_message_box.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/chrome_paths_internal.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "components/startup_metric_utils/startup_metric_utils.h"
16 #include "content/public/common/main_function_params.h"
17 #include "grit/chromium_strings.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21
22 #if defined(OS_LINUX)
23 #include "base/environment.h"
24 #endif
25
26 #if defined(OS_MACOSX) || defined(OS_WIN)
27 #include "chrome/browser/policy/policy_path_parser.h"
28 #endif
29
30 namespace chrome {
31
32 namespace {
33
34 GetUserDataDirCallback* custom_get_user_data_dir_callback = NULL;
35
36 void ShowUserDataDirWarning(const base::FilePath& user_data_dir) {
37 startup_metric_utils::SetNonBrowserUIDisplayed();
38
39 // Ensure the ResourceBundle is initialized for string resource access.
40 bool cleanup_resource_bundle = false;
41 if (!ResourceBundle::HasSharedInstance()) {
42 cleanup_resource_bundle = true;
43 std::string locale = l10n_util::GetApplicationLocale(std::string());
44 const char kUserDataDirDialogFallbackLocale[] = "en-US";
45 if (locale.empty())
46 locale = kUserDataDirDialogFallbackLocale;
47 ResourceBundle::InitSharedInstanceWithLocale(locale, NULL);
48 }
49
50 const base::string16& title =
51 l10n_util::GetStringUTF16(IDS_CANT_WRITE_USER_DIRECTORY_TITLE);
52 const base::string16& message =
53 l10n_util::GetStringFUTF16(IDS_CANT_WRITE_USER_DIRECTORY_SUMMARY,
54 user_data_dir.LossyDisplayName());
55
56 if (cleanup_resource_bundle)
57 ResourceBundle::CleanupSharedInstance();
58
59 // More complex dialogs cannot be shown before the earliest calls here.
60 ShowMessageBox(NULL, title, message, chrome::MESSAGE_BOX_TYPE_WARNING);
61 }
62
63 } // namespace
64
65 void InstallCustomGetUserDataDirCallbackForTest(
66 GetUserDataDirCallback* callback) {
67 custom_get_user_data_dir_callback = callback;
68 }
69
70 base::FilePath GetUserDataDir(const content::MainFunctionParams& parameters) {
71 if (custom_get_user_data_dir_callback)
72 return custom_get_user_data_dir_callback->Run();
73
74 base::FilePath user_data_dir =
75 parameters.command_line.GetSwitchValuePath(switches::kUserDataDir);
76 std::string process_type =
77 parameters.command_line.GetSwitchValueASCII(switches::kProcessType);
78
79 #if defined(OS_LINUX)
80 // On Linux, Chrome does not support running multiple copies under different
81 // DISPLAYs, so the profile directory can be specified in the environment to
82 // support the virtual desktop use-case.
83 if (user_data_dir.empty()) {
84 std::string user_data_dir_string;
85 scoped_ptr<base::Environment> environment(base::Environment::Create());
86 if (environment->GetVar("CHROME_USER_DATA_DIR", &user_data_dir_string) &&
87 IsStringUTF8(user_data_dir_string)) {
88 user_data_dir = base::FilePath::FromUTF8Unsafe(user_data_dir_string);
89 }
90 }
91 #endif
92 #if defined(OS_MACOSX) || defined(OS_WIN)
93 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
94 #endif
95
96 // If the directory does not exist and cannot be created, prompt the user.
97 if (!user_data_dir.empty() &&
98 !PathService::OverrideAndCreateIfNeeded(chrome::DIR_USER_DATA,
99 user_data_dir, chrome::ProcessNeedsProfileDir(process_type))) {
100 ShowUserDataDirWarning(user_data_dir);
101 }
102
103 // Getting the user data directory can fail if the directory isn't creatable.
104 // ProcessSingleton needs a real user data directory on Mac/Linux, so it's
105 // better to fail here than fail mysteriously elsewhere.
106 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
107 << "Must be able to get user data directory!";
108 return user_data_dir;
109 }
110
111 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/user_data_dir_extractor.h ('k') | chrome/browser/user_data_dir_extractor_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698