| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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/firefox3_importer.h" | |
| 6 | |
| 7 #include <set> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/scoped_ptr.h" | |
| 11 #include "base/string_util.h" | |
| 12 #include "chrome/browser/firefox2_importer.h" | |
| 13 #include "chrome/browser/firefox_importer_utils.h" | |
| 14 #include "chrome/common/l10n_util.h" | |
| 15 #include "chrome/common/time_format.h" | |
| 16 #include "generated_resources.h" | |
| 17 | |
| 18 // Wraps the function sqlite3_close() in a class that is | |
| 19 // used in scoped_ptr_malloc. | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class DBClose { | |
| 24 public: | |
| 25 inline void operator()(sqlite3* x) const { | |
| 26 sqlite3_close(x); | |
| 27 } | |
| 28 }; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 void Firefox3Importer::StartImport(ProfileInfo profile_info, | |
| 33 uint16 items, ProfileWriter* writer, | |
| 34 ImporterHost* host) { | |
| 35 writer_ = writer; | |
| 36 source_path_ = profile_info.source_path; | |
| 37 app_path_ = profile_info.app_path; | |
| 38 importer_host_ = host; | |
| 39 | |
| 40 | |
| 41 // The order here is important! | |
| 42 NotifyStarted(); | |
| 43 if ((items & HOME_PAGE) && !cancelled()) | |
| 44 ImportHomepage(); // Doesn't have a UI item. | |
| 45 if ((items & FAVORITES) && !cancelled()) { | |
| 46 NotifyItemStarted(FAVORITES); | |
| 47 ImportBookmarks(); | |
| 48 NotifyItemEnded(FAVORITES); | |
| 49 } | |
| 50 if ((items & SEARCH_ENGINES) && !cancelled()) { | |
| 51 NotifyItemStarted(SEARCH_ENGINES); | |
| 52 ImportSearchEngines(); | |
| 53 NotifyItemEnded(SEARCH_ENGINES); | |
| 54 } | |
| 55 if ((items & PASSWORDS) && !cancelled()) { | |
| 56 NotifyItemStarted(PASSWORDS); | |
| 57 ImportPasswords(); | |
| 58 NotifyItemEnded(PASSWORDS); | |
| 59 } | |
| 60 if ((items & HISTORY) && !cancelled()) { | |
| 61 NotifyItemStarted(HISTORY); | |
| 62 ImportHistory(); | |
| 63 NotifyItemEnded(HISTORY); | |
| 64 } | |
| 65 NotifyEnded(); | |
| 66 } | |
| 67 | |
| 68 void Firefox3Importer::ImportHistory() { | |
| 69 std::wstring file = source_path_; | |
| 70 file_util::AppendToPath(&file, L"places.sqlite"); | |
| 71 if (!file_util::PathExists(file)) | |
| 72 return; | |
| 73 | |
| 74 sqlite3* sqlite; | |
| 75 if (sqlite3_open(WideToUTF8(file).c_str(), &sqlite) != SQLITE_OK) | |
| 76 return; | |
| 77 scoped_ptr_malloc<sqlite3, DBClose> db(sqlite); | |
| 78 | |
| 79 SQLStatement s; | |
| 80 // |visit_type| represent the transition type of URLs (typed, click, | |
| 81 // redirect, bookmark, etc.) We eliminate some URLs like sub-frames and | |
| 82 // redirects, since we don't want them to appear in history. | |
| 83 // Firefox transition types are defined in: | |
| 84 // toolkit/components/places/public/nsINavHistoryService.idl | |
| 85 const char* stmt = "SELECT h.url, h.title, h.visit_count, " | |
| 86 "h.hidden, h.typed, v.visit_date " | |
| 87 "FROM moz_places h JOIN moz_historyvisits v " | |
| 88 "ON h.id = v.place_id " | |
| 89 "WHERE v.visit_type <= 3"; | |
| 90 | |
| 91 if (s.prepare(db.get(), stmt) != SQLITE_OK) | |
| 92 return; | |
| 93 | |
| 94 std::vector<history::URLRow> rows; | |
| 95 while (s.step() == SQLITE_ROW && !cancelled()) { | |
| 96 GURL url(s.column_string(0)); | |
| 97 | |
| 98 // Filter out unwanted URLs. | |
| 99 if (!CanImportURL(url)) | |
| 100 continue; | |
| 101 | |
| 102 history::URLRow row(url); | |
| 103 row.set_title(s.column_string16(1)); | |
| 104 row.set_visit_count(s.column_int(2)); | |
| 105 row.set_hidden(s.column_int(3) == 1); | |
| 106 row.set_typed_count(s.column_int(4)); | |
| 107 row.set_last_visit(Time::FromTimeT(s.column_int64(5)/1000000)); | |
| 108 | |
| 109 rows.push_back(row); | |
| 110 } | |
| 111 if (!rows.empty() && !cancelled()) { | |
| 112 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | |
| 113 &ProfileWriter::AddHistoryPage, rows)); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void Firefox3Importer::ImportBookmarks() { | |
| 118 std::wstring file = source_path_; | |
| 119 file_util::AppendToPath(&file, L"places.sqlite"); | |
| 120 if (!file_util::PathExists(file)) | |
| 121 return; | |
| 122 | |
| 123 sqlite3* sqlite; | |
| 124 if (sqlite3_open(WideToUTF8(file).c_str(), &sqlite) != SQLITE_OK) | |
| 125 return; | |
| 126 scoped_ptr_malloc<sqlite3, DBClose> db(sqlite); | |
| 127 | |
| 128 // Get the bookmark folders that we are interested in. | |
| 129 int toolbar_folder_id = -1; | |
| 130 int menu_folder_id = -1; | |
| 131 int unsorted_folder_id = -1; | |
| 132 LoadRootNodeID(db.get(), &toolbar_folder_id, &menu_folder_id, | |
| 133 &unsorted_folder_id); | |
| 134 | |
| 135 // Load livemark IDs. | |
| 136 std::set<int> livemark_id; | |
| 137 LoadLivemarkIDs(db.get(), &livemark_id); | |
| 138 | |
| 139 // Load the default bookmarks. Its storage is the same as Firefox 2. | |
| 140 std::set<GURL> default_urls; | |
| 141 Firefox2Importer::LoadDefaultBookmarks(app_path_, &default_urls); | |
| 142 | |
| 143 BookmarkList list; | |
| 144 GetTopBookmarkFolder(db.get(), toolbar_folder_id, &list); | |
| 145 GetTopBookmarkFolder(db.get(), menu_folder_id, &list); | |
| 146 GetTopBookmarkFolder(db.get(), unsorted_folder_id, &list); | |
| 147 size_t count = list.size(); | |
| 148 for (size_t i = 0; i < count; ++i) | |
| 149 GetWholeBookmarkFolder(db.get(), &list, i); | |
| 150 | |
| 151 std::vector<ProfileWriter::BookmarkEntry> bookmarks; | |
| 152 std::vector<TemplateURL*> template_urls; | |
| 153 FaviconMap favicon_map; | |
| 154 | |
| 155 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based | |
| 156 // keywords yet. We won't include them in the list. | |
| 157 std::set<int> post_keyword_ids; | |
| 158 SQLStatement s; | |
| 159 const char* stmt = "SELECT b.id FROM moz_bookmarks b " | |
| 160 "INNER JOIN moz_items_annos ia ON ia.item_id = b.id " | |
| 161 "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id " | |
| 162 "WHERE aa.name = 'bookmarkProperties/POSTData'"; | |
| 163 if (s.prepare(db.get(), stmt) == SQLITE_OK) { | |
| 164 while (s.step() == SQLITE_ROW && !cancelled()) | |
| 165 post_keyword_ids.insert(s.column_int(0)); | |
| 166 } else { | |
| 167 NOTREACHED(); | |
| 168 } | |
| 169 | |
| 170 std::wstring firefox_folder = | |
| 171 l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); | |
| 172 for (size_t i = 0; i < list.size(); ++i) { | |
| 173 BookmarkItem* item = list[i]; | |
| 174 | |
| 175 // The type of bookmark items is 1. | |
| 176 if (item->type != 1) | |
| 177 continue; | |
| 178 | |
| 179 // Skip the default bookmarks and unwanted URLs. | |
| 180 if (!CanImportURL(item->url) || | |
| 181 default_urls.find(item->url) != default_urls.end() || | |
| 182 post_keyword_ids.find(item->id) != post_keyword_ids.end()) | |
| 183 continue; | |
| 184 | |
| 185 // Find the bookmark path by tracing their links to parent folders. | |
| 186 std::vector<std::wstring> path; | |
| 187 BookmarkItem* child = item; | |
| 188 bool found_path = false; | |
| 189 bool is_in_toolbar = false; | |
| 190 while (child->parent >= 0) { | |
| 191 BookmarkItem* parent = list[child->parent]; | |
| 192 if (parent->id == toolbar_folder_id) { | |
| 193 // This bookmark entry should be put in the bookmark bar. | |
| 194 // But we put it in the Firefox group after first run, so | |
| 195 // that do not mess up the bookmark bar. | |
| 196 if (first_run()) { | |
| 197 is_in_toolbar = true; | |
| 198 } else { | |
| 199 path.insert(path.begin(), parent->title); | |
| 200 path.insert(path.begin(), firefox_folder); | |
| 201 } | |
| 202 found_path = true; | |
| 203 break; | |
| 204 } else if (parent->id == menu_folder_id || | |
| 205 parent->id == unsorted_folder_id) { | |
| 206 // After the first run, the item will be placed in a folder in | |
| 207 // the "Other bookmarks". | |
| 208 if (!first_run()) | |
| 209 path.insert(path.begin(), firefox_folder); | |
| 210 found_path = true; | |
| 211 break; | |
| 212 } else if (livemark_id.find(parent->id) != livemark_id.end()) { | |
| 213 // If the entry is under a livemark folder, we don't import it. | |
| 214 break; | |
| 215 } | |
| 216 path.insert(path.begin(), parent->title); | |
| 217 child = parent; | |
| 218 } | |
| 219 | |
| 220 if (!found_path) | |
| 221 continue; | |
| 222 | |
| 223 ProfileWriter::BookmarkEntry entry; | |
| 224 entry.creation_time = item->date_added; | |
| 225 entry.title = item->title; | |
| 226 entry.url = item->url; | |
| 227 entry.path = path; | |
| 228 entry.in_toolbar = is_in_toolbar; | |
| 229 | |
| 230 bookmarks.push_back(entry); | |
| 231 | |
| 232 if (item->favicon) | |
| 233 favicon_map[item->favicon].insert(item->url); | |
| 234 | |
| 235 // This bookmark has a keyword, we import it to our TemplateURL model. | |
| 236 TemplateURL* t_url = Firefox2Importer::CreateTemplateURL( | |
| 237 item->title, UTF8ToWide(item->keyword), item->url); | |
| 238 if (t_url) | |
| 239 template_urls.push_back(t_url); | |
| 240 } | |
| 241 | |
| 242 STLDeleteContainerPointers(list.begin(), list.end()); | |
| 243 | |
| 244 // Write into profile. | |
| 245 if (!bookmarks.empty() && !cancelled()) { | |
| 246 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | |
| 247 &ProfileWriter::AddBookmarkEntry, bookmarks)); | |
| 248 } | |
| 249 if (!template_urls.empty() && !cancelled()) { | |
| 250 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | |
| 251 &ProfileWriter::AddKeywords, template_urls, -1, false)); | |
| 252 } else { | |
| 253 STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); | |
| 254 } | |
| 255 if (!favicon_map.empty() && !cancelled()) { | |
| 256 std::vector<history::ImportedFavIconUsage> favicons; | |
| 257 LoadFavicons(db.get(), favicon_map, &favicons); | |
| 258 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | |
| 259 &ProfileWriter::AddFavicons, favicons)); | |
| 260 } | |
| 261 } | |
| 262 | |
| 263 void Firefox3Importer::ImportPasswords() { | |
| 264 // Initializes NSS3. | |
| 265 NSSDecryptor decryptor; | |
| 266 if (!decryptor.Init(source_path_, source_path_) && | |
| 267 !decryptor.Init(app_path_, source_path_)) | |
| 268 return; | |
| 269 | |
| 270 // Firefox 3 uses signons3.txt to store the passwords. | |
| 271 std::wstring file = source_path_; | |
| 272 file_util::AppendToPath(&file, L"signons3.txt"); | |
| 273 if (!file_util::PathExists(file)) { | |
| 274 file = source_path_; | |
| 275 file_util::AppendToPath(&file, L"signons2.txt"); | |
| 276 } | |
| 277 | |
| 278 std::string content; | |
| 279 file_util::ReadFileToString(file, &content); | |
| 280 std::vector<PasswordForm> forms; | |
| 281 decryptor.ParseSignons(content, &forms); | |
| 282 | |
| 283 if (!cancelled()) { | |
| 284 for (size_t i = 0; i < forms.size(); ++i) { | |
| 285 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | |
| 286 &ProfileWriter::AddPasswordForm, forms[i])); | |
| 287 } | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 void Firefox3Importer::ImportSearchEngines() { | |
| 292 std::vector<std::wstring> files; | |
| 293 GetSearchEnginesXMLFiles(&files); | |
| 294 | |
| 295 std::vector<TemplateURL*> search_engines; | |
| 296 ParseSearchEnginesFromXMLFiles(files, &search_engines); | |
| 297 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | |
| 298 &ProfileWriter::AddKeywords, search_engines, | |
| 299 GetFirefoxDefaultSearchEngineIndex(search_engines, source_path_), true)); | |
| 300 } | |
| 301 | |
| 302 void Firefox3Importer::ImportHomepage() { | |
| 303 GURL homepage = GetHomepage(source_path_); | |
| 304 if (homepage.is_valid() && !IsDefaultHomepage(homepage, app_path_)) { | |
| 305 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | |
| 306 &ProfileWriter::AddHomepage, homepage)); | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 void Firefox3Importer::GetSearchEnginesXMLFiles( | |
| 311 std::vector<std::wstring>* files) { | |
| 312 std::wstring file = source_path_; | |
| 313 file_util::AppendToPath(&file, L"search.sqlite"); | |
| 314 if (!file_util::PathExists(file)) | |
| 315 return; | |
| 316 | |
| 317 sqlite3* sqlite; | |
| 318 if (sqlite3_open(WideToUTF8(file).c_str(), &sqlite) != SQLITE_OK) | |
| 319 return; | |
| 320 scoped_ptr_malloc<sqlite3, DBClose> db(sqlite); | |
| 321 | |
| 322 SQLStatement s; | |
| 323 const char* stmt = "SELECT engineid FROM engine_data ORDER BY value ASC"; | |
| 324 | |
| 325 if (s.prepare(db.get(), stmt) != SQLITE_OK) | |
| 326 return; | |
| 327 | |
| 328 std::wstring app_path = app_path_; | |
| 329 file_util::AppendToPath(&app_path, L"searchplugins"); | |
| 330 std::wstring profile_path = source_path_; | |
| 331 file_util::AppendToPath(&profile_path, L"searchplugins"); | |
| 332 | |
| 333 const std::wstring kAppPrefix = L"[app]/"; | |
| 334 const std::wstring kProfilePrefix = L"[profile]/"; | |
| 335 while (s.step() == SQLITE_ROW && !cancelled()) { | |
| 336 std::wstring file; | |
| 337 std::wstring engine = UTF8ToWide(s.column_string(0)); | |
| 338 // The string contains [app]/<name>.xml or [profile]/<name>.xml where the | |
| 339 // [app] and [profile] need to be replaced with the actual app or profile | |
| 340 // path. | |
| 341 size_t index = engine.find(kAppPrefix); | |
| 342 if (index != std::wstring::npos) { | |
| 343 file = app_path; | |
| 344 file_util::AppendToPath( | |
| 345 &file, | |
| 346 engine.substr(index + kAppPrefix.length())); // Remove '[app]/'. | |
| 347 } else if ((index = engine.find(kProfilePrefix)) != std::wstring::npos) { | |
| 348 file = profile_path; | |
| 349 file_util::AppendToPath( | |
| 350 &file, | |
| 351 engine.substr(index + kProfilePrefix.length())); // Remove | |
| 352 // '[profile]/'. | |
| 353 } else { | |
| 354 NOTREACHED() << "Unexpected Firefox 3 search engine id"; | |
| 355 continue; | |
| 356 } | |
| 357 files->push_back(file); | |
| 358 } | |
| 359 } | |
| 360 | |
| 361 void Firefox3Importer::LoadRootNodeID(sqlite3* db, | |
| 362 int* toolbar_folder_id, | |
| 363 int* menu_folder_id, | |
| 364 int* unsorted_folder_id) { | |
| 365 const char kToolbarFolderName[] = "toolbar"; | |
| 366 const char kMenuFolderName[] = "menu"; | |
| 367 const char kUnsortedFolderName[] = "unfiled"; | |
| 368 | |
| 369 SQLStatement s; | |
| 370 const char* stmt = "SELECT root_name, folder_id FROM moz_bookmarks_roots"; | |
| 371 if (s.prepare(db, stmt) != SQLITE_OK) | |
| 372 return; | |
| 373 | |
| 374 while (s.step() == SQLITE_ROW) { | |
| 375 std::string folder = s.column_string(0); | |
| 376 int id = s.column_int(1); | |
| 377 if (folder == kToolbarFolderName) | |
| 378 *toolbar_folder_id = id; | |
| 379 else if (folder == kMenuFolderName) | |
| 380 *menu_folder_id = id; | |
| 381 else if (folder == kUnsortedFolderName) | |
| 382 *unsorted_folder_id = id; | |
| 383 } | |
| 384 } | |
| 385 | |
| 386 void Firefox3Importer::LoadLivemarkIDs(sqlite3* db, | |
| 387 std::set<int>* livemark) { | |
| 388 const char kFeedAnnotation[] = "livemark/feedURI"; | |
| 389 livemark->clear(); | |
| 390 | |
| 391 SQLStatement s; | |
| 392 const char* stmt = "SELECT b.item_id " | |
| 393 "FROM moz_anno_attributes a " | |
| 394 "JOIN moz_items_annos b ON a.id = b.anno_attribute_id " | |
| 395 "WHERE a.name = ? "; | |
| 396 if (s.prepare(db, stmt) != SQLITE_OK) | |
| 397 return; | |
| 398 | |
| 399 s.bind_string(0, kFeedAnnotation); | |
| 400 while (s.step() == SQLITE_ROW && !cancelled()) | |
| 401 livemark->insert(s.column_int(0)); | |
| 402 } | |
| 403 | |
| 404 void Firefox3Importer::GetTopBookmarkFolder(sqlite3* db, int folder_id, | |
| 405 BookmarkList* list) { | |
| 406 SQLStatement s; | |
| 407 const char* stmt = "SELECT b.title " | |
| 408 "FROM moz_bookmarks b " | |
| 409 "WHERE b.type = 2 AND b.id = ? " | |
| 410 "ORDER BY b.position"; | |
| 411 if (s.prepare(db, stmt) != SQLITE_OK) | |
| 412 return; | |
| 413 | |
| 414 s.bind_int(0, folder_id); | |
| 415 if (s.step() == SQLITE_ROW) { | |
| 416 BookmarkItem* item = new BookmarkItem; | |
| 417 item->parent = -1; // The top level folder has no parent. | |
| 418 item->id = folder_id; | |
| 419 item->title = s.column_string16(0); | |
| 420 item->type = 2; | |
| 421 item->favicon = 0; | |
| 422 list->push_back(item); | |
| 423 } | |
| 424 } | |
| 425 | |
| 426 void Firefox3Importer::GetWholeBookmarkFolder(sqlite3* db, BookmarkList* list, | |
| 427 size_t position) { | |
| 428 if (position >= list->size()) { | |
| 429 NOTREACHED(); | |
| 430 return; | |
| 431 } | |
| 432 | |
| 433 SQLStatement s; | |
| 434 const char* stmt = "SELECT b.id, h.url, COALESCE(b.title, h.title), " | |
| 435 "b.type, k.keyword, b.dateAdded, h.favicon_id " | |
| 436 "FROM moz_bookmarks b " | |
| 437 "LEFT JOIN moz_places h ON b.fk = h.id " | |
| 438 "LEFT JOIN moz_keywords k ON k.id = b.keyword_id " | |
| 439 "WHERE b.type IN (1,2) AND b.parent = ? " | |
| 440 "ORDER BY b.position"; | |
| 441 if (s.prepare(db, stmt) != SQLITE_OK) | |
| 442 return; | |
| 443 | |
| 444 s.bind_int(0, (*list)[position]->id); | |
| 445 BookmarkList temp_list; | |
| 446 while (s.step() == SQLITE_ROW) { | |
| 447 BookmarkItem* item = new BookmarkItem; | |
| 448 item->parent = static_cast<int>(position); | |
| 449 item->id = s.column_int(0); | |
| 450 item->url = GURL(s.column_string(1)); | |
| 451 item->title = s.column_string16(2); | |
| 452 item->type = s.column_int(3); | |
| 453 item->keyword = s.column_string(4); | |
| 454 item->date_added = Time::FromTimeT(s.column_int64(5)/1000000); | |
| 455 item->favicon = s.column_int64(6); | |
| 456 | |
| 457 temp_list.push_back(item); | |
| 458 } | |
| 459 | |
| 460 // Appends all items to the list. | |
| 461 for (BookmarkList::iterator i = temp_list.begin(); | |
| 462 i != temp_list.end(); ++i) { | |
| 463 list->push_back(*i); | |
| 464 // Recursive add bookmarks in sub-folders. | |
| 465 if ((*i)->type == 2) | |
| 466 GetWholeBookmarkFolder(db, list, list->size() - 1); | |
| 467 } | |
| 468 } | |
| 469 | |
| 470 void Firefox3Importer::LoadFavicons( | |
| 471 sqlite3* db, | |
| 472 const FaviconMap& favicon_map, | |
| 473 std::vector<history::ImportedFavIconUsage>* favicons) { | |
| 474 SQLStatement s; | |
| 475 const char* stmt = "SELECT url, data FROM moz_favicons WHERE id=?"; | |
| 476 if (s.prepare(db, stmt) != SQLITE_OK) | |
| 477 return; | |
| 478 | |
| 479 for (FaviconMap::const_iterator i = favicon_map.begin(); | |
| 480 i != favicon_map.end(); ++i) { | |
| 481 s.bind_int64(0, i->first); | |
| 482 if (s.step() == SQLITE_ROW) { | |
| 483 history::ImportedFavIconUsage usage; | |
| 484 | |
| 485 usage.favicon_url = GURL(s.column_string(0)); | |
| 486 if (!usage.favicon_url.is_valid()) | |
| 487 continue; // Don't bother importing favicons with invalid URLs. | |
| 488 | |
| 489 std::vector<unsigned char> data; | |
| 490 if (!s.column_blob_as_vector(1, &data) || data.empty()) | |
| 491 continue; // Data definitely invalid. | |
| 492 | |
| 493 if (!ReencodeFavicon(&data[0], data.size(), &usage.png_data)) | |
| 494 continue; // Unable to decode. | |
| 495 | |
| 496 usage.urls = i->second; | |
| 497 favicons->push_back(usage); | |
| 498 } | |
| 499 s.reset(); | |
| 500 } | |
| 501 } | |
| 502 | |
| OLD | NEW |