OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/common/importer/firefox_importer_utils.h" | 5 #include "chrome/common/importer/firefox_importer_utils.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/ini_parser.h" | 12 #include "base/ini_parser.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Retrieves the file system path of the profile name. | 26 // Retrieves the file system path of the profile name. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // since the other profiles are used mostly by developers for testing. | 87 // since the other profiles are used mostly by developers for testing. |
87 for (std::vector<std::string>::const_iterator it = profiles.begin(); | 88 for (std::vector<std::string>::const_iterator it = profiles.begin(); |
88 it != profiles.end(); ++it) | 89 it != profiles.end(); ++it) |
89 if (IsDefaultProfile(root, *it)) | 90 if (IsDefaultProfile(root, *it)) |
90 return GetProfilePath(root, *it); | 91 return GetProfilePath(root, *it); |
91 | 92 |
92 // If no default profile is found, the path to Profile0 will be returned. | 93 // If no default profile is found, the path to Profile0 will be returned. |
93 return GetProfilePath(root, profiles.front()); | 94 return GetProfilePath(root, profiles.front()); |
94 } | 95 } |
95 | 96 |
| 97 #if defined(OS_MACOSX) |
| 98 // Find the "*.app" component of the path and build up from there. |
| 99 // The resulting path will be .../Firefox.app/Contents/MacOS. |
| 100 // We do this because we don't trust LastAppDir to always be |
| 101 // this particular path, without any subdirs, and we want to make |
| 102 // our assumption about Firefox's root being in that path explicit. |
| 103 bool ComposeMacAppPath(const std::string& path_from_file, |
| 104 base::FilePath* output) { |
| 105 base::FilePath path(path_from_file); |
| 106 typedef std::vector<base::FilePath::StringType> ComponentVector; |
| 107 ComponentVector path_components; |
| 108 path.GetComponents(&path_components); |
| 109 if (path_components.empty()) |
| 110 return false; |
| 111 // The first path component is special because it may be absolute. Calling |
| 112 // Append with an absolute path component will trigger an assert, so we |
| 113 // must handle it differently and initialize output with it. |
| 114 *output = base::FilePath(path_components[0]); |
| 115 // Append next path components untill we find the *.app component. When we do, |
| 116 // append Contents/MacOS. |
| 117 for (size_t i = 1; i < path_components.size(); ++i) { |
| 118 *output = output->Append(path_components[i]); |
| 119 if (EndsWith(path_components[i], ".app", true)) { |
| 120 *output = output->Append("Contents"); |
| 121 *output = output->Append("MacOS"); |
| 122 return true; |
| 123 } |
| 124 } |
| 125 LOG(ERROR) << path_from_file << " doesn't look like a valid Firefox " |
| 126 << "installation path: missing /*.app/ directory."; |
| 127 return false; |
| 128 } |
| 129 #endif // OS_MACOSX |
| 130 |
96 bool GetFirefoxVersionAndPathFromProfile(const base::FilePath& profile_path, | 131 bool GetFirefoxVersionAndPathFromProfile(const base::FilePath& profile_path, |
97 int* version, | 132 int* version, |
98 base::FilePath* app_path) { | 133 base::FilePath* app_path) { |
99 bool ret = false; | 134 bool ret = false; |
100 base::FilePath compatibility_file = | 135 base::FilePath compatibility_file = |
101 profile_path.AppendASCII("compatibility.ini"); | 136 profile_path.AppendASCII("compatibility.ini"); |
102 std::string content; | 137 std::string content; |
103 base::ReadFileToString(compatibility_file, &content); | 138 base::ReadFileToString(compatibility_file, &content); |
104 ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n"); | 139 ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n"); |
105 std::vector<std::string> lines; | 140 std::vector<std::string> lines; |
106 base::SplitString(content, '\n', &lines); | 141 base::SplitString(content, '\n', &lines); |
107 | 142 |
108 for (size_t i = 0; i < lines.size(); ++i) { | 143 for (size_t i = 0; i < lines.size(); ++i) { |
109 const std::string& line = lines[i]; | 144 const std::string& line = lines[i]; |
110 if (line.empty() || line[0] == '#' || line[0] == ';') | 145 if (line.empty() || line[0] == '#' || line[0] == ';') |
111 continue; | 146 continue; |
112 size_t equal = line.find('='); | 147 size_t equal = line.find('='); |
113 if (equal != std::string::npos) { | 148 if (equal != std::string::npos) { |
114 std::string key = line.substr(0, equal); | 149 std::string key = line.substr(0, equal); |
115 if (key == "LastVersion") { | 150 if (key == "LastVersion") { |
116 base::StringToInt(line.substr(equal + 1), version); | 151 base::StringToInt(line.substr(equal + 1), version); |
117 ret = true; | 152 ret = true; |
118 } else if (key == "LastAppDir") { | 153 } else if (key == "LastAppDir") { |
119 // TODO(evanm): If the path in question isn't convertible to | 154 // TODO(evanm): If the path in question isn't convertible to |
120 // UTF-8, what does Firefox do? If it puts raw bytes in the | 155 // UTF-8, what does Firefox do? If it puts raw bytes in the |
121 // file, we could go straight from bytes -> filepath; | 156 // file, we could go straight from bytes -> filepath; |
122 // otherwise, we're out of luck here. | 157 // otherwise, we're out of luck here. |
| 158 #if defined(OS_MACOSX) |
| 159 // Extract path from "LastAppDir=/actual/path" |
| 160 size_t separator_pos = line.find_first_of('='); |
| 161 const std::string& path_from_ini = line.substr(separator_pos + 1); |
| 162 if (!ComposeMacAppPath(path_from_ini, app_path)) |
| 163 return false; |
| 164 #else // !OS_MACOSX |
123 *app_path = base::FilePath::FromUTF8Unsafe(line.substr(equal + 1)); | 165 *app_path = base::FilePath::FromUTF8Unsafe(line.substr(equal + 1)); |
| 166 #endif // OS_MACOSX |
124 } | 167 } |
125 } | 168 } |
126 } | 169 } |
127 return ret; | 170 return ret; |
128 } | 171 } |
129 | 172 |
130 bool ReadPrefFile(const base::FilePath& path, std::string* content) { | 173 bool ReadPrefFile(const base::FilePath& path, std::string* content) { |
131 if (content == NULL) | 174 if (content == NULL) |
132 return false; | 175 return false; |
133 | 176 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 320 } |
278 } | 321 } |
279 } | 322 } |
280 } | 323 } |
281 | 324 |
282 StringToLowerASCII(&branding_name); | 325 StringToLowerASCII(&branding_name); |
283 if (branding_name.find("iceweasel") != std::string::npos) | 326 if (branding_name.find("iceweasel") != std::string::npos) |
284 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL); | 327 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_ICEWEASEL); |
285 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX); | 328 return l10n_util::GetStringUTF16(IDS_IMPORT_FROM_FIREFOX); |
286 } | 329 } |
OLD | NEW |