Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / importer / external_process_importer_host.cc
blob4700e4bdaa5b5a4fbb6e4003b877c9801d6482e6
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"
7 #include "base/bind.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()
24 : headless_(false),
25 parent_window_(NULL),
26 observer_(NULL),
27 profile_(NULL),
28 waiting_for_bookmarkbar_model_(false),
29 installed_bookmark_observer_(false),
30 is_source_readable_(true),
31 client_(NULL),
32 items_(0),
33 cancelled_(false),
34 weak_ptr_factory_(this) {
37 void ExternalProcessImporterHost::Cancel() {
38 cancelled_ = true;
39 // There is only a |client_| if the import was started.
40 if (client_)
41 client_->Cancel();
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,
48 uint16 items,
49 ProfileWriter* writer) {
50 // We really only support importing from one host at a time.
51 DCHECK(!profile_);
52 DCHECK(target_profile);
54 profile_ = target_profile;
55 writer_ = writer;
56 source_profile_ = source_profile;
57 items_ = items;
59 if (!CheckForFirefoxLock(source_profile)) {
60 Cancel();
61 return;
64 CheckForLoadedModels(items);
66 LaunchImportIfReady();
69 void ExternalProcessImporterHost::NotifyImportStarted() {
70 if (observer_)
71 observer_->ImportStarted();
74 void ExternalProcessImporterHost::NotifyImportItemStarted(
75 importer::ImportItem item) {
76 if (observer_)
77 observer_->ImportItemStarted(item);
80 void ExternalProcessImporterHost::NotifyImportItemEnded(
81 importer::ImportItem item) {
82 if (observer_)
83 observer_->ImportItemEnded(item);
86 void ExternalProcessImporterHost::NotifyImportEnded() {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
88 firefox_lock_.reset();
89 if (observer_)
90 observer_->ImportEnded();
91 delete this;
94 ExternalProcessImporterHost::~ExternalProcessImporterHost() {
95 if (installed_bookmark_observer_) {
96 DCHECK(profile_);
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_)
104 return;
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);
116 client_->Start();
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() {
143 DCHECK(!headless_);
144 importer::ShowImportLockDialog(
145 parent_window_,
146 base::Bind(&ExternalProcessImporterHost::OnImportLockDialogEnd,
147 weak_ptr_factory_.GetWeakPtr()));
150 void ExternalProcessImporterHost::OnImportLockDialogEnd(bool is_continue) {
151 if (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();
159 } else {
160 ShowWarningDialog();
162 } else {
163 NotifyImportEnded();
167 bool ExternalProcessImporterHost::CheckForFirefoxLock(
168 const importer::SourceProfile& source_profile) {
169 if (source_profile.importer_type != importer::TYPE_FIREFOX)
170 return true;
172 DCHECK(!firefox_lock_.get());
173 firefox_lock_.reset(new FirefoxProfileLock(source_profile.source_path));
174 if (firefox_lock_->HasAcquired())
175 return true;
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
179 // must be aborted).
180 is_source_readable_ = false;
181 if (headless_)
182 return false;
184 ShowWarningDialog();
185 return true;
188 void ExternalProcessImporterHost::CheckForLoadedModels(uint16 items) {
189 // A target profile must be loaded by StartImportSettings().
190 DCHECK(profile_);
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()));
210 model->Load();