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/channel_info.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_result_codes.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "components/version_info/version_info.h"
22 #include "content/public/browser/browser_thread.h"
26 const char kChannelsFileName
[] = "Channels";
28 std::string
GetChannelMarkForThisExecutable() {
29 std::string product_channel_name
;
30 version_info::Channel
product_channel(chrome::GetChannel());
31 switch (product_channel
) {
32 case version_info::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(chrome::GetChannelString());
40 product_channel_name
= "unknown (" + version_string_modifier
+ ")";
43 case version_info::Channel::CANARY
:
44 product_channel_name
= "canary";
46 case version_info::Channel::DEV
:
47 product_channel_name
= "dev";
49 case version_info::Channel::BETA
:
50 product_channel_name
= "beta";
52 case version_info::Channel::STABLE
:
53 product_channel_name
= "stable";
55 // Rely on -Wswitch compiler warning to detect unhandled enum values.
58 return product_channel_name
;
61 bool DoAddChannelMarkToUserDataDir(const base::FilePath
& user_data_dir
) {
62 std::string
product_channel_name(GetChannelMarkForThisExecutable());
63 base::FilePath
channels_path(user_data_dir
.AppendASCII(kChannelsFileName
));
64 std::vector
<std::string
> user_data_dir_channels
;
66 // Note: failure to read the channels file is not fatal. It's possible
67 // and legitimate that it doesn't exist, e.g. for new profile or for profile
68 // existing before channel marks have been introduced.
69 std::string channels_contents
;
70 if (base::ReadFileToString(channels_path
, &channels_contents
)) {
71 base::SplitString(channels_contents
, "\n",
72 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
75 if (std::find(user_data_dir_channels
.begin(),
76 user_data_dir_channels
.end(),
77 product_channel_name
) != user_data_dir_channels
.end()) {
78 // No need to do further disk writes if our channel mark is already present.
82 user_data_dir_channels
.push_back(product_channel_name
);
83 return base::ImportantFileWriter::WriteFileAtomically(
85 base::JoinString(user_data_dir_channels
, "\n"));
92 void AddChannelMarkToUserDataDir() {
93 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
94 base::FilePath user_data_dir
;
95 if (!PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
)) {
96 LOG(ERROR
) << "Failed to get user data dir path. The profile will not be "
97 << "automatically migrated for updated Linux packages.";
101 if (!DoAddChannelMarkToUserDataDir(user_data_dir
)) {
102 LOG(ERROR
) << "Failed to add channel mark to the user data dir ("
103 << user_data_dir
.value() << "). This profile will not be "
104 << "automatically migrated for updated Linux packages.";
108 bool ShouldMigrateUserDataDir() {
109 return base::CommandLine::ForCurrentProcess()->HasSwitch(
110 switches::kMigrateDataDirForSxS
);
113 int MigrateUserDataDir() {
114 DCHECK(ShouldMigrateUserDataDir());
116 base::FilePath source_path
;
117 if (!PathService::Get(chrome::DIR_USER_DATA
, &source_path
)) {
118 LOG(ERROR
) << "Failed to get value of chrome::DIR_USER_DATA";
119 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
122 base::FilePath
channels_path(source_path
.AppendASCII(kChannelsFileName
));
124 std::string channels_contents
;
125 if (!base::ReadFileToString(channels_path
, &channels_contents
)) {
126 LOG(WARNING
) << "Failed to read channels file.";
127 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
130 std::vector
<std::string
> user_data_dir_channels
= base::SplitString(
131 channels_contents
, "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
133 if (user_data_dir_channels
.size() != 1) {
134 LOG(WARNING
) << "User data dir migration is only possible when the profile "
135 << "is only used with a single channel.";
136 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
139 if (user_data_dir_channels
[0] != GetChannelMarkForThisExecutable()) {
140 LOG(WARNING
) << "User data dir migration is only possible when the profile "
141 << "is used with the same channel.";
142 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
145 base::FilePath target_path
=
146 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
147 switches::kMigrateDataDirForSxS
);
149 if (!base::Move(source_path
, target_path
)) {
150 LOG(ERROR
) << "Failed to rename '" << source_path
.value()
151 << "' to '" << target_path
.value() << "'";
152 return chrome::RESULT_CODE_SXS_MIGRATION_FAILED
;
155 return content::RESULT_CODE_NORMAL_EXIT
;
158 } // namespace sxs_linux