[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 6c9851a | 2008-09-13 00:56:27 | [diff] [blame] | 5 | #include "chrome/browser/importer/firefox3_importer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <set> |
| 8 | |
| 9 | #include "base/file_util.h" |
[email protected] | e74b81d | 2009-07-20 22:18:42 | [diff] [blame] | 10 | #include "base/message_loop.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/scoped_ptr.h" |
[email protected] | e74b81d | 2009-07-20 22:18:42 | [diff] [blame] | 12 | #include "base/stl_util-inl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/string_util.h" |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 813ed750 | 2010-09-01 22:58:24 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | 9c92d19 | 2009-12-02 08:03:16 | [diff] [blame] | 16 | #include "chrome/browser/history/history_types.h" |
[email protected] | 6c9851a | 2008-09-13 00:56:27 | [diff] [blame] | 17 | #include "chrome/browser/importer/firefox2_importer.h" |
| 18 | #include "chrome/browser/importer/firefox_importer_utils.h" |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 19 | #include "chrome/browser/importer/importer_bridge.h" |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 20 | #include "chrome/browser/importer/importer_data_types.h" |
[email protected] | a1f55c87f | 2009-07-22 00:53:48 | [diff] [blame] | 21 | #include "chrome/browser/importer/nss_decryptor.h" |
[email protected] | e74b81d | 2009-07-20 22:18:42 | [diff] [blame] | 22 | #include "chrome/browser/search_engines/template_url.h" |
[email protected] | e74b81d | 2009-07-20 22:18:42 | [diff] [blame] | 23 | #include "chrome/common/sqlite_utils.h" |
[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 24 | #include "chrome/common/time_format.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 25 | #include "grit/generated_resources.h" |
[email protected] | e74b81d | 2009-07-20 22:18:42 | [diff] [blame] | 26 | #include "webkit/glue/password_form.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 28 | // Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/ |
| 29 | // components/places/public/nsINavBookmarksService.idl |
| 30 | enum BookmarkItemType { |
| 31 | TYPE_BOOKMARK = 1, |
| 32 | TYPE_FOLDER = 2, |
| 33 | TYPE_SEPARATOR = 3, |
| 34 | TYPE_DYNAMIC_CONTAINER = 4 |
| 35 | }; |
| 36 | |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 37 | struct Firefox3Importer::BookmarkItem { |
| 38 | int parent; |
| 39 | int id; |
| 40 | GURL url; |
| 41 | std::wstring title; |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 42 | BookmarkItemType type; |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 43 | std::string keyword; |
| 44 | base::Time date_added; |
| 45 | int64 favicon; |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 46 | bool empty_folder; |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 47 | }; |
| 48 | |
[email protected] | 813ed750 | 2010-09-01 22:58:24 | [diff] [blame] | 49 | Firefox3Importer::Firefox3Importer() { |
[email protected] | 33a6db37 | 2010-09-07 23:58:49 | [diff] [blame] | 50 | #if defined(OS_LINUX) |
[email protected] | 0aa26f4b | 2010-10-10 05:00:51 | [diff] [blame] | 51 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 813ed750 | 2010-09-01 22:58:24 | [diff] [blame] | 52 | locale_ = g_browser_process->GetApplicationLocale(); |
[email protected] | 33a6db37 | 2010-09-07 23:58:49 | [diff] [blame] | 53 | #endif |
[email protected] | 813ed750 | 2010-09-01 22:58:24 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Firefox3Importer::~Firefox3Importer() { |
| 57 | } |
| 58 | |
[email protected] | aad8e8f | 2010-11-09 02:04:17 | [diff] [blame] | 59 | void Firefox3Importer::StartImport(const importer::ProfileInfo& profile_info, |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 60 | uint16 items, |
| 61 | ImporterBridge* bridge) { |
[email protected] | 33a6db37 | 2010-09-07 23:58:49 | [diff] [blame] | 62 | #if defined(OS_LINUX) |
[email protected] | 0aa26f4b | 2010-10-10 05:00:51 | [diff] [blame] | 63 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 33a6db37 | 2010-09-07 23:58:49 | [diff] [blame] | 64 | #endif |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 65 | bridge_ = bridge; |
[email protected] | ca0a0ca | 2010-05-15 14:31:14 | [diff] [blame] | 66 | source_path_ = profile_info.source_path; |
| 67 | app_path_ = profile_info.app_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | // The order here is important! |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 70 | bridge_->NotifyStarted(); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 71 | if ((items & importer::HOME_PAGE) && !cancelled()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | ImportHomepage(); // Doesn't have a UI item. |
[email protected] | 67cb4c8 | 2009-09-11 21:08:53 | [diff] [blame] | 73 | |
| 74 | // Note history should be imported before bookmarks because bookmark import |
| 75 | // will also import favicons and we store favicon for a URL only if the URL |
| 76 | // exist in history or bookmarks. |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 77 | if ((items & importer::HISTORY) && !cancelled()) { |
| 78 | bridge_->NotifyItemStarted(importer::HISTORY); |
[email protected] | 67cb4c8 | 2009-09-11 21:08:53 | [diff] [blame] | 79 | ImportHistory(); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 80 | bridge_->NotifyItemEnded(importer::HISTORY); |
[email protected] | 67cb4c8 | 2009-09-11 21:08:53 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 83 | if ((items & importer::FAVORITES) && !cancelled()) { |
| 84 | bridge_->NotifyItemStarted(importer::FAVORITES); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | ImportBookmarks(); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 86 | bridge_->NotifyItemEnded(importer::FAVORITES); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | } |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 88 | if ((items & importer::SEARCH_ENGINES) && !cancelled()) { |
| 89 | bridge_->NotifyItemStarted(importer::SEARCH_ENGINES); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | ImportSearchEngines(); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 91 | bridge_->NotifyItemEnded(importer::SEARCH_ENGINES); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | } |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 93 | if ((items & importer::PASSWORDS) && !cancelled()) { |
| 94 | bridge_->NotifyItemStarted(importer::PASSWORDS); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | ImportPasswords(); |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 96 | bridge_->NotifyItemEnded(importer::PASSWORDS); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | } |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 98 | bridge_->NotifyEnded(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void Firefox3Importer::ImportHistory() { |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 102 | FilePath file = source_path_.AppendASCII("places.sqlite"); |
| 103 | if (!file_util::PathExists(file)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | return; |
| 105 | |
| 106 | sqlite3* sqlite; |
[email protected] | 91243c3 | 2011-01-28 18:27:17 | [diff] [blame] | 107 | if (sqlite_utils::OpenSqliteDb(file, &sqlite) != SQLITE_OK) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | return; |
[email protected] | 06a793f8 | 2009-08-05 23:50:35 | [diff] [blame] | 109 | sqlite_utils::scoped_sqlite_db_ptr db(sqlite); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | |
| 111 | SQLStatement s; |
| 112 | // |visit_type| represent the transition type of URLs (typed, click, |
| 113 | // redirect, bookmark, etc.) We eliminate some URLs like sub-frames and |
| 114 | // redirects, since we don't want them to appear in history. |
| 115 | // Firefox transition types are defined in: |
| 116 | // toolkit/components/places/public/nsINavHistoryService.idl |
| 117 | const char* stmt = "SELECT h.url, h.title, h.visit_count, " |
| 118 | "h.hidden, h.typed, v.visit_date " |
| 119 | "FROM moz_places h JOIN moz_historyvisits v " |
| 120 | "ON h.id = v.place_id " |
| 121 | "WHERE v.visit_type <= 3"; |
| 122 | |
| 123 | if (s.prepare(db.get(), stmt) != SQLITE_OK) |
| 124 | return; |
| 125 | |
| 126 | std::vector<history::URLRow> rows; |
| 127 | while (s.step() == SQLITE_ROW && !cancelled()) { |
| 128 | GURL url(s.column_string(0)); |
| 129 | |
| 130 | // Filter out unwanted URLs. |
| 131 | if (!CanImportURL(url)) |
| 132 | continue; |
| 133 | |
| 134 | history::URLRow row(url); |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 135 | row.set_title(UTF8ToUTF16(s.column_string(1))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | row.set_visit_count(s.column_int(2)); |
| 137 | row.set_hidden(s.column_int(3) == 1); |
| 138 | row.set_typed_count(s.column_int(4)); |
[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 139 | row.set_last_visit(base::Time::FromTimeT(s.column_int64(5)/1000000)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | |
| 141 | rows.push_back(row); |
| 142 | } |
| 143 | if (!rows.empty() && !cancelled()) { |
[email protected] | 5989f1a | 2010-08-21 02:21:11 | [diff] [blame] | 144 | bridge_->SetHistoryItems(rows, history::SOURCE_FIREFOX_IMPORTED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void Firefox3Importer::ImportBookmarks() { |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 149 | FilePath file = source_path_.AppendASCII("places.sqlite"); |
| 150 | if (!file_util::PathExists(file)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | return; |
| 152 | |
| 153 | sqlite3* sqlite; |
[email protected] | 91243c3 | 2011-01-28 18:27:17 | [diff] [blame] | 154 | if (sqlite_utils::OpenSqliteDb(file, &sqlite) != SQLITE_OK) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | return; |
[email protected] | 06a793f8 | 2009-08-05 23:50:35 | [diff] [blame] | 156 | sqlite_utils::scoped_sqlite_db_ptr db(sqlite); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | |
| 158 | // Get the bookmark folders that we are interested in. |
| 159 | int toolbar_folder_id = -1; |
| 160 | int menu_folder_id = -1; |
| 161 | int unsorted_folder_id = -1; |
| 162 | LoadRootNodeID(db.get(), &toolbar_folder_id, &menu_folder_id, |
| 163 | &unsorted_folder_id); |
| 164 | |
| 165 | // Load livemark IDs. |
| 166 | std::set<int> livemark_id; |
| 167 | LoadLivemarkIDs(db.get(), &livemark_id); |
| 168 | |
| 169 | // Load the default bookmarks. Its storage is the same as Firefox 2. |
| 170 | std::set<GURL> default_urls; |
| 171 | Firefox2Importer::LoadDefaultBookmarks(app_path_, &default_urls); |
| 172 | |
| 173 | BookmarkList list; |
| 174 | GetTopBookmarkFolder(db.get(), toolbar_folder_id, &list); |
| 175 | GetTopBookmarkFolder(db.get(), menu_folder_id, &list); |
| 176 | GetTopBookmarkFolder(db.get(), unsorted_folder_id, &list); |
| 177 | size_t count = list.size(); |
| 178 | for (size_t i = 0; i < count; ++i) |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 179 | GetWholeBookmarkFolder(db.get(), &list, i, NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | |
| 181 | std::vector<ProfileWriter::BookmarkEntry> bookmarks; |
| 182 | std::vector<TemplateURL*> template_urls; |
| 183 | FaviconMap favicon_map; |
| 184 | |
| 185 | // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based |
| 186 | // keywords yet. We won't include them in the list. |
| 187 | std::set<int> post_keyword_ids; |
| 188 | SQLStatement s; |
| 189 | const char* stmt = "SELECT b.id FROM moz_bookmarks b " |
| 190 | "INNER JOIN moz_items_annos ia ON ia.item_id = b.id " |
| 191 | "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id " |
| 192 | "WHERE aa.name = 'bookmarkProperties/POSTData'"; |
| 193 | if (s.prepare(db.get(), stmt) == SQLITE_OK) { |
| 194 | while (s.step() == SQLITE_ROW && !cancelled()) |
| 195 | post_keyword_ids.insert(s.column_int(0)); |
| 196 | } else { |
| 197 | NOTREACHED(); |
[email protected] | b665bf2 | 2010-09-10 22:33:58 | [diff] [blame] | 198 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | std::wstring firefox_folder = |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 202 | bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | for (size_t i = 0; i < list.size(); ++i) { |
| 204 | BookmarkItem* item = list[i]; |
| 205 | |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 206 | if (item->type == TYPE_FOLDER) { |
| 207 | // Folders are added implicitly on adding children, |
| 208 | // so now we pass only empty folders to add them explicitly. |
| 209 | if (!item->empty_folder) |
| 210 | continue; |
| 211 | } else if (item->type == TYPE_BOOKMARK) { |
| 212 | // Import only valid bookmarks |
| 213 | if (!CanImportURL(item->url)) |
| 214 | continue; |
| 215 | } else { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | continue; |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 217 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | |
| 219 | // Skip the default bookmarks and unwanted URLs. |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 220 | if (default_urls.find(item->url) != default_urls.end() || |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 221 | post_keyword_ids.find(item->id) != post_keyword_ids.end()) |
| 222 | continue; |
| 223 | |
| 224 | // Find the bookmark path by tracing their links to parent folders. |
| 225 | std::vector<std::wstring> path; |
| 226 | BookmarkItem* child = item; |
| 227 | bool found_path = false; |
| 228 | bool is_in_toolbar = false; |
| 229 | while (child->parent >= 0) { |
| 230 | BookmarkItem* parent = list[child->parent]; |
| 231 | if (parent->id == toolbar_folder_id) { |
| 232 | // This bookmark entry should be put in the bookmark bar. |
| 233 | // But we put it in the Firefox group after first run, so |
| 234 | // that do not mess up the bookmark bar. |
[email protected] | 0072d87 | 2009-06-18 21:14:34 | [diff] [blame] | 235 | if (import_to_bookmark_bar()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | is_in_toolbar = true; |
| 237 | } else { |
| 238 | path.insert(path.begin(), parent->title); |
| 239 | path.insert(path.begin(), firefox_folder); |
| 240 | } |
| 241 | found_path = true; |
| 242 | break; |
| 243 | } else if (parent->id == menu_folder_id || |
| 244 | parent->id == unsorted_folder_id) { |
| 245 | // After the first run, the item will be placed in a folder in |
| 246 | // the "Other bookmarks". |
[email protected] | 0072d87 | 2009-06-18 21:14:34 | [diff] [blame] | 247 | if (!import_to_bookmark_bar()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | path.insert(path.begin(), firefox_folder); |
| 249 | found_path = true; |
| 250 | break; |
| 251 | } else if (livemark_id.find(parent->id) != livemark_id.end()) { |
| 252 | // If the entry is under a livemark folder, we don't import it. |
| 253 | break; |
| 254 | } |
| 255 | path.insert(path.begin(), parent->title); |
| 256 | child = parent; |
| 257 | } |
| 258 | |
| 259 | if (!found_path) |
| 260 | continue; |
| 261 | |
| 262 | ProfileWriter::BookmarkEntry entry; |
| 263 | entry.creation_time = item->date_added; |
| 264 | entry.title = item->title; |
| 265 | entry.url = item->url; |
| 266 | entry.path = path; |
| 267 | entry.in_toolbar = is_in_toolbar; |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 268 | entry.is_folder = item->type == TYPE_FOLDER; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | |
| 270 | bookmarks.push_back(entry); |
| 271 | |
[email protected] | df8c99a | 2011-02-28 20:45:50 | [diff] [blame] | 272 | if (item->type == TYPE_BOOKMARK) { |
| 273 | if (item->favicon) |
| 274 | favicon_map[item->favicon].insert(item->url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | |
[email protected] | df8c99a | 2011-02-28 20:45:50 | [diff] [blame] | 276 | // This bookmark has a keyword, we import it to our TemplateURL model. |
| 277 | TemplateURL* t_url = Firefox2Importer::CreateTemplateURL( |
| 278 | item->title, UTF8ToWide(item->keyword), item->url); |
| 279 | if (t_url) |
| 280 | template_urls.push_back(t_url); |
| 281 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | STLDeleteContainerPointers(list.begin(), list.end()); |
| 285 | |
| 286 | // Write into profile. |
| 287 | if (!bookmarks.empty() && !cancelled()) { |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 288 | const std::wstring& first_folder_name = |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 289 | bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 290 | int options = 0; |
| 291 | if (import_to_bookmark_bar()) |
| 292 | options = ProfileWriter::IMPORT_TO_BOOKMARK_BAR; |
| 293 | bridge_->AddBookmarkEntries(bookmarks, first_folder_name, options); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | } |
| 295 | if (!template_urls.empty() && !cancelled()) { |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 296 | bridge_->SetKeywords(template_urls, -1, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | } else { |
| 298 | STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); |
| 299 | } |
| 300 | if (!favicon_map.empty() && !cancelled()) { |
| 301 | std::vector<history::ImportedFavIconUsage> favicons; |
| 302 | LoadFavicons(db.get(), favicon_map, &favicons); |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 303 | bridge_->SetFavIcons(favicons); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | void Firefox3Importer::ImportPasswords() { |
| 308 | // Initializes NSS3. |
| 309 | NSSDecryptor decryptor; |
[email protected] | 91243c3 | 2011-01-28 18:27:17 | [diff] [blame] | 310 | if (!decryptor.Init(source_path_, source_path_) && |
| 311 | !decryptor.Init(app_path_, source_path_)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 312 | return; |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 313 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | |
[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 315 | std::vector<webkit_glue::PasswordForm> forms; |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 316 | FilePath source_path = source_path_; |
[email protected] | 1851d3b | 2009-08-15 00:27:20 | [diff] [blame] | 317 | FilePath file = source_path.AppendASCII("signons.sqlite"); |
| 318 | if (file_util::PathExists(file)) { |
| 319 | // Since Firefox 3.1, passwords are in signons.sqlite db. |
| 320 | decryptor.ReadAndParseSignons(file, &forms); |
| 321 | } else { |
| 322 | // Firefox 3.0 uses signons3.txt to store the passwords. |
| 323 | file = source_path.AppendASCII("signons3.txt"); |
| 324 | if (!file_util::PathExists(file)) |
| 325 | file = source_path.AppendASCII("signons2.txt"); |
| 326 | |
| 327 | std::string content; |
| 328 | file_util::ReadFileToString(file, &content); |
| 329 | decryptor.ParseSignons(content, &forms); |
| 330 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | |
| 332 | if (!cancelled()) { |
| 333 | for (size_t i = 0; i < forms.size(); ++i) { |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 334 | bridge_->SetPasswordForm(forms[i]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void Firefox3Importer::ImportSearchEngines() { |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 340 | std::vector<FilePath> files; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | GetSearchEnginesXMLFiles(&files); |
| 342 | |
| 343 | std::vector<TemplateURL*> search_engines; |
| 344 | ParseSearchEnginesFromXMLFiles(files, &search_engines); |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 345 | int default_index = |
| 346 | GetFirefoxDefaultSearchEngineIndex(search_engines, source_path_); |
| 347 | bridge_->SetKeywords(search_engines, default_index, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void Firefox3Importer::ImportHomepage() { |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 351 | GURL home_page = GetHomepage(source_path_); |
| 352 | if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) { |
| 353 | bridge_->AddHomePage(home_page); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | void Firefox3Importer::GetSearchEnginesXMLFiles( |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 358 | std::vector<FilePath>* files) { |
| 359 | FilePath file = source_path_.AppendASCII("search.sqlite"); |
| 360 | if (!file_util::PathExists(file)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | return; |
| 362 | |
| 363 | sqlite3* sqlite; |
[email protected] | 91243c3 | 2011-01-28 18:27:17 | [diff] [blame] | 364 | if (sqlite_utils::OpenSqliteDb(file, &sqlite) != SQLITE_OK) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 365 | return; |
[email protected] | 06a793f8 | 2009-08-05 23:50:35 | [diff] [blame] | 366 | sqlite_utils::scoped_sqlite_db_ptr db(sqlite); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | |
| 368 | SQLStatement s; |
[email protected] | e0262c9 | 2009-05-29 20:06:41 | [diff] [blame] | 369 | const char* stmt = "SELECT engineid FROM engine_data " |
| 370 | "WHERE engineid NOT IN " |
| 371 | "(SELECT engineid FROM engine_data " |
| 372 | "WHERE name='hidden') " |
| 373 | "ORDER BY value ASC"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | |
| 375 | if (s.prepare(db.get(), stmt) != SQLITE_OK) |
| 376 | return; |
| 377 | |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 378 | FilePath app_path = app_path_.AppendASCII("searchplugins"); |
| 379 | FilePath profile_path = source_path_.AppendASCII("searchplugins"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 380 | |
[email protected] | 813ed750 | 2010-09-01 22:58:24 | [diff] [blame] | 381 | // Firefox doesn't store a search engine in its sqlite database unless the |
| 382 | // user has added a engine. So we get search engines from sqlite db as well |
| 383 | // as from the file system. |
[email protected] | 67dc31d | 2009-06-24 15:41:21 | [diff] [blame] | 384 | if (s.step() == SQLITE_ROW) { |
[email protected] | e0262c9 | 2009-05-29 20:06:41 | [diff] [blame] | 385 | const std::wstring kAppPrefix = L"[app]/"; |
| 386 | const std::wstring kProfilePrefix = L"[profile]/"; |
| 387 | do { |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 388 | FilePath file; |
[email protected] | e0262c9 | 2009-05-29 20:06:41 | [diff] [blame] | 389 | std::wstring engine = UTF8ToWide(s.column_string(0)); |
| 390 | |
| 391 | // The string contains [app]/<name>.xml or [profile]/<name>.xml where |
| 392 | // the [app] and [profile] need to be replaced with the actual app or |
| 393 | // profile path. |
| 394 | size_t index = engine.find(kAppPrefix); |
| 395 | if (index != std::wstring::npos) { |
| 396 | // Remove '[app]/'. |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 397 | file = app_path.Append(FilePath::FromWStringHack( |
| 398 | engine.substr(index + kAppPrefix.length()))); |
[email protected] | 67dc31d | 2009-06-24 15:41:21 | [diff] [blame] | 399 | } else if ((index = engine.find(kProfilePrefix)) != std::wstring::npos) { |
[email protected] | e0262c9 | 2009-05-29 20:06:41 | [diff] [blame] | 400 | // Remove '[profile]/'. |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 401 | file = profile_path.Append( |
| 402 | FilePath::FromWStringHack( |
| 403 | engine.substr(index + kProfilePrefix.length()))); |
[email protected] | e0262c9 | 2009-05-29 20:06:41 | [diff] [blame] | 404 | } else { |
[email protected] | 67dc31d | 2009-06-24 15:41:21 | [diff] [blame] | 405 | // Looks like absolute path to the file. |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 406 | file = FilePath::FromWStringHack(engine); |
[email protected] | e0262c9 | 2009-05-29 20:06:41 | [diff] [blame] | 407 | } |
| 408 | files->push_back(file); |
| 409 | } while (s.step() == SQLITE_ROW && !cancelled()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | } |
[email protected] | 67dc31d | 2009-06-24 15:41:21 | [diff] [blame] | 411 | |
[email protected] | 813ed750 | 2010-09-01 22:58:24 | [diff] [blame] | 412 | #if defined(OS_LINUX) |
| 413 | // Ubuntu-flavored Firefox3 supports locale-specific search engines via |
| 414 | // locale-named subdirectories. They fall back to en-US. |
| 415 | // See http://crbug.com/53899 |
| 416 | // TODO(jshin): we need to make sure our locale code matches that of |
| 417 | // Firefox. |
| 418 | FilePath locale_app_path = app_path.AppendASCII(locale_); |
| 419 | FilePath default_locale_app_path = app_path.AppendASCII("en-US"); |
| 420 | if (file_util::DirectoryExists(locale_app_path)) |
| 421 | app_path = locale_app_path; |
| 422 | else if (file_util::DirectoryExists(default_locale_app_path)) |
| 423 | app_path = default_locale_app_path; |
| 424 | #endif |
| 425 | |
[email protected] | 67dc31d | 2009-06-24 15:41:21 | [diff] [blame] | 426 | // Get search engine definition from file system. |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 427 | file_util::FileEnumerator engines(app_path, false, |
[email protected] | 67dc31d | 2009-06-24 15:41:21 | [diff] [blame] | 428 | file_util::FileEnumerator::FILES); |
| 429 | for (FilePath engine_path = engines.Next(); !engine_path.value().empty(); |
| 430 | engine_path = engines.Next()) { |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 431 | files->push_back(engine_path); |
[email protected] | 67dc31d | 2009-06-24 15:41:21 | [diff] [blame] | 432 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void Firefox3Importer::LoadRootNodeID(sqlite3* db, |
| 436 | int* toolbar_folder_id, |
| 437 | int* menu_folder_id, |
| 438 | int* unsorted_folder_id) { |
| 439 | const char kToolbarFolderName[] = "toolbar"; |
| 440 | const char kMenuFolderName[] = "menu"; |
| 441 | const char kUnsortedFolderName[] = "unfiled"; |
| 442 | |
| 443 | SQLStatement s; |
| 444 | const char* stmt = "SELECT root_name, folder_id FROM moz_bookmarks_roots"; |
| 445 | if (s.prepare(db, stmt) != SQLITE_OK) |
| 446 | return; |
| 447 | |
| 448 | while (s.step() == SQLITE_ROW) { |
| 449 | std::string folder = s.column_string(0); |
| 450 | int id = s.column_int(1); |
| 451 | if (folder == kToolbarFolderName) |
| 452 | *toolbar_folder_id = id; |
| 453 | else if (folder == kMenuFolderName) |
| 454 | *menu_folder_id = id; |
| 455 | else if (folder == kUnsortedFolderName) |
| 456 | *unsorted_folder_id = id; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void Firefox3Importer::LoadLivemarkIDs(sqlite3* db, |
| 461 | std::set<int>* livemark) { |
| 462 | const char kFeedAnnotation[] = "livemark/feedURI"; |
| 463 | livemark->clear(); |
| 464 | |
| 465 | SQLStatement s; |
| 466 | const char* stmt = "SELECT b.item_id " |
| 467 | "FROM moz_anno_attributes a " |
| 468 | "JOIN moz_items_annos b ON a.id = b.anno_attribute_id " |
| 469 | "WHERE a.name = ? "; |
| 470 | if (s.prepare(db, stmt) != SQLITE_OK) |
| 471 | return; |
| 472 | |
| 473 | s.bind_string(0, kFeedAnnotation); |
| 474 | while (s.step() == SQLITE_ROW && !cancelled()) |
| 475 | livemark->insert(s.column_int(0)); |
| 476 | } |
| 477 | |
| 478 | void Firefox3Importer::GetTopBookmarkFolder(sqlite3* db, int folder_id, |
| 479 | BookmarkList* list) { |
| 480 | SQLStatement s; |
| 481 | const char* stmt = "SELECT b.title " |
| 482 | "FROM moz_bookmarks b " |
| 483 | "WHERE b.type = 2 AND b.id = ? " |
| 484 | "ORDER BY b.position"; |
| 485 | if (s.prepare(db, stmt) != SQLITE_OK) |
| 486 | return; |
| 487 | |
| 488 | s.bind_int(0, folder_id); |
| 489 | if (s.step() == SQLITE_ROW) { |
| 490 | BookmarkItem* item = new BookmarkItem; |
| 491 | item->parent = -1; // The top level folder has no parent. |
| 492 | item->id = folder_id; |
[email protected] | 9660ddd3 | 2009-01-30 19:33:53 | [diff] [blame] | 493 | item->title = s.column_wstring(0); |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 494 | item->type = TYPE_FOLDER; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 495 | item->favicon = 0; |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 496 | item->empty_folder = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 497 | list->push_back(item); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void Firefox3Importer::GetWholeBookmarkFolder(sqlite3* db, BookmarkList* list, |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 502 | size_t position, |
| 503 | bool* empty_folder) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | if (position >= list->size()) { |
| 505 | NOTREACHED(); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | SQLStatement s; |
| 510 | const char* stmt = "SELECT b.id, h.url, COALESCE(b.title, h.title), " |
| 511 | "b.type, k.keyword, b.dateAdded, h.favicon_id " |
| 512 | "FROM moz_bookmarks b " |
| 513 | "LEFT JOIN moz_places h ON b.fk = h.id " |
| 514 | "LEFT JOIN moz_keywords k ON k.id = b.keyword_id " |
| 515 | "WHERE b.type IN (1,2) AND b.parent = ? " |
| 516 | "ORDER BY b.position"; |
| 517 | if (s.prepare(db, stmt) != SQLITE_OK) |
| 518 | return; |
| 519 | |
| 520 | s.bind_int(0, (*list)[position]->id); |
| 521 | BookmarkList temp_list; |
| 522 | while (s.step() == SQLITE_ROW) { |
| 523 | BookmarkItem* item = new BookmarkItem; |
| 524 | item->parent = static_cast<int>(position); |
| 525 | item->id = s.column_int(0); |
| 526 | item->url = GURL(s.column_string(1)); |
[email protected] | 9660ddd3 | 2009-01-30 19:33:53 | [diff] [blame] | 527 | item->title = s.column_wstring(2); |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 528 | item->type = static_cast<BookmarkItemType>(s.column_int(3)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | item->keyword = s.column_string(4); |
[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 530 | item->date_added = base::Time::FromTimeT(s.column_int64(5)/1000000); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | item->favicon = s.column_int64(6); |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 532 | item->empty_folder = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 533 | |
| 534 | temp_list.push_back(item); |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 535 | if (empty_folder != NULL) |
| 536 | *empty_folder = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // Appends all items to the list. |
| 540 | for (BookmarkList::iterator i = temp_list.begin(); |
| 541 | i != temp_list.end(); ++i) { |
| 542 | list->push_back(*i); |
| 543 | // Recursive add bookmarks in sub-folders. |
[email protected] | 0540212 | 2011-01-28 06:48:59 | [diff] [blame] | 544 | if ((*i)->type == TYPE_FOLDER) |
| 545 | GetWholeBookmarkFolder(db, list, list->size() - 1, &(*i)->empty_folder); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | void Firefox3Importer::LoadFavicons( |
| 550 | sqlite3* db, |
| 551 | const FaviconMap& favicon_map, |
| 552 | std::vector<history::ImportedFavIconUsage>* favicons) { |
| 553 | SQLStatement s; |
| 554 | const char* stmt = "SELECT url, data FROM moz_favicons WHERE id=?"; |
| 555 | if (s.prepare(db, stmt) != SQLITE_OK) |
| 556 | return; |
| 557 | |
| 558 | for (FaviconMap::const_iterator i = favicon_map.begin(); |
| 559 | i != favicon_map.end(); ++i) { |
| 560 | s.bind_int64(0, i->first); |
| 561 | if (s.step() == SQLITE_ROW) { |
| 562 | history::ImportedFavIconUsage usage; |
| 563 | |
| 564 | usage.favicon_url = GURL(s.column_string(0)); |
| 565 | if (!usage.favicon_url.is_valid()) |
| 566 | continue; // Don't bother importing favicons with invalid URLs. |
| 567 | |
| 568 | std::vector<unsigned char> data; |
| 569 | if (!s.column_blob_as_vector(1, &data) || data.empty()) |
| 570 | continue; // Data definitely invalid. |
| 571 | |
| 572 | if (!ReencodeFavicon(&data[0], data.size(), &usage.png_data)) |
| 573 | continue; // Unable to decode. |
| 574 | |
| 575 | usage.urls = i->second; |
| 576 | favicons->push_back(usage); |
| 577 | } |
| 578 | s.reset(); |
| 579 | } |
| 580 | } |