| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/importer/firefox3_importer.h" | 5 #include "chrome/browser/importer/firefox3_importer.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_html_reader.h" |
| 16 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" |
| 15 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/history/history_types.h" | 18 #include "chrome/browser/history/history_types.h" |
| 17 #include "chrome/browser/importer/firefox2_importer.h" | |
| 18 #include "chrome/browser/importer/firefox_importer_utils.h" | 19 #include "chrome/browser/importer/firefox_importer_utils.h" |
| 19 #include "chrome/browser/importer/importer_bridge.h" | 20 #include "chrome/browser/importer/importer_bridge.h" |
| 20 #include "chrome/browser/importer/importer_util.h" | 21 #include "chrome/browser/importer/importer_util.h" |
| 21 #include "chrome/browser/importer/nss_decryptor.h" | 22 #include "chrome/browser/importer/nss_decryptor.h" |
| 22 #include "chrome/browser/search_engines/template_url.h" | 23 #include "chrome/browser/search_engines/template_url.h" |
| 23 #include "chrome/common/time_format.h" | 24 #include "chrome/common/time_format.h" |
| 24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/common/password_form.h" | 26 #include "content/public/common/password_form.h" |
| 26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
| 27 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
| 28 #include "sql/connection.h" | 29 #include "sql/connection.h" |
| 29 #include "sql/statement.h" | 30 #include "sql/statement.h" |
| 30 | 31 |
| 31 using content::BrowserThread; | 32 using content::BrowserThread; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 // Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/ | 36 // Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/ |
| 36 // components/places/public/nsINavBookmarksService.idl | 37 // components/places/public/nsINavBookmarksService.idl |
| 37 enum BookmarkItemType { | 38 enum BookmarkItemType { |
| 38 TYPE_BOOKMARK = 1, | 39 TYPE_BOOKMARK = 1, |
| 39 TYPE_FOLDER = 2, | 40 TYPE_FOLDER = 2, |
| 40 TYPE_SEPARATOR = 3, | 41 TYPE_SEPARATOR = 3, |
| 41 TYPE_DYNAMIC_CONTAINER = 4 | 42 TYPE_DYNAMIC_CONTAINER = 4 |
| 42 }; | 43 }; |
| 43 | 44 |
| 45 // Creates a TemplateURL with the |keyword| and |url|. |title| may be empty. |
| 46 // This function transfers ownership of the created TemplateURL to the caller. |
| 47 TemplateURL* CreateTemplateURL(const string16& title, |
| 48 const string16& keyword, |
| 49 const GURL& url) { |
| 50 // Skip if the keyword or url is invalid. |
| 51 if (keyword.empty() || !url.is_valid()) |
| 52 return NULL; |
| 53 |
| 54 TemplateURLData data; |
| 55 // We set short name by using the title if it exists. |
| 56 // Otherwise, we use the shortcut. |
| 57 data.short_name = title.empty() ? keyword : title; |
| 58 data.SetKeyword(keyword); |
| 59 data.SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url.spec()))); |
| 60 return new TemplateURL(NULL, data); |
| 61 } |
| 62 |
| 63 // Loads the default bookmarks in the Firefox installed at |app_path|, |
| 64 // and stores their locations in |urls|. |
| 65 void LoadDefaultBookmarks(const base::FilePath& app_path, |
| 66 std::set<GURL>* urls) { |
| 67 base::FilePath file = app_path.AppendASCII("defaults") |
| 68 .AppendASCII("profile") |
| 69 .AppendASCII("bookmarks.html"); |
| 70 urls->clear(); |
| 71 |
| 72 std::vector<ImportedBookmarkEntry> bookmarks; |
| 73 bookmark_html_reader::ImportBookmarksFile(NULL, NULL, file, &bookmarks, NULL); |
| 74 for (size_t i = 0; i < bookmarks.size(); ++i) |
| 75 urls->insert(bookmarks[i].url); |
| 76 } |
| 77 |
| 44 } // namespace | 78 } // namespace |
| 45 | 79 |
| 46 struct Firefox3Importer::BookmarkItem { | 80 struct Firefox3Importer::BookmarkItem { |
| 47 int parent; | 81 int parent; |
| 48 int id; | 82 int id; |
| 49 GURL url; | 83 GURL url; |
| 50 string16 title; | 84 string16 title; |
| 51 BookmarkItemType type; | 85 BookmarkItemType type; |
| 52 std::string keyword; | 86 std::string keyword; |
| 53 base::Time date_added; | 87 base::Time date_added; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 // Get the bookmark folders that we are interested in. | 198 // Get the bookmark folders that we are interested in. |
| 165 int toolbar_folder_id = -1; | 199 int toolbar_folder_id = -1; |
| 166 int menu_folder_id = -1; | 200 int menu_folder_id = -1; |
| 167 int unsorted_folder_id = -1; | 201 int unsorted_folder_id = -1; |
| 168 LoadRootNodeID(&db, &toolbar_folder_id, &menu_folder_id, &unsorted_folder_id); | 202 LoadRootNodeID(&db, &toolbar_folder_id, &menu_folder_id, &unsorted_folder_id); |
| 169 | 203 |
| 170 // Load livemark IDs. | 204 // Load livemark IDs. |
| 171 std::set<int> livemark_id; | 205 std::set<int> livemark_id; |
| 172 LoadLivemarkIDs(&db, &livemark_id); | 206 LoadLivemarkIDs(&db, &livemark_id); |
| 173 | 207 |
| 174 // Load the default bookmarks. Its storage is the same as Firefox 2. | 208 // Load the default bookmarks. |
| 175 std::set<GURL> default_urls; | 209 std::set<GURL> default_urls; |
| 176 Firefox2Importer::LoadDefaultBookmarks(app_path_, &default_urls); | 210 LoadDefaultBookmarks(app_path_, &default_urls); |
| 177 | 211 |
| 178 BookmarkList list; | 212 BookmarkList list; |
| 179 GetTopBookmarkFolder(&db, toolbar_folder_id, &list); | 213 GetTopBookmarkFolder(&db, toolbar_folder_id, &list); |
| 180 GetTopBookmarkFolder(&db, menu_folder_id, &list); | 214 GetTopBookmarkFolder(&db, menu_folder_id, &list); |
| 181 GetTopBookmarkFolder(&db, unsorted_folder_id, &list); | 215 GetTopBookmarkFolder(&db, unsorted_folder_id, &list); |
| 182 size_t count = list.size(); | 216 size_t count = list.size(); |
| 183 for (size_t i = 0; i < count; ++i) | 217 for (size_t i = 0; i < count; ++i) |
| 184 GetWholeBookmarkFolder(&db, &list, i, NULL); | 218 GetWholeBookmarkFolder(&db, &list, i, NULL); |
| 185 | 219 |
| 186 std::vector<ProfileWriter::BookmarkEntry> bookmarks; | 220 std::vector<ImportedBookmarkEntry> bookmarks; |
| 187 std::vector<TemplateURL*> template_urls; | 221 std::vector<TemplateURL*> template_urls; |
| 188 FaviconMap favicon_map; | 222 FaviconMap favicon_map; |
| 189 | 223 |
| 190 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based | 224 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based |
| 191 // keywords yet. We won't include them in the list. | 225 // keywords yet. We won't include them in the list. |
| 192 std::set<int> post_keyword_ids; | 226 std::set<int> post_keyword_ids; |
| 193 const char* query = "SELECT b.id FROM moz_bookmarks b " | 227 const char* query = "SELECT b.id FROM moz_bookmarks b " |
| 194 "INNER JOIN moz_items_annos ia ON ia.item_id = b.id " | 228 "INNER JOIN moz_items_annos ia ON ia.item_id = b.id " |
| 195 "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id " | 229 "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id " |
| 196 "WHERE aa.name = 'bookmarkProperties/POSTData'"; | 230 "WHERE aa.name = 'bookmarkProperties/POSTData'"; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 found_path = true; | 285 found_path = true; |
| 252 break; | 286 break; |
| 253 } | 287 } |
| 254 | 288 |
| 255 child = parent; | 289 child = parent; |
| 256 } | 290 } |
| 257 | 291 |
| 258 if (!found_path) | 292 if (!found_path) |
| 259 continue; | 293 continue; |
| 260 | 294 |
| 261 ProfileWriter::BookmarkEntry entry; | 295 ImportedBookmarkEntry entry; |
| 262 entry.creation_time = item->date_added; | 296 entry.creation_time = item->date_added; |
| 263 entry.title = item->title; | 297 entry.title = item->title; |
| 264 entry.url = item->url; | 298 entry.url = item->url; |
| 265 entry.path = path; | 299 entry.path = path; |
| 266 entry.in_toolbar = is_in_toolbar; | 300 entry.in_toolbar = is_in_toolbar; |
| 267 entry.is_folder = item->type == TYPE_FOLDER; | 301 entry.is_folder = item->type == TYPE_FOLDER; |
| 268 | 302 |
| 269 bookmarks.push_back(entry); | 303 bookmarks.push_back(entry); |
| 270 | 304 |
| 271 if (item->type == TYPE_BOOKMARK) { | 305 if (item->type == TYPE_BOOKMARK) { |
| 272 if (item->favicon) | 306 if (item->favicon) |
| 273 favicon_map[item->favicon].insert(item->url); | 307 favicon_map[item->favicon].insert(item->url); |
| 274 | 308 |
| 275 // This bookmark has a keyword, we import it to our TemplateURL model. | 309 // This bookmark has a keyword, we import it to our TemplateURL model. |
| 276 TemplateURL* t_url = Firefox2Importer::CreateTemplateURL( | 310 TemplateURL* t_url = CreateTemplateURL( |
| 277 item->title, UTF8ToUTF16(item->keyword), item->url); | 311 item->title, UTF8ToUTF16(item->keyword), item->url); |
| 278 if (t_url) | 312 if (t_url) |
| 279 template_urls.push_back(t_url); | 313 template_urls.push_back(t_url); |
| 280 } | 314 } |
| 281 } | 315 } |
| 282 | 316 |
| 283 STLDeleteElements(&list); | 317 STLDeleteElements(&list); |
| 284 | 318 |
| 285 // Write into profile. | 319 // Write into profile. |
| 286 if (!bookmarks.empty() && !cancelled()) { | 320 if (!bookmarks.empty() && !cancelled()) { |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 | 596 |
| 563 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) | 597 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) |
| 564 continue; // Unable to decode. | 598 continue; // Unable to decode. |
| 565 | 599 |
| 566 usage.urls = i->second; | 600 usage.urls = i->second; |
| 567 favicons->push_back(usage); | 601 favicons->push_back(usage); |
| 568 } | 602 } |
| 569 s.Reset(true); | 603 s.Reset(true); |
| 570 } | 604 } |
| 571 } | 605 } |
| OLD | NEW |