| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/importer/ie_importer.h" | 5 #include "chrome/browser/importer/ie_importer.h" |
| 6 | 6 |
| 7 #include <ole2.h> | 7 #include <ole2.h> |
| 8 #include <intshcut.h> | 8 #include <intshcut.h> |
| 9 #include <shlobj.h> | 9 #include <shlobj.h> |
| 10 #include <urlhist.h> | 10 #include <urlhist.h> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 22 #include "base/strings/string_split.h" | 22 #include "base/strings/string_split.h" |
| 23 #include "base/time.h" | 23 #include "base/time.h" |
| 24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
| 25 #include "base/win/registry.h" | 25 #include "base/win/registry.h" |
| 26 #include "base/win/scoped_co_mem.h" | 26 #include "base/win/scoped_co_mem.h" |
| 27 #include "base/win/scoped_comptr.h" | 27 #include "base/win/scoped_comptr.h" |
| 28 #include "base/win/scoped_handle.h" | 28 #include "base/win/scoped_handle.h" |
| 29 #include "base/win/scoped_propvariant.h" | 29 #include "base/win/scoped_propvariant.h" |
| 30 #include "base/win/windows_version.h" | 30 #include "base/win/windows_version.h" |
| 31 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" |
| 32 #include "chrome/browser/favicon/favicon_util.h" |
| 33 #include "chrome/browser/favicon/imported_favicon_usage.h" |
| 31 #include "chrome/browser/importer/importer_bridge.h" | 34 #include "chrome/browser/importer/importer_bridge.h" |
| 32 #include "chrome/browser/importer/importer_data_types.h" | 35 #include "chrome/browser/importer/importer_data_types.h" |
| 33 #include "chrome/browser/importer/importer_util.h" | |
| 34 #include "chrome/browser/importer/pstore_declarations.h" | 36 #include "chrome/browser/importer/pstore_declarations.h" |
| 35 #include "chrome/browser/search_engines/template_url.h" | 37 #include "chrome/browser/search_engines/template_url.h" |
| 36 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 38 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 37 #include "chrome/browser/search_engines/template_url_service.h" | 39 #include "chrome/browser/search_engines/template_url_service.h" |
| 38 #include "chrome/common/time_format.h" | 40 #include "chrome/common/time_format.h" |
| 39 #include "chrome/common/url_constants.h" | 41 #include "chrome/common/url_constants.h" |
| 40 #include "components/webdata/encryptor/ie7_password.h" | 42 #include "components/webdata/encryptor/ie7_password.h" |
| 41 #include "content/public/common/password_form.h" | 43 #include "content/public/common/password_form.h" |
| 42 #include "googleurl/src/gurl.h" | 44 #include "googleurl/src/gurl.h" |
| 43 #include "grit/generated_resources.h" | 45 #include "grit/generated_resources.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 if (id_cb == 0) | 112 if (id_cb == 0) |
| 111 break; | 113 break; |
| 112 head += id_cb; | 114 head += id_cb; |
| 113 } | 115 } |
| 114 return reinterpret_cast<LPCITEMIDLIST>(&blob[offset]); | 116 return reinterpret_cast<LPCITEMIDLIST>(&blob[offset]); |
| 115 } | 117 } |
| 116 | 118 |
| 117 // Compares the two bookmarks in the order of IE's Favorites menu. | 119 // Compares the two bookmarks in the order of IE's Favorites menu. |
| 118 // Returns true if rhs should come later than lhs (lhs < rhs). | 120 // Returns true if rhs should come later than lhs (lhs < rhs). |
| 119 struct IEOrderBookmarkComparator { | 121 struct IEOrderBookmarkComparator { |
| 120 bool operator()(const ProfileWriter::BookmarkEntry& lhs, | 122 bool operator()(const ImportedBookmarkEntry& lhs, |
| 121 const ProfileWriter::BookmarkEntry& rhs) const { | 123 const ImportedBookmarkEntry& rhs) const { |
| 122 static const uint32 kNotSorted = 0xfffffffb; // IE uses this magic value. | 124 static const uint32 kNotSorted = 0xfffffffb; // IE uses this magic value. |
| 123 base::FilePath lhs_prefix; | 125 base::FilePath lhs_prefix; |
| 124 base::FilePath rhs_prefix; | 126 base::FilePath rhs_prefix; |
| 125 for (size_t i = 0; i <= lhs.path.size() && i <= rhs.path.size(); ++i) { | 127 for (size_t i = 0; i <= lhs.path.size() && i <= rhs.path.size(); ++i) { |
| 126 const base::FilePath::StringType lhs_i = | 128 const base::FilePath::StringType lhs_i = |
| 127 (i < lhs.path.size() ? lhs.path[i] : lhs.title + L".url"); | 129 (i < lhs.path.size() ? lhs.path[i] : lhs.title + L".url"); |
| 128 const base::FilePath::StringType rhs_i = | 130 const base::FilePath::StringType rhs_i = |
| 129 (i < rhs.path.size() ? rhs.path[i] : rhs.title + L".url"); | 131 (i < rhs.path.size() ? rhs.path[i] : rhs.title + L".url"); |
| 130 lhs_prefix = lhs_prefix.Append(lhs_i); | 132 lhs_prefix = lhs_prefix.Append(lhs_i); |
| 131 rhs_prefix = rhs_prefix.Append(rhs_i); | 133 rhs_prefix = rhs_prefix.Append(rhs_i); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 if (!key.Valid()) | 256 if (!key.Valid()) |
| 255 return false; | 257 return false; |
| 256 return ParseFavoritesOrderRegistryTree(importer, key, base::FilePath(), | 258 return ParseFavoritesOrderRegistryTree(importer, key, base::FilePath(), |
| 257 sort_index); | 259 sort_index); |
| 258 } | 260 } |
| 259 | 261 |
| 260 // Reads the sort order from registry. If failed, we don't touch the list | 262 // Reads the sort order from registry. If failed, we don't touch the list |
| 261 // and use the default (alphabetical) order. | 263 // and use the default (alphabetical) order. |
| 262 void SortBookmarksInIEOrder( | 264 void SortBookmarksInIEOrder( |
| 263 const Importer* importer, | 265 const Importer* importer, |
| 264 std::vector<ProfileWriter::BookmarkEntry>* bookmarks) { | 266 std::vector<ImportedBookmarkEntry>* bookmarks) { |
| 265 std::map<base::FilePath, uint32> sort_index; | 267 std::map<base::FilePath, uint32> sort_index; |
| 266 if (!ParseFavoritesOrderInfo(importer, &sort_index)) | 268 if (!ParseFavoritesOrderInfo(importer, &sort_index)) |
| 267 return; | 269 return; |
| 268 IEOrderBookmarkComparator compare = {&sort_index}; | 270 IEOrderBookmarkComparator compare = {&sort_index}; |
| 269 std::sort(bookmarks->begin(), bookmarks->end(), compare); | 271 std::sort(bookmarks->begin(), bookmarks->end(), compare); |
| 270 } | 272 } |
| 271 | 273 |
| 272 // Reads an internet shortcut (*.url) |file| and returns a COM object | 274 // Reads an internet shortcut (*.url) |file| and returns a COM object |
| 273 // representing it. | 275 // representing it. |
| 274 bool LoadInternetShortcut( | 276 bool LoadInternetShortcut( |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 const GURL& favicon_url, | 362 const GURL& favicon_url, |
| 361 std::vector<unsigned char>* data) { | 363 std::vector<unsigned char>* data) { |
| 362 std::string image_data; | 364 std::string image_data; |
| 363 if (!ReadFaviconDataFromInternetShortcut(file, &image_data) && | 365 if (!ReadFaviconDataFromInternetShortcut(file, &image_data) && |
| 364 !ReadFaviconDataFromCache(favicon_url, &image_data)) { | 366 !ReadFaviconDataFromCache(favicon_url, &image_data)) { |
| 365 return false; | 367 return false; |
| 366 } | 368 } |
| 367 | 369 |
| 368 const unsigned char* ptr = | 370 const unsigned char* ptr = |
| 369 reinterpret_cast<const unsigned char*>(image_data.c_str()); | 371 reinterpret_cast<const unsigned char*>(image_data.c_str()); |
| 370 return importer::ReencodeFavicon(ptr, image_data.size(), data); | 372 return FaviconUtil::ReencodeFavicon(ptr, image_data.size(), data); |
| 371 } | 373 } |
| 372 | 374 |
| 373 // Loads favicon image data and registers to |favicon_map|. | 375 // Loads favicon image data and registers to |favicon_map|. |
| 374 void UpdateFaviconMap( | 376 void UpdateFaviconMap( |
| 375 const string16& url_file, | 377 const string16& url_file, |
| 376 const GURL& url, | 378 const GURL& url, |
| 377 IUniformResourceLocator* url_locator, | 379 IUniformResourceLocator* url_locator, |
| 378 std::map<GURL, history::ImportedFaviconUsage>* favicon_map) { | 380 std::map<GURL, ImportedFaviconUsage>* favicon_map) { |
| 379 GURL favicon_url = ReadFaviconURLFromInternetShortcut(url_locator); | 381 GURL favicon_url = ReadFaviconURLFromInternetShortcut(url_locator); |
| 380 if (!favicon_url.is_valid()) | 382 if (!favicon_url.is_valid()) |
| 381 return; | 383 return; |
| 382 | 384 |
| 383 std::map<GURL, history::ImportedFaviconUsage>::iterator it = | 385 std::map<GURL, ImportedFaviconUsage>::iterator it = |
| 384 favicon_map->find(favicon_url); | 386 favicon_map->find(favicon_url); |
| 385 if (it != favicon_map->end()) { | 387 if (it != favicon_map->end()) { |
| 386 // Known favicon URL. | 388 // Known favicon URL. |
| 387 it->second.urls.insert(url); | 389 it->second.urls.insert(url); |
| 388 } else { | 390 } else { |
| 389 // New favicon URL. Read the image data and store. | 391 // New favicon URL. Read the image data and store. |
| 390 history::ImportedFaviconUsage usage; | 392 ImportedFaviconUsage usage; |
| 391 if (ReadReencodedFaviconData(url_file, favicon_url, &usage.png_data)) { | 393 if (ReadReencodedFaviconData(url_file, favicon_url, &usage.png_data)) { |
| 392 usage.favicon_url = favicon_url; | 394 usage.favicon_url = favicon_url; |
| 393 usage.urls.insert(url); | 395 usage.urls.insert(url); |
| 394 favicon_map->insert(std::make_pair(favicon_url, usage)); | 396 favicon_map->insert(std::make_pair(favicon_url, usage)); |
| 395 } | 397 } |
| 396 } | 398 } |
| 397 } | 399 } |
| 398 | 400 |
| 399 } // namespace | 401 } // namespace |
| 400 | 402 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 | 463 |
| 462 IEImporter::~IEImporter() { | 464 IEImporter::~IEImporter() { |
| 463 } | 465 } |
| 464 | 466 |
| 465 void IEImporter::ImportFavorites() { | 467 void IEImporter::ImportFavorites() { |
| 466 FavoritesInfo info; | 468 FavoritesInfo info; |
| 467 if (!GetFavoritesInfo(&info)) | 469 if (!GetFavoritesInfo(&info)) |
| 468 return; | 470 return; |
| 469 | 471 |
| 470 BookmarkVector bookmarks; | 472 BookmarkVector bookmarks; |
| 471 std::vector<history::ImportedFaviconUsage> favicons; | 473 std::vector<ImportedFaviconUsage> favicons; |
| 472 ParseFavoritesFolder(info, &bookmarks, &favicons); | 474 ParseFavoritesFolder(info, &bookmarks, &favicons); |
| 473 | 475 |
| 474 if (!bookmarks.empty() && !cancelled()) { | 476 if (!bookmarks.empty() && !cancelled()) { |
| 475 const string16& first_folder_name = | 477 const string16& first_folder_name = |
| 476 l10n_util::GetStringUTF16(IDS_BOOKMARK_GROUP_FROM_IE); | 478 l10n_util::GetStringUTF16(IDS_BOOKMARK_GROUP_FROM_IE); |
| 477 bridge_->AddBookmarks(bookmarks, first_folder_name); | 479 bridge_->AddBookmarks(bookmarks, first_folder_name); |
| 478 } | 480 } |
| 479 if (!favicons.empty() && !cancelled()) | 481 if (!favicons.empty() && !cancelled()) |
| 480 bridge_->SetFavicons(favicons); | 482 bridge_->SetFavicons(favicons); |
| 481 } | 483 } |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 } else { | 804 } else { |
| 803 info->links_folder = L"Links"; | 805 info->links_folder = L"Links"; |
| 804 } | 806 } |
| 805 | 807 |
| 806 return true; | 808 return true; |
| 807 } | 809 } |
| 808 | 810 |
| 809 void IEImporter::ParseFavoritesFolder( | 811 void IEImporter::ParseFavoritesFolder( |
| 810 const FavoritesInfo& info, | 812 const FavoritesInfo& info, |
| 811 BookmarkVector* bookmarks, | 813 BookmarkVector* bookmarks, |
| 812 std::vector<history::ImportedFaviconUsage>* favicons) { | 814 std::vector<ImportedFaviconUsage>* favicons) { |
| 813 base::FilePath file; | 815 base::FilePath file; |
| 814 std::vector<base::FilePath::StringType> file_list; | 816 std::vector<base::FilePath::StringType> file_list; |
| 815 base::FilePath favorites_path(info.path); | 817 base::FilePath favorites_path(info.path); |
| 816 // Favorites path length. Make sure it doesn't include the trailing \. | 818 // Favorites path length. Make sure it doesn't include the trailing \. |
| 817 size_t favorites_path_len = | 819 size_t favorites_path_len = |
| 818 favorites_path.StripTrailingSeparators().value().size(); | 820 favorites_path.StripTrailingSeparators().value().size(); |
| 819 file_util::FileEnumerator file_enumerator( | 821 file_util::FileEnumerator file_enumerator( |
| 820 favorites_path, true, file_util::FileEnumerator::FILES); | 822 favorites_path, true, file_util::FileEnumerator::FILES); |
| 821 while (!(file = file_enumerator.Next()).value().empty() && !cancelled()) | 823 while (!(file = file_enumerator.Next()).value().empty() && !cancelled()) |
| 822 file_list.push_back(file.value()); | 824 file_list.push_back(file.value()); |
| 823 | 825 |
| 824 // Keep the bookmarks in alphabetical order. | 826 // Keep the bookmarks in alphabetical order. |
| 825 std::sort(file_list.begin(), file_list.end()); | 827 std::sort(file_list.begin(), file_list.end()); |
| 826 | 828 |
| 827 // Map from favicon URLs to the favicon data (the binary image data and the | 829 // Map from favicon URLs to the favicon data (the binary image data and the |
| 828 // set of bookmark URLs referring to the favicon). | 830 // set of bookmark URLs referring to the favicon). |
| 829 typedef std::map<GURL, history::ImportedFaviconUsage> FaviconMap; | 831 typedef std::map<GURL, ImportedFaviconUsage> FaviconMap; |
| 830 FaviconMap favicon_map; | 832 FaviconMap favicon_map; |
| 831 | 833 |
| 832 for (std::vector<base::FilePath::StringType>::iterator it = file_list.begin(); | 834 for (std::vector<base::FilePath::StringType>::iterator it = file_list.begin(); |
| 833 it != file_list.end(); ++it) { | 835 it != file_list.end(); ++it) { |
| 834 base::FilePath shortcut(*it); | 836 base::FilePath shortcut(*it); |
| 835 if (!LowerCaseEqualsASCII(shortcut.Extension(), ".url")) | 837 if (!LowerCaseEqualsASCII(shortcut.Extension(), ".url")) |
| 836 continue; | 838 continue; |
| 837 | 839 |
| 838 // Skip the bookmark with invalid URL. | 840 // Skip the bookmark with invalid URL. |
| 839 base::win::ScopedComPtr<IUniformResourceLocator> url_locator; | 841 base::win::ScopedComPtr<IUniformResourceLocator> url_locator; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 856 // ex. Suppose that the Favorites folder is C:\Users\Foo\Favorites. | 858 // ex. Suppose that the Favorites folder is C:\Users\Foo\Favorites. |
| 857 // C:\Users\Foo\Favorites\Foo.url -> "" | 859 // C:\Users\Foo\Favorites\Foo.url -> "" |
| 858 // C:\Users\Foo\Favorites\Links\Bar\Baz.url -> "Links\Bar" | 860 // C:\Users\Foo\Favorites\Links\Bar\Baz.url -> "Links\Bar" |
| 859 base::FilePath::StringType relative_string = | 861 base::FilePath::StringType relative_string = |
| 860 shortcut.DirName().value().substr(favorites_path_len); | 862 shortcut.DirName().value().substr(favorites_path_len); |
| 861 if (!relative_string.empty() && | 863 if (!relative_string.empty() && |
| 862 base::FilePath::IsSeparator(relative_string[0])) | 864 base::FilePath::IsSeparator(relative_string[0])) |
| 863 relative_string = relative_string.substr(1); | 865 relative_string = relative_string.substr(1); |
| 864 base::FilePath relative_path(relative_string); | 866 base::FilePath relative_path(relative_string); |
| 865 | 867 |
| 866 ProfileWriter::BookmarkEntry entry; | 868 ImportedBookmarkEntry entry; |
| 867 // Remove the dot, the file extension, and the directory path. | 869 // Remove the dot, the file extension, and the directory path. |
| 868 entry.title = shortcut.RemoveExtension().BaseName().value(); | 870 entry.title = shortcut.RemoveExtension().BaseName().value(); |
| 869 entry.url = url; | 871 entry.url = url; |
| 870 entry.creation_time = GetFileCreationTime(*it); | 872 entry.creation_time = GetFileCreationTime(*it); |
| 871 if (!relative_path.empty()) | 873 if (!relative_path.empty()) |
| 872 relative_path.GetComponents(&entry.path); | 874 relative_path.GetComponents(&entry.path); |
| 873 | 875 |
| 874 // Add the bookmark. | 876 // Add the bookmark. |
| 875 if (!entry.path.empty() && entry.path[0] == info.links_folder) { | 877 if (!entry.path.empty() && entry.path[0] == info.links_folder) { |
| 876 // Bookmarks in the Link folder should be imported to the toolbar. | 878 // Bookmarks in the Link folder should be imported to the toolbar. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 892 static int version = -1; | 894 static int version = -1; |
| 893 if (version < 0) { | 895 if (version < 0) { |
| 894 wchar_t buffer[128]; | 896 wchar_t buffer[128]; |
| 895 DWORD buffer_length = sizeof(buffer); | 897 DWORD buffer_length = sizeof(buffer); |
| 896 base::win::RegKey reg_key(HKEY_LOCAL_MACHINE, kIEVersionKey, KEY_READ); | 898 base::win::RegKey reg_key(HKEY_LOCAL_MACHINE, kIEVersionKey, KEY_READ); |
| 897 LONG result = reg_key.ReadValue(L"Version", buffer, &buffer_length, NULL); | 899 LONG result = reg_key.ReadValue(L"Version", buffer, &buffer_length, NULL); |
| 898 version = ((result == ERROR_SUCCESS)? _wtoi(buffer) : 0); | 900 version = ((result == ERROR_SUCCESS)? _wtoi(buffer) : 0); |
| 899 } | 901 } |
| 900 return version; | 902 return version; |
| 901 } | 903 } |
| OLD | NEW |