blob: 75c426a226231bafb330e130eaf2aef678fe4d11 [file] [log] [blame]
[email protected]a4d9b5a2011-03-05 01:23:011// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]6c9851a2008-09-13 00:56:275#include "chrome/browser/importer/firefox3_importer.h"
initial.commit09911bf2008-07-26 23:55:296
7#include <set>
8
9#include "base/file_util.h"
[email protected]e74b81d2009-07-20 22:18:4210#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/scoped_ptr.h"
[email protected]e74b81d2009-07-20 22:18:4212#include "base/stl_util-inl.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/string_util.h"
[email protected]c02c853d72010-08-07 06:23:2414#include "base/values.h"
[email protected]813ed7502010-09-01 22:58:2415#include "chrome/browser/browser_process.h"
[email protected]9c92d192009-12-02 08:03:1616#include "chrome/browser/history/history_types.h"
[email protected]6c9851a2008-09-13 00:56:2717#include "chrome/browser/importer/firefox2_importer.h"
18#include "chrome/browser/importer/firefox_importer_utils.h"
[email protected]f6061aed2009-10-06 16:28:5719#include "chrome/browser/importer/importer_bridge.h"
[email protected]89cfc19d2010-06-04 15:36:3520#include "chrome/browser/importer/importer_data_types.h"
[email protected]a1f55c87f2009-07-22 00:53:4821#include "chrome/browser/importer/nss_decryptor.h"
[email protected]e74b81d2009-07-20 22:18:4222#include "chrome/browser/search_engines/template_url.h"
[email protected]e74b81d2009-07-20 22:18:4223#include "chrome/common/sqlite_utils.h"
[email protected]a4d9b5a2011-03-05 01:23:0124#include "chrome/common/time_format.h"
[email protected]34ac8f32009-02-22 23:03:2725#include "grit/generated_resources.h"
[email protected]e74b81d2009-07-20 22:18:4226#include "webkit/glue/password_form.h"
initial.commit09911bf2008-07-26 23:55:2927
[email protected]05402122011-01-28 06:48:5928// Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/
29// components/places/public/nsINavBookmarksService.idl
30enum BookmarkItemType {
31 TYPE_BOOKMARK = 1,
32 TYPE_FOLDER = 2,
33 TYPE_SEPARATOR = 3,
34 TYPE_DYNAMIC_CONTAINER = 4
35};
36
[email protected]93aa89c72010-10-20 21:32:0437struct Firefox3Importer::BookmarkItem {
38 int parent;
39 int id;
40 GURL url;
41 std::wstring title;
[email protected]05402122011-01-28 06:48:5942 BookmarkItemType type;
[email protected]93aa89c72010-10-20 21:32:0443 std::string keyword;
44 base::Time date_added;
45 int64 favicon;
[email protected]05402122011-01-28 06:48:5946 bool empty_folder;
[email protected]93aa89c72010-10-20 21:32:0447};
48
[email protected]813ed7502010-09-01 22:58:2449Firefox3Importer::Firefox3Importer() {
[email protected]33a6db372010-09-07 23:58:4950#if defined(OS_LINUX)
[email protected]0aa26f4b2010-10-10 05:00:5151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]813ed7502010-09-01 22:58:2452 locale_ = g_browser_process->GetApplicationLocale();
[email protected]33a6db372010-09-07 23:58:4953#endif
[email protected]813ed7502010-09-01 22:58:2454}
55
56Firefox3Importer::~Firefox3Importer() {
57}
58
[email protected]aad8e8f2010-11-09 02:04:1759void Firefox3Importer::StartImport(const importer::ProfileInfo& profile_info,
[email protected]f6061aed2009-10-06 16:28:5760 uint16 items,
61 ImporterBridge* bridge) {
[email protected]33a6db372010-09-07 23:58:4962#if defined(OS_LINUX)
[email protected]0aa26f4b2010-10-10 05:00:5163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]33a6db372010-09-07 23:58:4964#endif
[email protected]f6061aed2009-10-06 16:28:5765 bridge_ = bridge;
[email protected]ca0a0ca2010-05-15 14:31:1466 source_path_ = profile_info.source_path;
67 app_path_ = profile_info.app_path;
initial.commit09911bf2008-07-26 23:55:2968
initial.commit09911bf2008-07-26 23:55:2969 // The order here is important!
[email protected]f6061aed2009-10-06 16:28:5770 bridge_->NotifyStarted();
[email protected]89cfc19d2010-06-04 15:36:3571 if ((items & importer::HOME_PAGE) && !cancelled())
initial.commit09911bf2008-07-26 23:55:2972 ImportHomepage(); // Doesn't have a UI item.
[email protected]67cb4c82009-09-11 21:08:5373
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]89cfc19d2010-06-04 15:36:3577 if ((items & importer::HISTORY) && !cancelled()) {
78 bridge_->NotifyItemStarted(importer::HISTORY);
[email protected]67cb4c82009-09-11 21:08:5379 ImportHistory();
[email protected]89cfc19d2010-06-04 15:36:3580 bridge_->NotifyItemEnded(importer::HISTORY);
[email protected]67cb4c82009-09-11 21:08:5381 }
82
[email protected]89cfc19d2010-06-04 15:36:3583 if ((items & importer::FAVORITES) && !cancelled()) {
84 bridge_->NotifyItemStarted(importer::FAVORITES);
initial.commit09911bf2008-07-26 23:55:2985 ImportBookmarks();
[email protected]89cfc19d2010-06-04 15:36:3586 bridge_->NotifyItemEnded(importer::FAVORITES);
initial.commit09911bf2008-07-26 23:55:2987 }
[email protected]89cfc19d2010-06-04 15:36:3588 if ((items & importer::SEARCH_ENGINES) && !cancelled()) {
89 bridge_->NotifyItemStarted(importer::SEARCH_ENGINES);
initial.commit09911bf2008-07-26 23:55:2990 ImportSearchEngines();
[email protected]89cfc19d2010-06-04 15:36:3591 bridge_->NotifyItemEnded(importer::SEARCH_ENGINES);
initial.commit09911bf2008-07-26 23:55:2992 }
[email protected]89cfc19d2010-06-04 15:36:3593 if ((items & importer::PASSWORDS) && !cancelled()) {
94 bridge_->NotifyItemStarted(importer::PASSWORDS);
initial.commit09911bf2008-07-26 23:55:2995 ImportPasswords();
[email protected]89cfc19d2010-06-04 15:36:3596 bridge_->NotifyItemEnded(importer::PASSWORDS);
initial.commit09911bf2008-07-26 23:55:2997 }
[email protected]f6061aed2009-10-06 16:28:5798 bridge_->NotifyEnded();
initial.commit09911bf2008-07-26 23:55:2999}
100
101void Firefox3Importer::ImportHistory() {
[email protected]750d1df02010-02-22 18:05:00102 FilePath file = source_path_.AppendASCII("places.sqlite");
103 if (!file_util::PathExists(file))
initial.commit09911bf2008-07-26 23:55:29104 return;
105
106 sqlite3* sqlite;
[email protected]91243c32011-01-28 18:27:17107 if (sqlite_utils::OpenSqliteDb(file, &sqlite) != SQLITE_OK)
initial.commit09911bf2008-07-26 23:55:29108 return;
[email protected]06a793f82009-08-05 23:50:35109 sqlite_utils::scoped_sqlite_db_ptr db(sqlite);
initial.commit09911bf2008-07-26 23:55:29110
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]e53668962010-06-23 15:35:25135 row.set_title(UTF8ToUTF16(s.column_string(1)));
initial.commit09911bf2008-07-26 23:55:29136 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]a4d9b5a2011-03-05 01:23:01139 row.set_last_visit(base::Time::FromTimeT(s.column_int64(5)/1000000));
initial.commit09911bf2008-07-26 23:55:29140
141 rows.push_back(row);
142 }
143 if (!rows.empty() && !cancelled()) {
[email protected]5989f1a2010-08-21 02:21:11144 bridge_->SetHistoryItems(rows, history::SOURCE_FIREFOX_IMPORTED);
initial.commit09911bf2008-07-26 23:55:29145 }
146}
147
148void Firefox3Importer::ImportBookmarks() {
[email protected]750d1df02010-02-22 18:05:00149 FilePath file = source_path_.AppendASCII("places.sqlite");
150 if (!file_util::PathExists(file))
initial.commit09911bf2008-07-26 23:55:29151 return;
152
153 sqlite3* sqlite;
[email protected]91243c32011-01-28 18:27:17154 if (sqlite_utils::OpenSqliteDb(file, &sqlite) != SQLITE_OK)
initial.commit09911bf2008-07-26 23:55:29155 return;
[email protected]06a793f82009-08-05 23:50:35156 sqlite_utils::scoped_sqlite_db_ptr db(sqlite);
initial.commit09911bf2008-07-26 23:55:29157
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]05402122011-01-28 06:48:59179 GetWholeBookmarkFolder(db.get(), &list, i, NULL);
initial.commit09911bf2008-07-26 23:55:29180
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]b665bf22010-09-10 22:33:58198 return;
initial.commit09911bf2008-07-26 23:55:29199 }
200
201 std::wstring firefox_folder =
[email protected]89cfc19d2010-06-04 15:36:35202 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX);
initial.commit09911bf2008-07-26 23:55:29203 for (size_t i = 0; i < list.size(); ++i) {
204 BookmarkItem* item = list[i];
205
[email protected]05402122011-01-28 06:48:59206 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.commit09911bf2008-07-26 23:55:29216 continue;
[email protected]05402122011-01-28 06:48:59217 }
initial.commit09911bf2008-07-26 23:55:29218
219 // Skip the default bookmarks and unwanted URLs.
[email protected]05402122011-01-28 06:48:59220 if (default_urls.find(item->url) != default_urls.end() ||
initial.commit09911bf2008-07-26 23:55:29221 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]0072d872009-06-18 21:14:34235 if (import_to_bookmark_bar()) {
initial.commit09911bf2008-07-26 23:55:29236 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]0072d872009-06-18 21:14:34247 if (!import_to_bookmark_bar())
initial.commit09911bf2008-07-26 23:55:29248 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]05402122011-01-28 06:48:59268 entry.is_folder = item->type == TYPE_FOLDER;
initial.commit09911bf2008-07-26 23:55:29269
270 bookmarks.push_back(entry);
271
[email protected]df8c99a2011-02-28 20:45:50272 if (item->type == TYPE_BOOKMARK) {
273 if (item->favicon)
274 favicon_map[item->favicon].insert(item->url);
initial.commit09911bf2008-07-26 23:55:29275
[email protected]df8c99a2011-02-28 20:45:50276 // 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.commit09911bf2008-07-26 23:55:29282 }
283
284 STLDeleteContainerPointers(list.begin(), list.end());
285
286 // Write into profile.
287 if (!bookmarks.empty() && !cancelled()) {
[email protected]f6061aed2009-10-06 16:28:57288 const std::wstring& first_folder_name =
[email protected]89cfc19d2010-06-04 15:36:35289 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX);
[email protected]f6061aed2009-10-06 16:28:57290 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.commit09911bf2008-07-26 23:55:29294 }
295 if (!template_urls.empty() && !cancelled()) {
[email protected]f6061aed2009-10-06 16:28:57296 bridge_->SetKeywords(template_urls, -1, false);
initial.commit09911bf2008-07-26 23:55:29297 } 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]f6061aed2009-10-06 16:28:57303 bridge_->SetFavIcons(favicons);
initial.commit09911bf2008-07-26 23:55:29304 }
305}
306
307void Firefox3Importer::ImportPasswords() {
308 // Initializes NSS3.
309 NSSDecryptor decryptor;
[email protected]91243c32011-01-28 18:27:17310 if (!decryptor.Init(source_path_, source_path_) &&
311 !decryptor.Init(app_path_, source_path_)) {
initial.commit09911bf2008-07-26 23:55:29312 return;
[email protected]750d1df02010-02-22 18:05:00313 }
initial.commit09911bf2008-07-26 23:55:29314
[email protected]a4d9b5a2011-03-05 01:23:01315 std::vector<webkit_glue::PasswordForm> forms;
[email protected]750d1df02010-02-22 18:05:00316 FilePath source_path = source_path_;
[email protected]1851d3b2009-08-15 00:27:20317 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.commit09911bf2008-07-26 23:55:29331
332 if (!cancelled()) {
333 for (size_t i = 0; i < forms.size(); ++i) {
[email protected]f6061aed2009-10-06 16:28:57334 bridge_->SetPasswordForm(forms[i]);
initial.commit09911bf2008-07-26 23:55:29335 }
336 }
337}
338
339void Firefox3Importer::ImportSearchEngines() {
[email protected]750d1df02010-02-22 18:05:00340 std::vector<FilePath> files;
initial.commit09911bf2008-07-26 23:55:29341 GetSearchEnginesXMLFiles(&files);
342
343 std::vector<TemplateURL*> search_engines;
344 ParseSearchEnginesFromXMLFiles(files, &search_engines);
[email protected]f6061aed2009-10-06 16:28:57345 int default_index =
346 GetFirefoxDefaultSearchEngineIndex(search_engines, source_path_);
347 bridge_->SetKeywords(search_engines, default_index, true);
initial.commit09911bf2008-07-26 23:55:29348}
349
350void Firefox3Importer::ImportHomepage() {
[email protected]f6061aed2009-10-06 16:28:57351 GURL home_page = GetHomepage(source_path_);
352 if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) {
353 bridge_->AddHomePage(home_page);
initial.commit09911bf2008-07-26 23:55:29354 }
355}
356
357void Firefox3Importer::GetSearchEnginesXMLFiles(
[email protected]750d1df02010-02-22 18:05:00358 std::vector<FilePath>* files) {
359 FilePath file = source_path_.AppendASCII("search.sqlite");
360 if (!file_util::PathExists(file))
initial.commit09911bf2008-07-26 23:55:29361 return;
362
363 sqlite3* sqlite;
[email protected]91243c32011-01-28 18:27:17364 if (sqlite_utils::OpenSqliteDb(file, &sqlite) != SQLITE_OK)
initial.commit09911bf2008-07-26 23:55:29365 return;
[email protected]06a793f82009-08-05 23:50:35366 sqlite_utils::scoped_sqlite_db_ptr db(sqlite);
initial.commit09911bf2008-07-26 23:55:29367
368 SQLStatement s;
[email protected]e0262c92009-05-29 20:06:41369 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.commit09911bf2008-07-26 23:55:29374
375 if (s.prepare(db.get(), stmt) != SQLITE_OK)
376 return;
377
[email protected]750d1df02010-02-22 18:05:00378 FilePath app_path = app_path_.AppendASCII("searchplugins");
379 FilePath profile_path = source_path_.AppendASCII("searchplugins");
initial.commit09911bf2008-07-26 23:55:29380
[email protected]813ed7502010-09-01 22:58:24381 // 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]67dc31d2009-06-24 15:41:21384 if (s.step() == SQLITE_ROW) {
[email protected]e0262c92009-05-29 20:06:41385 const std::wstring kAppPrefix = L"[app]/";
386 const std::wstring kProfilePrefix = L"[profile]/";
387 do {
[email protected]750d1df02010-02-22 18:05:00388 FilePath file;
[email protected]e0262c92009-05-29 20:06:41389 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]750d1df02010-02-22 18:05:00397 file = app_path.Append(FilePath::FromWStringHack(
398 engine.substr(index + kAppPrefix.length())));
[email protected]67dc31d2009-06-24 15:41:21399 } else if ((index = engine.find(kProfilePrefix)) != std::wstring::npos) {
[email protected]e0262c92009-05-29 20:06:41400 // Remove '[profile]/'.
[email protected]750d1df02010-02-22 18:05:00401 file = profile_path.Append(
402 FilePath::FromWStringHack(
403 engine.substr(index + kProfilePrefix.length())));
[email protected]e0262c92009-05-29 20:06:41404 } else {
[email protected]67dc31d2009-06-24 15:41:21405 // Looks like absolute path to the file.
[email protected]750d1df02010-02-22 18:05:00406 file = FilePath::FromWStringHack(engine);
[email protected]e0262c92009-05-29 20:06:41407 }
408 files->push_back(file);
409 } while (s.step() == SQLITE_ROW && !cancelled());
initial.commit09911bf2008-07-26 23:55:29410 }
[email protected]67dc31d2009-06-24 15:41:21411
[email protected]813ed7502010-09-01 22:58:24412#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]67dc31d2009-06-24 15:41:21426 // Get search engine definition from file system.
[email protected]750d1df02010-02-22 18:05:00427 file_util::FileEnumerator engines(app_path, false,
[email protected]67dc31d2009-06-24 15:41:21428 file_util::FileEnumerator::FILES);
429 for (FilePath engine_path = engines.Next(); !engine_path.value().empty();
430 engine_path = engines.Next()) {
[email protected]750d1df02010-02-22 18:05:00431 files->push_back(engine_path);
[email protected]67dc31d2009-06-24 15:41:21432 }
initial.commit09911bf2008-07-26 23:55:29433}
434
435void 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
460void 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
478void 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]9660ddd32009-01-30 19:33:53493 item->title = s.column_wstring(0);
[email protected]05402122011-01-28 06:48:59494 item->type = TYPE_FOLDER;
initial.commit09911bf2008-07-26 23:55:29495 item->favicon = 0;
[email protected]05402122011-01-28 06:48:59496 item->empty_folder = true;
initial.commit09911bf2008-07-26 23:55:29497 list->push_back(item);
498 }
499}
500
501void Firefox3Importer::GetWholeBookmarkFolder(sqlite3* db, BookmarkList* list,
[email protected]05402122011-01-28 06:48:59502 size_t position,
503 bool* empty_folder) {
initial.commit09911bf2008-07-26 23:55:29504 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]9660ddd32009-01-30 19:33:53527 item->title = s.column_wstring(2);
[email protected]05402122011-01-28 06:48:59528 item->type = static_cast<BookmarkItemType>(s.column_int(3));
initial.commit09911bf2008-07-26 23:55:29529 item->keyword = s.column_string(4);
[email protected]a4d9b5a2011-03-05 01:23:01530 item->date_added = base::Time::FromTimeT(s.column_int64(5)/1000000);
initial.commit09911bf2008-07-26 23:55:29531 item->favicon = s.column_int64(6);
[email protected]05402122011-01-28 06:48:59532 item->empty_folder = true;
initial.commit09911bf2008-07-26 23:55:29533
534 temp_list.push_back(item);
[email protected]05402122011-01-28 06:48:59535 if (empty_folder != NULL)
536 *empty_folder = false;
initial.commit09911bf2008-07-26 23:55:29537 }
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]05402122011-01-28 06:48:59544 if ((*i)->type == TYPE_FOLDER)
545 GetWholeBookmarkFolder(db, list, list->size() - 1, &(*i)->empty_folder);
initial.commit09911bf2008-07-26 23:55:29546 }
547}
548
549void 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}