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
3 // found in the LICENSE file.
5 #include "chrome/browser/importer/external_process_importer_host.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/importer/external_process_importer_client.h"
11 #include "chrome/browser/importer/firefox_profile_lock.h"
12 #include "chrome/browser/importer/importer_lock_dialog.h"
13 #include "chrome/browser/importer/importer_progress_observer.h"
14 #include "chrome/browser/importer/in_process_importer_bridge.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/search_engines/template_url_service.h"
18 #include "content/public/browser/browser_thread.h"
20 using bookmarks::BookmarkModel
;
21 using content::BrowserThread
;
23 ExternalProcessImporterHost::ExternalProcessImporterHost()
28 waiting_for_bookmarkbar_model_(false),
29 installed_bookmark_observer_(false),
30 is_source_readable_(true),
34 weak_ptr_factory_(this) {
37 void ExternalProcessImporterHost::Cancel() {
39 // There is only a |client_| if the import was started.
42 NotifyImportEnded(); // Tells the observer that we're done, and deletes us.
45 void ExternalProcessImporterHost::StartImportSettings(
46 const importer::SourceProfile
& source_profile
,
47 Profile
* target_profile
,
49 ProfileWriter
* writer
) {
50 // We really only support importing from one host at a time.
52 DCHECK(target_profile
);
54 profile_
= target_profile
;
56 source_profile_
= source_profile
;
59 if (!CheckForFirefoxLock(source_profile
)) {
64 CheckForLoadedModels(items
);
66 LaunchImportIfReady();
69 void ExternalProcessImporterHost::NotifyImportStarted() {
71 observer_
->ImportStarted();
74 void ExternalProcessImporterHost::NotifyImportItemStarted(
75 importer::ImportItem item
) {
77 observer_
->ImportItemStarted(item
);
80 void ExternalProcessImporterHost::NotifyImportItemEnded(
81 importer::ImportItem item
) {
83 observer_
->ImportItemEnded(item
);
86 void ExternalProcessImporterHost::NotifyImportEnded() {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
88 firefox_lock_
.reset();
90 observer_
->ImportEnded();
94 ExternalProcessImporterHost::~ExternalProcessImporterHost() {
95 if (installed_bookmark_observer_
) {
97 BookmarkModelFactory::GetForProfile(profile_
)->RemoveObserver(this);
101 void ExternalProcessImporterHost::LaunchImportIfReady() {
102 if (waiting_for_bookmarkbar_model_
|| template_service_subscription_
.get() ||
103 !is_source_readable_
|| cancelled_
)
106 // This is the in-process half of the bridge, which catches data from the IPC
107 // pipe and feeds it to the ProfileWriter. The external process half of the
108 // bridge lives in the external process (see ProfileImportThread).
109 // The ExternalProcessImporterClient created in the next line owns the bridge,
110 // and will delete it.
111 InProcessImporterBridge
* bridge
=
112 new InProcessImporterBridge(writer_
.get(),
113 weak_ptr_factory_
.GetWeakPtr());
114 client_
= new ExternalProcessImporterClient(
115 weak_ptr_factory_
.GetWeakPtr(), source_profile_
, items_
, bridge
);
119 void ExternalProcessImporterHost::BookmarkModelLoaded(BookmarkModel
* model
,
120 bool ids_reassigned
) {
121 DCHECK(model
->loaded());
122 model
->RemoveObserver(this);
123 waiting_for_bookmarkbar_model_
= false;
124 installed_bookmark_observer_
= false;
126 LaunchImportIfReady();
129 void ExternalProcessImporterHost::BookmarkModelBeingDeleted(
130 BookmarkModel
* model
) {
131 installed_bookmark_observer_
= false;
134 void ExternalProcessImporterHost::BookmarkModelChanged() {
137 void ExternalProcessImporterHost::OnTemplateURLServiceLoaded() {
138 template_service_subscription_
.reset();
139 LaunchImportIfReady();
142 void ExternalProcessImporterHost::ShowWarningDialog() {
144 importer::ShowImportLockDialog(
146 base::Bind(&ExternalProcessImporterHost::OnImportLockDialogEnd
,
147 weak_ptr_factory_
.GetWeakPtr()));
150 void ExternalProcessImporterHost::OnImportLockDialogEnd(bool is_continue
) {
152 // User chose to continue, then we check the lock again to make
153 // sure that Firefox has been closed. Try to import the settings
154 // if successful. Otherwise, show a warning dialog.
155 firefox_lock_
->Lock();
156 if (firefox_lock_
->HasAcquired()) {
157 is_source_readable_
= true;
158 LaunchImportIfReady();
167 bool ExternalProcessImporterHost::CheckForFirefoxLock(
168 const importer::SourceProfile
& source_profile
) {
169 if (source_profile
.importer_type
!= importer::TYPE_FIREFOX
)
172 DCHECK(!firefox_lock_
.get());
173 firefox_lock_
.reset(new FirefoxProfileLock(source_profile
.source_path
));
174 if (firefox_lock_
->HasAcquired())
177 // If fail to acquire the lock, we set the source unreadable and
178 // show a warning dialog, unless running without UI (in which case the import
180 is_source_readable_
= false;
188 void ExternalProcessImporterHost::CheckForLoadedModels(uint16 items
) {
189 // A target profile must be loaded by StartImportSettings().
192 // BookmarkModel should be loaded before adding IE favorites. So we observe
193 // the BookmarkModel if needed, and start the task after it has been loaded.
194 if ((items
& importer::FAVORITES
) && !writer_
->BookmarkModelIsLoaded()) {
195 BookmarkModelFactory::GetForProfile(profile_
)->AddObserver(this);
196 waiting_for_bookmarkbar_model_
= true;
197 installed_bookmark_observer_
= true;
200 // Observes the TemplateURLService if needed to import search engines from the
201 // other browser. We also check to see if we're importing bookmarks because
202 // we can import bookmark keywords from Firefox as search engines.
203 if ((items
& importer::SEARCH_ENGINES
) || (items
& importer::FAVORITES
)) {
204 if (!writer_
->TemplateURLServiceIsLoaded()) {
205 TemplateURLService
* model
=
206 TemplateURLServiceFactory::GetForProfile(profile_
);
207 template_service_subscription_
= model
->RegisterOnLoadedCallback(
208 base::Bind(&ExternalProcessImporterHost::OnTemplateURLServiceLoaded
,
209 weak_ptr_factory_
.GetWeakPtr()));