OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 t_url->set_keyword(keyword); | 141 t_url->set_keyword(keyword); |
142 t_url->SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url.spec())), | 142 t_url->SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url.spec())), |
143 0, 0); | 143 0, 0); |
144 return t_url; | 144 return t_url; |
145 } | 145 } |
146 | 146 |
147 // static | 147 // static |
148 void Firefox2Importer::ImportBookmarksFile( | 148 void Firefox2Importer::ImportBookmarksFile( |
149 const FilePath& file_path, | 149 const FilePath& file_path, |
150 const std::set<GURL>& default_urls, | 150 const std::set<GURL>& default_urls, |
151 bool import_to_bookmark_bar, | |
152 const string16& first_folder_name, | |
153 Importer* importer, | 151 Importer* importer, |
154 std::vector<ProfileWriter::BookmarkEntry>* bookmarks, | 152 std::vector<ProfileWriter::BookmarkEntry>* bookmarks, |
155 std::vector<TemplateURL*>* template_urls, | 153 std::vector<TemplateURL*>* template_urls, |
156 std::vector<history::ImportedFaviconUsage>* favicons) { | 154 std::vector<history::ImportedFaviconUsage>* favicons) { |
157 std::string content; | 155 std::string content; |
158 file_util::ReadFileToString(file_path, &content); | 156 file_util::ReadFileToString(file_path, &content); |
159 std::vector<std::string> lines; | 157 std::vector<std::string> lines; |
160 base::SplitString(content, '\n', &lines); | 158 base::SplitString(content, '\n', &lines); |
161 | 159 |
162 std::vector<ProfileWriter::BookmarkEntry> toolbar_bookmarks; | 160 std::vector<ProfileWriter::BookmarkEntry> toolbar_bookmarks; |
163 string16 last_folder = first_folder_name; | 161 string16 last_folder; |
164 bool last_folder_on_toolbar = false; | 162 bool last_folder_on_toolbar = false; |
165 bool last_folder_is_empty = true; | 163 bool last_folder_is_empty = true; |
166 bool has_subfolder = false; | 164 bool has_subfolder = false; |
167 base::Time last_folder_add_date; | 165 base::Time last_folder_add_date; |
168 std::vector<string16> path; | 166 std::vector<string16> path; |
169 size_t toolbar_folder = 0; | 167 size_t toolbar_folder = 0; |
170 std::string charset; | 168 std::string charset; |
171 for (size_t i = 0; i < lines.size() && (!importer || !importer->cancelled()); | 169 for (size_t i = 0; i < lines.size() && (!importer || !importer->cancelled()); |
172 ++i) { | 170 ++i) { |
173 std::string line; | 171 std::string line; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 if (toolbar_folder > path.size() && !path.empty()) { | 205 if (toolbar_folder > path.size() && !path.empty()) { |
208 NOTREACHED(); // error in parsing. | 206 NOTREACHED(); // error in parsing. |
209 break; | 207 break; |
210 } | 208 } |
211 | 209 |
212 ProfileWriter::BookmarkEntry entry; | 210 ProfileWriter::BookmarkEntry entry; |
213 entry.creation_time = add_date; | 211 entry.creation_time = add_date; |
214 entry.url = url; | 212 entry.url = url; |
215 entry.title = title; | 213 entry.title = title; |
216 | 214 |
217 if (import_to_bookmark_bar && toolbar_folder) { | 215 if (toolbar_folder) { |
218 // Flatten the items in toolbar. | 216 // The toolbar folder should be at the top level. |
219 entry.in_toolbar = true; | 217 entry.in_toolbar = true; |
220 entry.path.assign(path.begin() + toolbar_folder, path.end()); | 218 entry.path.assign(path.begin() + toolbar_folder - 1, path.end()); |
221 toolbar_bookmarks.push_back(entry); | 219 toolbar_bookmarks.push_back(entry); |
222 } else { | 220 } else { |
223 // Insert the item into the "Imported from Firefox" folder. | 221 // Add this bookmark to the list of |bookmarks|. |
224 if (!has_subfolder && !last_folder.empty()) { | 222 if (!has_subfolder && !last_folder.empty()) { |
225 path.push_back(last_folder); | 223 path.push_back(last_folder); |
226 last_folder.clear(); | 224 last_folder.clear(); |
227 } | 225 } |
228 entry.path.assign(path.begin(), path.end()); | 226 entry.path.assign(path.begin(), path.end()); |
229 if (import_to_bookmark_bar) { | |
230 DCHECK(!entry.path.empty()); | |
231 entry.path.erase(entry.path.begin()); | |
232 } | |
233 bookmarks->push_back(entry); | 227 bookmarks->push_back(entry); |
234 } | 228 } |
235 | 229 |
236 // Save the favicon. DataURLToFaviconUsage will handle the case where | 230 // Save the favicon. DataURLToFaviconUsage will handle the case where |
237 // there is no favicon. | 231 // there is no favicon. |
238 if (favicons) | 232 if (favicons) |
239 DataURLToFaviconUsage(url, favicon, favicons); | 233 DataURLToFaviconUsage(url, favicon, favicons); |
240 | 234 |
241 if (template_urls) { | 235 if (template_urls) { |
242 // If there is a SHORTCUT attribute for this bookmark, we | 236 // If there is a SHORTCUT attribute for this bookmark, we |
243 // add it as our keywords. | 237 // add it as our keywords. |
244 TemplateURL* t_url = CreateTemplateURL(title, shortcut, url); | 238 TemplateURL* t_url = CreateTemplateURL(title, shortcut, url); |
245 if (t_url) | 239 if (t_url) |
246 template_urls->push_back(t_url); | 240 template_urls->push_back(t_url); |
247 } | 241 } |
248 | 242 |
249 continue; | 243 continue; |
250 } | 244 } |
251 | 245 |
252 // Bookmarks in sub-folder are encapsulated with <DL> tag. | 246 // Bookmarks in sub-folder are encapsulated with <DL> tag. |
253 if (StartsWithASCII(line, "<DL>", false)) { | 247 if (StartsWithASCII(line, "<DL>", false)) { |
254 has_subfolder = true; | 248 has_subfolder = true; |
255 if (last_folder.length() != 0) { | 249 if (!last_folder.empty()) { |
256 path.push_back(last_folder); | 250 path.push_back(last_folder); |
257 last_folder.clear(); | 251 last_folder.clear(); |
258 } | 252 } |
259 if (last_folder_on_toolbar && !toolbar_folder) | 253 if (last_folder_on_toolbar && !toolbar_folder) |
260 toolbar_folder = path.size(); | 254 toolbar_folder = path.size(); |
261 | 255 |
262 // Mark next folder empty as initial state. | 256 // Mark next folder empty as initial state. |
263 last_folder_is_empty = true; | 257 last_folder_is_empty = true; |
264 } else if (StartsWithASCII(line, "</DL>", false)) { | 258 } else if (StartsWithASCII(line, "</DL>", false)) { |
265 if (path.empty()) | 259 if (path.empty()) |
266 break; // Mismatch <DL>. | 260 break; // Mismatch <DL>. |
267 | 261 |
268 string16 folder_title = path.back(); | 262 string16 folder_title = path.back(); |
269 path.pop_back(); | 263 path.pop_back(); |
270 | 264 |
271 if (last_folder_is_empty) { | 265 if (last_folder_is_empty) { |
272 // Empty folder should be added explicitly. | 266 // Empty folder should be added explicitly. |
273 ProfileWriter::BookmarkEntry entry; | 267 ProfileWriter::BookmarkEntry entry; |
274 entry.is_folder = true; | 268 entry.is_folder = true; |
275 entry.creation_time = last_folder_add_date; | 269 entry.creation_time = last_folder_add_date; |
276 entry.title = folder_title; | 270 entry.title = folder_title; |
277 if (import_to_bookmark_bar && toolbar_folder) { | 271 if (toolbar_folder) { |
278 // Flatten the folder in toolbar. | 272 // The toolbar folder should be at the top level. |
279 entry.in_toolbar = true; | 273 // Make sure we don't add the toolbar folder itself if it is empty. |
280 entry.path.assign(path.begin() + toolbar_folder, path.end()); | 274 if (toolbar_folder <= path.size()) { |
281 toolbar_bookmarks.push_back(entry); | 275 entry.in_toolbar = true; |
| 276 entry.path.assign(path.begin() + toolbar_folder - 1, path.end()); |
| 277 toolbar_bookmarks.push_back(entry); |
| 278 } |
282 } else { | 279 } else { |
283 // Insert the folder into the "Imported from Firefox" folder. | 280 // Add this folder to the list of |bookmarks|. |
284 entry.path.assign(path.begin(), path.end()); | 281 entry.path.assign(path.begin(), path.end()); |
285 if (import_to_bookmark_bar) { | |
286 DCHECK(!entry.path.empty()); | |
287 entry.path.erase(entry.path.begin()); | |
288 } | |
289 bookmarks->push_back(entry); | 282 bookmarks->push_back(entry); |
290 } | 283 } |
291 | 284 |
292 // Parent folder include current one, so it's not empty. | 285 // Parent folder include current one, so it's not empty. |
293 last_folder_is_empty = false; | 286 last_folder_is_empty = false; |
294 } | 287 } |
295 | 288 |
296 if (toolbar_folder > path.size()) | 289 if (toolbar_folder > path.size()) |
297 toolbar_folder = 0; | 290 toolbar_folder = 0; |
298 } | 291 } |
299 } | 292 } |
300 | 293 |
301 bookmarks->insert(bookmarks->begin(), toolbar_bookmarks.begin(), | 294 bookmarks->insert(bookmarks->begin(), toolbar_bookmarks.begin(), |
302 toolbar_bookmarks.end()); | 295 toolbar_bookmarks.end()); |
303 } | 296 } |
304 | 297 |
305 void Firefox2Importer::ImportBookmarks() { | 298 void Firefox2Importer::ImportBookmarks() { |
306 // Load the default bookmarks. | 299 // Load the default bookmarks. |
307 std::set<GURL> default_urls; | 300 std::set<GURL> default_urls; |
308 if (!parsing_bookmarks_html_file_) | 301 if (!parsing_bookmarks_html_file_) |
309 LoadDefaultBookmarks(app_path_, &default_urls); | 302 LoadDefaultBookmarks(app_path_, &default_urls); |
310 | 303 |
311 // Parse the bookmarks.html file. | 304 // Parse the bookmarks.html file. |
312 std::vector<ProfileWriter::BookmarkEntry> bookmarks, toolbar_bookmarks; | 305 std::vector<ProfileWriter::BookmarkEntry> bookmarks, toolbar_bookmarks; |
313 std::vector<TemplateURL*> template_urls; | 306 std::vector<TemplateURL*> template_urls; |
314 std::vector<history::ImportedFaviconUsage> favicons; | 307 std::vector<history::ImportedFaviconUsage> favicons; |
315 FilePath file = source_path_; | 308 FilePath file = source_path_; |
316 if (!parsing_bookmarks_html_file_) | 309 if (!parsing_bookmarks_html_file_) |
317 file = file.AppendASCII("bookmarks.html"); | 310 file = file.AppendASCII("bookmarks.html"); |
318 string16 first_folder_name = bridge_->GetLocalizedString( | |
319 parsing_bookmarks_html_file_ ? IDS_BOOKMARK_GROUP : | |
320 IDS_BOOKMARK_GROUP_FROM_FIREFOX); | |
321 | 311 |
322 ImportBookmarksFile(file, default_urls, import_to_bookmark_bar(), | 312 ImportBookmarksFile(file, default_urls, this, &bookmarks, &template_urls, |
323 first_folder_name, this, &bookmarks, &template_urls, | |
324 &favicons); | 313 &favicons); |
325 | 314 |
326 // Write data into profile. | 315 // Write data into profile. |
327 if (!bookmarks.empty() && !cancelled()) { | 316 if (!bookmarks.empty() && !cancelled()) { |
328 int options = 0; | 317 string16 first_folder_name = bridge_->GetLocalizedString( |
329 if (import_to_bookmark_bar()) | 318 parsing_bookmarks_html_file_ ? IDS_BOOKMARK_GROUP : |
330 options |= ProfileWriter::IMPORT_TO_BOOKMARK_BAR; | 319 IDS_BOOKMARK_GROUP_FROM_FIREFOX); |
331 if (bookmark_bar_disabled()) | 320 bridge_->AddBookmarks(bookmarks, first_folder_name); |
332 options |= ProfileWriter::BOOKMARK_BAR_DISABLED; | |
333 bridge_->AddBookmarks(bookmarks, first_folder_name, options); | |
334 } | 321 } |
335 if (!parsing_bookmarks_html_file_ && !template_urls.empty() && | 322 if (!parsing_bookmarks_html_file_ && !template_urls.empty() && |
336 !cancelled()) { | 323 !cancelled()) { |
337 bridge_->SetKeywords(template_urls, -1, false); | 324 bridge_->SetKeywords(template_urls, -1, false); |
338 } else { | 325 } else { |
339 STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); | 326 STLDeleteContainerPointers(template_urls.begin(), template_urls.end()); |
340 } | 327 } |
341 if (!favicons.empty()) | 328 if (!favicons.empty()) |
342 bridge_->SetFavicons(favicons); | 329 bridge_->SetFavicons(favicons); |
343 } | 330 } |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 | 666 |
680 // We need to make up a URL for the favicon. We use a version of the page's | 667 // We need to make up a URL for the favicon. We use a version of the page's |
681 // URL so that we can be sure it will not collide. | 668 // URL so that we can be sure it will not collide. |
682 usage.favicon_url = GURL(std::string("made-up-favicon:") + link_url.spec()); | 669 usage.favicon_url = GURL(std::string("made-up-favicon:") + link_url.spec()); |
683 | 670 |
684 // We only have one URL per favicon for Firefox 2 bookmarks. | 671 // We only have one URL per favicon for Firefox 2 bookmarks. |
685 usage.urls.insert(link_url); | 672 usage.urls.insert(link_url); |
686 | 673 |
687 favicons->push_back(usage); | 674 favicons->push_back(usage); |
688 } | 675 } |
OLD | NEW |