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.
5 #include "chrome/utility/importer/ie_importer_win.h"
18 #include "base/files/file_enumerator.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/strings/string16.h"
22 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "base/time/time.h"
26 #include "base/win/registry.h"
27 #include "base/win/scoped_co_mem.h"
28 #include "base/win/scoped_comptr.h"
29 #include "base/win/scoped_handle.h"
30 #include "base/win/scoped_propvariant.h"
31 #include "base/win/windows_version.h"
32 #include "chrome/common/importer/ie_importer_utils_win.h"
33 #include "chrome/common/importer/imported_bookmark_entry.h"
34 #include "chrome/common/importer/importer_bridge.h"
35 #include "chrome/common/importer/importer_data_types.h"
36 #include "chrome/common/importer/importer_url_row.h"
37 #include "chrome/common/importer/pstore_declarations.h"
38 #include "chrome/grit/generated_resources.h"
39 #include "chrome/utility/importer/favicon_reencode.h"
40 #include "components/autofill/core/common/password_form.h"
41 #include "ui/base/l10n/l10n_util.h"
43 #include "url/url_constants.h"
47 // Registry key paths from which we import IE settings.
48 const base::char16 kSearchScopePath
[] =
49 L
"Software\\Microsoft\\Internet Explorer\\SearchScopes";
50 const base::char16 kIEVersionKey
[] =
51 L
"Software\\Microsoft\\Internet Explorer";
52 const base::char16 kIEToolbarKey
[] =
53 L
"Software\\Microsoft\\Internet Explorer\\Toolbar";
55 // NTFS stream name of favicon image data.
56 const base::char16 kFaviconStreamName
[] = L
":favicon:$DATA";
58 // A struct that hosts the information of AutoComplete data in PStore.
59 struct AutoCompleteInfo
{
61 std::vector
<base::string16
> data
;
65 // Gets the creation time of the given file or directory.
66 base::Time
GetFileCreationTime(const base::string16
& file
) {
67 base::Time creation_time
;
68 base::win::ScopedHandle
file_handle(
69 CreateFile(file
.c_str(), GENERIC_READ
,
70 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
72 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
));
73 FILETIME creation_filetime
;
74 if (!file_handle
.IsValid())
76 if (GetFileTime(file_handle
.Get(), &creation_filetime
, NULL
, NULL
))
77 creation_time
= base::Time::FromFileTime(creation_filetime
);
81 // Safely read an object of type T from a raw sequence of bytes.
83 bool BinaryRead(T
* data
, size_t offset
, const std::vector
<uint8
>& blob
) {
84 if (offset
+ sizeof(T
) > blob
.size())
86 memcpy(data
, &blob
[offset
], sizeof(T
));
90 // Safely read an ITEMIDLIST from a raw sequence of bytes.
92 // An ITEMIDLIST is a list of SHITEMIDs, terminated by a SHITEMID with
93 // .cb = 0. Here, before simply casting &blob[offset] to LPITEMIDLIST,
94 // we verify that the list structure is not overrunning the boundary of
96 LPCITEMIDLIST
BinaryReadItemIDList(size_t offset
, size_t idlist_size
,
97 const std::vector
<uint8
>& blob
) {
100 // Use a USHORT instead of SHITEMID to avoid buffer over read.
102 if (head
>= idlist_size
|| !BinaryRead(&id_cb
, offset
+ head
, blob
))
108 return reinterpret_cast<LPCITEMIDLIST
>(&blob
[offset
]);
111 // Compares the two bookmarks in the order of IE's Favorites menu.
112 // Returns true if rhs should come later than lhs (lhs < rhs).
113 struct IEOrderBookmarkComparator
{
114 bool operator()(const ImportedBookmarkEntry
& lhs
,
115 const ImportedBookmarkEntry
& rhs
) const {
116 static const uint32 kNotSorted
= 0xfffffffb; // IE uses this magic value.
117 base::FilePath lhs_prefix
;
118 base::FilePath rhs_prefix
;
119 for (size_t i
= 0; i
<= lhs
.path
.size() && i
<= rhs
.path
.size(); ++i
) {
120 const base::FilePath::StringType lhs_i
=
121 (i
< lhs
.path
.size() ? lhs
.path
[i
] : lhs
.title
+ L
".url");
122 const base::FilePath::StringType rhs_i
=
123 (i
< rhs
.path
.size() ? rhs
.path
[i
] : rhs
.title
+ L
".url");
124 lhs_prefix
= lhs_prefix
.Append(lhs_i
);
125 rhs_prefix
= rhs_prefix
.Append(rhs_i
);
128 // The first path element that differs between the two.
129 std::map
<base::FilePath
, uint32
>::const_iterator lhs_iter
=
130 sort_index_
->find(lhs_prefix
);
131 std::map
<base::FilePath
, uint32
>::const_iterator rhs_iter
=
132 sort_index_
->find(rhs_prefix
);
133 uint32 lhs_sort_index
= (lhs_iter
== sort_index_
->end() ? kNotSorted
135 uint32 rhs_sort_index
= (rhs_iter
== sort_index_
->end() ? kNotSorted
137 if (lhs_sort_index
!= rhs_sort_index
)
138 return lhs_sort_index
< rhs_sort_index
;
139 // If they have the same sort order, sort alphabetically.
140 return lhs_i
< rhs_i
;
142 return lhs
.path
.size() < rhs
.path
.size();
144 const std::map
<base::FilePath
, uint32
>* sort_index_
;
147 // IE stores the order of the Favorites menu in registry under:
148 // HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder\Favorites.
149 // The folder hierarchy of Favorites menu is directly mapped to the key
150 // hierarchy in the registry.
152 // If the order of the items in a folder is customized by user, the order is
153 // recorded in the REG_BINARY value named "Order" of the corresponding key.
154 // The content of the "Order" value is a raw binary dump of an array of the
155 // following data structure
157 // uint32 size; // Note that ITEMIDLIST is variably-sized.
158 // uint32 sort_index; // 0 means this is the first item, 1 the second, ...
159 // ITEMIDLIST item_id;
161 // where each item_id should correspond to a favorites link file (*.url) in
162 // the current folder.
163 bool ParseFavoritesOrderBlob(
164 const Importer
* importer
,
165 const std::vector
<uint8
>& blob
,
166 const base::FilePath
& path
,
167 std::map
<base::FilePath
, uint32
>* sort_index
) WARN_UNUSED_RESULT
{
168 static const int kItemCountOffset
= 16;
169 static const int kItemListStartOffset
= 20;
171 // Read the number of items.
172 uint32 item_count
= 0;
173 if (!BinaryRead(&item_count
, kItemCountOffset
, blob
))
176 // Traverse over the items.
177 size_t base_offset
= kItemListStartOffset
;
178 for (uint32 i
= 0; i
< item_count
&& !importer
->cancelled(); ++i
) {
179 static const int kSizeOffset
= 0;
180 static const int kSortIndexOffset
= 4;
181 static const int kItemIDListOffset
= 8;
183 // Read the size (number of bytes) of the current item.
184 uint32 item_size
= 0;
185 if (!BinaryRead(&item_size
, base_offset
+ kSizeOffset
, blob
) ||
186 base_offset
+ item_size
<= base_offset
|| // checking overflow
187 base_offset
+ item_size
> blob
.size())
190 // Read the sort index of the current item.
191 uint32 item_sort_index
= 0;
192 if (!BinaryRead(&item_sort_index
, base_offset
+ kSortIndexOffset
, blob
))
195 // Read the file name from the ITEMIDLIST structure.
196 LPCITEMIDLIST idlist
= BinaryReadItemIDList(
197 base_offset
+ kItemIDListOffset
, item_size
- kItemIDListOffset
, blob
);
198 TCHAR item_filename
[MAX_PATH
];
199 if (!idlist
|| !SHGetPathFromIDList(idlist
, item_filename
))
201 base::FilePath item_relative_path
=
202 path
.Append(base::FilePath(item_filename
).BaseName());
204 // Record the retrieved information and go to the next item.
205 sort_index
->insert(std::make_pair(item_relative_path
, item_sort_index
));
206 base_offset
+= item_size
;
211 bool ParseFavoritesOrderRegistryTree(
212 const Importer
* importer
,
213 const base::win::RegKey
& key
,
214 const base::FilePath
& path
,
215 std::map
<base::FilePath
, uint32
>* sort_index
) WARN_UNUSED_RESULT
{
216 // Parse the order information of the current folder.
217 DWORD blob_length
= 0;
218 if (key
.ReadValue(L
"Order", NULL
, &blob_length
, NULL
) == ERROR_SUCCESS
) {
219 std::vector
<uint8
> blob(blob_length
);
220 if (blob_length
> 0 &&
221 key
.ReadValue(L
"Order", reinterpret_cast<DWORD
*>(&blob
[0]),
222 &blob_length
, NULL
) == ERROR_SUCCESS
) {
223 if (!ParseFavoritesOrderBlob(importer
, blob
, path
, sort_index
))
228 // Recursively parse subfolders.
229 for (base::win::RegistryKeyIterator
child(key
.Handle(), L
"");
230 child
.Valid() && !importer
->cancelled();
232 base::win::RegKey
subkey(key
.Handle(), child
.Name(), KEY_READ
);
233 if (subkey
.Valid()) {
234 base::FilePath
subpath(path
.Append(child
.Name()));
235 if (!ParseFavoritesOrderRegistryTree(importer
, subkey
, subpath
,
244 bool ParseFavoritesOrderInfo(
245 const Importer
* importer
,
246 std::map
<base::FilePath
, uint32
>* sort_index
) WARN_UNUSED_RESULT
{
247 base::string16
key_path(importer::GetIEFavoritesOrderKey());
248 base::win::RegKey
key(HKEY_CURRENT_USER
, key_path
.c_str(), KEY_READ
);
251 return ParseFavoritesOrderRegistryTree(importer
, key
, base::FilePath(),
255 // Reads the sort order from registry. If failed, we don't touch the list
256 // and use the default (alphabetical) order.
257 void SortBookmarksInIEOrder(
258 const Importer
* importer
,
259 std::vector
<ImportedBookmarkEntry
>* bookmarks
) {
260 std::map
<base::FilePath
, uint32
> sort_index
;
261 if (!ParseFavoritesOrderInfo(importer
, &sort_index
))
263 IEOrderBookmarkComparator compare
= {&sort_index
};
264 std::sort(bookmarks
->begin(), bookmarks
->end(), compare
);
267 // Reads an internet shortcut (*.url) |file| and returns a COM object
269 bool LoadInternetShortcut(
270 const base::string16
& file
,
271 base::win::ScopedComPtr
<IUniformResourceLocator
>* shortcut
) {
272 base::win::ScopedComPtr
<IUniformResourceLocator
> url_locator
;
273 if (FAILED(url_locator
.CreateInstance(CLSID_InternetShortcut
, NULL
,
274 CLSCTX_INPROC_SERVER
)))
277 base::win::ScopedComPtr
<IPersistFile
> persist_file
;
278 if (FAILED(persist_file
.QueryFrom(url_locator
.get())))
281 // Loads the Internet Shortcut from persistent storage.
282 if (FAILED(persist_file
->Load(file
.c_str(), STGM_READ
)))
285 std::swap(url_locator
, *shortcut
);
289 // Reads the URL stored in the internet shortcut.
290 GURL
ReadURLFromInternetShortcut(IUniformResourceLocator
* url_locator
) {
291 base::win::ScopedCoMem
<wchar_t> url
;
292 // GetURL can return S_FALSE (FAILED(S_FALSE) is false) when url == NULL.
293 return (FAILED(url_locator
->GetURL(&url
)) || !url
) ?
294 GURL() : GURL(url
.get());
297 // Reads the URL of the favicon of the internet shortcut.
298 GURL
ReadFaviconURLFromInternetShortcut(IUniformResourceLocator
* url_locator
) {
299 base::win::ScopedComPtr
<IPropertySetStorage
> property_set_storage
;
300 if (FAILED(property_set_storage
.QueryFrom(url_locator
)))
303 base::win::ScopedComPtr
<IPropertyStorage
> property_storage
;
304 if (FAILED(property_set_storage
->Open(FMTID_Intshcut
, STGM_READ
,
305 property_storage
.Receive()))) {
309 PROPSPEC properties
[] = {{PRSPEC_PROPID
, {PID_IS_ICONFILE
}}};
310 // ReadMultiple takes a non-const array of PROPVARIANTs, but since this code
311 // only needs an array of size 1: a non-const pointer to |output| is
313 base::win::ScopedPropVariant output
;
314 // ReadMultiple can return S_FALSE (FAILED(S_FALSE) is false) when the
315 // property is not found, in which case output[0].vt is set to VT_EMPTY.
316 if (FAILED(property_storage
->ReadMultiple(1, properties
, output
.Receive())) ||
317 output
.get().vt
!= VT_LPWSTR
)
319 return GURL(output
.get().pwszVal
);
322 // Reads the favicon imaga data in an NTFS alternate data stream. This is where
323 // IE7 and above store the data.
324 bool ReadFaviconDataFromInternetShortcut(const base::string16
& file
,
326 return base::ReadFileToString(
327 base::FilePath(file
+ kFaviconStreamName
), data
);
330 // Reads the favicon imaga data in the Internet cache. IE6 doesn't hold the data
331 // explicitly, but it might be found in the cache.
332 bool ReadFaviconDataFromCache(const GURL
& favicon_url
, std::string
* data
) {
333 std::wstring
url_wstring(base::UTF8ToWide(favicon_url
.spec()));
335 GetUrlCacheEntryInfoEx(url_wstring
.c_str(), NULL
, &info_size
, NULL
, NULL
,
337 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
340 std::vector
<char> buf(info_size
);
341 INTERNET_CACHE_ENTRY_INFO
* cache
=
342 reinterpret_cast<INTERNET_CACHE_ENTRY_INFO
*>(&buf
[0]);
343 if (!GetUrlCacheEntryInfoEx(url_wstring
.c_str(), cache
, &info_size
, NULL
,
347 return base::ReadFileToString(base::FilePath(cache
->lpszLocalFileName
), data
);
350 // Reads the binary image data of favicon of an internet shortcut file |file|.
351 // |favicon_url| read by ReadFaviconURLFromInternetShortcut is also needed to
352 // examine the IE cache.
353 bool ReadReencodedFaviconData(const base::string16
& file
,
354 const GURL
& favicon_url
,
355 std::vector
<unsigned char>* data
) {
356 std::string image_data
;
357 if (!ReadFaviconDataFromInternetShortcut(file
, &image_data
) &&
358 !ReadFaviconDataFromCache(favicon_url
, &image_data
)) {
362 const unsigned char* ptr
=
363 reinterpret_cast<const unsigned char*>(image_data
.c_str());
364 return importer::ReencodeFavicon(ptr
, image_data
.size(), data
);
367 // Loads favicon image data and registers to |favicon_map|.
368 void UpdateFaviconMap(
369 const base::string16
& url_file
,
371 IUniformResourceLocator
* url_locator
,
372 std::map
<GURL
, favicon_base::FaviconUsageData
>* favicon_map
) {
373 GURL favicon_url
= ReadFaviconURLFromInternetShortcut(url_locator
);
374 if (!favicon_url
.is_valid())
377 std::map
<GURL
, favicon_base::FaviconUsageData
>::iterator it
=
378 favicon_map
->find(favicon_url
);
379 if (it
!= favicon_map
->end()) {
380 // Known favicon URL.
381 it
->second
.urls
.insert(url
);
383 // New favicon URL. Read the image data and store.
384 favicon_base::FaviconUsageData usage
;
385 if (ReadReencodedFaviconData(url_file
, favicon_url
, &usage
.png_data
)) {
386 usage
.favicon_url
= favicon_url
;
387 usage
.urls
.insert(url
);
388 favicon_map
->insert(std::make_pair(favicon_url
, usage
));
396 // {E161255A-37C3-11D2-BCAA-00C04fD929DB}
397 const GUID
IEImporter::kPStoreAutocompleteGUID
= {
398 0xe161255a, 0x37c3, 0x11d2,
399 { 0xbc, 0xaa, 0x00, 0xc0, 0x4f, 0xd9, 0x29, 0xdb }
401 // {A79029D6-753E-4e27-B807-3D46AB1545DF}
402 const GUID
IEImporter::kUnittestGUID
= {
403 0xa79029d6, 0x753e, 0x4e27,
404 { 0xb8, 0x7, 0x3d, 0x46, 0xab, 0x15, 0x45, 0xdf }
407 IEImporter::IEImporter() {
410 void IEImporter::StartImport(const importer::SourceProfile
& source_profile
,
412 ImporterBridge
* bridge
) {
414 source_path_
= source_profile
.source_path
;
416 bridge_
->NotifyStarted();
418 if ((items
& importer::HOME_PAGE
) && !cancelled()) {
419 bridge_
->NotifyItemStarted(importer::HOME_PAGE
);
420 ImportHomepage(); // Doesn't have a UI item.
421 bridge_
->NotifyItemEnded(importer::HOME_PAGE
);
423 // The order here is important!
424 if ((items
& importer::HISTORY
) && !cancelled()) {
425 bridge_
->NotifyItemStarted(importer::HISTORY
);
427 bridge_
->NotifyItemEnded(importer::HISTORY
);
429 if ((items
& importer::FAVORITES
) && !cancelled()) {
430 bridge_
->NotifyItemStarted(importer::FAVORITES
);
432 bridge_
->NotifyItemEnded(importer::FAVORITES
);
434 if ((items
& importer::SEARCH_ENGINES
) && !cancelled()) {
435 bridge_
->NotifyItemStarted(importer::SEARCH_ENGINES
);
436 ImportSearchEngines();
437 bridge_
->NotifyItemEnded(importer::SEARCH_ENGINES
);
439 if ((items
& importer::PASSWORDS
) && !cancelled()) {
440 bridge_
->NotifyItemStarted(importer::PASSWORDS
);
441 // Always import IE6 passwords.
442 ImportPasswordsIE6();
444 if (CurrentIEVersion() >= 7)
445 ImportPasswordsIE7();
446 bridge_
->NotifyItemEnded(importer::PASSWORDS
);
448 bridge_
->NotifyEnded();
451 IEImporter::~IEImporter() {
454 void IEImporter::ImportFavorites() {
456 if (!GetFavoritesInfo(&info
))
459 BookmarkVector bookmarks
;
460 favicon_base::FaviconUsageDataList favicons
;
461 ParseFavoritesFolder(info
, &bookmarks
, &favicons
);
463 if (!bookmarks
.empty() && !cancelled()) {
464 const base::string16
& first_folder_name
=
465 l10n_util::GetStringUTF16(IDS_BOOKMARK_GROUP_FROM_IE
);
466 bridge_
->AddBookmarks(bookmarks
, first_folder_name
);
468 if (!favicons
.empty() && !cancelled())
469 bridge_
->SetFavicons(favicons
);
472 void IEImporter::ImportHistory() {
473 const std::string kSchemes
[] = {url::kHttpScheme
,
477 int total_schemes
= arraysize(kSchemes
);
479 base::win::ScopedComPtr
<IUrlHistoryStg2
> url_history_stg2
;
480 if (FAILED(url_history_stg2
.CreateInstance(CLSID_CUrlHistory
, NULL
,
481 CLSCTX_INPROC_SERVER
))) {
484 base::win::ScopedComPtr
<IEnumSTATURL
> enum_url
;
485 if (SUCCEEDED(url_history_stg2
->EnumUrls(enum_url
.Receive()))) {
486 std::vector
<ImporterURLRow
> rows
;
489 // IEnumSTATURL::Next() doesn't fill STATURL::dwFlags by default. Need to
490 // call IEnumSTATURL::SetFilter() with STATURL_QUERYFLAG_TOPLEVEL flag to
491 // specify how STATURL structure will be filled.
492 // The first argument of IEnumSTATURL::SetFilter() specifies the URL prefix
493 // that is used by IEnumSTATURL::Next() for filtering items by URL.
494 // So need to pass an empty string here to get all history items.
495 enum_url
->SetFilter(L
"", STATURL_QUERYFLAG_TOPLEVEL
);
496 while (!cancelled() &&
497 enum_url
->Next(1, &stat_url
, NULL
) == S_OK
) {
498 base::string16 url_string
;
499 if (stat_url
.pwcsUrl
) {
500 url_string
= stat_url
.pwcsUrl
;
501 CoTaskMemFree(stat_url
.pwcsUrl
);
503 base::string16 title_string
;
504 if (stat_url
.pwcsTitle
) {
505 title_string
= stat_url
.pwcsTitle
;
506 CoTaskMemFree(stat_url
.pwcsTitle
);
509 GURL
url(url_string
);
510 // Skips the URLs that are invalid or have other schemes.
511 if (!url
.is_valid() ||
512 (std::find(kSchemes
, kSchemes
+ total_schemes
, url
.scheme()) ==
513 kSchemes
+ total_schemes
))
516 ImporterURLRow
row(url
);
517 row
.title
= title_string
;
518 row
.last_visit
= base::Time::FromFileTime(stat_url
.ftLastVisited
);
519 if (stat_url
.dwFlags
== STATURLFLAG_ISTOPLEVEL
) {
523 // dwFlags should only contain the STATURLFLAG_ISTOPLEVEL bit per
524 // the filter set above.
525 DCHECK(!stat_url
.dwFlags
);
532 if (!rows
.empty() && !cancelled()) {
533 bridge_
->SetHistoryItems(rows
, importer::VISIT_SOURCE_IE_IMPORTED
);
538 void IEImporter::ImportPasswordsIE6() {
539 GUID AutocompleteGUID
= kPStoreAutocompleteGUID
;
540 if (!source_path_
.empty()) {
541 // We supply a fake GUID for testting.
542 AutocompleteGUID
= kUnittestGUID
;
545 // The PStoreCreateInstance function retrieves an interface pointer
546 // to a storage provider. But this function has no associated import
547 // library or header file, we must call it using the LoadLibrary()
548 // and GetProcAddress() functions.
549 typedef HRESULT (WINAPI
*PStoreCreateFunc
)(IPStore
**, DWORD
, DWORD
, DWORD
);
550 HMODULE pstorec_dll
= LoadLibrary(L
"pstorec.dll");
553 PStoreCreateFunc PStoreCreateInstance
=
554 (PStoreCreateFunc
)GetProcAddress(pstorec_dll
, "PStoreCreateInstance");
555 if (!PStoreCreateInstance
) {
556 FreeLibrary(pstorec_dll
);
560 base::win::ScopedComPtr
<IPStore
, &IID_IPStore
> pstore
;
561 HRESULT result
= PStoreCreateInstance(pstore
.Receive(), 0, 0, 0);
562 if (result
!= S_OK
) {
563 FreeLibrary(pstorec_dll
);
567 std::vector
<AutoCompleteInfo
> ac_list
;
569 // Enumerates AutoComplete items in the protected database.
570 base::win::ScopedComPtr
<IEnumPStoreItems
, &IID_IEnumPStoreItems
> item
;
571 result
= pstore
->EnumItems(0, &AutocompleteGUID
,
572 &AutocompleteGUID
, 0, item
.Receive());
573 if (result
!= PST_E_OK
) {
575 FreeLibrary(pstorec_dll
);
580 while (!cancelled() && SUCCEEDED(item
->Next(1, &item_name
, 0))) {
582 unsigned char* buffer
= NULL
;
583 result
= pstore
->ReadItem(0, &AutocompleteGUID
, &AutocompleteGUID
,
584 item_name
, &length
, &buffer
, NULL
, 0);
585 if (SUCCEEDED(result
)) {
589 data
.insert(0, reinterpret_cast<wchar_t*>(buffer
),
590 length
/ sizeof(wchar_t));
592 // The key name is always ended with ":StringData".
593 const wchar_t kDataSuffix
[] = L
":StringData";
594 size_t i
= ac
.key
.rfind(kDataSuffix
);
595 if (i
!= base::string16::npos
&& ac
.key
.substr(i
) == kDataSuffix
) {
597 ac
.is_url
= (ac
.key
.find(L
"://") != base::string16::npos
);
598 ac_list
.push_back(ac
);
599 ac_list
[ac_list
.size() - 1].data
= base::SplitString(
600 data
, base::string16(1, '\0'),
601 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
603 CoTaskMemFree(buffer
);
605 CoTaskMemFree(item_name
);
607 // Releases them before unload the dll.
610 FreeLibrary(pstorec_dll
);
613 for (i
= 0; i
< ac_list
.size(); i
++) {
614 if (!ac_list
[i
].is_url
|| ac_list
[i
].data
.size() < 2)
617 GURL
url(ac_list
[i
].key
.c_str());
618 if (!(base::LowerCaseEqualsASCII(url
.scheme(), url::kHttpScheme
) ||
619 base::LowerCaseEqualsASCII(url
.scheme(), url::kHttpsScheme
))) {
623 autofill::PasswordForm form
;
624 GURL::Replacements rp
;
629 form
.origin
= url
.ReplaceComponents(rp
);
630 form
.username_value
= ac_list
[i
].data
[0];
631 form
.password_value
= ac_list
[i
].data
[1];
632 form
.signon_realm
= url
.GetOrigin().spec();
634 // This is not precise, because a scheme of https does not imply a valid
635 // certificate was presented; however we assign it this way so that if we
636 // import a password from IE whose scheme is https, we give it the benefit
637 // of the doubt and DON'T auto-fill it unless the form appears under
638 // valid TLS conditions.
639 form
.ssl_valid
= url
.SchemeIsCryptographic();
641 // Goes through the list to find out the username field
643 size_t list_it
, item_it
;
644 for (list_it
= 0; list_it
< ac_list
.size(); ++list_it
) {
645 if (ac_list
[list_it
].is_url
)
648 for (item_it
= 0; item_it
< ac_list
[list_it
].data
.size(); ++item_it
)
649 if (ac_list
[list_it
].data
[item_it
] == form
.username_value
) {
650 form
.username_element
= ac_list
[list_it
].key
;
655 bridge_
->SetPasswordForm(form
);
659 void IEImporter::ImportPasswordsIE7() {
660 base::string16
key_path(importer::GetIE7PasswordsKey());
661 base::win::RegKey
key(HKEY_CURRENT_USER
, key_path
.c_str(), KEY_READ
);
662 base::win::RegistryValueIterator
reg_iterator(HKEY_CURRENT_USER
,
664 importer::ImporterIE7PasswordInfo password_info
;
665 while (reg_iterator
.Valid() && !cancelled()) {
666 // Get the size of the encrypted data.
668 key
.ReadValue(reg_iterator
.Name(), NULL
, &value_len
, NULL
);
670 // Query the encrypted data.
671 password_info
.encrypted_data
.resize(value_len
);
672 if (key
.ReadValue(reg_iterator
.Name(),
673 &password_info
.encrypted_data
.front(),
674 &value_len
, NULL
) == ERROR_SUCCESS
) {
675 password_info
.url_hash
= reg_iterator
.Name();
676 password_info
.date_created
= base::Time::Now();
678 bridge_
->AddIE7PasswordInfo(password_info
);
686 void IEImporter::ImportSearchEngines() {
687 // On IE, search engines are stored in the registry, under:
688 // Software\Microsoft\Internet Explorer\SearchScopes
689 // Each key represents a search engine. The URL value contains the URL and
690 // the DisplayName the name.
691 typedef std::map
<std::string
, base::string16
> SearchEnginesMap
;
692 SearchEnginesMap search_engines_map
;
693 for (base::win::RegistryKeyIterator
key_iter(HKEY_CURRENT_USER
,
694 kSearchScopePath
); key_iter
.Valid(); ++key_iter
) {
695 base::string16 sub_key_name
= kSearchScopePath
;
696 sub_key_name
.append(L
"\\").append(key_iter
.Name());
697 base::win::RegKey
sub_key(HKEY_CURRENT_USER
, sub_key_name
.c_str(),
699 base::string16 wide_url
;
700 if ((sub_key
.ReadValue(L
"URL", &wide_url
) != ERROR_SUCCESS
) ||
702 VLOG(1) << "No URL for IE search engine at " << key_iter
.Name();
705 // For the name, we try the default value first (as Live Search uses a
706 // non displayable name in DisplayName, and the readable name under the
709 if ((sub_key
.ReadValue(NULL
, &name
) != ERROR_SUCCESS
) || name
.empty()) {
710 // Try the displayable name.
711 if ((sub_key
.ReadValue(L
"DisplayName", &name
) != ERROR_SUCCESS
) ||
713 VLOG(1) << "No name for IE search engine at " << key_iter
.Name();
718 std::string
url(base::WideToUTF8(wide_url
));
719 SearchEnginesMap::iterator t_iter
= search_engines_map
.find(url
);
720 if (t_iter
== search_engines_map
.end()) {
721 // First time we see that URL.
723 if (gurl
.is_valid()) {
724 t_iter
= search_engines_map
.insert(std::make_pair(url
, name
)).first
;
728 // ProfileWriter::AddKeywords() requires a vector and we have a map.
729 std::vector
<importer::SearchEngineInfo
> search_engines
;
730 for (SearchEnginesMap::iterator i
= search_engines_map
.begin();
731 i
!= search_engines_map
.end(); ++i
) {
732 importer::SearchEngineInfo search_engine_info
;
733 search_engine_info
.url
= base::UTF8ToUTF16(i
->first
);
734 search_engine_info
.display_name
= i
->second
;
735 search_engines
.push_back(search_engine_info
);
737 bridge_
->SetKeywords(search_engines
, true);
740 void IEImporter::ImportHomepage() {
741 const wchar_t* kIEHomepage
= L
"Start Page";
742 const wchar_t* kIEDefaultHomepage
= L
"Default_Page_URL";
744 base::string16
key_path(importer::GetIESettingsKey());
746 base::win::RegKey
key(HKEY_CURRENT_USER
, key_path
.c_str(), KEY_READ
);
747 base::string16 homepage_url
;
748 if (key
.ReadValue(kIEHomepage
, &homepage_url
) != ERROR_SUCCESS
||
749 homepage_url
.empty())
752 GURL homepage
= GURL(homepage_url
);
753 if (!homepage
.is_valid())
756 // Check to see if this is the default website and skip import.
757 base::win::RegKey
keyDefault(HKEY_LOCAL_MACHINE
, key_path
.c_str(), KEY_READ
);
758 base::string16 default_homepage_url
;
759 LONG result
= keyDefault
.ReadValue(kIEDefaultHomepage
, &default_homepage_url
);
760 if (result
== ERROR_SUCCESS
&& !default_homepage_url
.empty()) {
761 if (homepage
.spec() == GURL(default_homepage_url
).spec())
764 bridge_
->AddHomePage(homepage
);
767 bool IEImporter::GetFavoritesInfo(IEImporter::FavoritesInfo
* info
) {
768 if (!source_path_
.empty()) {
769 // Source path exists during testing.
770 info
->path
= source_path_
;
771 info
->path
= info
->path
.AppendASCII("Favorites");
772 info
->links_folder
= L
"Links";
776 // IE stores the favorites in the Favorites under user profile's folder.
777 wchar_t buffer
[MAX_PATH
];
778 if (FAILED(SHGetFolderPath(NULL
, CSIDL_FAVORITES
, NULL
,
779 SHGFP_TYPE_CURRENT
, buffer
)))
781 info
->path
= base::FilePath(buffer
);
783 // There is a Links folder under Favorites folder in Windows Vista, but it
784 // is not recording in Vista's registry. So in Vista, we assume the Links
785 // folder is under Favorites folder since it looks like there is not name
786 // different in every language version of Windows Vista.
787 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
788 // The Link folder name is stored in the registry.
789 DWORD buffer_length
= sizeof(buffer
);
790 base::win::RegKey
reg_key(HKEY_CURRENT_USER
, kIEToolbarKey
, KEY_READ
);
791 if (reg_key
.ReadValue(L
"LinksFolderName", buffer
,
792 &buffer_length
, NULL
) != ERROR_SUCCESS
)
794 info
->links_folder
= buffer
;
796 info
->links_folder
= L
"Links";
802 void IEImporter::ParseFavoritesFolder(
803 const FavoritesInfo
& info
,
804 BookmarkVector
* bookmarks
,
805 favicon_base::FaviconUsageDataList
* favicons
) {
807 std::vector
<base::FilePath::StringType
> file_list
;
808 base::FilePath
favorites_path(info
.path
);
809 // Favorites path length. Make sure it doesn't include the trailing \.
810 size_t favorites_path_len
=
811 favorites_path
.StripTrailingSeparators().value().size();
812 base::FileEnumerator
file_enumerator(
813 favorites_path
, true, base::FileEnumerator::FILES
);
814 while (!(file
= file_enumerator
.Next()).value().empty() && !cancelled())
815 file_list
.push_back(file
.value());
817 // Keep the bookmarks in alphabetical order.
818 std::sort(file_list
.begin(), file_list
.end());
820 // Map from favicon URLs to the favicon data (the binary image data and the
821 // set of bookmark URLs referring to the favicon).
822 typedef std::map
<GURL
, favicon_base::FaviconUsageData
> FaviconMap
;
823 FaviconMap favicon_map
;
825 for (std::vector
<base::FilePath::StringType
>::iterator it
= file_list
.begin();
826 it
!= file_list
.end(); ++it
) {
827 base::FilePath
shortcut(*it
);
828 if (!base::LowerCaseEqualsASCII(shortcut
.Extension(), ".url"))
831 // Skip the bookmark with invalid URL.
832 base::win::ScopedComPtr
<IUniformResourceLocator
> url_locator
;
833 if (!LoadInternetShortcut(*it
, &url_locator
))
835 GURL url
= ReadURLFromInternetShortcut(url_locator
.get());
838 // Skip default bookmarks. go.microsoft.com redirects to
839 // search.microsoft.com, and http://go.microsoft.com/fwlink/?LinkId=XXX,
840 // which URLs IE has as default, to some another sites.
841 // We expect that users will never themselves create bookmarks having this
843 if (url
.host() == "go.microsoft.com")
846 UpdateFaviconMap(*it
, url
, url_locator
.get(), &favicon_map
);
848 // Make the relative path from the Favorites folder, without the basename.
849 // ex. Suppose that the Favorites folder is C:\Users\Foo\Favorites.
850 // C:\Users\Foo\Favorites\Foo.url -> ""
851 // C:\Users\Foo\Favorites\Links\Bar\Baz.url -> "Links\Bar"
852 base::FilePath::StringType relative_string
=
853 shortcut
.DirName().value().substr(favorites_path_len
);
854 if (!relative_string
.empty() &&
855 base::FilePath::IsSeparator(relative_string
[0]))
856 relative_string
= relative_string
.substr(1);
857 base::FilePath
relative_path(relative_string
);
859 ImportedBookmarkEntry entry
;
860 // Remove the dot, the file extension, and the directory path.
861 entry
.title
= shortcut
.RemoveExtension().BaseName().value();
863 entry
.creation_time
= GetFileCreationTime(*it
);
864 if (!relative_path
.empty())
865 relative_path
.GetComponents(&entry
.path
);
868 if (!entry
.path
.empty() && entry
.path
[0] == info
.links_folder
) {
869 // Bookmarks in the Link folder should be imported to the toolbar.
870 entry
.in_toolbar
= true;
872 bookmarks
->push_back(entry
);
875 // Reflect the menu order in IE.
876 SortBookmarksInIEOrder(this, bookmarks
);
878 // Record favicon data.
879 for (FaviconMap::iterator iter
= favicon_map
.begin();
880 iter
!= favicon_map
.end(); ++iter
)
881 favicons
->push_back(iter
->second
);
884 int IEImporter::CurrentIEVersion() const {
885 static int version
= -1;
888 DWORD buffer_length
= sizeof(buffer
);
889 base::win::RegKey
reg_key(HKEY_LOCAL_MACHINE
, kIEVersionKey
, KEY_READ
);
890 LONG result
= reg_key
.ReadValue(L
"Version", buffer
, &buffer_length
, NULL
);
891 version
= ((result
== ERROR_SUCCESS
)? _wtoi(buffer
) : 0);