Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: chrome/browser/importer/firefox3_importer.cc

Issue 18501013: Move most importer code to chrome/utility/importer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: another gyp attempt Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/firefox3_importer.h"
6
7 #include <set>
8
9 #include "base/file_util.h"
10 #include "base/files/file_enumerator.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/importer/bookmark_html_reader.h"
18 #include "chrome/browser/importer/firefox_importer_utils.h"
19 #include "chrome/browser/importer/importer_bridge.h"
20 #include "chrome/browser/importer/nss_decryptor.h"
21 #include "chrome/browser/importer/reencode_favicon.h"
22 #include "chrome/common/importer/imported_bookmark_entry.h"
23 #include "chrome/common/importer/imported_favicon_usage.h"
24 #include "chrome/common/importer/importer_url_row.h"
25 #include "chrome/common/time_format.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/common/password_form.h"
28 #include "grit/generated_resources.h"
29 #include "sql/connection.h"
30 #include "sql/statement.h"
31 #include "url/gurl.h"
32
33 using content::BrowserThread;
34
35 namespace {
36
37 // Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/
38 // components/places/public/nsINavBookmarksService.idl
39 enum BookmarkItemType {
40 TYPE_BOOKMARK = 1,
41 TYPE_FOLDER = 2,
42 TYPE_SEPARATOR = 3,
43 TYPE_DYNAMIC_CONTAINER = 4
44 };
45
46 // Loads the default bookmarks in the Firefox installed at |app_path|,
47 // and stores their locations in |urls|.
48 void LoadDefaultBookmarks(const base::FilePath& app_path,
49 std::set<GURL>* urls) {
50 base::FilePath file = app_path.AppendASCII("defaults")
51 .AppendASCII("profile")
52 .AppendASCII("bookmarks.html");
53 urls->clear();
54
55 std::vector<ImportedBookmarkEntry> bookmarks;
56 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(),
57 base::Callback<bool(const GURL&)>(),
58 file,
59 &bookmarks,
60 NULL);
61 for (size_t i = 0; i < bookmarks.size(); ++i)
62 urls->insert(bookmarks[i].url);
63 }
64
65 } // namespace
66
67 struct Firefox3Importer::BookmarkItem {
68 int parent;
69 int id;
70 GURL url;
71 string16 title;
72 BookmarkItemType type;
73 std::string keyword;
74 base::Time date_added;
75 int64 favicon;
76 bool empty_folder;
77 };
78
79 Firefox3Importer::Firefox3Importer() {
80 }
81
82 Firefox3Importer::~Firefox3Importer() {
83 }
84
85 void Firefox3Importer::StartImport(
86 const importer::SourceProfile& source_profile,
87 uint16 items,
88 ImporterBridge* bridge) {
89 bridge_ = bridge;
90 source_path_ = source_profile.source_path;
91 app_path_ = source_profile.app_path;
92
93 #if defined(OS_POSIX)
94 locale_ = source_profile.locale;
95 #endif
96
97 // The order here is important!
98 bridge_->NotifyStarted();
99 if ((items & importer::HOME_PAGE) && !cancelled()) {
100 bridge_->NotifyItemStarted(importer::HOME_PAGE);
101 ImportHomepage(); // Doesn't have a UI item.
102 bridge_->NotifyItemEnded(importer::HOME_PAGE);
103 }
104
105 // Note history should be imported before bookmarks because bookmark import
106 // will also import favicons and we store favicon for a URL only if the URL
107 // exist in history or bookmarks.
108 if ((items & importer::HISTORY) && !cancelled()) {
109 bridge_->NotifyItemStarted(importer::HISTORY);
110 ImportHistory();
111 bridge_->NotifyItemEnded(importer::HISTORY);
112 }
113
114 if ((items & importer::FAVORITES) && !cancelled()) {
115 bridge_->NotifyItemStarted(importer::FAVORITES);
116 ImportBookmarks();
117 bridge_->NotifyItemEnded(importer::FAVORITES);
118 }
119 if ((items & importer::SEARCH_ENGINES) && !cancelled()) {
120 bridge_->NotifyItemStarted(importer::SEARCH_ENGINES);
121 ImportSearchEngines();
122 bridge_->NotifyItemEnded(importer::SEARCH_ENGINES);
123 }
124 if ((items & importer::PASSWORDS) && !cancelled()) {
125 bridge_->NotifyItemStarted(importer::PASSWORDS);
126 ImportPasswords();
127 bridge_->NotifyItemEnded(importer::PASSWORDS);
128 }
129 bridge_->NotifyEnded();
130 }
131
132 void Firefox3Importer::ImportHistory() {
133 base::FilePath file = source_path_.AppendASCII("places.sqlite");
134 if (!base::PathExists(file))
135 return;
136
137 sql::Connection db;
138 if (!db.Open(file))
139 return;
140
141 // |visit_type| represent the transition type of URLs (typed, click,
142 // redirect, bookmark, etc.) We eliminate some URLs like sub-frames and
143 // redirects, since we don't want them to appear in history.
144 // Firefox transition types are defined in:
145 // toolkit/components/places/public/nsINavHistoryService.idl
146 const char* query = "SELECT h.url, h.title, h.visit_count, "
147 "h.hidden, h.typed, v.visit_date "
148 "FROM moz_places h JOIN moz_historyvisits v "
149 "ON h.id = v.place_id "
150 "WHERE v.visit_type <= 3";
151
152 sql::Statement s(db.GetUniqueStatement(query));
153
154 std::vector<ImporterURLRow> rows;
155 while (s.Step() && !cancelled()) {
156 GURL url(s.ColumnString(0));
157
158 // Filter out unwanted URLs.
159 if (!CanImportURL(url))
160 continue;
161
162 ImporterURLRow row(url);
163 row.title = s.ColumnString16(1);
164 row.visit_count = s.ColumnInt(2);
165 row.hidden = s.ColumnInt(3) == 1;
166 row.typed_count = s.ColumnInt(4);
167 row.last_visit = base::Time::FromTimeT(s.ColumnInt64(5)/1000000);
168
169 rows.push_back(row);
170 }
171
172 if (!rows.empty() && !cancelled())
173 bridge_->SetHistoryItems(rows, importer::VISIT_SOURCE_FIREFOX_IMPORTED);
174 }
175
176 void Firefox3Importer::ImportBookmarks() {
177 base::FilePath file = source_path_.AppendASCII("places.sqlite");
178 if (!base::PathExists(file))
179 return;
180
181 sql::Connection db;
182 if (!db.Open(file))
183 return;
184
185 // Get the bookmark folders that we are interested in.
186 int toolbar_folder_id = -1;
187 int menu_folder_id = -1;
188 int unsorted_folder_id = -1;
189 LoadRootNodeID(&db, &toolbar_folder_id, &menu_folder_id, &unsorted_folder_id);
190
191 // Load livemark IDs.
192 std::set<int> livemark_id;
193 LoadLivemarkIDs(&db, &livemark_id);
194
195 // Load the default bookmarks.
196 std::set<GURL> default_urls;
197 LoadDefaultBookmarks(app_path_, &default_urls);
198
199 BookmarkList list;
200 GetTopBookmarkFolder(&db, toolbar_folder_id, &list);
201 GetTopBookmarkFolder(&db, menu_folder_id, &list);
202 GetTopBookmarkFolder(&db, unsorted_folder_id, &list);
203 size_t count = list.size();
204 for (size_t i = 0; i < count; ++i)
205 GetWholeBookmarkFolder(&db, &list, i, NULL);
206
207 std::vector<ImportedBookmarkEntry> bookmarks;
208 std::vector<importer::URLKeywordInfo> url_keywords;
209 FaviconMap favicon_map;
210
211 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based
212 // keywords yet. We won't include them in the list.
213 std::set<int> post_keyword_ids;
214 const char* query = "SELECT b.id FROM moz_bookmarks b "
215 "INNER JOIN moz_items_annos ia ON ia.item_id = b.id "
216 "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id "
217 "WHERE aa.name = 'bookmarkProperties/POSTData'";
218 sql::Statement s(db.GetUniqueStatement(query));
219
220 if (!s.is_valid())
221 return;
222
223 while (s.Step() && !cancelled())
224 post_keyword_ids.insert(s.ColumnInt(0));
225
226 for (size_t i = 0; i < list.size(); ++i) {
227 BookmarkItem* item = list[i];
228
229 if (item->type == TYPE_FOLDER) {
230 // Folders are added implicitly on adding children, so we only explicitly
231 // add empty folders.
232 if (!item->empty_folder)
233 continue;
234 } else if (item->type == TYPE_BOOKMARK) {
235 // Import only valid bookmarks
236 if (!CanImportURL(item->url))
237 continue;
238 } else {
239 continue;
240 }
241
242 // Skip the default bookmarks and unwanted URLs.
243 if (default_urls.find(item->url) != default_urls.end() ||
244 post_keyword_ids.find(item->id) != post_keyword_ids.end())
245 continue;
246
247 // Find the bookmark path by tracing their links to parent folders.
248 std::vector<string16> path;
249 BookmarkItem* child = item;
250 bool found_path = false;
251 bool is_in_toolbar = false;
252 while (child->parent >= 0) {
253 BookmarkItem* parent = list[child->parent];
254 if (livemark_id.find(parent->id) != livemark_id.end()) {
255 // Don't import live bookmarks.
256 break;
257 }
258
259 if (parent->id != menu_folder_id) {
260 // To avoid excessive nesting, omit the name for the bookmarks menu
261 // folder.
262 path.insert(path.begin(), parent->title);
263 }
264
265 if (parent->id == toolbar_folder_id)
266 is_in_toolbar = true;
267
268 if (parent->id == toolbar_folder_id ||
269 parent->id == menu_folder_id ||
270 parent->id == unsorted_folder_id) {
271 // We've reached a root node, hooray!
272 found_path = true;
273 break;
274 }
275
276 child = parent;
277 }
278
279 if (!found_path)
280 continue;
281
282 ImportedBookmarkEntry entry;
283 entry.creation_time = item->date_added;
284 entry.title = item->title;
285 entry.url = item->url;
286 entry.path = path;
287 entry.in_toolbar = is_in_toolbar;
288 entry.is_folder = item->type == TYPE_FOLDER;
289
290 bookmarks.push_back(entry);
291
292 if (item->type == TYPE_BOOKMARK) {
293 if (item->favicon)
294 favicon_map[item->favicon].insert(item->url);
295
296 // This bookmark has a keyword, we should import it.
297 if (!item->keyword.empty() && item->url.is_valid()) {
298 importer::URLKeywordInfo url_keyword_info;
299 url_keyword_info.url = item->url;
300 url_keyword_info.keyword.assign(UTF8ToUTF16(item->keyword));
301 url_keyword_info.display_name = item->title;
302 url_keywords.push_back(url_keyword_info);
303 }
304 }
305 }
306
307 STLDeleteElements(&list);
308
309 // Write into profile.
310 if (!bookmarks.empty() && !cancelled()) {
311 const string16& first_folder_name =
312 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX);
313 bridge_->AddBookmarks(bookmarks, first_folder_name);
314 }
315 if (!url_keywords.empty() && !cancelled()) {
316 bridge_->SetKeywords(url_keywords, false);
317 }
318 if (!favicon_map.empty() && !cancelled()) {
319 std::vector<ImportedFaviconUsage> favicons;
320 LoadFavicons(&db, favicon_map, &favicons);
321 bridge_->SetFavicons(favicons);
322 }
323 }
324
325 void Firefox3Importer::ImportPasswords() {
326 // Initializes NSS3.
327 NSSDecryptor decryptor;
328 if (!decryptor.Init(source_path_, source_path_) &&
329 !decryptor.Init(app_path_, source_path_)) {
330 return;
331 }
332
333 std::vector<content::PasswordForm> forms;
334 base::FilePath source_path = source_path_;
335 base::FilePath file = source_path.AppendASCII("signons.sqlite");
336 if (base::PathExists(file)) {
337 // Since Firefox 3.1, passwords are in signons.sqlite db.
338 decryptor.ReadAndParseSignons(file, &forms);
339 } else {
340 // Firefox 3.0 uses signons3.txt to store the passwords.
341 file = source_path.AppendASCII("signons3.txt");
342 if (!base::PathExists(file))
343 file = source_path.AppendASCII("signons2.txt");
344
345 std::string content;
346 file_util::ReadFileToString(file, &content);
347 decryptor.ParseSignons(content, &forms);
348 }
349
350 if (!cancelled()) {
351 for (size_t i = 0; i < forms.size(); ++i) {
352 bridge_->SetPasswordForm(forms[i]);
353 }
354 }
355 }
356
357 void Firefox3Importer::ImportSearchEngines() {
358 std::vector<std::string> search_engine_data;
359 GetSearchEnginesXMLData(&search_engine_data);
360
361 bridge_->SetFirefoxSearchEnginesXMLData(search_engine_data);
362 }
363
364 void Firefox3Importer::ImportHomepage() {
365 GURL home_page = GetHomepage(source_path_);
366 if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) {
367 bridge_->AddHomePage(home_page);
368 }
369 }
370
371 void Firefox3Importer::GetSearchEnginesXMLData(
372 std::vector<std::string>* search_engine_data) {
373 base::FilePath file = source_path_.AppendASCII("search.sqlite");
374 if (!base::PathExists(file))
375 return;
376
377 sql::Connection db;
378 if (!db.Open(file))
379 return;
380
381 const char* query = "SELECT engineid FROM engine_data "
382 "WHERE engineid NOT IN "
383 "(SELECT engineid FROM engine_data "
384 "WHERE name='hidden') "
385 "ORDER BY value ASC";
386
387 sql::Statement s(db.GetUniqueStatement(query));
388 if (!s.is_valid())
389 return;
390
391 base::FilePath app_path = app_path_.AppendASCII("searchplugins");
392 base::FilePath profile_path = source_path_.AppendASCII("searchplugins");
393
394 // Firefox doesn't store a search engine in its sqlite database unless the
395 // user has added a engine. So we get search engines from sqlite db as well
396 // as from the file system.
397 if (s.Step()) {
398 const std::string kAppPrefix("[app]/");
399 const std::string kProfilePrefix("[profile]/");
400 do {
401 base::FilePath file;
402 std::string engine(s.ColumnString(0));
403
404 // The string contains [app]/<name>.xml or [profile]/<name>.xml where
405 // the [app] and [profile] need to be replaced with the actual app or
406 // profile path.
407 size_t index = engine.find(kAppPrefix);
408 if (index != std::string::npos) {
409 // Remove '[app]/'.
410 file = app_path.AppendASCII(engine.substr(index + kAppPrefix.length()));
411 } else if ((index = engine.find(kProfilePrefix)) != std::string::npos) {
412 // Remove '[profile]/'.
413 file = profile_path.AppendASCII(
414 engine.substr(index + kProfilePrefix.length()));
415 } else {
416 // Looks like absolute path to the file.
417 file = base::FilePath::FromUTF8Unsafe(engine);
418 }
419 std::string file_data;
420 file_util::ReadFileToString(file, &file_data);
421 search_engine_data->push_back(file_data);
422 } while (s.Step() && !cancelled());
423 }
424
425 #if defined(OS_POSIX)
426 // Ubuntu-flavored Firefox3 supports locale-specific search engines via
427 // locale-named subdirectories. They fall back to en-US.
428 // See http://crbug.com/53899
429 // TODO(jshin): we need to make sure our locale code matches that of
430 // Firefox.
431 DCHECK(!locale_.empty());
432 base::FilePath locale_app_path = app_path.AppendASCII(locale_);
433 base::FilePath default_locale_app_path = app_path.AppendASCII("en-US");
434 if (file_util::DirectoryExists(locale_app_path))
435 app_path = locale_app_path;
436 else if (file_util::DirectoryExists(default_locale_app_path))
437 app_path = default_locale_app_path;
438 #endif
439
440 // Get search engine definition from file system.
441 base::FileEnumerator engines(app_path, false, base::FileEnumerator::FILES);
442 for (base::FilePath engine_path = engines.Next();
443 !engine_path.value().empty(); engine_path = engines.Next()) {
444 std::string file_data;
445 file_util::ReadFileToString(file, &file_data);
446 search_engine_data->push_back(file_data);
447 }
448 }
449
450 void Firefox3Importer::LoadRootNodeID(sql::Connection* db,
451 int* toolbar_folder_id,
452 int* menu_folder_id,
453 int* unsorted_folder_id) {
454 static const char* kToolbarFolderName = "toolbar";
455 static const char* kMenuFolderName = "menu";
456 static const char* kUnsortedFolderName = "unfiled";
457
458 const char* query = "SELECT root_name, folder_id FROM moz_bookmarks_roots";
459 sql::Statement s(db->GetUniqueStatement(query));
460
461 while (s.Step()) {
462 std::string folder = s.ColumnString(0);
463 int id = s.ColumnInt(1);
464 if (folder == kToolbarFolderName)
465 *toolbar_folder_id = id;
466 else if (folder == kMenuFolderName)
467 *menu_folder_id = id;
468 else if (folder == kUnsortedFolderName)
469 *unsorted_folder_id = id;
470 }
471 }
472
473 void Firefox3Importer::LoadLivemarkIDs(sql::Connection* db,
474 std::set<int>* livemark) {
475 static const char* kFeedAnnotation = "livemark/feedURI";
476 livemark->clear();
477
478 const char* query = "SELECT b.item_id "
479 "FROM moz_anno_attributes a "
480 "JOIN moz_items_annos b ON a.id = b.anno_attribute_id "
481 "WHERE a.name = ? ";
482 sql::Statement s(db->GetUniqueStatement(query));
483 s.BindString(0, kFeedAnnotation);
484
485 while (s.Step() && !cancelled())
486 livemark->insert(s.ColumnInt(0));
487 }
488
489 void Firefox3Importer::GetTopBookmarkFolder(sql::Connection* db,
490 int folder_id,
491 BookmarkList* list) {
492 const char* query = "SELECT b.title "
493 "FROM moz_bookmarks b "
494 "WHERE b.type = 2 AND b.id = ? "
495 "ORDER BY b.position";
496 sql::Statement s(db->GetUniqueStatement(query));
497 s.BindInt(0, folder_id);
498
499 if (s.Step()) {
500 BookmarkItem* item = new BookmarkItem;
501 item->parent = -1; // The top level folder has no parent.
502 item->id = folder_id;
503 item->title = s.ColumnString16(0);
504 item->type = TYPE_FOLDER;
505 item->favicon = 0;
506 item->empty_folder = true;
507 list->push_back(item);
508 }
509 }
510
511 void Firefox3Importer::GetWholeBookmarkFolder(sql::Connection* db,
512 BookmarkList* list,
513 size_t position,
514 bool* empty_folder) {
515 if (position >= list->size()) {
516 NOTREACHED();
517 return;
518 }
519
520 const char* query = "SELECT b.id, h.url, COALESCE(b.title, h.title), "
521 "b.type, k.keyword, b.dateAdded, h.favicon_id "
522 "FROM moz_bookmarks b "
523 "LEFT JOIN moz_places h ON b.fk = h.id "
524 "LEFT JOIN moz_keywords k ON k.id = b.keyword_id "
525 "WHERE b.type IN (1,2) AND b.parent = ? "
526 "ORDER BY b.position";
527 sql::Statement s(db->GetUniqueStatement(query));
528 s.BindInt(0, (*list)[position]->id);
529
530 BookmarkList temp_list;
531 while (s.Step()) {
532 BookmarkItem* item = new BookmarkItem;
533 item->parent = static_cast<int>(position);
534 item->id = s.ColumnInt(0);
535 item->url = GURL(s.ColumnString(1));
536 item->title = s.ColumnString16(2);
537 item->type = static_cast<BookmarkItemType>(s.ColumnInt(3));
538 item->keyword = s.ColumnString(4);
539 item->date_added = base::Time::FromTimeT(s.ColumnInt64(5)/1000000);
540 item->favicon = s.ColumnInt64(6);
541 item->empty_folder = true;
542
543 temp_list.push_back(item);
544 if (empty_folder != NULL)
545 *empty_folder = false;
546 }
547
548 // Appends all items to the list.
549 for (BookmarkList::iterator i = temp_list.begin();
550 i != temp_list.end(); ++i) {
551 list->push_back(*i);
552 // Recursive add bookmarks in sub-folders.
553 if ((*i)->type == TYPE_FOLDER)
554 GetWholeBookmarkFolder(db, list, list->size() - 1, &(*i)->empty_folder);
555 }
556 }
557
558 void Firefox3Importer::LoadFavicons(
559 sql::Connection* db,
560 const FaviconMap& favicon_map,
561 std::vector<ImportedFaviconUsage>* favicons) {
562 const char* query = "SELECT url, data FROM moz_favicons WHERE id=?";
563 sql::Statement s(db->GetUniqueStatement(query));
564
565 if (!s.is_valid())
566 return;
567
568 for (FaviconMap::const_iterator i = favicon_map.begin();
569 i != favicon_map.end(); ++i) {
570 s.BindInt64(0, i->first);
571 if (s.Step()) {
572 ImportedFaviconUsage usage;
573
574 usage.favicon_url = GURL(s.ColumnString(0));
575 if (!usage.favicon_url.is_valid())
576 continue; // Don't bother importing favicons with invalid URLs.
577
578 std::vector<unsigned char> data;
579 s.ColumnBlobAsVector(1, &data);
580 if (data.empty())
581 continue; // Data definitely invalid.
582
583 if (!ReencodeFavicon(&data[0], data.size(), &usage.png_data))
584 continue; // Unable to decode.
585
586 usage.urls = i->second;
587 favicons->push_back(usage);
588 }
589 s.Reset(true);
590 }
591 }
OLDNEW
« no previous file with comments | « chrome/browser/importer/firefox3_importer.h ('k') | chrome/browser/importer/firefox_importer_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698