| 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/toolbar_importer.h" | 5 #include "chrome/browser/importer/toolbar_importer.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" |
| 14 #include "chrome/browser/first_run/first_run.h" | 15 #include "chrome/browser/first_run/first_run.h" |
| 15 #include "chrome/browser/importer/importer_bridge.h" | 16 #include "chrome/browser/importer/importer_bridge.h" |
| 16 #include "chrome/browser/importer/importer_data_types.h" | 17 #include "chrome/browser/importer/importer_data_types.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 20 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 21 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
| 22 #include "third_party/libxml/chromium/libxml_utils.h" | 23 #include "third_party/libxml/chromium/libxml_utils.h" |
| 23 | 24 |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 if (cancelled()) { | 263 if (cancelled()) { |
| 263 EndImport(); | 264 EndImport(); |
| 264 return; | 265 return; |
| 265 } | 266 } |
| 266 | 267 |
| 267 state_ = PARSE_BOOKMARKS; | 268 state_ = PARSE_BOOKMARKS; |
| 268 | 269 |
| 269 XmlReader reader; | 270 XmlReader reader; |
| 270 if (reader.Load(response) && !cancelled()) { | 271 if (reader.Load(response) && !cancelled()) { |
| 271 // Construct Bookmarks | 272 // Construct Bookmarks |
| 272 std::vector<ProfileWriter::BookmarkEntry> bookmarks; | 273 std::vector<ImportedBookmarkEntry> bookmarks; |
| 273 if (ParseBookmarksFromReader(&reader, &bookmarks, | 274 if (ParseBookmarksFromReader(&reader, &bookmarks, |
| 274 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_GOOGLE_TOOLBAR))) | 275 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_GOOGLE_TOOLBAR))) |
| 275 AddBookmarksToChrome(bookmarks); | 276 AddBookmarksToChrome(bookmarks); |
| 276 } | 277 } |
| 277 EndImportBookmarks(); | 278 EndImportBookmarks(); |
| 278 } | 279 } |
| 279 | 280 |
| 280 bool Toolbar5Importer::ParseAuthenticationTokenResponse( | 281 bool Toolbar5Importer::ParseAuthenticationTokenResponse( |
| 281 const std::string& response, | 282 const std::string& response, |
| 282 std::string* token) { | 283 std::string* token) { |
| 283 DCHECK(token); | 284 DCHECK(token); |
| 284 | 285 |
| 285 *token = response; | 286 *token = response; |
| 286 size_t position = token->find(kAuthorizationTokenPrefix); | 287 size_t position = token->find(kAuthorizationTokenPrefix); |
| 287 if (0 != position) | 288 if (0 != position) |
| 288 return false; | 289 return false; |
| 289 token->replace(position, arraysize(kAuthorizationTokenPrefix) - 1, ""); | 290 token->replace(position, arraysize(kAuthorizationTokenPrefix) - 1, ""); |
| 290 | 291 |
| 291 position = token->find(kAuthorizationTokenSuffix); | 292 position = token->find(kAuthorizationTokenSuffix); |
| 292 if (token->size() != (position + (arraysize(kAuthorizationTokenSuffix) - 1))) | 293 if (token->size() != (position + (arraysize(kAuthorizationTokenSuffix) - 1))) |
| 293 return false; | 294 return false; |
| 294 token->replace(position, arraysize(kAuthorizationTokenSuffix) - 1, ""); | 295 token->replace(position, arraysize(kAuthorizationTokenSuffix) - 1, ""); |
| 295 | 296 |
| 296 return true; | 297 return true; |
| 297 } | 298 } |
| 298 | 299 |
| 299 // Parsing | 300 // Parsing |
| 300 bool Toolbar5Importer::ParseBookmarksFromReader( | 301 bool Toolbar5Importer::ParseBookmarksFromReader( |
| 301 XmlReader* reader, | 302 XmlReader* reader, |
| 302 std::vector<ProfileWriter::BookmarkEntry>* bookmarks, | 303 std::vector<ImportedBookmarkEntry>* bookmarks, |
| 303 const string16& bookmark_group_string) { | 304 const string16& bookmark_group_string) { |
| 304 DCHECK(reader); | 305 DCHECK(reader); |
| 305 DCHECK(bookmarks); | 306 DCHECK(bookmarks); |
| 306 | 307 |
| 307 // The XML blob returned from the server is described in the | 308 // The XML blob returned from the server is described in the |
| 308 // Toolbar-Notebook/Bookmarks Protocol document located at | 309 // Toolbar-Notebook/Bookmarks Protocol document located at |
| 309 // https://docs.google.com/a/google.com/Doc?docid=cgt3m7dr_24djt62m&hl=en | 310 // https://docs.google.com/a/google.com/Doc?docid=cgt3m7dr_24djt62m&hl=en |
| 310 // We are searching for the section with structure | 311 // We are searching for the section with structure |
| 311 // <bookmarks><bookmark>...</bookmark><bookmark>...</bookmark></bookmarks> | 312 // <bookmarks><bookmark>...</bookmark><bookmark>...</bookmark></bookmarks> |
| 312 | 313 |
| 313 // Locate the |bookmarks| blob. | 314 // Locate the |bookmarks| blob. |
| 314 if (!reader->SkipToElement()) | 315 if (!reader->SkipToElement()) |
| 315 return false; | 316 return false; |
| 316 | 317 |
| 317 if (!LocateNextTagByName(reader, kBookmarksXmlTag)) | 318 if (!LocateNextTagByName(reader, kBookmarksXmlTag)) |
| 318 return false; | 319 return false; |
| 319 | 320 |
| 320 // Parse each |bookmark| blob | 321 // Parse each |bookmark| blob |
| 321 while (LocateNextTagWithStopByName(reader, kBookmarkXmlTag, | 322 while (LocateNextTagWithStopByName(reader, kBookmarkXmlTag, |
| 322 kBookmarksXmlTag)) { | 323 kBookmarksXmlTag)) { |
| 323 ProfileWriter::BookmarkEntry bookmark_entry; | 324 ImportedBookmarkEntry bookmark_entry; |
| 324 std::vector<BookmarkFolderType> folders; | 325 std::vector<BookmarkFolderType> folders; |
| 325 if (ExtractBookmarkInformation(reader, &bookmark_entry, &folders, | 326 if (ExtractBookmarkInformation(reader, &bookmark_entry, &folders, |
| 326 bookmark_group_string)) { | 327 bookmark_group_string)) { |
| 327 // For each folder we create a new bookmark entry. Duplicates will | 328 // For each folder we create a new bookmark entry. Duplicates will |
| 328 // be detected when we attempt to create the bookmark in the profile. | 329 // be detected when we attempt to create the bookmark in the profile. |
| 329 for (std::vector<BookmarkFolderType>::iterator folder = folders.begin(); | 330 for (std::vector<BookmarkFolderType>::iterator folder = folders.begin(); |
| 330 folder != folders.end(); | 331 folder != folders.end(); |
| 331 ++folder) { | 332 ++folder) { |
| 332 bookmark_entry.path = *folder; | 333 bookmark_entry.path = *folder; |
| 333 bookmarks->push_back(bookmark_entry); | 334 bookmarks->push_back(bookmark_entry); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 return false; | 377 return false; |
| 377 // If we encounter the stop word return false. | 378 // If we encounter the stop word return false. |
| 378 if (stop == reader->NodeName()) | 379 if (stop == reader->NodeName()) |
| 379 return false; | 380 return false; |
| 380 } | 381 } |
| 381 return true; | 382 return true; |
| 382 } | 383 } |
| 383 | 384 |
| 384 bool Toolbar5Importer::ExtractBookmarkInformation( | 385 bool Toolbar5Importer::ExtractBookmarkInformation( |
| 385 XmlReader* reader, | 386 XmlReader* reader, |
| 386 ProfileWriter::BookmarkEntry* bookmark_entry, | 387 ImportedBookmarkEntry* bookmark_entry, |
| 387 std::vector<BookmarkFolderType>* bookmark_folders, | 388 std::vector<BookmarkFolderType>* bookmark_folders, |
| 388 const string16& bookmark_group_string) { | 389 const string16& bookmark_group_string) { |
| 389 DCHECK(reader); | 390 DCHECK(reader); |
| 390 DCHECK(bookmark_entry); | 391 DCHECK(bookmark_entry); |
| 391 DCHECK(bookmark_folders); | 392 DCHECK(bookmark_folders); |
| 392 | 393 |
| 393 // The following is a typical bookmark entry. | 394 // The following is a typical bookmark entry. |
| 394 // The reader should be pointing to the <title> tag at the moment. | 395 // The reader should be pointing to the <title> tag at the moment. |
| 395 // | 396 // |
| 396 // <bookmark> | 397 // <bookmark> |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 | 450 |
| 450 if (name != reader->NodeName()) | 451 if (name != reader->NodeName()) |
| 451 return false; | 452 return false; |
| 452 if (!reader->ReadElementContent(buffer)) | 453 if (!reader->ReadElementContent(buffer)) |
| 453 return false; | 454 return false; |
| 454 return true; | 455 return true; |
| 455 } | 456 } |
| 456 | 457 |
| 457 bool Toolbar5Importer::ExtractTitleFromXmlReader( | 458 bool Toolbar5Importer::ExtractTitleFromXmlReader( |
| 458 XmlReader* reader, | 459 XmlReader* reader, |
| 459 ProfileWriter::BookmarkEntry* entry) { | 460 ImportedBookmarkEntry* entry) { |
| 460 DCHECK(reader); | 461 DCHECK(reader); |
| 461 DCHECK(entry); | 462 DCHECK(entry); |
| 462 | 463 |
| 463 if (!LocateNextTagWithStopByName(reader, kTitleXmlTag, kUrlXmlTag)) | 464 if (!LocateNextTagWithStopByName(reader, kTitleXmlTag, kUrlXmlTag)) |
| 464 return false; | 465 return false; |
| 465 std::string buffer; | 466 std::string buffer; |
| 466 if (!ExtractNamedValueFromXmlReader(reader, kTitleXmlTag, &buffer)) { | 467 if (!ExtractNamedValueFromXmlReader(reader, kTitleXmlTag, &buffer)) { |
| 467 return false; | 468 return false; |
| 468 } | 469 } |
| 469 entry->title = UTF8ToUTF16(buffer); | 470 entry->title = UTF8ToUTF16(buffer); |
| 470 return true; | 471 return true; |
| 471 } | 472 } |
| 472 | 473 |
| 473 bool Toolbar5Importer::ExtractUrlFromXmlReader( | 474 bool Toolbar5Importer::ExtractUrlFromXmlReader( |
| 474 XmlReader* reader, | 475 XmlReader* reader, |
| 475 ProfileWriter::BookmarkEntry* entry) { | 476 ImportedBookmarkEntry* entry) { |
| 476 DCHECK(reader); | 477 DCHECK(reader); |
| 477 DCHECK(entry); | 478 DCHECK(entry); |
| 478 | 479 |
| 479 if (!LocateNextTagWithStopByName(reader, kUrlXmlTag, kTimestampXmlTag)) | 480 if (!LocateNextTagWithStopByName(reader, kUrlXmlTag, kTimestampXmlTag)) |
| 480 return false; | 481 return false; |
| 481 std::string buffer; | 482 std::string buffer; |
| 482 if (!ExtractNamedValueFromXmlReader(reader, kUrlXmlTag, &buffer)) { | 483 if (!ExtractNamedValueFromXmlReader(reader, kUrlXmlTag, &buffer)) { |
| 483 return false; | 484 return false; |
| 484 } | 485 } |
| 485 entry->url = GURL(buffer); | 486 entry->url = GURL(buffer); |
| 486 return true; | 487 return true; |
| 487 } | 488 } |
| 488 | 489 |
| 489 bool Toolbar5Importer::ExtractTimeFromXmlReader( | 490 bool Toolbar5Importer::ExtractTimeFromXmlReader( |
| 490 XmlReader* reader, | 491 XmlReader* reader, |
| 491 ProfileWriter::BookmarkEntry* entry) { | 492 ImportedBookmarkEntry* entry) { |
| 492 DCHECK(reader); | 493 DCHECK(reader); |
| 493 DCHECK(entry); | 494 DCHECK(entry); |
| 494 if (!LocateNextTagWithStopByName(reader, kTimestampXmlTag, kLabelsXmlTag)) | 495 if (!LocateNextTagWithStopByName(reader, kTimestampXmlTag, kLabelsXmlTag)) |
| 495 return false; | 496 return false; |
| 496 std::string buffer; | 497 std::string buffer; |
| 497 if (!ExtractNamedValueFromXmlReader(reader, kTimestampXmlTag, &buffer)) { | 498 if (!ExtractNamedValueFromXmlReader(reader, kTimestampXmlTag, &buffer)) { |
| 498 return false; | 499 return false; |
| 499 } | 500 } |
| 500 int64 timestamp; | 501 int64 timestamp; |
| 501 if (!base::StringToInt64(buffer, ×tamp)) { | 502 if (!base::StringToInt64(buffer, ×tamp)) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 std::vector<string16> folder_names; | 557 std::vector<string16> folder_names; |
| 557 base::SplitString(label_vector[index], ':', &folder_names); | 558 base::SplitString(label_vector[index], ':', &folder_names); |
| 558 (*bookmark_folders)[index].insert((*bookmark_folders)[index].end(), | 559 (*bookmark_folders)[index].insert((*bookmark_folders)[index].end(), |
| 559 folder_names.begin(), folder_names.end()); | 560 folder_names.begin(), folder_names.end()); |
| 560 } | 561 } |
| 561 | 562 |
| 562 return true; | 563 return true; |
| 563 } | 564 } |
| 564 | 565 |
| 565 void Toolbar5Importer::AddBookmarksToChrome( | 566 void Toolbar5Importer::AddBookmarksToChrome( |
| 566 const std::vector<ProfileWriter::BookmarkEntry>& bookmarks) { | 567 const std::vector<ImportedBookmarkEntry>& bookmarks) { |
| 567 if (!bookmarks.empty() && !cancelled()) { | 568 if (!bookmarks.empty() && !cancelled()) { |
| 568 const string16& first_folder_name = | 569 const string16& first_folder_name = |
| 569 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_GOOGLE_TOOLBAR); | 570 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_GOOGLE_TOOLBAR); |
| 570 bridge_->AddBookmarks(bookmarks, first_folder_name); | 571 bridge_->AddBookmarks(bookmarks, first_folder_name); |
| 571 } | 572 } |
| 572 } | 573 } |
| OLD | NEW |