| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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/firefox2_importer.h" | 5 #include "chrome/browser/importer/firefox2_importer.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/registry.h" | 9 #include "base/registry.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/importer/firefox_importer_utils.h" | 12 #include "chrome/browser/importer/firefox_importer_utils.h" |
| 13 #include "chrome/browser/importer/mork_reader.h" | 13 #include "chrome/browser/importer/mork_reader.h" |
| 14 #include "chrome/browser/template_url.h" | 14 #include "chrome/browser/template_url.h" |
| 15 #include "chrome/browser/template_url_parser.h" | 15 #include "chrome/browser/template_url_parser.h" |
| 16 #include "chrome/common/l10n_util.h" | 16 #include "chrome/common/l10n_util.h" |
| 17 #include "chrome/common/time_format.h" | 17 #include "chrome/common/time_format.h" |
| 18 #include "generated_resources.h" | 18 #include "generated_resources.h" |
| 19 #include "net/base/data_url.h" | 19 #include "net/base/data_url.h" |
| 20 | 20 |
| 21 using base::Time; | 21 using base::Time; |
| 22 | 22 |
| 23 // Firefox2Importer. | 23 // Firefox2Importer. |
| 24 | 24 |
| 25 Firefox2Importer::Firefox2Importer() { | 25 Firefox2Importer::Firefox2Importer() : parsing_bookmarks_html_file_(false) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 Firefox2Importer::~Firefox2Importer() { | 28 Firefox2Importer::~Firefox2Importer() { |
| 29 } | 29 } |
| 30 | 30 |
| 31 void Firefox2Importer::StartImport(ProfileInfo profile_info, | 31 void Firefox2Importer::StartImport(ProfileInfo profile_info, |
| 32 uint16 items, ProfileWriter* writer, | 32 uint16 items, ProfileWriter* writer, |
| 33 MessageLoop* delagate_loop, | 33 MessageLoop* delagate_loop, |
| 34 ImporterHost* host) { | 34 ImporterHost* host) { |
| 35 writer_ = writer; | 35 writer_ = writer; |
| 36 source_path_ = profile_info.source_path; | 36 source_path_ = profile_info.source_path; |
| 37 app_path_ = profile_info.app_path; | 37 app_path_ = profile_info.app_path; |
| 38 importer_host_ = host; | 38 importer_host_ = host; |
| 39 | 39 |
| 40 parsing_bookmarks_html_file_ = (profile_info.browser_type == BOOKMARKS_HTML); |
| 41 |
| 40 // The order here is important! | 42 // The order here is important! |
| 41 NotifyStarted(); | 43 NotifyStarted(); |
| 42 if ((items & HOME_PAGE) && !cancelled()) | 44 if ((items & HOME_PAGE) && !cancelled()) |
| 43 ImportHomepage(); // Doesn't have a UI item. | 45 ImportHomepage(); // Doesn't have a UI item. |
| 44 if ((items & FAVORITES) && !cancelled()) { | 46 if ((items & FAVORITES) && !cancelled()) { |
| 45 NotifyItemStarted(FAVORITES); | 47 NotifyItemStarted(FAVORITES); |
| 46 ImportBookmarks(); | 48 ImportBookmarks(); |
| 47 NotifyItemEnded(FAVORITES); | 49 NotifyItemEnded(FAVORITES); |
| 48 } | 50 } |
| 49 if ((items & SEARCH_ENGINES) && !cancelled()) { | 51 if ((items & SEARCH_ENGINES) && !cancelled()) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 TemplateURL* t_url = new TemplateURL(); | 115 TemplateURL* t_url = new TemplateURL(); |
| 114 // We set short name by using the title if it exists. | 116 // We set short name by using the title if it exists. |
| 115 // Otherwise, we use the shortcut. | 117 // Otherwise, we use the shortcut. |
| 116 t_url->set_short_name(!title.empty() ? title : keyword); | 118 t_url->set_short_name(!title.empty() ? title : keyword); |
| 117 t_url->set_keyword(keyword); | 119 t_url->set_keyword(keyword); |
| 118 t_url->SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToWide(url.spec())), | 120 t_url->SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToWide(url.spec())), |
| 119 0, 0); | 121 0, 0); |
| 120 return t_url; | 122 return t_url; |
| 121 } | 123 } |
| 122 | 124 |
| 123 void Firefox2Importer::ImportBookmarks() { | 125 // static |
| 124 // Read the whole file. | 126 void Firefox2Importer::ImportBookmarksFile( |
| 125 std::wstring file = source_path_; | 127 const std::wstring& file_path, |
| 126 file_util::AppendToPath(&file, L"bookmarks.html"); | 128 const std::set<GURL>& default_urls, |
| 129 bool first_run, |
| 130 const std::wstring& first_folder_name, |
| 131 Importer* importer, |
| 132 std::vector<ProfileWriter::BookmarkEntry>* bookmarks, |
| 133 std::vector<TemplateURL*>* template_urls, |
| 134 std::vector<history::ImportedFavIconUsage>* favicons) { |
| 127 std::string content; | 135 std::string content; |
| 128 file_util::ReadFileToString(file, &content); | 136 file_util::ReadFileToString(file_path, &content); |
| 129 std::vector<std::string> lines; | 137 std::vector<std::string> lines; |
| 130 SplitString(content, '\n', &lines); | 138 SplitString(content, '\n', &lines); |
| 131 | 139 |
| 132 // Load the default bookmarks. | 140 std::vector<ProfileWriter::BookmarkEntry> toolbar_bookmarks; |
| 133 std::set<GURL> default_urls; | 141 std::wstring last_folder = first_folder_name; |
| 134 LoadDefaultBookmarks(app_path_, &default_urls); | |
| 135 | |
| 136 // Parse the bookmarks.html file. | |
| 137 std::vector<ProfileWriter::BookmarkEntry> bookmarks, toolbar_bookmarks; | |
| 138 std::vector<TemplateURL*> template_urls; | |
| 139 std::vector<history::ImportedFavIconUsage> favicons; | |
| 140 std::wstring last_folder | |
| 141 = l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); | |
| 142 bool last_folder_on_toolbar = false; | 142 bool last_folder_on_toolbar = false; |
| 143 std::vector<std::wstring> path; | 143 std::vector<std::wstring> path; |
| 144 size_t toolbar_folder = 0; | 144 size_t toolbar_folder = 0; |
| 145 std::string charset; | 145 std::string charset; |
| 146 for (size_t i = 0; i < lines.size() && !cancelled(); ++i) { | 146 for (size_t i = 0; i < lines.size() && (!importer || !importer->cancelled()); |
| 147 ++i) { |
| 147 std::string line; | 148 std::string line; |
| 148 TrimString(lines[i], " ", &line); | 149 TrimString(lines[i], " ", &line); |
| 149 | 150 |
| 150 // Get the encoding of the bookmark file. | 151 // Get the encoding of the bookmark file. |
| 151 if (ParseCharsetFromLine(line, &charset)) | 152 if (ParseCharsetFromLine(line, &charset)) |
| 152 continue; | 153 continue; |
| 153 | 154 |
| 154 // Get the folder name. | 155 // Get the folder name. |
| 155 if (ParseFolderNameFromLine(line, charset, &last_folder, | 156 if (ParseFolderNameFromLine(line, charset, &last_folder, |
| 156 &last_folder_on_toolbar)) | 157 &last_folder_on_toolbar)) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 172 if (toolbar_folder > path.size() && path.size() > 0) { | 173 if (toolbar_folder > path.size() && path.size() > 0) { |
| 173 NOTREACHED(); // error in parsing. | 174 NOTREACHED(); // error in parsing. |
| 174 break; | 175 break; |
| 175 } | 176 } |
| 176 | 177 |
| 177 ProfileWriter::BookmarkEntry entry; | 178 ProfileWriter::BookmarkEntry entry; |
| 178 entry.creation_time = add_date; | 179 entry.creation_time = add_date; |
| 179 entry.url = url; | 180 entry.url = url; |
| 180 entry.title = title; | 181 entry.title = title; |
| 181 | 182 |
| 182 if (first_run() && toolbar_folder) { | 183 if (first_run && toolbar_folder) { |
| 183 // Flatten the items in toolbar. | 184 // Flatten the items in toolbar. |
| 184 entry.in_toolbar = true; | 185 entry.in_toolbar = true; |
| 185 entry.path.assign(path.begin() + toolbar_folder, path.end()); | 186 entry.path.assign(path.begin() + toolbar_folder, path.end()); |
| 186 toolbar_bookmarks.push_back(entry); | 187 toolbar_bookmarks.push_back(entry); |
| 187 } else { | 188 } else { |
| 188 // Insert the item into the "Imported from Firefox" folder after | 189 // Insert the item into the "Imported from Firefox" folder after |
| 189 // the first run. | 190 // the first run. |
| 190 entry.path.assign(path.begin(), path.end()); | 191 entry.path.assign(path.begin(), path.end()); |
| 191 if (first_run()) | 192 if (first_run) |
| 192 entry.path.erase(entry.path.begin()); | 193 entry.path.erase(entry.path.begin()); |
| 193 bookmarks.push_back(entry); | 194 bookmarks->push_back(entry); |
| 194 } | 195 } |
| 195 | 196 |
| 196 // Save the favicon. DataURLToFaviconUsage will handle the case where | 197 // Save the favicon. DataURLToFaviconUsage will handle the case where |
| 197 // there is no favicon. | 198 // there is no favicon. |
| 198 DataURLToFaviconUsage(url, favicon, &favicons); | 199 if (favicons) |
| 200 DataURLToFaviconUsage(url, favicon, favicons); |
| 199 | 201 |
| 200 // If there is a SHORTCUT attribute for this bookmark, we | 202 if (template_urls) { |
| 201 // add it as our keywords. | 203 // If there is a SHORTCUT attribute for this bookmark, we |
| 202 TemplateURL* t_url = CreateTemplateURL(title, shortcut, url); | 204 // add it as our keywords. |
| 203 if (t_url) | 205 TemplateURL* t_url = CreateTemplateURL(title, shortcut, url); |
| 204 template_urls.push_back(t_url); | 206 if (t_url) |
| 207 template_urls->push_back(t_url); |
| 208 } |
| 205 | 209 |
| 206 continue; | 210 continue; |
| 207 } | 211 } |
| 208 | 212 |
| 209 // Bookmarks in sub-folder are encapsulated with <DL> tag. | 213 // Bookmarks in sub-folder are encapsulated with <DL> tag. |
| 210 if (StartsWithASCII(line, "<DL>", true)) { | 214 if (StartsWithASCII(line, "<DL>", true)) { |
| 211 path.push_back(last_folder); | 215 path.push_back(last_folder); |
| 212 last_folder.clear(); | 216 last_folder.clear(); |
| 213 if (last_folder_on_toolbar && !toolbar_folder) | 217 if (last_folder_on_toolbar && !toolbar_folder) |
| 214 toolbar_folder = path.size(); | 218 toolbar_folder = path.size(); |
| 215 } else if (StartsWithASCII(line, "</DL>", true)) { | 219 } else if (StartsWithASCII(line, "</DL>", true)) { |
| 216 if (path.empty()) | 220 if (path.empty()) |
| 217 break; // Mismatch <DL>. | 221 break; // Mismatch <DL>. |
| 218 path.pop_back(); | 222 path.pop_back(); |
| 219 if (toolbar_folder > path.size()) | 223 if (toolbar_folder > path.size()) |
| 220 toolbar_folder = 0; | 224 toolbar_folder = 0; |
| 221 } | 225 } |
| 222 } | 226 } |
| 223 | 227 |
| 228 bookmarks->insert(bookmarks->begin(), toolbar_bookmarks.begin(), |
| 229 toolbar_bookmarks.end()); |
| 230 } |
| 231 |
| 232 void Firefox2Importer::ImportBookmarks() { |
| 233 // Load the default bookmarks. |
| 234 std::set<GURL> default_urls; |
| 235 if (!parsing_bookmarks_html_file_) |
| 236 LoadDefaultBookmarks(app_path_, &default_urls); |
| 237 |
| 238 // Parse the bookmarks.html file. |
| 239 std::vector<ProfileWriter::BookmarkEntry> bookmarks, toolbar_bookmarks; |
| 240 std::vector<TemplateURL*> template_urls; |
| 241 std::vector<history::ImportedFavIconUsage> favicons; |
| 242 std::wstring file = source_path_; |
| 243 if (!parsing_bookmarks_html_file_) |
| 244 file_util::AppendToPath(&file, L"bookmarks.html"); |
| 245 std::wstring first_folder_name; |
| 246 if (parsing_bookmarks_html_file_) |
| 247 first_folder_name = l10n_util::GetString(IDS_BOOKMARK_GROUP); |
| 248 else |
| 249 first_folder_name = l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); |
| 250 |
| 251 ImportBookmarksFile(file, default_urls, first_run(), |
| 252 first_folder_name, this, &bookmarks, &template_urls, |
| 253 &favicons); |
| 254 |
| 224 // Write data into profile. | 255 // Write data into profile. |
| 225 bookmarks.insert(bookmarks.begin(), toolbar_bookmarks.begin(), | |
| 226 toolbar_bookmarks.end()); | |
| 227 if (!bookmarks.empty() && !cancelled()) { | 256 if (!bookmarks.empty() && !cancelled()) { |
| 228 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | 257 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, |
| 229 &ProfileWriter::AddBookmarkEntry, bookmarks, false)); | 258 &ProfileWriter::AddBookmarkEntry, bookmarks, |
| 259 first_folder_name, |
| 260 first_run() ? ProfileWriter::FIRST_RUN : 0)); |
| 230 } | 261 } |
| 231 if (!template_urls.empty() && !cancelled()) { | 262 if (!parsing_bookmarks_html_file_ && !template_urls.empty() && |
| 263 !cancelled()) { |
| 232 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | 264 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, |
| 233 &ProfileWriter::AddKeywords, template_urls, -1, false)); | 265 &ProfileWriter::AddKeywords, template_urls, -1, false)); |
| 234 } else { | 266 } else { |
| 235 STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); | 267 STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); |
| 236 } | 268 } |
| 237 if (!favicons.empty()) { | 269 if (!favicons.empty()) { |
| 238 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, | 270 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, |
| 239 &ProfileWriter::AddFavicons, favicons)); | 271 &ProfileWriter::AddFavicons, favicons)); |
| 240 } | 272 } |
| 241 } | 273 } |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 | 540 |
| 509 // We need to make up a URL for the favicon. We use a version of the page's | 541 // We need to make up a URL for the favicon. We use a version of the page's |
| 510 // URL so that we can be sure it will not collide. | 542 // URL so that we can be sure it will not collide. |
| 511 usage.favicon_url = GURL(std::string("made-up-favicon:") + link_url.spec()); | 543 usage.favicon_url = GURL(std::string("made-up-favicon:") + link_url.spec()); |
| 512 | 544 |
| 513 // We only have one URL per favicon for Firefox 2 bookmarks. | 545 // We only have one URL per favicon for Firefox 2 bookmarks. |
| 514 usage.urls.insert(link_url); | 546 usage.urls.insert(link_url); |
| 515 | 547 |
| 516 favicons->push_back(usage); | 548 favicons->push_back(usage); |
| 517 } | 549 } |
| OLD | NEW |