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