OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/user_data_dir_extractor.h" | 5 #include "chrome/browser/user_data_dir_extractor.h" |
6 | 6 |
7 #include "base/command_line.h" | |
7 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "chrome/browser/ui/simple_message_box.h" | |
10 #include "chrome/common/chrome_paths.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 | |
11 | 29 |
12 namespace chrome { | 30 namespace chrome { |
13 | 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(); | |
sky
2014/02/13 01:35:07
I'm not sure we've ever done this outside of tests
msw
2014/02/13 01:49:33
Skipping this causes a DCHECK and memory leak in R
msw
2014/02/13 01:49:33
Seems like it's called in ChromeMainDelegate::Proc
sky
2014/02/13 18:01:42
Leave what you have in.
| |
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 | |
14 base::FilePath GetUserDataDir(const content::MainFunctionParams& parameters) { | 70 base::FilePath GetUserDataDir(const content::MainFunctionParams& parameters) { |
15 base::FilePath user_data_dir; | 71 if (custom_get_user_data_dir_callback) |
72 return custom_get_user_data_dir_callback->Run(); | |
16 | 73 |
17 // Getting the user data dir can fail if the directory isn't creatable, for | 74 base::FilePath user_data_dir = |
18 // example: on Windows we bring up a dialog prompting the user to pick a | 75 parameters.command_line.GetSwitchValuePath(switches::kUserDataDir); |
19 // different directory. However, ProcessSingleton needs a real user_data_dir | 76 std::string process_type = |
20 // on Mac/Linux, so it's better to fail here than fail mysteriously elsewhere. | 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. | |
21 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) | 106 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) |
22 << "Must be able to get user data directory!"; | 107 << "Must be able to get user data directory!"; |
23 return user_data_dir; | 108 return user_data_dir; |
24 } | 109 } |
25 | 110 |
26 } // namespace chrome | 111 } // namespace chrome |
OLD | NEW |