1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/importer/importer.h"
#include "app/l10n_util.h"
#include "base/string_util.h"
#include "base/thread.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browsing_instance.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/importer/firefox_profile_lock.h"
#include "chrome/browser/importer/importer_bridge.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/renderer_host/site_instance.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/pref_names.h"
#include "gfx/codec/png_codec.h"
#include "gfx/favicon_size.h"
#include "grit/generated_resources.h"
#include "skia/ext/image_operations.h"
#include "webkit/glue/image_decoder.h"
// TODO(port): Port these files.
#if defined(OS_WIN)
#include "app/win_util.h"
#include "chrome/browser/views/importer_lock_view.h"
#include "views/window/window.h"
#elif defined(OS_MACOSX)
#include "chrome/browser/cocoa/importer_lock_dialog.h"
#elif defined(TOOLKIT_USES_GTK)
#include "chrome/browser/gtk/import_lock_dialog_gtk.h"
#endif
using webkit_glue::PasswordForm;
// Importer.
Importer::Importer()
: cancelled_(false),
import_to_bookmark_bar_(false) {
}
Importer::~Importer() {
}
// static
bool Importer::ReencodeFavicon(const unsigned char* src_data, size_t src_len,
std::vector<unsigned char>* png_data) {
// Decode the favicon using WebKit's image decoder.
webkit_glue::ImageDecoder decoder(gfx::Size(kFavIconSize, kFavIconSize));
SkBitmap decoded = decoder.Decode(src_data, src_len);
if (decoded.empty())
return false; // Unable to decode.
if (decoded.width() != kFavIconSize || decoded.height() != kFavIconSize) {
// The bitmap is not the correct size, re-sample.
int new_width = decoded.width();
int new_height = decoded.height();
calc_favicon_target_size(&new_width, &new_height);
decoded = skia::ImageOperations::Resize(
decoded, skia::ImageOperations::RESIZE_LANCZOS3, new_width, new_height);
}
// Encode our bitmap as a PNG.
gfx::PNGCodec::EncodeBGRASkBitmap(decoded, false, png_data);
return true;
}
// ImporterHost.
ImporterHost::ImporterHost()
: profile_(NULL),
observer_(NULL),
task_(NULL),
importer_(NULL),
waiting_for_bookmarkbar_model_(false),
installed_bookmark_observer_(false),
is_source_readable_(true),
headless_(false),
parent_window_(NULL) {
importer_list_.DetectSourceProfiles();
}
ImporterHost::~ImporterHost() {
if (NULL != importer_)
importer_->Release();
if (installed_bookmark_observer_) {
DCHECK(profile_); // Only way for waiting_for_bookmarkbar_model_ to be true
// is if we have a profile.
profile_->GetBookmarkModel()->RemoveObserver(this);
}
}
void ImporterHost::Loaded(BookmarkModel* model) {
DCHECK(model->IsLoaded());
model->RemoveObserver(this);
waiting_for_bookmarkbar_model_ = false;
installed_bookmark_observer_ = false;
std::vector<GURL> starred_urls;
model->GetBookmarks(&starred_urls);
importer_->set_import_to_bookmark_bar(starred_urls.size() == 0);
InvokeTaskIfDone();
}
void ImporterHost::BookmarkModelBeingDeleted(BookmarkModel* model) {
installed_bookmark_observer_ = false;
}
void ImporterHost::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::TEMPLATE_URL_MODEL_LOADED);
registrar_.RemoveAll();
InvokeTaskIfDone();
}
void ImporterHost::ShowWarningDialog() {
if (headless_) {
OnLockViewEnd(false);
} else {
#if defined(OS_WIN)
views::Window::CreateChromeWindow(GetActiveWindow(), gfx::Rect(),
new ImporterLockView(this))->Show();
#elif defined(TOOLKIT_USES_GTK)
ImportLockDialogGtk::Show(parent_window_, this);
#else
ImportLockDialogCocoa::ShowWarning(this);
#endif
}
}
void ImporterHost::OnLockViewEnd(bool is_continue) {
if (is_continue) {
// User chose to continue, then we check the lock again to make
// sure that Firefox has been closed. Try to import the settings
// if successful. Otherwise, show a warning dialog.
firefox_lock_->Lock();
if (firefox_lock_->HasAcquired()) {
is_source_readable_ = true;
InvokeTaskIfDone();
} else {
ShowWarningDialog();
}
} else {
// User chose to skip the import process. We should delete
// the task and notify the ImporterHost to finish.
delete task_;
task_ = NULL;
importer_ = NULL;
ImportEnded();
}
}
void ImporterHost::StartImportSettings(
const importer::ProfileInfo& profile_info,
Profile* target_profile,
uint16 items,
ProfileWriter* writer,
bool first_run) {
DCHECK(!profile_); // We really only support importing from one host at a
// time.
profile_ = target_profile;
// Preserves the observer and creates a task, since we do async import
// so that it doesn't block the UI. When the import is complete, observer
// will be notified.
writer_ = writer;
importer_ = importer_list_.CreateImporterByType(profile_info.browser_type);
// If we fail to create Importer, exit as we can not do anything.
if (!importer_) {
ImportEnded();
return;
}
importer_->AddRef();
bool import_to_bookmark_bar = first_run;
if (target_profile && target_profile->GetBookmarkModel()->IsLoaded()) {
std::vector<GURL> starred_urls;
target_profile->GetBookmarkModel()->GetBookmarks(&starred_urls);
import_to_bookmark_bar = (starred_urls.size() == 0);
}
importer_->set_import_to_bookmark_bar(import_to_bookmark_bar);
scoped_refptr<ImporterBridge> bridge(
new InProcessImporterBridge(writer_.get(), this));
task_ = NewRunnableMethod(importer_, &Importer::StartImport,
profile_info, items, bridge);
// We should lock the Firefox profile directory to prevent corruption.
if (profile_info.browser_type == importer::FIREFOX2 ||
profile_info.browser_type == importer::FIREFOX3) {
firefox_lock_.reset(new FirefoxProfileLock(profile_info.source_path));
if (!firefox_lock_->HasAcquired()) {
// If fail to acquire the lock, we set the source unreadable and
// show a warning dialog.
// However, if we're running without a UI (silently) and trying to
// import just the home page, then import anyway. The home page setting
// is stored in an unlocked text file, so it is the only preference safe
// to import even if Firefox is running.
if (items == importer::HOME_PAGE && first_run && this->headless_) {
AddRef();
InvokeTaskIfDone();
return;
}
is_source_readable_ = false;
ShowWarningDialog();
}
}
#if defined(OS_WIN)
// For google toolbar import, we need the user to log in and store their GAIA
// credentials.
if (profile_info.browser_type == importer::GOOGLE_TOOLBAR5) {
if (!toolbar_importer_utils::IsGoogleGAIACookieInstalled()) {
win_util::MessageBox(
NULL,
l10n_util::GetString(IDS_IMPORTER_GOOGLE_LOGIN_TEXT).c_str(),
L"",
MB_OK | MB_TOPMOST);
GURL url("https://www.google.com/accounts/ServiceLogin");
BrowsingInstance* instance = new BrowsingInstance(writer_->profile());
SiteInstance* site = instance->GetSiteInstanceForURL(url);
Browser* browser = BrowserList::GetLastActive();
browser->AddTabWithURL(url, GURL(), PageTransition::TYPED,
-1, Browser::ADD_SELECTED, site, std::string());
MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
this, &ImporterHost::OnLockViewEnd, false));
is_source_readable_ = false;
}
}
#endif
// BookmarkModel should be loaded before adding IE favorites. So we observe
// the BookmarkModel if needed, and start the task after it has been loaded.
if ((items & importer::FAVORITES) && !writer_->BookmarkModelIsLoaded()) {
target_profile->GetBookmarkModel()->AddObserver(this);
waiting_for_bookmarkbar_model_ = true;
installed_bookmark_observer_ = true;
}
// Observes the TemplateURLModel if needed to import search engines from the
// other browser. We also check to see if we're importing bookmarks because
// we can import bookmark keywords from Firefox as search engines.
if ((items & importer::SEARCH_ENGINES) || (items & importer::FAVORITES)) {
if (!writer_->TemplateURLModelIsLoaded()) {
TemplateURLModel* model = target_profile->GetTemplateURLModel();
registrar_.Add(this, NotificationType::TEMPLATE_URL_MODEL_LOADED,
Source<TemplateURLModel>(model));
model->Load();
}
}
AddRef();
InvokeTaskIfDone();
}
void ImporterHost::Cancel() {
if (importer_)
importer_->Cancel();
}
void ImporterHost::SetObserver(Observer* observer) {
observer_ = observer;
}
void ImporterHost::InvokeTaskIfDone() {
if (waiting_for_bookmarkbar_model_ || !registrar_.IsEmpty() ||
!is_source_readable_)
return;
ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, task_);
}
void ImporterHost::ImportItemStarted(importer::ImportItem item) {
if (observer_)
observer_->ImportItemStarted(item);
}
void ImporterHost::ImportItemEnded(importer::ImportItem item) {
if (observer_)
observer_->ImportItemEnded(item);
}
void ImporterHost::ImportStarted() {
if (observer_)
observer_->ImportStarted();
}
void ImporterHost::ImportEnded() {
firefox_lock_.reset(); // Release the Firefox profile lock.
if (observer_)
observer_->ImportEnded();
Release();
}
|