OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/importer/firefox_importer_utils.h" | 5 #include "chrome/browser/importer/firefox_importer_utils.h" |
6 | 6 |
7 #include <shlobj.h> | 7 #include <shlobj.h> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/registry.h" | 10 #include "base/registry.h" |
11 | 11 |
12 // NOTE: Keep these in order since we need test all those paths according | 12 // NOTE: Keep these in order since we need test all those paths according |
13 // to priority. For example. One machine has multiple users. One non-admin | 13 // to priority. For example. One machine has multiple users. One non-admin |
14 // user installs Firefox 2, which causes there is a Firefox2 entry under HKCU. | 14 // user installs Firefox 2, which causes there is a Firefox2 entry under HKCU. |
15 // One admin user installs Firefox 3, which causes there is a Firefox 3 entry | 15 // One admin user installs Firefox 3, which causes there is a Firefox 3 entry |
16 // under HKLM. So when the non-admin user log in, we should deal with Firefox 2 | 16 // under HKLM. So when the non-admin user log in, we should deal with Firefox 2 |
17 // related data instead of Firefox 3. | 17 // related data instead of Firefox 3. |
18 static const HKEY kFireFoxRegistryPaths[] = { | 18 static const HKEY kFireFoxRegistryPaths[] = { |
19 HKEY_CURRENT_USER, | 19 HKEY_CURRENT_USER, |
20 HKEY_LOCAL_MACHINE | 20 HKEY_LOCAL_MACHINE |
21 }; | 21 }; |
22 | 22 |
23 int GetCurrentFirefoxMajorVersionFromRegistry() { | 23 int GetCurrentFirefoxMajorVersionFromRegistry() { |
24 TCHAR ver_buffer[128]; | 24 TCHAR ver_buffer[128]; |
25 DWORD ver_buffer_length = sizeof(ver_buffer); | 25 DWORD ver_buffer_length = sizeof(ver_buffer); |
26 int highest_version = 0; | 26 int highest_version = 0; |
27 // When installing Firefox with admin account, the product keys will be | 27 // When installing Firefox with admin account, the product keys will be |
28 // written under HKLM\Mozilla. Otherwise it the keys will be written under | 28 // written under HKLM\Mozilla. Otherwise it the keys will be written under |
29 // HKCU\Mozilla. | 29 // HKCU\Mozilla. |
30 for (int i = 0; i < arraysize(kFireFoxRegistryPaths); ++i) { | 30 for (int i = 0; i < arraysize(kFireFoxRegistryPaths); ++i) { |
31 bool result = ReadFromRegistry(kFireFoxRegistryPaths[i], | 31 RegKey reg_key(kFireFoxRegistryPaths[i], |
32 L"Software\\Mozilla\\Mozilla Firefox", | 32 L"Software\\Mozilla\\Mozilla Firefox"); |
33 L"CurrentVersion", ver_buffer, &ver_buffer_length); | 33 |
| 34 bool result = reg_key.ReadValue(L"CurrentVersion", ver_buffer, |
| 35 &ver_buffer_length, NULL); |
34 if (!result) | 36 if (!result) |
35 continue; | 37 continue; |
36 highest_version = std::max(highest_version, _wtoi(ver_buffer)); | 38 highest_version = std::max(highest_version, _wtoi(ver_buffer)); |
37 } | 39 } |
38 return highest_version; | 40 return highest_version; |
39 } | 41 } |
40 | 42 |
41 std::wstring GetFirefoxInstallPathFromRegistry() { | 43 std::wstring GetFirefoxInstallPathFromRegistry() { |
42 // Detects the path that Firefox is installed in. | 44 // Detects the path that Firefox is installed in. |
43 std::wstring registry_path = L"Software\\Mozilla\\Mozilla Firefox"; | 45 std::wstring registry_path = L"Software\\Mozilla\\Mozilla Firefox"; |
44 TCHAR buffer[MAX_PATH]; | 46 TCHAR buffer[MAX_PATH]; |
45 DWORD buffer_length = sizeof(buffer); | 47 DWORD buffer_length = sizeof(buffer); |
46 bool result; | 48 RegKey reg_key(HKEY_LOCAL_MACHINE, registry_path.c_str()); |
47 result = ReadFromRegistry(HKEY_LOCAL_MACHINE, registry_path.c_str(), | 49 bool result = reg_key.ReadValue(L"CurrentVersion", buffer, |
48 L"CurrentVersion", buffer, &buffer_length); | 50 &buffer_length, NULL); |
49 if (!result) | 51 if (!result) |
50 return std::wstring(); | 52 return std::wstring(); |
51 registry_path += L"\\" + std::wstring(buffer) + L"\\Main"; | 53 registry_path += L"\\" + std::wstring(buffer) + L"\\Main"; |
52 buffer_length = sizeof(buffer); | 54 buffer_length = sizeof(buffer); |
53 result = ReadFromRegistry(HKEY_LOCAL_MACHINE, registry_path.c_str(), | 55 reg_key = RegKey(HKEY_LOCAL_MACHINE, registry_path.c_str()); |
54 L"Install Directory", buffer, &buffer_length); | 56 result = reg_key.ReadValue(L"Install Directory", buffer, |
| 57 &buffer_length, NULL); |
55 if (!result) | 58 if (!result) |
56 return std::wstring(); | 59 return std::wstring(); |
57 return buffer; | 60 return buffer; |
58 } | 61 } |
59 | 62 |
60 FilePath GetProfilesINI() { | 63 FilePath GetProfilesINI() { |
61 FilePath ini_file; | 64 FilePath ini_file; |
62 // The default location of the profile folder containing user data is | 65 // The default location of the profile folder containing user data is |
63 // under the "Application Data" folder in Windows XP. | 66 // under the "Application Data" folder in Windows XP. |
64 wchar_t buffer[MAX_PATH] = {0}; | 67 wchar_t buffer[MAX_PATH] = {0}; |
65 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, | 68 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, |
66 SHGFP_TYPE_CURRENT, buffer))) { | 69 SHGFP_TYPE_CURRENT, buffer))) { |
67 ini_file = FilePath(buffer).Append(L"Mozilla\\Firefox\\profiles.ini"); | 70 ini_file = FilePath(buffer).Append(L"Mozilla\\Firefox\\profiles.ini"); |
68 } | 71 } |
69 if (file_util::PathExists(ini_file)) | 72 if (file_util::PathExists(ini_file)) |
70 return ini_file; | 73 return ini_file; |
71 | 74 |
72 return FilePath(); | 75 return FilePath(); |
73 } | 76 } |
OLD | NEW |