1 // Copyright 2015 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/edge_importer_utils_win.h"
6 
7 #include <Shlobj.h>
8 
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/win/registry.h"
13 #include "base/win/windows_version.h"
14 #include "chrome/common/importer/importer_test_registry_overrider_win.h"
15 
16 namespace {
17 
18 const base::char16 kEdgeSettingsMainKey[] = L"MicrosoftEdge\\Main";
19 
20 const base::char16 kEdgePackageName[] =
21     L"microsoft.microsoftedge_8wekyb3d8bbwe";
22 
23 // We assume at the moment that the package name never changes for Edge.
GetEdgePackageName()24 base::string16 GetEdgePackageName() {
25   return kEdgePackageName;
26 }
27 
GetEdgeRegistryKey(const base::string16 & key_name)28 base::string16 GetEdgeRegistryKey(const base::string16& key_name) {
29   base::string16 registry_key =
30       L"Software\\Classes\\Local Settings\\"
31       L"Software\\Microsoft\\Windows\\CurrentVersion\\AppContainer\\"
32       L"Storage\\";
33   registry_key += GetEdgePackageName();
34   registry_key += L"\\";
35   registry_key += key_name;
36   return registry_key;
37 }
38 
GetPotentiallyOverridenEdgeKey(const base::string16 & desired_key_path)39 base::string16 GetPotentiallyOverridenEdgeKey(
40     const base::string16& desired_key_path) {
41   base::string16 test_registry_override(
42       ImporterTestRegistryOverrider::GetTestRegistryOverride());
43   return test_registry_override.empty() ? GetEdgeRegistryKey(desired_key_path)
44                                         : test_registry_override;
45 }
46 
47 }  // namespace
48 
49 namespace importer {
50 
GetEdgeSettingsKey()51 base::string16 GetEdgeSettingsKey() {
52   return GetPotentiallyOverridenEdgeKey(kEdgeSettingsMainKey);
53 }
54 
GetEdgeDataFilePath()55 base::FilePath GetEdgeDataFilePath() {
56   wchar_t buffer[MAX_PATH];
57   if (::SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
58                         buffer) != S_OK)
59     return base::FilePath();
60 
61   base::FilePath base_path(buffer);
62   base::string16 rel_path = L"Packages\\";
63   rel_path += GetEdgePackageName();
64   rel_path += L"\\AC\\MicrosoftEdge\\User\\Default";
65   return base_path.Append(rel_path);
66 }
67 
IsEdgeFavoritesLegacyMode()68 bool IsEdgeFavoritesLegacyMode() {
69   base::win::RegKey key(HKEY_CURRENT_USER, GetEdgeSettingsKey().c_str(),
70                         KEY_READ);
71   DWORD ese_enabled = 0;
72   // Check whether Edge is using the new Extensible Store Engine (ESE) format
73   // for its favorites.
74 
75   if (key.ReadValueDW(L"FavoritesESEEnabled", &ese_enabled) == ERROR_SUCCESS)
76     return !ese_enabled;
77   // If the registry key is missing, check the Windows version.
78   // Edge switched to ESE in Windows 10 Build 10565 (somewhere between
79   // Windows 10 RTM and Windows 10 November 1511 Update).
80   return base::win::GetVersion() < base::win::Version::WIN10_TH2;
81 }
82 
EdgeImporterCanImport()83 bool EdgeImporterCanImport() {
84   base::File::Info file_info;
85   if (base::win::GetVersion() < base::win::Version::WIN10)
86     return false;
87   return base::GetFileInfo(GetEdgeDataFilePath(), &file_info) &&
88          file_info.is_directory;
89 }
90 
91 }  // namespace importer
92