| OLD | NEW |
| (Empty) |
| 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/browser/importer/ie_importer.h" | |
| 6 | |
| 7 #include <ole2.h> | |
| 8 #include <intshcut.h> | |
| 9 #include <shlobj.h> | |
| 10 #include <urlhist.h> | |
| 11 #include <wininet.h> | |
| 12 | |
| 13 #include <algorithm> | |
| 14 #include <map> | |
| 15 #include <string> | |
| 16 #include <vector> | |
| 17 | |
| 18 #include "base/file_util.h" | |
| 19 #include "base/files/file_enumerator.h" | |
| 20 #include "base/files/file_path.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/browser/favicon/favicon_util.h" | |
| 33 #include "chrome/browser/importer/ie_importer_utils_win.h" | |
| 34 #include "chrome/browser/importer/importer_bridge.h" | |
| 35 #include "chrome/browser/importer/pstore_declarations.h" | |
| 36 #include "chrome/common/importer/imported_bookmark_entry.h" | |
| 37 #include "chrome/common/importer/imported_favicon_usage.h" | |
| 38 #include "chrome/common/importer/importer_data_types.h" | |
| 39 #include "chrome/common/importer/importer_url_row.h" | |
| 40 #include "chrome/common/time_format.h" | |
| 41 #include "chrome/common/url_constants.h" | |
| 42 #include "content/public/common/password_form.h" | |
| 43 #include "grit/generated_resources.h" | |
| 44 #include "ui/base/l10n/l10n_util.h" | |
| 45 #include "url/gurl.h" | |
| 46 | |
| 47 namespace { | |
| 48 | |
| 49 // Registry key paths from which we import IE settings. | |
| 50 const char16 kSearchScopePath[] = | |
| 51 L"Software\\Microsoft\\Internet Explorer\\SearchScopes"; | |
| 52 const char16 kIEVersionKey[] = | |
| 53 L"Software\\Microsoft\\Internet Explorer"; | |
| 54 const char16 kIEToolbarKey[] = | |
| 55 L"Software\\Microsoft\\Internet Explorer\\Toolbar"; | |
| 56 | |
| 57 // NTFS stream name of favicon image data. | |
| 58 const char16 kFaviconStreamName[] = L":favicon:$DATA"; | |
| 59 | |
| 60 // A struct that hosts the information of AutoComplete data in PStore. | |
| 61 struct AutoCompleteInfo { | |
| 62 string16 key; | |
| 63 std::vector<string16> data; | |
| 64 bool is_url; | |
| 65 }; | |
| 66 | |
| 67 // Gets the creation time of the given file or directory. | |
| 68 base::Time GetFileCreationTime(const string16& file) { | |
| 69 base::Time creation_time; | |
| 70 base::win::ScopedHandle file_handle( | |
| 71 CreateFile(file.c_str(), GENERIC_READ, | |
| 72 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | |
| 73 NULL, OPEN_EXISTING, | |
| 74 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL)); | |
| 75 FILETIME creation_filetime; | |
| 76 if (GetFileTime(file_handle, &creation_filetime, NULL, NULL)) | |
| 77 creation_time = base::Time::FromFileTime(creation_filetime); | |
| 78 return creation_time; | |
| 79 } | |
| 80 | |
| 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; | |
| 88 } | |
| 89 | |
| 90 // Safely read an ITEMIDLIST from a raw sequence of bytes. | |
| 91 // | |
| 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; | |
| 107 } | |
| 108 return reinterpret_cast<LPCITEMIDLIST>(&blob[offset]); | |
| 109 } | |
| 110 | |
| 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; | |
| 141 } | |
| 142 return lhs.path.size() < rhs.path.size(); | |
| 143 } | |
| 144 const std::map<base::FilePath, uint32>* sort_index_; | |
| 145 }; | |
| 146 | |
| 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. | |
| 151 // | |
| 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; | |
| 170 | |
| 171 // Read the number of items. | |
| 172 uint32 item_count = 0; | |
| 173 if (!BinaryRead(&item_count, kItemCountOffset, blob)) | |
| 174 return false; | |
| 175 | |
| 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; | |
| 182 | |
| 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; | |
| 189 | |
| 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; | |
| 194 | |
| 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 || FAILED(SHGetPathFromIDList(idlist, item_filename))) | |
| 200 return false; | |
| 201 base::FilePath item_relative_path = | |
| 202 path.Append(base::FilePath(item_filename).BaseName()); | |
| 203 | |
| 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; | |
| 207 } | |
| 208 return true; | |
| 209 } | |
| 210 | |
| 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; | |
| 225 } | |
| 226 } | |
| 227 | |
| 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; | |
| 238 } | |
| 239 } | |
| 240 } | |
| 241 return true; | |
| 242 } | |
| 243 | |
| 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); | |
| 253 } | |
| 254 | |
| 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); | |
| 265 } | |
| 266 | |
| 267 // Reads an internet shortcut (*.url) |file| and returns a COM object | |
| 268 // representing it. | |
| 269 bool LoadInternetShortcut( | |
| 270 const 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; | |
| 276 | |
| 277 base::win::ScopedComPtr<IPersistFile> persist_file; | |
| 278 if (FAILED(persist_file.QueryFrom(url_locator))) | |
| 279 return false; | |
| 280 | |
| 281 // Loads the Internet Shortcut from persistent storage. | |
| 282 if (FAILED(persist_file->Load(file.c_str(), STGM_READ))) | |
| 283 return false; | |
| 284 | |
| 285 std::swap(url_locator, *shortcut); | |
| 286 return true; | |
| 287 } | |
| 288 | |
| 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(WideToUTF16(std::wstring(url))); | |
| 295 } | |
| 296 | |
| 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(); | |
| 302 | |
| 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(); | |
| 307 } | |
| 308 | |
| 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(WideToUTF16(output.get().pwszVal)); | |
| 320 } | |
| 321 | |
| 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 string16& file, | |
| 325 std::string* data) { | |
| 326 return file_util::ReadFileToString( | |
| 327 base::FilePath(file + kFaviconStreamName), data); | |
| 328 } | |
| 329 | |
| 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(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; | |
| 339 | |
| 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; | |
| 346 } | |
| 347 return file_util::ReadFileToString(base::FilePath(cache->lpszLocalFileName), | |
| 348 data); | |
| 349 } | |
| 350 | |
| 351 // Reads the binary image data of favicon of an internet shortcut file |file|. | |
| 352 // |favicon_url| read by ReadFaviconURLFromInternetShortcut is also needed to | |
| 353 // examine the IE cache. | |
| 354 bool ReadReencodedFaviconData(const string16& file, | |
| 355 const GURL& favicon_url, | |
| 356 std::vector<unsigned char>* data) { | |
| 357 std::string image_data; | |
| 358 if (!ReadFaviconDataFromInternetShortcut(file, &image_data) && | |
| 359 !ReadFaviconDataFromCache(favicon_url, &image_data)) { | |
| 360 return false; | |
| 361 } | |
| 362 | |
| 363 const unsigned char* ptr = | |
| 364 reinterpret_cast<const unsigned char*>(image_data.c_str()); | |
| 365 return FaviconUtil::ReencodeFavicon(ptr, image_data.size(), data); | |
| 366 } | |
| 367 | |
| 368 // Loads favicon image data and registers to |favicon_map|. | |
| 369 void UpdateFaviconMap( | |
| 370 const string16& url_file, | |
| 371 const GURL& url, | |
| 372 IUniformResourceLocator* url_locator, | |
| 373 std::map<GURL, ImportedFaviconUsage>* favicon_map) { | |
| 374 GURL favicon_url = ReadFaviconURLFromInternetShortcut(url_locator); | |
| 375 if (!favicon_url.is_valid()) | |
| 376 return; | |
| 377 | |
| 378 std::map<GURL, ImportedFaviconUsage>::iterator it = | |
| 379 favicon_map->find(favicon_url); | |
| 380 if (it != favicon_map->end()) { | |
| 381 // Known favicon URL. | |
| 382 it->second.urls.insert(url); | |
| 383 } else { | |
| 384 // New favicon URL. Read the image data and store. | |
| 385 ImportedFaviconUsage usage; | |
| 386 if (ReadReencodedFaviconData(url_file, favicon_url, &usage.png_data)) { | |
| 387 usage.favicon_url = favicon_url; | |
| 388 usage.urls.insert(url); | |
| 389 favicon_map->insert(std::make_pair(favicon_url, usage)); | |
| 390 } | |
| 391 } | |
| 392 } | |
| 393 | |
| 394 } // namespace | |
| 395 | |
| 396 // static | |
| 397 // {E161255A-37C3-11D2-BCAA-00C04fD929DB} | |
| 398 const GUID IEImporter::kPStoreAutocompleteGUID = { | |
| 399 0xe161255a, 0x37c3, 0x11d2, | |
| 400 { 0xbc, 0xaa, 0x00, 0xc0, 0x4f, 0xd9, 0x29, 0xdb } | |
| 401 }; | |
| 402 // {A79029D6-753E-4e27-B807-3D46AB1545DF} | |
| 403 const GUID IEImporter::kUnittestGUID = { | |
| 404 0xa79029d6, 0x753e, 0x4e27, | |
| 405 { 0xb8, 0x7, 0x3d, 0x46, 0xab, 0x15, 0x45, 0xdf } | |
| 406 }; | |
| 407 | |
| 408 IEImporter::IEImporter() { | |
| 409 } | |
| 410 | |
| 411 void IEImporter::StartImport(const importer::SourceProfile& source_profile, | |
| 412 uint16 items, | |
| 413 ImporterBridge* bridge) { | |
| 414 bridge_ = bridge; | |
| 415 source_path_ = source_profile.source_path; | |
| 416 | |
| 417 bridge_->NotifyStarted(); | |
| 418 | |
| 419 if ((items & importer::HOME_PAGE) && !cancelled()) { | |
| 420 bridge_->NotifyItemStarted(importer::HOME_PAGE); | |
| 421 ImportHomepage(); // Doesn't have a UI item. | |
| 422 bridge_->NotifyItemEnded(importer::HOME_PAGE); | |
| 423 } | |
| 424 // The order here is important! | |
| 425 if ((items & importer::HISTORY) && !cancelled()) { | |
| 426 bridge_->NotifyItemStarted(importer::HISTORY); | |
| 427 ImportHistory(); | |
| 428 bridge_->NotifyItemEnded(importer::HISTORY); | |
| 429 } | |
| 430 if ((items & importer::FAVORITES) && !cancelled()) { | |
| 431 bridge_->NotifyItemStarted(importer::FAVORITES); | |
| 432 ImportFavorites(); | |
| 433 bridge_->NotifyItemEnded(importer::FAVORITES); | |
| 434 } | |
| 435 if ((items & importer::SEARCH_ENGINES) && !cancelled()) { | |
| 436 bridge_->NotifyItemStarted(importer::SEARCH_ENGINES); | |
| 437 ImportSearchEngines(); | |
| 438 bridge_->NotifyItemEnded(importer::SEARCH_ENGINES); | |
| 439 } | |
| 440 if ((items & importer::PASSWORDS) && !cancelled()) { | |
| 441 bridge_->NotifyItemStarted(importer::PASSWORDS); | |
| 442 // Always import IE6 passwords. | |
| 443 ImportPasswordsIE6(); | |
| 444 | |
| 445 if (CurrentIEVersion() >= 7) | |
| 446 ImportPasswordsIE7(); | |
| 447 bridge_->NotifyItemEnded(importer::PASSWORDS); | |
| 448 } | |
| 449 bridge_->NotifyEnded(); | |
| 450 } | |
| 451 | |
| 452 IEImporter::~IEImporter() { | |
| 453 } | |
| 454 | |
| 455 void IEImporter::ImportFavorites() { | |
| 456 FavoritesInfo info; | |
| 457 if (!GetFavoritesInfo(&info)) | |
| 458 return; | |
| 459 | |
| 460 BookmarkVector bookmarks; | |
| 461 std::vector<ImportedFaviconUsage> favicons; | |
| 462 ParseFavoritesFolder(info, &bookmarks, &favicons); | |
| 463 | |
| 464 if (!bookmarks.empty() && !cancelled()) { | |
| 465 const string16& first_folder_name = | |
| 466 l10n_util::GetStringUTF16(IDS_BOOKMARK_GROUP_FROM_IE); | |
| 467 bridge_->AddBookmarks(bookmarks, first_folder_name); | |
| 468 } | |
| 469 if (!favicons.empty() && !cancelled()) | |
| 470 bridge_->SetFavicons(favicons); | |
| 471 } | |
| 472 | |
| 473 void IEImporter::ImportHistory() { | |
| 474 const std::string kSchemes[] = {chrome::kHttpScheme, | |
| 475 chrome::kHttpsScheme, | |
| 476 chrome::kFtpScheme, | |
| 477 chrome::kFileScheme}; | |
| 478 int total_schemes = arraysize(kSchemes); | |
| 479 | |
| 480 base::win::ScopedComPtr<IUrlHistoryStg2> url_history_stg2; | |
| 481 HRESULT result; | |
| 482 result = url_history_stg2.CreateInstance(CLSID_CUrlHistory, NULL, | |
| 483 CLSCTX_INPROC_SERVER); | |
| 484 if (FAILED(result)) | |
| 485 return; | |
| 486 base::win::ScopedComPtr<IEnumSTATURL> enum_url; | |
| 487 if (SUCCEEDED(result = url_history_stg2->EnumUrls(enum_url.Receive()))) { | |
| 488 std::vector<ImporterURLRow> rows; | |
| 489 STATURL stat_url; | |
| 490 ULONG fetched; | |
| 491 while (!cancelled() && | |
| 492 (result = enum_url->Next(1, &stat_url, &fetched)) == S_OK) { | |
| 493 string16 url_string; | |
| 494 if (stat_url.pwcsUrl) { | |
| 495 url_string = stat_url.pwcsUrl; | |
| 496 CoTaskMemFree(stat_url.pwcsUrl); | |
| 497 } | |
| 498 string16 title_string; | |
| 499 if (stat_url.pwcsTitle) { | |
| 500 title_string = stat_url.pwcsTitle; | |
| 501 CoTaskMemFree(stat_url.pwcsTitle); | |
| 502 } | |
| 503 | |
| 504 GURL url(url_string); | |
| 505 // Skips the URLs that are invalid or have other schemes. | |
| 506 if (!url.is_valid() || | |
| 507 (std::find(kSchemes, kSchemes + total_schemes, url.scheme()) == | |
| 508 kSchemes + total_schemes)) | |
| 509 continue; | |
| 510 | |
| 511 ImporterURLRow row(url); | |
| 512 row.title = title_string; | |
| 513 row.last_visit = base::Time::FromFileTime(stat_url.ftLastVisited); | |
| 514 if (stat_url.dwFlags == STATURL_QUERYFLAG_TOPLEVEL) { | |
| 515 row.visit_count = 1; | |
| 516 row.hidden = false; | |
| 517 } else { | |
| 518 row.hidden = true; | |
| 519 } | |
| 520 | |
| 521 rows.push_back(row); | |
| 522 } | |
| 523 | |
| 524 if (!rows.empty() && !cancelled()) { | |
| 525 bridge_->SetHistoryItems(rows, importer::VISIT_SOURCE_IE_IMPORTED); | |
| 526 } | |
| 527 } | |
| 528 } | |
| 529 | |
| 530 void IEImporter::ImportPasswordsIE6() { | |
| 531 GUID AutocompleteGUID = kPStoreAutocompleteGUID; | |
| 532 if (!source_path_.empty()) { | |
| 533 // We supply a fake GUID for testting. | |
| 534 AutocompleteGUID = kUnittestGUID; | |
| 535 } | |
| 536 | |
| 537 // The PStoreCreateInstance function retrieves an interface pointer | |
| 538 // to a storage provider. But this function has no associated import | |
| 539 // library or header file, we must call it using the LoadLibrary() | |
| 540 // and GetProcAddress() functions. | |
| 541 typedef HRESULT (WINAPI *PStoreCreateFunc)(IPStore**, DWORD, DWORD, DWORD); | |
| 542 HMODULE pstorec_dll = LoadLibrary(L"pstorec.dll"); | |
| 543 if (!pstorec_dll) | |
| 544 return; | |
| 545 PStoreCreateFunc PStoreCreateInstance = | |
| 546 (PStoreCreateFunc)GetProcAddress(pstorec_dll, "PStoreCreateInstance"); | |
| 547 if (!PStoreCreateInstance) { | |
| 548 FreeLibrary(pstorec_dll); | |
| 549 return; | |
| 550 } | |
| 551 | |
| 552 base::win::ScopedComPtr<IPStore, &IID_IPStore> pstore; | |
| 553 HRESULT result = PStoreCreateInstance(pstore.Receive(), 0, 0, 0); | |
| 554 if (result != S_OK) { | |
| 555 FreeLibrary(pstorec_dll); | |
| 556 return; | |
| 557 } | |
| 558 | |
| 559 std::vector<AutoCompleteInfo> ac_list; | |
| 560 | |
| 561 // Enumerates AutoComplete items in the protected database. | |
| 562 base::win::ScopedComPtr<IEnumPStoreItems, &IID_IEnumPStoreItems> item; | |
| 563 result = pstore->EnumItems(0, &AutocompleteGUID, | |
| 564 &AutocompleteGUID, 0, item.Receive()); | |
| 565 if (result != PST_E_OK) { | |
| 566 pstore.Release(); | |
| 567 FreeLibrary(pstorec_dll); | |
| 568 return; | |
| 569 } | |
| 570 | |
| 571 wchar_t* item_name; | |
| 572 while (!cancelled() && SUCCEEDED(item->Next(1, &item_name, 0))) { | |
| 573 DWORD length = 0; | |
| 574 unsigned char* buffer = NULL; | |
| 575 result = pstore->ReadItem(0, &AutocompleteGUID, &AutocompleteGUID, | |
| 576 item_name, &length, &buffer, NULL, 0); | |
| 577 if (SUCCEEDED(result)) { | |
| 578 AutoCompleteInfo ac; | |
| 579 ac.key = item_name; | |
| 580 string16 data; | |
| 581 data.insert(0, reinterpret_cast<wchar_t*>(buffer), | |
| 582 length / sizeof(wchar_t)); | |
| 583 | |
| 584 // The key name is always ended with ":StringData". | |
| 585 const wchar_t kDataSuffix[] = L":StringData"; | |
| 586 size_t i = ac.key.rfind(kDataSuffix); | |
| 587 if (i != string16::npos && ac.key.substr(i) == kDataSuffix) { | |
| 588 ac.key.erase(i); | |
| 589 ac.is_url = (ac.key.find(L"://") != string16::npos); | |
| 590 ac_list.push_back(ac); | |
| 591 base::SplitString(data, L'\0', &ac_list[ac_list.size() - 1].data); | |
| 592 } | |
| 593 CoTaskMemFree(buffer); | |
| 594 } | |
| 595 CoTaskMemFree(item_name); | |
| 596 } | |
| 597 // Releases them before unload the dll. | |
| 598 item.Release(); | |
| 599 pstore.Release(); | |
| 600 FreeLibrary(pstorec_dll); | |
| 601 | |
| 602 size_t i; | |
| 603 for (i = 0; i < ac_list.size(); i++) { | |
| 604 if (!ac_list[i].is_url || ac_list[i].data.size() < 2) | |
| 605 continue; | |
| 606 | |
| 607 GURL url(ac_list[i].key.c_str()); | |
| 608 if (!(LowerCaseEqualsASCII(url.scheme(), chrome::kHttpScheme) || | |
| 609 LowerCaseEqualsASCII(url.scheme(), chrome::kHttpsScheme))) { | |
| 610 continue; | |
| 611 } | |
| 612 | |
| 613 content::PasswordForm form; | |
| 614 GURL::Replacements rp; | |
| 615 rp.ClearUsername(); | |
| 616 rp.ClearPassword(); | |
| 617 rp.ClearQuery(); | |
| 618 rp.ClearRef(); | |
| 619 form.origin = url.ReplaceComponents(rp); | |
| 620 form.username_value = ac_list[i].data[0]; | |
| 621 form.password_value = ac_list[i].data[1]; | |
| 622 form.signon_realm = url.GetOrigin().spec(); | |
| 623 | |
| 624 // This is not precise, because a scheme of https does not imply a valid | |
| 625 // certificate was presented; however we assign it this way so that if we | |
| 626 // import a password from IE whose scheme is https, we give it the benefit | |
| 627 // of the doubt and DONT auto-fill it unless the form appears under | |
| 628 // valid SSL conditions. | |
| 629 form.ssl_valid = url.SchemeIsSecure(); | |
| 630 | |
| 631 // Goes through the list to find out the username field | |
| 632 // of the web page. | |
| 633 size_t list_it, item_it; | |
| 634 for (list_it = 0; list_it < ac_list.size(); ++list_it) { | |
| 635 if (ac_list[list_it].is_url) | |
| 636 continue; | |
| 637 | |
| 638 for (item_it = 0; item_it < ac_list[list_it].data.size(); ++item_it) | |
| 639 if (ac_list[list_it].data[item_it] == form.username_value) { | |
| 640 form.username_element = ac_list[list_it].key; | |
| 641 break; | |
| 642 } | |
| 643 } | |
| 644 | |
| 645 bridge_->SetPasswordForm(form); | |
| 646 } | |
| 647 } | |
| 648 | |
| 649 void IEImporter::ImportPasswordsIE7() { | |
| 650 base::string16 key_path(importer::GetIE7PasswordsKey()); | |
| 651 base::win::RegKey key(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ); | |
| 652 base::win::RegistryValueIterator reg_iterator(HKEY_CURRENT_USER, | |
| 653 key_path.c_str()); | |
| 654 importer::ImporterIE7PasswordInfo password_info; | |
| 655 while (reg_iterator.Valid() && !cancelled()) { | |
| 656 // Get the size of the encrypted data. | |
| 657 DWORD value_len = 0; | |
| 658 key.ReadValue(reg_iterator.Name(), NULL, &value_len, NULL); | |
| 659 if (value_len) { | |
| 660 // Query the encrypted data. | |
| 661 password_info.encrypted_data.resize(value_len); | |
| 662 if (key.ReadValue(reg_iterator.Name(), | |
| 663 &password_info.encrypted_data.front(), | |
| 664 &value_len, NULL) == ERROR_SUCCESS) { | |
| 665 password_info.url_hash = reg_iterator.Name(); | |
| 666 password_info.date_created = base::Time::Now(); | |
| 667 | |
| 668 bridge_->AddIE7PasswordInfo(password_info); | |
| 669 } | |
| 670 } | |
| 671 | |
| 672 ++reg_iterator; | |
| 673 } | |
| 674 } | |
| 675 | |
| 676 void IEImporter::ImportSearchEngines() { | |
| 677 // On IE, search engines are stored in the registry, under: | |
| 678 // Software\Microsoft\Internet Explorer\SearchScopes | |
| 679 // Each key represents a search engine. The URL value contains the URL and | |
| 680 // the DisplayName the name. | |
| 681 typedef std::map<std::string, string16> SearchEnginesMap; | |
| 682 SearchEnginesMap search_engines_map; | |
| 683 for (base::win::RegistryKeyIterator key_iter(HKEY_CURRENT_USER, | |
| 684 kSearchScopePath); key_iter.Valid(); ++key_iter) { | |
| 685 string16 sub_key_name = kSearchScopePath; | |
| 686 sub_key_name.append(L"\\").append(key_iter.Name()); | |
| 687 base::win::RegKey sub_key(HKEY_CURRENT_USER, sub_key_name.c_str(), | |
| 688 KEY_READ); | |
| 689 string16 wide_url; | |
| 690 if ((sub_key.ReadValue(L"URL", &wide_url) != ERROR_SUCCESS) || | |
| 691 wide_url.empty()) { | |
| 692 VLOG(1) << "No URL for IE search engine at " << key_iter.Name(); | |
| 693 continue; | |
| 694 } | |
| 695 // For the name, we try the default value first (as Live Search uses a | |
| 696 // non displayable name in DisplayName, and the readable name under the | |
| 697 // default value). | |
| 698 string16 name; | |
| 699 if ((sub_key.ReadValue(NULL, &name) != ERROR_SUCCESS) || name.empty()) { | |
| 700 // Try the displayable name. | |
| 701 if ((sub_key.ReadValue(L"DisplayName", &name) != ERROR_SUCCESS) || | |
| 702 name.empty()) { | |
| 703 VLOG(1) << "No name for IE search engine at " << key_iter.Name(); | |
| 704 continue; | |
| 705 } | |
| 706 } | |
| 707 | |
| 708 std::string url(WideToUTF8(wide_url)); | |
| 709 SearchEnginesMap::iterator t_iter = search_engines_map.find(url); | |
| 710 if (t_iter == search_engines_map.end()) { | |
| 711 // First time we see that URL. | |
| 712 GURL gurl(url); | |
| 713 if (gurl.is_valid()) { | |
| 714 t_iter = search_engines_map.insert(std::make_pair(url, name)).first; | |
| 715 } | |
| 716 } | |
| 717 } | |
| 718 // ProfileWriter::AddKeywords() requires a vector and we have a map. | |
| 719 std::vector<importer::URLKeywordInfo> url_keywords; | |
| 720 for (SearchEnginesMap::iterator i = search_engines_map.begin(); | |
| 721 i != search_engines_map.end(); ++i) { | |
| 722 importer::URLKeywordInfo url_keyword_info; | |
| 723 url_keyword_info.url = GURL(i->first); | |
| 724 url_keyword_info.display_name = i->second; | |
| 725 url_keywords.push_back(url_keyword_info); | |
| 726 } | |
| 727 bridge_->SetKeywords(url_keywords, true); | |
| 728 } | |
| 729 | |
| 730 void IEImporter::ImportHomepage() { | |
| 731 const wchar_t* kIEHomepage = L"Start Page"; | |
| 732 const wchar_t* kIEDefaultHomepage = L"Default_Page_URL"; | |
| 733 | |
| 734 base::string16 key_path(importer::GetIESettingsKey()); | |
| 735 | |
| 736 base::win::RegKey key(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ); | |
| 737 string16 homepage_url; | |
| 738 if (key.ReadValue(kIEHomepage, &homepage_url) != ERROR_SUCCESS || | |
| 739 homepage_url.empty()) | |
| 740 return; | |
| 741 | |
| 742 GURL homepage = GURL(homepage_url); | |
| 743 if (!homepage.is_valid()) | |
| 744 return; | |
| 745 | |
| 746 // Check to see if this is the default website and skip import. | |
| 747 base::win::RegKey keyDefault(HKEY_LOCAL_MACHINE, key_path.c_str(), KEY_READ); | |
| 748 string16 default_homepage_url; | |
| 749 LONG result = keyDefault.ReadValue(kIEDefaultHomepage, &default_homepage_url); | |
| 750 if (result == ERROR_SUCCESS && !default_homepage_url.empty()) { | |
| 751 if (homepage.spec() == GURL(default_homepage_url).spec()) | |
| 752 return; | |
| 753 } | |
| 754 bridge_->AddHomePage(homepage); | |
| 755 } | |
| 756 | |
| 757 bool IEImporter::GetFavoritesInfo(IEImporter::FavoritesInfo* info) { | |
| 758 if (!source_path_.empty()) { | |
| 759 // Source path exists during testing. | |
| 760 info->path = source_path_; | |
| 761 info->path = info->path.AppendASCII("Favorites"); | |
| 762 info->links_folder = L"Links"; | |
| 763 return true; | |
| 764 } | |
| 765 | |
| 766 // IE stores the favorites in the Favorites under user profile's folder. | |
| 767 wchar_t buffer[MAX_PATH]; | |
| 768 if (FAILED(SHGetFolderPath(NULL, CSIDL_FAVORITES, NULL, | |
| 769 SHGFP_TYPE_CURRENT, buffer))) | |
| 770 return false; | |
| 771 info->path = base::FilePath(buffer); | |
| 772 | |
| 773 // There is a Links folder under Favorites folder in Windows Vista, but it | |
| 774 // is not recording in Vista's registry. So in Vista, we assume the Links | |
| 775 // folder is under Favorites folder since it looks like there is not name | |
| 776 // different in every language version of Windows Vista. | |
| 777 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | |
| 778 // The Link folder name is stored in the registry. | |
| 779 DWORD buffer_length = sizeof(buffer); | |
| 780 base::win::RegKey reg_key(HKEY_CURRENT_USER, kIEToolbarKey, KEY_READ); | |
| 781 if (reg_key.ReadValue(L"LinksFolderName", buffer, | |
| 782 &buffer_length, NULL) != ERROR_SUCCESS) | |
| 783 return false; | |
| 784 info->links_folder = buffer; | |
| 785 } else { | |
| 786 info->links_folder = L"Links"; | |
| 787 } | |
| 788 | |
| 789 return true; | |
| 790 } | |
| 791 | |
| 792 void IEImporter::ParseFavoritesFolder( | |
| 793 const FavoritesInfo& info, | |
| 794 BookmarkVector* bookmarks, | |
| 795 std::vector<ImportedFaviconUsage>* favicons) { | |
| 796 base::FilePath file; | |
| 797 std::vector<base::FilePath::StringType> file_list; | |
| 798 base::FilePath favorites_path(info.path); | |
| 799 // Favorites path length. Make sure it doesn't include the trailing \. | |
| 800 size_t favorites_path_len = | |
| 801 favorites_path.StripTrailingSeparators().value().size(); | |
| 802 base::FileEnumerator file_enumerator( | |
| 803 favorites_path, true, base::FileEnumerator::FILES); | |
| 804 while (!(file = file_enumerator.Next()).value().empty() && !cancelled()) | |
| 805 file_list.push_back(file.value()); | |
| 806 | |
| 807 // Keep the bookmarks in alphabetical order. | |
| 808 std::sort(file_list.begin(), file_list.end()); | |
| 809 | |
| 810 // Map from favicon URLs to the favicon data (the binary image data and the | |
| 811 // set of bookmark URLs referring to the favicon). | |
| 812 typedef std::map<GURL, ImportedFaviconUsage> FaviconMap; | |
| 813 FaviconMap favicon_map; | |
| 814 | |
| 815 for (std::vector<base::FilePath::StringType>::iterator it = file_list.begin(); | |
| 816 it != file_list.end(); ++it) { | |
| 817 base::FilePath shortcut(*it); | |
| 818 if (!LowerCaseEqualsASCII(shortcut.Extension(), ".url")) | |
| 819 continue; | |
| 820 | |
| 821 // Skip the bookmark with invalid URL. | |
| 822 base::win::ScopedComPtr<IUniformResourceLocator> url_locator; | |
| 823 if (!LoadInternetShortcut(*it, &url_locator)) | |
| 824 continue; | |
| 825 GURL url = ReadURLFromInternetShortcut(url_locator); | |
| 826 if (!url.is_valid()) | |
| 827 continue; | |
| 828 // Skip default bookmarks. go.microsoft.com redirects to | |
| 829 // search.microsoft.com, and http://go.microsoft.com/fwlink/?LinkId=XXX, | |
| 830 // which URLs IE has as default, to some another sites. | |
| 831 // We expect that users will never themselves create bookmarks having this | |
| 832 // hostname. | |
| 833 if (url.host() == "go.microsoft.com") | |
| 834 continue; | |
| 835 // Read favicon. | |
| 836 UpdateFaviconMap(*it, url, url_locator, &favicon_map); | |
| 837 | |
| 838 // Make the relative path from the Favorites folder, without the basename. | |
| 839 // ex. Suppose that the Favorites folder is C:\Users\Foo\Favorites. | |
| 840 // C:\Users\Foo\Favorites\Foo.url -> "" | |
| 841 // C:\Users\Foo\Favorites\Links\Bar\Baz.url -> "Links\Bar" | |
| 842 base::FilePath::StringType relative_string = | |
| 843 shortcut.DirName().value().substr(favorites_path_len); | |
| 844 if (!relative_string.empty() && | |
| 845 base::FilePath::IsSeparator(relative_string[0])) | |
| 846 relative_string = relative_string.substr(1); | |
| 847 base::FilePath relative_path(relative_string); | |
| 848 | |
| 849 ImportedBookmarkEntry entry; | |
| 850 // Remove the dot, the file extension, and the directory path. | |
| 851 entry.title = shortcut.RemoveExtension().BaseName().value(); | |
| 852 entry.url = url; | |
| 853 entry.creation_time = GetFileCreationTime(*it); | |
| 854 if (!relative_path.empty()) | |
| 855 relative_path.GetComponents(&entry.path); | |
| 856 | |
| 857 // Add the bookmark. | |
| 858 if (!entry.path.empty() && entry.path[0] == info.links_folder) { | |
| 859 // Bookmarks in the Link folder should be imported to the toolbar. | |
| 860 entry.in_toolbar = true; | |
| 861 } | |
| 862 bookmarks->push_back(entry); | |
| 863 } | |
| 864 | |
| 865 // Reflect the menu order in IE. | |
| 866 SortBookmarksInIEOrder(this, bookmarks); | |
| 867 | |
| 868 // Record favicon data. | |
| 869 for (FaviconMap::iterator iter = favicon_map.begin(); | |
| 870 iter != favicon_map.end(); ++iter) | |
| 871 favicons->push_back(iter->second); | |
| 872 } | |
| 873 | |
| 874 int IEImporter::CurrentIEVersion() const { | |
| 875 static int version = -1; | |
| 876 if (version < 0) { | |
| 877 wchar_t buffer[128]; | |
| 878 DWORD buffer_length = sizeof(buffer); | |
| 879 base::win::RegKey reg_key(HKEY_LOCAL_MACHINE, kIEVersionKey, KEY_READ); | |
| 880 LONG result = reg_key.ReadValue(L"Version", buffer, &buffer_length, NULL); | |
| 881 version = ((result == ERROR_SUCCESS)? _wtoi(buffer) : 0); | |
| 882 } | |
| 883 return version; | |
| 884 } | |
| OLD | NEW |