Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / utility / importer / firefox_importer.cc
1 // Copyright 2013 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/utility/importer/firefox_importer.h"
6
7 #include <set>
8
9 #include "base/file_util.h"
10 #include "base/files/file_enumerator.h"
11 #include "base/json/json_file_value_serializer.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/common/importer/firefox_importer_utils.h"
18 #include "chrome/common/importer/firefox_importer_utils.h"
19 #include "chrome/common/importer/imported_bookmark_entry.h"
20 #include "chrome/common/importer/imported_favicon_usage.h"
21 #include "chrome/common/importer/importer_bridge.h"
22 #include "chrome/common/importer/importer_url_row.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "chrome/utility/importer/bookmark_html_reader.h"
25 #include "chrome/utility/importer/favicon_reencode.h"
26 #include "chrome/utility/importer/nss_decryptor.h"
27 #include "components/autofill/core/common/password_form.h"
28 #include "sql/connection.h"
29 #include "sql/statement.h"
30 #include "url/gurl.h"
31
32 namespace {
33
34 // Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/
35 //  components/places/public/nsINavBookmarksService.idl
36 enum BookmarkItemType {
37   TYPE_BOOKMARK = 1,
38   TYPE_FOLDER = 2,
39   TYPE_SEPARATOR = 3,
40   TYPE_DYNAMIC_CONTAINER = 4
41 };
42
43 // Loads the default bookmarks in the Firefox installed at |app_path|,
44 // and stores their locations in |urls|.
45 void LoadDefaultBookmarks(const base::FilePath& app_path,
46                           std::set<GURL>* urls) {
47   base::FilePath file = app_path.AppendASCII("defaults")
48       .AppendASCII("profile")
49       .AppendASCII("bookmarks.html");
50   urls->clear();
51
52   std::vector<ImportedBookmarkEntry> bookmarks;
53   bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(),
54                                             base::Callback<bool(const GURL&)>(),
55                                             file,
56                                             &bookmarks,
57                                             NULL);
58   for (size_t i = 0; i < bookmarks.size(); ++i)
59     urls->insert(bookmarks[i].url);
60 }
61
62 // Returns true if |url| has a valid scheme that we allow to import. We
63 // filter out the URL with a unsupported scheme.
64 bool CanImportURL(const GURL& url) {
65   // The URL is not valid.
66   if (!url.is_valid())
67     return false;
68
69   // Filter out the URLs with unsupported schemes.
70   const char* const kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"};
71   for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) {
72     if (url.SchemeIs(kInvalidSchemes[i]))
73       return false;
74   }
75
76   return true;
77 }
78
79 }  // namespace
80
81 struct FirefoxImporter::BookmarkItem {
82   int parent;
83   int id;
84   GURL url;
85   base::string16 title;
86   BookmarkItemType type;
87   std::string keyword;
88   base::Time date_added;
89   int64 favicon;
90   bool empty_folder;
91 };
92
93 FirefoxImporter::FirefoxImporter() {
94 }
95
96 FirefoxImporter::~FirefoxImporter() {
97 }
98
99 void FirefoxImporter::StartImport(
100     const importer::SourceProfile& source_profile,
101     uint16 items,
102     ImporterBridge* bridge) {
103   bridge_ = bridge;
104   source_path_ = source_profile.source_path;
105   app_path_ = source_profile.app_path;
106
107 #if defined(OS_POSIX)
108   locale_ = source_profile.locale;
109 #endif
110
111   // The order here is important!
112   bridge_->NotifyStarted();
113   if ((items & importer::HOME_PAGE) && !cancelled()) {
114     bridge_->NotifyItemStarted(importer::HOME_PAGE);
115     ImportHomepage();  // Doesn't have a UI item.
116     bridge_->NotifyItemEnded(importer::HOME_PAGE);
117   }
118
119   // Note history should be imported before bookmarks because bookmark import
120   // will also import favicons and we store favicon for a URL only if the URL
121   // exist in history or bookmarks.
122   if ((items & importer::HISTORY) && !cancelled()) {
123     bridge_->NotifyItemStarted(importer::HISTORY);
124     ImportHistory();
125     bridge_->NotifyItemEnded(importer::HISTORY);
126   }
127
128   if ((items & importer::FAVORITES) && !cancelled()) {
129     bridge_->NotifyItemStarted(importer::FAVORITES);
130     ImportBookmarks();
131     bridge_->NotifyItemEnded(importer::FAVORITES);
132   }
133   if ((items & importer::SEARCH_ENGINES) && !cancelled()) {
134     bridge_->NotifyItemStarted(importer::SEARCH_ENGINES);
135     ImportSearchEngines();
136     bridge_->NotifyItemEnded(importer::SEARCH_ENGINES);
137   }
138   if ((items & importer::PASSWORDS) && !cancelled()) {
139     bridge_->NotifyItemStarted(importer::PASSWORDS);
140     ImportPasswords();
141     bridge_->NotifyItemEnded(importer::PASSWORDS);
142   }
143   bridge_->NotifyEnded();
144 }
145
146 void FirefoxImporter::ImportHistory() {
147   base::FilePath file = source_path_.AppendASCII("places.sqlite");
148   if (!base::PathExists(file))
149     return;
150
151   sql::Connection db;
152   if (!db.Open(file))
153     return;
154
155   // |visit_type| represent the transition type of URLs (typed, click,
156   // redirect, bookmark, etc.) We eliminate some URLs like sub-frames and
157   // redirects, since we don't want them to appear in history.
158   // Firefox transition types are defined in:
159   //   toolkit/components/places/public/nsINavHistoryService.idl
160   const char* query = "SELECT h.url, h.title, h.visit_count, "
161                       "h.hidden, h.typed, v.visit_date "
162                       "FROM moz_places h JOIN moz_historyvisits v "
163                       "ON h.id = v.place_id "
164                       "WHERE v.visit_type <= 3";
165
166   sql::Statement s(db.GetUniqueStatement(query));
167
168   std::vector<ImporterURLRow> rows;
169   while (s.Step() && !cancelled()) {
170     GURL url(s.ColumnString(0));
171
172     // Filter out unwanted URLs.
173     if (!CanImportURL(url))
174       continue;
175
176     ImporterURLRow row(url);
177     row.title = s.ColumnString16(1);
178     row.visit_count = s.ColumnInt(2);
179     row.hidden = s.ColumnInt(3) == 1;
180     row.typed_count = s.ColumnInt(4);
181     row.last_visit = base::Time::FromTimeT(s.ColumnInt64(5)/1000000);
182
183     rows.push_back(row);
184   }
185
186   if (!rows.empty() && !cancelled())
187     bridge_->SetHistoryItems(rows, importer::VISIT_SOURCE_FIREFOX_IMPORTED);
188 }
189
190 void FirefoxImporter::ImportBookmarks() {
191   base::FilePath file = source_path_.AppendASCII("places.sqlite");
192   if (!base::PathExists(file))
193     return;
194
195   sql::Connection db;
196   if (!db.Open(file))
197     return;
198
199   // Get the bookmark folders that we are interested in.
200   int toolbar_folder_id = -1;
201   int menu_folder_id = -1;
202   int unsorted_folder_id = -1;
203   LoadRootNodeID(&db, &toolbar_folder_id, &menu_folder_id, &unsorted_folder_id);
204
205   // Load livemark IDs.
206   std::set<int> livemark_id;
207   LoadLivemarkIDs(&db, &livemark_id);
208
209   // Load the default bookmarks.
210   std::set<GURL> default_urls;
211   LoadDefaultBookmarks(app_path_, &default_urls);
212
213   BookmarkList list;
214   GetTopBookmarkFolder(&db, toolbar_folder_id, &list);
215   GetTopBookmarkFolder(&db, menu_folder_id, &list);
216   GetTopBookmarkFolder(&db, unsorted_folder_id, &list);
217   size_t count = list.size();
218   for (size_t i = 0; i < count; ++i)
219     GetWholeBookmarkFolder(&db, &list, i, NULL);
220
221   std::vector<ImportedBookmarkEntry> bookmarks;
222   std::vector<importer::URLKeywordInfo> url_keywords;
223   FaviconMap favicon_map;
224
225   // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based
226   //                keywords yet.  We won't include them in the list.
227   std::set<int> post_keyword_ids;
228   const char* query = "SELECT b.id FROM moz_bookmarks b "
229       "INNER JOIN moz_items_annos ia ON ia.item_id = b.id "
230       "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id "
231       "WHERE aa.name = 'bookmarkProperties/POSTData'";
232   sql::Statement s(db.GetUniqueStatement(query));
233
234   if (!s.is_valid())
235     return;
236
237   while (s.Step() && !cancelled())
238     post_keyword_ids.insert(s.ColumnInt(0));
239
240   for (size_t i = 0; i < list.size(); ++i) {
241     BookmarkItem* item = list[i];
242
243     if (item->type == TYPE_FOLDER) {
244       // Folders are added implicitly on adding children, so we only explicitly
245       // add empty folders.
246       if (!item->empty_folder)
247         continue;
248     } else if (item->type == TYPE_BOOKMARK) {
249       // Import only valid bookmarks
250       if (!CanImportURL(item->url))
251         continue;
252     } else {
253       continue;
254     }
255
256     // Skip the default bookmarks and unwanted URLs.
257     if (default_urls.find(item->url) != default_urls.end() ||
258         post_keyword_ids.find(item->id) != post_keyword_ids.end())
259       continue;
260
261     // Find the bookmark path by tracing their links to parent folders.
262     std::vector<base::string16> path;
263     BookmarkItem* child = item;
264     bool found_path = false;
265     bool is_in_toolbar = false;
266     while (child->parent >= 0) {
267       BookmarkItem* parent = list[child->parent];
268       if (livemark_id.find(parent->id) != livemark_id.end()) {
269         // Don't import live bookmarks.
270         break;
271       }
272
273       if (parent->id != menu_folder_id) {
274         // To avoid excessive nesting, omit the name for the bookmarks menu
275         // folder.
276         path.insert(path.begin(), parent->title);
277       }
278
279       if (parent->id == toolbar_folder_id)
280         is_in_toolbar = true;
281
282       if (parent->id == toolbar_folder_id ||
283           parent->id == menu_folder_id ||
284           parent->id == unsorted_folder_id) {
285         // We've reached a root node, hooray!
286         found_path = true;
287         break;
288       }
289
290       child = parent;
291     }
292
293     if (!found_path)
294       continue;
295
296     ImportedBookmarkEntry entry;
297     entry.creation_time = item->date_added;
298     entry.title = item->title;
299     entry.url = item->url;
300     entry.path = path;
301     entry.in_toolbar = is_in_toolbar;
302     entry.is_folder = item->type == TYPE_FOLDER;
303
304     bookmarks.push_back(entry);
305
306     if (item->type == TYPE_BOOKMARK) {
307       if (item->favicon)
308         favicon_map[item->favicon].insert(item->url);
309
310       // This bookmark has a keyword, we should import it.
311       if (!item->keyword.empty() && item->url.is_valid()) {
312         importer::URLKeywordInfo url_keyword_info;
313         url_keyword_info.url = item->url;
314         url_keyword_info.keyword.assign(base::UTF8ToUTF16(item->keyword));
315         url_keyword_info.display_name = item->title;
316         url_keywords.push_back(url_keyword_info);
317       }
318     }
319   }
320
321   STLDeleteElements(&list);
322
323   // Write into profile.
324   if (!bookmarks.empty() && !cancelled()) {
325     const base::string16& first_folder_name =
326         bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX);
327     bridge_->AddBookmarks(bookmarks, first_folder_name);
328   }
329   if (!url_keywords.empty() && !cancelled()) {
330     bridge_->SetKeywords(url_keywords, false);
331   }
332   if (!favicon_map.empty() && !cancelled()) {
333     std::vector<ImportedFaviconUsage> favicons;
334     LoadFavicons(&db, favicon_map, &favicons);
335     bridge_->SetFavicons(favicons);
336   }
337 }
338
339 void FirefoxImporter::ImportPasswords() {
340   // Initializes NSS3.
341   NSSDecryptor decryptor;
342   if (!decryptor.Init(source_path_, source_path_) &&
343       !decryptor.Init(app_path_, source_path_)) {
344     return;
345   }
346
347   std::vector<autofill::PasswordForm> forms;
348   base::FilePath source_path = source_path_;
349   base::FilePath file = source_path.AppendASCII("signons.sqlite");
350   if (base::PathExists(file)) {
351     // Since Firefox 3.1, passwords are in signons.sqlite db.
352     decryptor.ReadAndParseSignons(file, &forms);
353   } else {
354     // Firefox 3.0 uses signons3.txt to store the passwords.
355     file = source_path.AppendASCII("signons3.txt");
356     if (!base::PathExists(file))
357       file = source_path.AppendASCII("signons2.txt");
358
359     std::string content;
360     base::ReadFileToString(file, &content);
361     decryptor.ParseSignons(content, &forms);
362   }
363
364   if (!cancelled()) {
365     for (size_t i = 0; i < forms.size(); ++i) {
366       bridge_->SetPasswordForm(forms[i]);
367     }
368   }
369 }
370
371 void FirefoxImporter::ImportSearchEngines() {
372   std::vector<std::string> search_engine_data;
373   GetSearchEnginesXMLData(&search_engine_data);
374
375   bridge_->SetFirefoxSearchEnginesXMLData(search_engine_data);
376 }
377
378 void FirefoxImporter::ImportHomepage() {
379   GURL home_page = GetHomepage(source_path_);
380   if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) {
381     bridge_->AddHomePage(home_page);
382   }
383 }
384
385 void FirefoxImporter::GetSearchEnginesXMLData(
386     std::vector<std::string>* search_engine_data) {
387   base::FilePath file = source_path_.AppendASCII("search.sqlite");
388   if (!base::PathExists(file)) {
389     // Since Firefox 3.5, search engines are no longer stored in search.sqlite.
390     // Instead, search.json is used for storing search engines.
391     GetSearchEnginesXMLDataFromJSON(search_engine_data);
392     return;
393   }
394
395   sql::Connection db;
396   if (!db.Open(file))
397     return;
398
399   const char* query = "SELECT engineid FROM engine_data "
400                       "WHERE engineid NOT IN "
401                       "(SELECT engineid FROM engine_data "
402                       "WHERE name='hidden') "
403                       "ORDER BY value ASC";
404
405   sql::Statement s(db.GetUniqueStatement(query));
406   if (!s.is_valid())
407     return;
408
409   const base::FilePath searchplugins_path(FILE_PATH_LITERAL("searchplugins"));
410   // Search engine definitions are XMLs stored in two directories. Default
411   // engines are in the app directory (app_path_) and custom engines are
412   // in the profile directory (source_path_).
413
414   // Since Firefox 21, app_path_ engines are in 'browser' subdirectory:
415   base::FilePath app_path =
416       app_path_.AppendASCII("browser").Append(searchplugins_path);
417   if (!base::PathExists(app_path)) {
418     // This might be an older Firefox, try old location without the 'browser'
419     // path component:
420     app_path = app_path_.Append(searchplugins_path);
421   }
422
423   base::FilePath profile_path = source_path_.Append(searchplugins_path);
424
425   // Firefox doesn't store a search engine in its sqlite database unless the
426   // user has added a engine. So we get search engines from sqlite db as well
427   // as from the file system.
428   if (s.Step()) {
429     const std::string kAppPrefix("[app]/");
430     const std::string kProfilePrefix("[profile]/");
431     do {
432       base::FilePath file;
433       std::string engine(s.ColumnString(0));
434
435       // The string contains [app]/<name>.xml or [profile]/<name>.xml where
436       // the [app] and [profile] need to be replaced with the actual app or
437       // profile path.
438       size_t index = engine.find(kAppPrefix);
439       if (index != std::string::npos) {
440         // Remove '[app]/'.
441         file = app_path.AppendASCII(engine.substr(index + kAppPrefix.length()));
442       } else if ((index = engine.find(kProfilePrefix)) != std::string::npos) {
443         // Remove '[profile]/'.
444           file = profile_path.AppendASCII(
445               engine.substr(index + kProfilePrefix.length()));
446       } else {
447         // Looks like absolute path to the file.
448         file = base::FilePath::FromUTF8Unsafe(engine);
449       }
450       std::string file_data;
451       base::ReadFileToString(file, &file_data);
452       search_engine_data->push_back(file_data);
453     } while (s.Step() && !cancelled());
454   }
455
456 #if defined(OS_POSIX)
457   // Ubuntu-flavored Firefox supports locale-specific search engines via
458   // locale-named subdirectories. They fall back to en-US.
459   // See http://crbug.com/53899
460   // TODO(jshin): we need to make sure our locale code matches that of
461   // Firefox.
462   DCHECK(!locale_.empty());
463   base::FilePath locale_app_path = app_path.AppendASCII(locale_);
464   base::FilePath default_locale_app_path = app_path.AppendASCII("en-US");
465   if (base::DirectoryExists(locale_app_path))
466     app_path = locale_app_path;
467   else if (base::DirectoryExists(default_locale_app_path))
468     app_path = default_locale_app_path;
469 #endif
470
471   // Get search engine definition from file system.
472   base::FileEnumerator engines(app_path, false, base::FileEnumerator::FILES);
473   for (base::FilePath engine_path = engines.Next();
474        !engine_path.value().empty(); engine_path = engines.Next()) {
475     std::string file_data;
476     base::ReadFileToString(file, &file_data);
477     search_engine_data->push_back(file_data);
478   }
479 }
480
481 void FirefoxImporter::GetSearchEnginesXMLDataFromJSON(
482     std::vector<std::string>* search_engine_data) {
483   base::FilePath search_json_file = source_path_.AppendASCII("search.json");
484   if (!base::PathExists(search_json_file))
485     return;
486
487   JSONFileValueSerializer serializer(search_json_file);
488   scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
489   const base::DictionaryValue* search_root = NULL;
490   if (!root || !root->GetAsDictionary(&search_root))
491     return;
492
493   const std::string kDirectories("directories");
494   const base::DictionaryValue* search_directories = NULL;
495   if (!search_root->GetDictionary(kDirectories, &search_directories))
496     return;
497
498   // Dictionary |search_directories| contains a list of search engines
499   // (default and installed). The list can be found from key <engines>
500   // of the dictionary. Key <engines> is a grandchild of key <directories>.
501   // However, key <engines> parent's key is dynamic which depends on
502   // operating systems. For example,
503   //   Ubuntu (for default search engine):
504   //     /usr/lib/firefox/distribution/searchplugins/locale/en-US
505   //   Ubuntu (for installed search engines):
506   //     /home/<username>/.mozilla/firefox/lcd50n4n.default/searchplugins
507   //   Windows (for default search engine):
508   //     C:\\Program Files (x86)\\Mozilla Firefox\\browser\\searchplugins
509   // Therefore, it needs to be retrieved by searching.
510
511   for (base::DictionaryValue::Iterator it(*search_directories); !it.IsAtEnd();
512        it.Advance()) {
513     // The key of |it| may contains dot (.) which cannot be used as <key>
514     // for retrieving <engines>. Hence, it is needed to get |it| as dictionary.
515     // The resulted dictionary can be used for retrieving <engines>.
516     const std::string kEngines("engines");
517     const base::DictionaryValue* search_directory = NULL;
518     if (!it.value().GetAsDictionary(&search_directory))
519       continue;
520
521     const base::ListValue* search_engines = NULL;
522     if (!search_directory->GetList(kEngines, &search_engines))
523       continue;
524
525     const std::string kFilePath("filePath");
526     const std::string kHidden("_hidden");
527     for (size_t i = 0; i < search_engines->GetSize(); ++i) {
528       const base::DictionaryValue* engine_info = NULL;
529       if (!search_engines->GetDictionary(i, &engine_info))
530         continue;
531
532       bool is_hidden = false;
533       std::string file_path;
534       if (!engine_info->GetBoolean(kHidden, &is_hidden) ||
535           !engine_info->GetString(kFilePath, &file_path))
536         continue;
537
538       if (!is_hidden) {
539         const std::string kAppPrefix("[app]/");
540         const std::string kProfilePrefix("[profile]/");
541         base::FilePath xml_file = base::FilePath::FromUTF8Unsafe(file_path);
542
543         // If |file_path| contains [app] or [profile] then they need to be
544         // replaced with the actual app or profile path.
545         size_t index = file_path.find(kAppPrefix);
546         if (index != std::string::npos) {
547           // Replace '[app]/' with actual app path.
548           xml_file = app_path_.AppendASCII("searchplugins").AppendASCII(
549               file_path.substr(index + kAppPrefix.length()));
550         } else if ((index = file_path.find(kProfilePrefix)) !=
551                    std::string::npos) {
552           // Replace '[profile]/' with actual profile path.
553           xml_file = source_path_.AppendASCII("searchplugins").AppendASCII(
554               file_path.substr(index + kProfilePrefix.length()));
555         }
556
557         std::string file_data;
558         base::ReadFileToString(xml_file, &file_data);
559         search_engine_data->push_back(file_data);
560       }
561     }
562   }
563 }
564
565 void FirefoxImporter::LoadRootNodeID(sql::Connection* db,
566                                       int* toolbar_folder_id,
567                                       int* menu_folder_id,
568                                       int* unsorted_folder_id) {
569   static const char* kToolbarFolderName = "toolbar";
570   static const char* kMenuFolderName = "menu";
571   static const char* kUnsortedFolderName = "unfiled";
572
573   const char* query = "SELECT root_name, folder_id FROM moz_bookmarks_roots";
574   sql::Statement s(db->GetUniqueStatement(query));
575
576   while (s.Step()) {
577     std::string folder = s.ColumnString(0);
578     int id = s.ColumnInt(1);
579     if (folder == kToolbarFolderName)
580       *toolbar_folder_id = id;
581     else if (folder == kMenuFolderName)
582       *menu_folder_id = id;
583     else if (folder == kUnsortedFolderName)
584       *unsorted_folder_id = id;
585   }
586 }
587
588 void FirefoxImporter::LoadLivemarkIDs(sql::Connection* db,
589                                        std::set<int>* livemark) {
590   static const char* kFeedAnnotation = "livemark/feedURI";
591   livemark->clear();
592
593   const char* query = "SELECT b.item_id "
594                       "FROM moz_anno_attributes a "
595                       "JOIN moz_items_annos b ON a.id = b.anno_attribute_id "
596                       "WHERE a.name = ? ";
597   sql::Statement s(db->GetUniqueStatement(query));
598   s.BindString(0, kFeedAnnotation);
599
600   while (s.Step() && !cancelled())
601     livemark->insert(s.ColumnInt(0));
602 }
603
604 void FirefoxImporter::GetTopBookmarkFolder(sql::Connection* db,
605                                             int folder_id,
606                                             BookmarkList* list) {
607   const char* query = "SELECT b.title "
608                      "FROM moz_bookmarks b "
609                      "WHERE b.type = 2 AND b.id = ? "
610                      "ORDER BY b.position";
611   sql::Statement s(db->GetUniqueStatement(query));
612   s.BindInt(0, folder_id);
613
614   if (s.Step()) {
615     BookmarkItem* item = new BookmarkItem;
616     item->parent = -1;  // The top level folder has no parent.
617     item->id = folder_id;
618     item->title = s.ColumnString16(0);
619     item->type = TYPE_FOLDER;
620     item->favicon = 0;
621     item->empty_folder = true;
622     list->push_back(item);
623   }
624 }
625
626 void FirefoxImporter::GetWholeBookmarkFolder(sql::Connection* db,
627                                               BookmarkList* list,
628                                               size_t position,
629                                               bool* empty_folder) {
630   if (position >= list->size()) {
631     NOTREACHED();
632     return;
633   }
634
635   const char* query = "SELECT b.id, h.url, COALESCE(b.title, h.title), "
636          "b.type, k.keyword, b.dateAdded, h.favicon_id "
637          "FROM moz_bookmarks b "
638          "LEFT JOIN moz_places h ON b.fk = h.id "
639          "LEFT JOIN moz_keywords k ON k.id = b.keyword_id "
640          "WHERE b.type IN (1,2) AND b.parent = ? "
641          "ORDER BY b.position";
642   sql::Statement s(db->GetUniqueStatement(query));
643   s.BindInt(0, (*list)[position]->id);
644
645   BookmarkList temp_list;
646   while (s.Step()) {
647     BookmarkItem* item = new BookmarkItem;
648     item->parent = static_cast<int>(position);
649     item->id = s.ColumnInt(0);
650     item->url = GURL(s.ColumnString(1));
651     item->title = s.ColumnString16(2);
652     item->type = static_cast<BookmarkItemType>(s.ColumnInt(3));
653     item->keyword = s.ColumnString(4);
654     item->date_added = base::Time::FromTimeT(s.ColumnInt64(5)/1000000);
655     item->favicon = s.ColumnInt64(6);
656     item->empty_folder = true;
657
658     temp_list.push_back(item);
659     if (empty_folder != NULL)
660       *empty_folder = false;
661   }
662
663   // Appends all items to the list.
664   for (BookmarkList::iterator i = temp_list.begin();
665        i != temp_list.end(); ++i) {
666     list->push_back(*i);
667     // Recursive add bookmarks in sub-folders.
668     if ((*i)->type == TYPE_FOLDER)
669       GetWholeBookmarkFolder(db, list, list->size() - 1, &(*i)->empty_folder);
670   }
671 }
672
673 void FirefoxImporter::LoadFavicons(
674     sql::Connection* db,
675     const FaviconMap& favicon_map,
676     std::vector<ImportedFaviconUsage>* favicons) {
677   const char* query = "SELECT url, data FROM moz_favicons WHERE id=?";
678   sql::Statement s(db->GetUniqueStatement(query));
679
680   if (!s.is_valid())
681     return;
682
683   for (FaviconMap::const_iterator i = favicon_map.begin();
684        i != favicon_map.end(); ++i) {
685     s.BindInt64(0, i->first);
686     if (s.Step()) {
687       ImportedFaviconUsage usage;
688
689       usage.favicon_url = GURL(s.ColumnString(0));
690       if (!usage.favicon_url.is_valid())
691         continue;  // Don't bother importing favicons with invalid URLs.
692
693       std::vector<unsigned char> data;
694       s.ColumnBlobAsVector(1, &data);
695       if (data.empty())
696         continue;  // Data definitely invalid.
697
698       if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data))
699         continue;  // Unable to decode.
700
701       usage.urls = i->second;
702       favicons->push_back(usage);
703     }
704     s.Reset(true);
705   }
706 }