| 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.h" | 5 #include "chrome/browser/importer.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/gfx/image_operations.h" | 10 #include "base/gfx/image_operations.h" |
| 11 #include "base/gfx/png_encoder.h" | 11 #include "base/gfx/png_encoder.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_bar_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/firefox2_importer.h" | 15 #include "chrome/browser/firefox2_importer.h" |
| 16 #include "chrome/browser/firefox3_importer.h" | 16 #include "chrome/browser/firefox3_importer.h" |
| 17 #include "chrome/browser/firefox_importer_utils.h" | 17 #include "chrome/browser/firefox_importer_utils.h" |
| 18 #include "chrome/browser/firefox_profile_lock.h" | 18 #include "chrome/browser/firefox_profile_lock.h" |
| 19 #include "chrome/browser/ie_importer.h" | 19 #include "chrome/browser/ie_importer.h" |
| 20 #include "chrome/browser/template_url_model.h" | 20 #include "chrome/browser/template_url_model.h" |
| 21 #include "chrome/browser/shell_integration.h" | 21 #include "chrome/browser/shell_integration.h" |
| 22 #include "chrome/browser/webdata/web_data_service.h" | 22 #include "chrome/browser/webdata/web_data_service.h" |
| 23 #include "chrome/common/gfx/favicon_size.h" | 23 #include "chrome/common/gfx/favicon_size.h" |
| 24 #include "chrome/common/l10n_util.h" | 24 #include "chrome/common/l10n_util.h" |
| 25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/common/pref_service.h" | 26 #include "chrome/common/pref_service.h" |
| 27 #include "chrome/views/window.h" | 27 #include "chrome/views/window.h" |
| 28 #include "webkit/glue/image_decoder.h" | 28 #include "webkit/glue/image_decoder.h" |
| 29 | 29 |
| 30 #include "generated_resources.h" | 30 #include "generated_resources.h" |
| 31 | 31 |
| 32 // ProfileWriter. | 32 // ProfileWriter. |
| 33 | 33 |
| 34 bool ProfileWriter::BookmarkBarModelIsLoaded() const { | 34 bool ProfileWriter::BookmarkModelIsLoaded() const { |
| 35 return profile_->GetBookmarkBarModel()->IsLoaded(); | 35 return profile_->GetBookmarkModel()->IsLoaded(); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void ProfileWriter::AddBookmarkBarModelObserver( | 38 void ProfileWriter::AddBookmarkModelObserver(BookmarkModelObserver* observer) { |
| 39 BookmarkBarModelObserver* observer) { | 39 profile_->GetBookmarkModel()->AddObserver(observer); |
| 40 profile_->GetBookmarkBarModel()->AddObserver(observer); | |
| 41 } | 40 } |
| 42 | 41 |
| 43 bool ProfileWriter::TemplateURLModelIsLoaded() const { | 42 bool ProfileWriter::TemplateURLModelIsLoaded() const { |
| 44 return profile_->GetTemplateURLModel()->loaded(); | 43 return profile_->GetTemplateURLModel()->loaded(); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void ProfileWriter::AddTemplateURLModelObserver( | 46 void ProfileWriter::AddTemplateURLModelObserver( |
| 48 NotificationObserver* observer) { | 47 NotificationObserver* observer) { |
| 49 TemplateURLModel* model = profile_->GetTemplateURLModel(); | 48 TemplateURLModel* model = profile_->GetTemplateURLModel(); |
| 50 NotificationService::current()->AddObserver( | 49 NotificationService::current()->AddObserver( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 70 DCHECK(profile_); | 69 DCHECK(profile_); |
| 71 | 70 |
| 72 PrefService* prefs = profile_->GetPrefs(); | 71 PrefService* prefs = profile_->GetPrefs(); |
| 73 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. | 72 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. |
| 74 prefs->SetString(prefs::kHomePage, ASCIIToWide(home_page.spec())); | 73 prefs->SetString(prefs::kHomePage, ASCIIToWide(home_page.spec())); |
| 75 prefs->ScheduleSavePersistentPrefs(g_browser_process->file_thread()); | 74 prefs->ScheduleSavePersistentPrefs(g_browser_process->file_thread()); |
| 76 } | 75 } |
| 77 | 76 |
| 78 void ProfileWriter::AddBookmarkEntry( | 77 void ProfileWriter::AddBookmarkEntry( |
| 79 const std::vector<BookmarkEntry>& bookmark) { | 78 const std::vector<BookmarkEntry>& bookmark) { |
| 80 BookmarkBarModel* model = profile_->GetBookmarkBarModel(); | 79 BookmarkModel* model = profile_->GetBookmarkModel(); |
| 81 DCHECK(model->IsLoaded()); | 80 DCHECK(model->IsLoaded()); |
| 82 | 81 |
| 83 bool show_bookmark_toolbar = false; | 82 bool show_bookmark_toolbar = false; |
| 84 std::set<BookmarkBarNode*> groups_added_to; | 83 std::set<BookmarkNode*> groups_added_to; |
| 85 for (std::vector<BookmarkEntry>::const_iterator it = bookmark.begin(); | 84 for (std::vector<BookmarkEntry>::const_iterator it = bookmark.begin(); |
| 86 it != bookmark.end(); ++it) { | 85 it != bookmark.end(); ++it) { |
| 87 // Don't insert this url if it exists in model or url is not valid. | 86 // Don't insert this url if it exists in model or url is not valid. |
| 88 if (model->GetNodeByURL(it->url) != NULL || !it->url.is_valid()) | 87 if (model->GetNodeByURL(it->url) != NULL || !it->url.is_valid()) |
| 89 continue; | 88 continue; |
| 90 | 89 |
| 91 // Set up groups in BookmarkBarModel in such a way that path[i] is | 90 // Set up groups in BookmarkModel in such a way that path[i] is |
| 92 // the subgroup of path[i-1]. Finally they construct a path in the | 91 // the subgroup of path[i-1]. Finally they construct a path in the |
| 93 // model: | 92 // model: |
| 94 // path[0] \ path[1] \ ... \ path[size() - 1] | 93 // path[0] \ path[1] \ ... \ path[size() - 1] |
| 95 BookmarkBarNode* parent = | 94 BookmarkNode* parent = |
| 96 (it->in_toolbar ? model->GetBookmarkBarNode() : model->other_node()); | 95 (it->in_toolbar ? model->GetBookmarkBarNode() : model->other_node()); |
| 97 for (std::vector<std::wstring>::const_iterator i = it->path.begin(); | 96 for (std::vector<std::wstring>::const_iterator i = it->path.begin(); |
| 98 i != it->path.end(); ++i) { | 97 i != it->path.end(); ++i) { |
| 99 BookmarkBarNode* child = NULL; | 98 BookmarkNode* child = NULL; |
| 100 for (int index = 0; index < parent->GetChildCount(); ++index) { | 99 for (int index = 0; index < parent->GetChildCount(); ++index) { |
| 101 BookmarkBarNode* node = parent->GetChild(index); | 100 BookmarkNode* node = parent->GetChild(index); |
| 102 if ((node->GetType() == history::StarredEntry::BOOKMARK_BAR || | 101 if ((node->GetType() == history::StarredEntry::BOOKMARK_BAR || |
| 103 node->GetType() == history::StarredEntry::USER_GROUP) && | 102 node->GetType() == history::StarredEntry::USER_GROUP) && |
| 104 node->GetTitle() == *i) { | 103 node->GetTitle() == *i) { |
| 105 child = node; | 104 child = node; |
| 106 break; | 105 break; |
| 107 } | 106 } |
| 108 } | 107 } |
| 109 if (child == NULL) | 108 if (child == NULL) |
| 110 child = model->AddGroup(parent, parent->GetChildCount(), *i); | 109 child = model->AddGroup(parent, parent->GetChildCount(), *i); |
| 111 parent = child; | 110 parent = child; |
| 112 } | 111 } |
| 113 groups_added_to.insert(parent); | 112 groups_added_to.insert(parent); |
| 114 model->AddURLWithCreationTime(parent, parent->GetChildCount(), | 113 model->AddURLWithCreationTime(parent, parent->GetChildCount(), |
| 115 it->title, it->url, it->creation_time); | 114 it->title, it->url, it->creation_time); |
| 116 | 115 |
| 117 // If some items are put into toolbar, it looks like the user was using | 116 // If some items are put into toolbar, it looks like the user was using |
| 118 // it in their last browser. We turn on the bookmarks toolbar. | 117 // it in their last browser. We turn on the bookmarks toolbar. |
| 119 if (it->in_toolbar) | 118 if (it->in_toolbar) |
| 120 show_bookmark_toolbar = true; | 119 show_bookmark_toolbar = true; |
| 121 } | 120 } |
| 122 | 121 |
| 123 // Reset the date modified time of the groups we added to. We do this to | 122 // Reset the date modified time of the groups we added to. We do this to |
| 124 // make sure the 'recently added to' combobox in the bubble doesn't get random | 123 // make sure the 'recently added to' combobox in the bubble doesn't get random |
| 125 // groups. | 124 // groups. |
| 126 for (std::set<BookmarkBarNode*>::const_iterator i = groups_added_to.begin(); | 125 for (std::set<BookmarkNode*>::const_iterator i = groups_added_to.begin(); |
| 127 i != groups_added_to.end(); ++i) { | 126 i != groups_added_to.end(); ++i) { |
| 128 model->ResetDateGroupModified(*i); | 127 model->ResetDateGroupModified(*i); |
| 129 } | 128 } |
| 130 | 129 |
| 131 if (show_bookmark_toolbar) | 130 if (show_bookmark_toolbar) |
| 132 ShowBookmarkBar(); | 131 ShowBookmarkBar(); |
| 133 } | 132 } |
| 134 | 133 |
| 135 void ProfileWriter::AddFavicons( | 134 void ProfileWriter::AddFavicons( |
| 136 const std::vector<history::ImportedFavIconUsage>& favicons) { | 135 const std::vector<history::ImportedFavIconUsage>& favicons) { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 waiting_for_bookmarkbar_model_(false), | 292 waiting_for_bookmarkbar_model_(false), |
| 294 waiting_for_template_url_model_(false), | 293 waiting_for_template_url_model_(false), |
| 295 is_source_readable_(true) { | 294 is_source_readable_(true) { |
| 296 DetectSourceProfiles(); | 295 DetectSourceProfiles(); |
| 297 } | 296 } |
| 298 | 297 |
| 299 ImporterHost::~ImporterHost() { | 298 ImporterHost::~ImporterHost() { |
| 300 STLDeleteContainerPointers(source_profiles_.begin(), source_profiles_.end()); | 299 STLDeleteContainerPointers(source_profiles_.begin(), source_profiles_.end()); |
| 301 } | 300 } |
| 302 | 301 |
| 303 void ImporterHost::Loaded(BookmarkBarModel* model) { | 302 void ImporterHost::Loaded(BookmarkModel* model) { |
| 304 model->RemoveObserver(this); | 303 model->RemoveObserver(this); |
| 305 waiting_for_bookmarkbar_model_ = false; | 304 waiting_for_bookmarkbar_model_ = false; |
| 306 InvokeTaskIfDone(); | 305 InvokeTaskIfDone(); |
| 307 } | 306 } |
| 308 | 307 |
| 309 void ImporterHost::Observe(NotificationType type, | 308 void ImporterHost::Observe(NotificationType type, |
| 310 const NotificationSource& source, | 309 const NotificationSource& source, |
| 311 const NotificationDetails& details) { | 310 const NotificationDetails& details) { |
| 312 DCHECK(type == TEMPLATE_URL_MODEL_LOADED); | 311 DCHECK(type == TEMPLATE_URL_MODEL_LOADED); |
| 313 TemplateURLModel* model = Source<TemplateURLModel>(source).ptr(); | 312 TemplateURLModel* model = Source<TemplateURLModel>(source).ptr(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 profile_info.browser_type == FIREFOX3) { | 362 profile_info.browser_type == FIREFOX3) { |
| 364 firefox_lock_.reset(new FirefoxProfileLock(profile_info.source_path)); | 363 firefox_lock_.reset(new FirefoxProfileLock(profile_info.source_path)); |
| 365 if (!firefox_lock_->HasAcquired()) { | 364 if (!firefox_lock_->HasAcquired()) { |
| 366 // If fail to acquire the lock, we set the source unreadable and | 365 // If fail to acquire the lock, we set the source unreadable and |
| 367 // show a warning dialog. | 366 // show a warning dialog. |
| 368 is_source_readable_ = false; | 367 is_source_readable_ = false; |
| 369 ShowWarningDialog(); | 368 ShowWarningDialog(); |
| 370 } | 369 } |
| 371 } | 370 } |
| 372 | 371 |
| 373 // BookmarkBarModel should be loaded before adding IE favorites. So we | 372 // BookmarkModel should be loaded before adding IE favorites. So we observe |
| 374 // observe the BookmarkBarModel if needed, and start the task after | 373 // the BookmarkModel if needed, and start the task after it has been loaded. |
| 375 // it has been loaded. | 374 if ((items & FAVORITES) && !writer_->BookmarkModelIsLoaded()) { |
| 376 if ((items & FAVORITES) && !writer_->BookmarkBarModelIsLoaded()) { | 375 writer_->AddBookmarkModelObserver(this); |
| 377 writer_->AddBookmarkBarModelObserver(this); | |
| 378 waiting_for_bookmarkbar_model_ = true; | 376 waiting_for_bookmarkbar_model_ = true; |
| 379 } | 377 } |
| 380 | 378 |
| 381 // Observes the TemplateURLModel if needed to import search engines from the | 379 // Observes the TemplateURLModel if needed to import search engines from the |
| 382 // other browser. We also check to see if we're importing bookmarks because | 380 // other browser. We also check to see if we're importing bookmarks because |
| 383 // we can import bookmark keywords from Firefox as search engines. | 381 // we can import bookmark keywords from Firefox as search engines. |
| 384 if ((items & SEARCH_ENGINES) || (items & FAVORITES)) { | 382 if ((items & SEARCH_ENGINES) || (items & FAVORITES)) { |
| 385 if (!writer_->TemplateURLModelIsLoaded()) { | 383 if (!writer_->TemplateURLModelIsLoaded()) { |
| 386 writer_->AddTemplateURLModelObserver(this); | 384 writer_->AddTemplateURLModelObserver(this); |
| 387 waiting_for_template_url_model_ = true; | 385 waiting_for_template_url_model_ = true; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 if (!source_path.empty()) { | 532 if (!source_path.empty()) { |
| 535 ProfileInfo* firefox = new ProfileInfo(); | 533 ProfileInfo* firefox = new ProfileInfo(); |
| 536 firefox->description = l10n_util::GetString(IDS_IMPORT_FROM_FIREFOX); | 534 firefox->description = l10n_util::GetString(IDS_IMPORT_FROM_FIREFOX); |
| 537 firefox->browser_type = firefox_type; | 535 firefox->browser_type = firefox_type; |
| 538 firefox->source_path = source_path; | 536 firefox->source_path = source_path; |
| 539 firefox->app_path = GetFirefoxInstallPath(); | 537 firefox->app_path = GetFirefoxInstallPath(); |
| 540 source_profiles_.push_back(firefox); | 538 source_profiles_.push_back(firefox); |
| 541 } | 539 } |
| 542 } | 540 } |
| 543 | 541 |
| OLD | NEW |