Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / chrome / utility / importer / ie_importer_win.cc
blobe1f5e4fb947d13787c0be5ab0220118bce55ec6e
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"
7 #include <ole2.h>
8 #include <intshcut.h>
9 #include <shlobj.h>
10 #include <urlhist.h>
11 #include <wininet.h>
13 #include <algorithm>
14 #include <map>
15 #include <string>
16 #include <vector>
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"
42 #include "url/gurl.h"
43 #include "url/url_constants.h"
45 namespace {
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 {
60 base::string16 key;
61 std::vector<base::string16> data;
62 bool is_url;
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,
71 NULL, OPEN_EXISTING,
72 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL));
73 FILETIME creation_filetime;
74 if (!file_handle.IsValid())
75 return creation_time;
76 if (GetFileTime(file_handle.Get(), &creation_filetime, NULL, NULL))
77 creation_time = base::Time::FromFileTime(creation_filetime);
78 return creation_time;
81 // Safely read an object of type T from a raw sequence of bytes.
82 template<typename T>
83 bool BinaryRead(T* data, size_t offset, const std::vector<uint8>& blob) {
84 if (offset + sizeof(T) > blob.size())
85 return false;
86 memcpy(data, &blob[offset], sizeof(T));
87 return true;
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
95 // the binary blob.
96 LPCITEMIDLIST BinaryReadItemIDList(size_t offset, size_t idlist_size,
97 const std::vector<uint8>& blob) {
98 size_t head = 0;
99 while (true) {
100 // Use a USHORT instead of SHITEMID to avoid buffer over read.
101 USHORT id_cb;
102 if (head >= idlist_size || !BinaryRead(&id_cb, offset + head, blob))
103 return NULL;
104 if (id_cb == 0)
105 break;
106 head += id_cb;
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);
126 if (lhs_i == rhs_i)
127 continue;
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
134 : lhs_iter->second);
135 uint32 rhs_sort_index = (rhs_iter == sort_index_->end() ? kNotSorted
136 : rhs_iter->second);
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
156 // struct {
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;
160 // };
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))
174 return false;
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())
188 return false;
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))
193 return false;
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))
200 return false;
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;
208 return true;
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))
224 return false;
228 // Recursively parse subfolders.
229 for (base::win::RegistryKeyIterator child(key.Handle(), L"");
230 child.Valid() && !importer->cancelled();
231 ++child) {
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,
236 sort_index)) {
237 return false;
241 return true;
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);
249 if (!key.Valid())
250 return false;
251 return ParseFavoritesOrderRegistryTree(importer, key, base::FilePath(),
252 sort_index);
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))
262 return;
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
268 // representing it.
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)))
275 return false;
277 base::win::ScopedComPtr<IPersistFile> persist_file;
278 if (FAILED(persist_file.QueryFrom(url_locator.get())))
279 return false;
281 // Loads the Internet Shortcut from persistent storage.
282 if (FAILED(persist_file->Load(file.c_str(), STGM_READ)))
283 return false;
285 std::swap(url_locator, *shortcut);
286 return true;
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)))
301 return GURL();
303 base::win::ScopedComPtr<IPropertyStorage> property_storage;
304 if (FAILED(property_set_storage->Open(FMTID_Intshcut, STGM_READ,
305 property_storage.Receive()))) {
306 return GURL();
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
312 // equivalent.
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)
318 return GURL();
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,
325 std::string* data) {
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()));
334 DWORD info_size = 0;
335 GetUrlCacheEntryInfoEx(url_wstring.c_str(), NULL, &info_size, NULL, NULL,
336 NULL, 0);
337 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
338 return false;
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,
344 NULL, NULL, 0)) {
345 return false;
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)) {
359 return false;
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,
370 const GURL& url,
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())
375 return;
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);
382 } else {
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));
393 } // namespace
395 // static
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,
411 uint16 items,
412 ImporterBridge* bridge) {
413 bridge_ = 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);
426 ImportHistory();
427 bridge_->NotifyItemEnded(importer::HISTORY);
429 if ((items & importer::FAVORITES) && !cancelled()) {
430 bridge_->NotifyItemStarted(importer::FAVORITES);
431 ImportFavorites();
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() {
455 FavoritesInfo info;
456 if (!GetFavoritesInfo(&info))
457 return;
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,
474 url::kHttpsScheme,
475 url::kFtpScheme,
476 url::kFileScheme};
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))) {
482 return;
484 base::win::ScopedComPtr<IEnumSTATURL> enum_url;
485 if (SUCCEEDED(url_history_stg2->EnumUrls(enum_url.Receive()))) {
486 std::vector<ImporterURLRow> rows;
487 STATURL stat_url;
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))
514 continue;
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) {
520 row.visit_count = 1;
521 row.hidden = false;
522 } else {
523 // dwFlags should only contain the STATURLFLAG_ISTOPLEVEL bit per
524 // the filter set above.
525 DCHECK(!stat_url.dwFlags);
526 row.hidden = true;
529 rows.push_back(row);
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");
551 if (!pstorec_dll)
552 return;
553 PStoreCreateFunc PStoreCreateInstance =
554 (PStoreCreateFunc)GetProcAddress(pstorec_dll, "PStoreCreateInstance");
555 if (!PStoreCreateInstance) {
556 FreeLibrary(pstorec_dll);
557 return;
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);
564 return;
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) {
574 pstore.Release();
575 FreeLibrary(pstorec_dll);
576 return;
579 wchar_t* item_name;
580 while (!cancelled() && SUCCEEDED(item->Next(1, &item_name, 0))) {
581 DWORD length = 0;
582 unsigned char* buffer = NULL;
583 result = pstore->ReadItem(0, &AutocompleteGUID, &AutocompleteGUID,
584 item_name, &length, &buffer, NULL, 0);
585 if (SUCCEEDED(result)) {
586 AutoCompleteInfo ac;
587 ac.key = item_name;
588 base::string16 data;
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) {
596 ac.key.erase(i);
597 ac.is_url = (ac.key.find(L"://") != base::string16::npos);
598 ac_list.push_back(ac);
599 base::SplitString(data, L'\0', &ac_list[ac_list.size() - 1].data);
601 CoTaskMemFree(buffer);
603 CoTaskMemFree(item_name);
605 // Releases them before unload the dll.
606 item.Release();
607 pstore.Release();
608 FreeLibrary(pstorec_dll);
610 size_t i;
611 for (i = 0; i < ac_list.size(); i++) {
612 if (!ac_list[i].is_url || ac_list[i].data.size() < 2)
613 continue;
615 GURL url(ac_list[i].key.c_str());
616 if (!(LowerCaseEqualsASCII(url.scheme(), url::kHttpScheme) ||
617 LowerCaseEqualsASCII(url.scheme(), url::kHttpsScheme))) {
618 continue;
621 autofill::PasswordForm form;
622 GURL::Replacements rp;
623 rp.ClearUsername();
624 rp.ClearPassword();
625 rp.ClearQuery();
626 rp.ClearRef();
627 form.origin = url.ReplaceComponents(rp);
628 form.username_value = ac_list[i].data[0];
629 form.password_value = ac_list[i].data[1];
630 form.signon_realm = url.GetOrigin().spec();
632 // This is not precise, because a scheme of https does not imply a valid
633 // certificate was presented; however we assign it this way so that if we
634 // import a password from IE whose scheme is https, we give it the benefit
635 // of the doubt and DON'T auto-fill it unless the form appears under
636 // valid TLS conditions.
637 form.ssl_valid = url.SchemeIsCryptographic();
639 // Goes through the list to find out the username field
640 // of the web page.
641 size_t list_it, item_it;
642 for (list_it = 0; list_it < ac_list.size(); ++list_it) {
643 if (ac_list[list_it].is_url)
644 continue;
646 for (item_it = 0; item_it < ac_list[list_it].data.size(); ++item_it)
647 if (ac_list[list_it].data[item_it] == form.username_value) {
648 form.username_element = ac_list[list_it].key;
649 break;
653 bridge_->SetPasswordForm(form);
657 void IEImporter::ImportPasswordsIE7() {
658 base::string16 key_path(importer::GetIE7PasswordsKey());
659 base::win::RegKey key(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ);
660 base::win::RegistryValueIterator reg_iterator(HKEY_CURRENT_USER,
661 key_path.c_str());
662 importer::ImporterIE7PasswordInfo password_info;
663 while (reg_iterator.Valid() && !cancelled()) {
664 // Get the size of the encrypted data.
665 DWORD value_len = 0;
666 key.ReadValue(reg_iterator.Name(), NULL, &value_len, NULL);
667 if (value_len) {
668 // Query the encrypted data.
669 password_info.encrypted_data.resize(value_len);
670 if (key.ReadValue(reg_iterator.Name(),
671 &password_info.encrypted_data.front(),
672 &value_len, NULL) == ERROR_SUCCESS) {
673 password_info.url_hash = reg_iterator.Name();
674 password_info.date_created = base::Time::Now();
676 bridge_->AddIE7PasswordInfo(password_info);
680 ++reg_iterator;
684 void IEImporter::ImportSearchEngines() {
685 // On IE, search engines are stored in the registry, under:
686 // Software\Microsoft\Internet Explorer\SearchScopes
687 // Each key represents a search engine. The URL value contains the URL and
688 // the DisplayName the name.
689 typedef std::map<std::string, base::string16> SearchEnginesMap;
690 SearchEnginesMap search_engines_map;
691 for (base::win::RegistryKeyIterator key_iter(HKEY_CURRENT_USER,
692 kSearchScopePath); key_iter.Valid(); ++key_iter) {
693 base::string16 sub_key_name = kSearchScopePath;
694 sub_key_name.append(L"\\").append(key_iter.Name());
695 base::win::RegKey sub_key(HKEY_CURRENT_USER, sub_key_name.c_str(),
696 KEY_READ);
697 base::string16 wide_url;
698 if ((sub_key.ReadValue(L"URL", &wide_url) != ERROR_SUCCESS) ||
699 wide_url.empty()) {
700 VLOG(1) << "No URL for IE search engine at " << key_iter.Name();
701 continue;
703 // For the name, we try the default value first (as Live Search uses a
704 // non displayable name in DisplayName, and the readable name under the
705 // default value).
706 base::string16 name;
707 if ((sub_key.ReadValue(NULL, &name) != ERROR_SUCCESS) || name.empty()) {
708 // Try the displayable name.
709 if ((sub_key.ReadValue(L"DisplayName", &name) != ERROR_SUCCESS) ||
710 name.empty()) {
711 VLOG(1) << "No name for IE search engine at " << key_iter.Name();
712 continue;
716 std::string url(base::WideToUTF8(wide_url));
717 SearchEnginesMap::iterator t_iter = search_engines_map.find(url);
718 if (t_iter == search_engines_map.end()) {
719 // First time we see that URL.
720 GURL gurl(url);
721 if (gurl.is_valid()) {
722 t_iter = search_engines_map.insert(std::make_pair(url, name)).first;
726 // ProfileWriter::AddKeywords() requires a vector and we have a map.
727 std::vector<importer::SearchEngineInfo> search_engines;
728 for (SearchEnginesMap::iterator i = search_engines_map.begin();
729 i != search_engines_map.end(); ++i) {
730 importer::SearchEngineInfo search_engine_info;
731 search_engine_info.url = base::UTF8ToUTF16(i->first);
732 search_engine_info.display_name = i->second;
733 search_engines.push_back(search_engine_info);
735 bridge_->SetKeywords(search_engines, true);
738 void IEImporter::ImportHomepage() {
739 const wchar_t* kIEHomepage = L"Start Page";
740 const wchar_t* kIEDefaultHomepage = L"Default_Page_URL";
742 base::string16 key_path(importer::GetIESettingsKey());
744 base::win::RegKey key(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ);
745 base::string16 homepage_url;
746 if (key.ReadValue(kIEHomepage, &homepage_url) != ERROR_SUCCESS ||
747 homepage_url.empty())
748 return;
750 GURL homepage = GURL(homepage_url);
751 if (!homepage.is_valid())
752 return;
754 // Check to see if this is the default website and skip import.
755 base::win::RegKey keyDefault(HKEY_LOCAL_MACHINE, key_path.c_str(), KEY_READ);
756 base::string16 default_homepage_url;
757 LONG result = keyDefault.ReadValue(kIEDefaultHomepage, &default_homepage_url);
758 if (result == ERROR_SUCCESS && !default_homepage_url.empty()) {
759 if (homepage.spec() == GURL(default_homepage_url).spec())
760 return;
762 bridge_->AddHomePage(homepage);
765 bool IEImporter::GetFavoritesInfo(IEImporter::FavoritesInfo* info) {
766 if (!source_path_.empty()) {
767 // Source path exists during testing.
768 info->path = source_path_;
769 info->path = info->path.AppendASCII("Favorites");
770 info->links_folder = L"Links";
771 return true;
774 // IE stores the favorites in the Favorites under user profile's folder.
775 wchar_t buffer[MAX_PATH];
776 if (FAILED(SHGetFolderPath(NULL, CSIDL_FAVORITES, NULL,
777 SHGFP_TYPE_CURRENT, buffer)))
778 return false;
779 info->path = base::FilePath(buffer);
781 // There is a Links folder under Favorites folder in Windows Vista, but it
782 // is not recording in Vista's registry. So in Vista, we assume the Links
783 // folder is under Favorites folder since it looks like there is not name
784 // different in every language version of Windows Vista.
785 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
786 // The Link folder name is stored in the registry.
787 DWORD buffer_length = sizeof(buffer);
788 base::win::RegKey reg_key(HKEY_CURRENT_USER, kIEToolbarKey, KEY_READ);
789 if (reg_key.ReadValue(L"LinksFolderName", buffer,
790 &buffer_length, NULL) != ERROR_SUCCESS)
791 return false;
792 info->links_folder = buffer;
793 } else {
794 info->links_folder = L"Links";
797 return true;
800 void IEImporter::ParseFavoritesFolder(
801 const FavoritesInfo& info,
802 BookmarkVector* bookmarks,
803 favicon_base::FaviconUsageDataList* favicons) {
804 base::FilePath file;
805 std::vector<base::FilePath::StringType> file_list;
806 base::FilePath favorites_path(info.path);
807 // Favorites path length. Make sure it doesn't include the trailing \.
808 size_t favorites_path_len =
809 favorites_path.StripTrailingSeparators().value().size();
810 base::FileEnumerator file_enumerator(
811 favorites_path, true, base::FileEnumerator::FILES);
812 while (!(file = file_enumerator.Next()).value().empty() && !cancelled())
813 file_list.push_back(file.value());
815 // Keep the bookmarks in alphabetical order.
816 std::sort(file_list.begin(), file_list.end());
818 // Map from favicon URLs to the favicon data (the binary image data and the
819 // set of bookmark URLs referring to the favicon).
820 typedef std::map<GURL, favicon_base::FaviconUsageData> FaviconMap;
821 FaviconMap favicon_map;
823 for (std::vector<base::FilePath::StringType>::iterator it = file_list.begin();
824 it != file_list.end(); ++it) {
825 base::FilePath shortcut(*it);
826 if (!LowerCaseEqualsASCII(shortcut.Extension(), ".url"))
827 continue;
829 // Skip the bookmark with invalid URL.
830 base::win::ScopedComPtr<IUniformResourceLocator> url_locator;
831 if (!LoadInternetShortcut(*it, &url_locator))
832 continue;
833 GURL url = ReadURLFromInternetShortcut(url_locator.get());
834 if (!url.is_valid())
835 continue;
836 // Skip default bookmarks. go.microsoft.com redirects to
837 // search.microsoft.com, and http://go.microsoft.com/fwlink/?LinkId=XXX,
838 // which URLs IE has as default, to some another sites.
839 // We expect that users will never themselves create bookmarks having this
840 // hostname.
841 if (url.host() == "go.microsoft.com")
842 continue;
843 // Read favicon.
844 UpdateFaviconMap(*it, url, url_locator.get(), &favicon_map);
846 // Make the relative path from the Favorites folder, without the basename.
847 // ex. Suppose that the Favorites folder is C:\Users\Foo\Favorites.
848 // C:\Users\Foo\Favorites\Foo.url -> ""
849 // C:\Users\Foo\Favorites\Links\Bar\Baz.url -> "Links\Bar"
850 base::FilePath::StringType relative_string =
851 shortcut.DirName().value().substr(favorites_path_len);
852 if (!relative_string.empty() &&
853 base::FilePath::IsSeparator(relative_string[0]))
854 relative_string = relative_string.substr(1);
855 base::FilePath relative_path(relative_string);
857 ImportedBookmarkEntry entry;
858 // Remove the dot, the file extension, and the directory path.
859 entry.title = shortcut.RemoveExtension().BaseName().value();
860 entry.url = url;
861 entry.creation_time = GetFileCreationTime(*it);
862 if (!relative_path.empty())
863 relative_path.GetComponents(&entry.path);
865 // Add the bookmark.
866 if (!entry.path.empty() && entry.path[0] == info.links_folder) {
867 // Bookmarks in the Link folder should be imported to the toolbar.
868 entry.in_toolbar = true;
870 bookmarks->push_back(entry);
873 // Reflect the menu order in IE.
874 SortBookmarksInIEOrder(this, bookmarks);
876 // Record favicon data.
877 for (FaviconMap::iterator iter = favicon_map.begin();
878 iter != favicon_map.end(); ++iter)
879 favicons->push_back(iter->second);
882 int IEImporter::CurrentIEVersion() const {
883 static int version = -1;
884 if (version < 0) {
885 wchar_t buffer[128];
886 DWORD buffer_length = sizeof(buffer);
887 base::win::RegKey reg_key(HKEY_LOCAL_MACHINE, kIEVersionKey, KEY_READ);
888 LONG result = reg_key.ReadValue(L"Version", buffer, &buffer_length, NULL);
889 version = ((result == ERROR_SUCCESS)? _wtoi(buffer) : 0);
891 return version;