1 // Copyright 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.
5 #include "chrome/browser/sxs_linux.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/important_file_writer.h"
13 #include "base/logging.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_result_codes.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/chrome_version_info.h"
21 #include "content/public/browser/browser_thread.h"
25 const char kChannelsFileName
[] = "Channels";
27 std::string
GetChannelMarkForThisExecutable() {
28 std::string product_channel_name
;
29 chrome::VersionInfo::Channel
product_channel(
30 chrome::VersionInfo::GetChannel());
31 switch (product_channel
) {
32 case chrome::VersionInfo::CHANNEL_UNKNOWN
: {
33 // Add the channel mark even for Chromium builds (which do not have
34 // channel) to better handle possibility of users using Chromium builds
35 // with their Google Chrome profiles. Include version string modifier
36 // as additional piece of information for debugging (it can't make
37 // a meaningful difference for the code since unknown does not match any
38 // real channel name).
39 std::string
version_string_modifier(
40 chrome::VersionInfo::GetVersionStringModifier());
41 product_channel_name
= "unknown (" + version_string_modifier
+ ")";
44 case chrome::VersionInfo::CHANNEL_CANARY
:
45 product_channel_name
= "canary";
47 case chrome::VersionInfo::CHANNEL_DEV
:
48 product_channel_name
= "dev";
50 case chrome::VersionInfo::CHANNEL_BETA
:
51 product_channel_name
= "beta";
53 case chrome::VersionInfo::CHANNEL_STABLE
:
54 product_channel_name
= "stable";
56 // Rely on -Wswitch compiler warning to detect unhandled enum values.
59 return product_channel_name
;
62 bool DoAddChannelMarkToUserDataDir(const base::FilePath
& user_data_dir
) {
63 std::string
product_channel_name(GetChannelMarkForThisExecutable());
64 base::FilePath
channels_path(user_data_dir
.AppendASCII(kChannelsFileName
));
65 std::vector
<std::string
> user_data_dir_channels
;
67 // Note: failure to read the channels file is not fatal. It's possible
68 // and legitimate that it doesn't exist, e.g. for new profile or for profile
69 // existing before channel marks have been introduced.
70 std::string channels_contents
;
71 if (base::ReadFileToString(channels_path
, &channels_contents
)) {
72 base::SplitString(channels_contents
, "\n",
73 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
76 if (std::find(user_data_dir_channels
.begin(),
77 user_data_dir_channels
.end(),
78 product_channel_name
) != user_data_dir_channels
.end()) {
79 // No need to do further disk writes if our channel mark is already present.
83 user_data_dir_channels
.push_back(product_channel_name
);
84 return base::ImportantFileWriter::WriteFileAtomically(
86 base::JoinString(user_data_dir_channels
, "\n"));
93 void AddChannelMarkToUserDataDir() {
94 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
95 base::FilePath user_data_dir
;
96 if (!PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
)) {
97 LOG(ERROR
) << "Failed to get user data dir path. The profile will not be "
98 << "automatically migrated for updated Linux packages.";
102 if (!DoAddChannelMarkToUserDataDir(user_data_dir
)) {
103 LOG(ERROR
) << "Failed to add channel mark to the user data dir ("
104 << user_data_dir
.value() << "). This profile will not be "
105 << "automatically migrated for updated Linux packages.";
109 bool ShouldMigrateUserDataDir() {
110 return base::CommandLine::ForCurrentProcess()->HasSwitch(
111 switches::kMigrateDataDirForSxS
);
114 int MigrateUserDataDir() {
115 DCHECK(ShouldMigrateUserDataDir());
117 base::FilePath source_path
;
118 if (!PathService::Get(chrome::DIR_USER_DATA
, &source_path
)) {
119 LOG(ERROR
) << "Failed to get value of chrome::DIR_USER_DATA";
120 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
123 base::FilePath
channels_path(source_path
.AppendASCII(kChannelsFileName
));
125 std::string channels_contents
;
126 if (!base::ReadFileToString(channels_path
, &channels_contents
)) {
127 LOG(WARNING
) << "Failed to read channels file.";
128 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
131 std::vector
<std::string
> user_data_dir_channels
= base::SplitString(
132 channels_contents
, "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
134 if (user_data_dir_channels
.size() != 1) {
135 LOG(WARNING
) << "User data dir migration is only possible when the profile "
136 << "is only used with a single channel.";
137 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
140 if (user_data_dir_channels
[0] != GetChannelMarkForThisExecutable()) {
141 LOG(WARNING
) << "User data dir migration is only possible when the profile "
142 << "is used with the same channel.";
143 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
146 base::FilePath target_path
=
147 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
148 switches::kMigrateDataDirForSxS
);
150 if (!base::Move(source_path
, target_path
)) {
151 LOG(ERROR
) << "Failed to rename '" << source_path
.value()
152 << "' to '" << target_path
.value() << "'";
153 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
156 return content::RESULT_CODE_NORMAL_EXIT
;
159 } // namespace sxs_linux