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/ui/webui/options/import_data_handler.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/importer/external_process_importer_host.h"
21 #include "chrome/browser/importer/importer_list.h"
22 #include "chrome/browser/importer/importer_uma.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/chrome_select_file_policy.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/web_ui.h"
29 #include "grit/chromium_strings.h"
30 #include "grit/generated_resources.h"
31 #include "ui/base/l10n/l10n_util.h"
33 using content::BrowserThread
;
37 ImportDataHandler::ImportDataHandler()
38 : importer_host_(NULL
),
39 import_did_succeed_(false) {
40 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
43 ImportDataHandler::~ImportDataHandler() {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
47 importer_host_
->set_observer(NULL
);
49 if (select_file_dialog_
)
50 select_file_dialog_
->ListenerDestroyed();
53 void ImportDataHandler::GetLocalizedValues(
54 base::DictionaryValue
* localized_strings
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
56 DCHECK(localized_strings
);
58 static OptionsStringResource resources
[] = {
59 { "importFromLabel", IDS_IMPORT_FROM_LABEL
},
60 { "importLoading", IDS_IMPORT_LOADING_PROFILES
},
61 { "importDescription", IDS_IMPORT_ITEMS_LABEL
},
62 { "importHistory", IDS_IMPORT_HISTORY_CHKBOX
},
63 { "importFavorites", IDS_IMPORT_FAVORITES_CHKBOX
},
64 { "importSearch", IDS_IMPORT_SEARCH_ENGINES_CHKBOX
},
65 { "importPasswords", IDS_IMPORT_PASSWORDS_CHKBOX
},
66 { "importChooseFile", IDS_IMPORT_CHOOSE_FILE
},
67 { "importCommit", IDS_IMPORT_COMMIT
},
68 { "noProfileFound", IDS_IMPORT_NO_PROFILE_FOUND
},
69 { "importSucceeded", IDS_IMPORT_SUCCEEDED
},
70 { "findYourImportedBookmarks", IDS_IMPORT_FIND_YOUR_BOOKMARKS
},
71 { "macPasswordKeychain", IDS_IMPORT_PASSWORD_KEYCHAIN_WARNING
},
74 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
75 RegisterTitle(localized_strings
, "importDataOverlay",
76 IDS_IMPORT_SETTINGS_TITLE
);
79 void ImportDataHandler::InitializeHandler() {
80 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
82 importer_list_
.reset(new ImporterList());
83 importer_list_
->DetectSourceProfiles(
84 g_browser_process
->GetApplicationLocale(),
85 true, // include_interactive_profiles?
86 base::Bind(&ImportDataHandler::InitializePage
, base::Unretained(this)));
89 void ImportDataHandler::RegisterMessages() {
90 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
92 web_ui()->RegisterMessageCallback(
94 base::Bind(&ImportDataHandler::ImportData
, base::Unretained(this)));
95 web_ui()->RegisterMessageCallback(
96 "chooseBookmarksFile",
97 base::Bind(&ImportDataHandler::HandleChooseBookmarksFile
,
98 base::Unretained(this)));
101 void ImportDataHandler::StartImport(
102 const importer::SourceProfile
& source_profile
,
103 uint16 imported_items
) {
104 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
109 // If another import is already ongoing, let it finish silently.
111 importer_host_
->set_observer(NULL
);
113 base::FundamentalValue
importing(true);
114 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
116 import_did_succeed_
= false;
118 importer_host_
= new ExternalProcessImporterHost();
119 importer_host_
->set_observer(this);
120 Profile
* profile
= Profile::FromWebUI(web_ui());
121 importer_host_
->StartImportSettings(source_profile
, profile
,
123 new ProfileWriter(profile
));
125 importer::LogImporterUseToMetrics("ImportDataHandler",
126 source_profile
.importer_type
);
129 void ImportDataHandler::ImportData(const base::ListValue
* args
) {
130 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
132 std::string string_value
;
135 if (!args
->GetString(0, &string_value
) ||
136 !base::StringToInt(string_value
, &browser_index
)) {
141 uint16 selected_items
= importer::NONE
;
142 if (args
->GetString(1, &string_value
) && string_value
== "true") {
143 selected_items
|= importer::HISTORY
;
145 if (args
->GetString(2, &string_value
) && string_value
== "true") {
146 selected_items
|= importer::FAVORITES
;
148 if (args
->GetString(3, &string_value
) && string_value
== "true") {
149 selected_items
|= importer::PASSWORDS
;
151 if (args
->GetString(4, &string_value
) && string_value
== "true") {
152 selected_items
|= importer::SEARCH_ENGINES
;
155 const importer::SourceProfile
& source_profile
=
156 importer_list_
->GetSourceProfileAt(browser_index
);
157 uint16 supported_items
= source_profile
.services_supported
;
159 uint16 imported_items
= (selected_items
& supported_items
);
160 if (imported_items
) {
161 StartImport(source_profile
, imported_items
);
163 LOG(WARNING
) << "There were no settings to import from '"
164 << source_profile
.importer_name
<< "'.";
168 void ImportDataHandler::InitializePage() {
169 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
171 base::ListValue browser_profiles
;
172 for (size_t i
= 0; i
< importer_list_
->count(); ++i
) {
173 const importer::SourceProfile
& source_profile
=
174 importer_list_
->GetSourceProfileAt(i
);
175 uint16 browser_services
= source_profile
.services_supported
;
177 base::DictionaryValue
* browser_profile
= new base::DictionaryValue();
178 browser_profile
->SetString("name", source_profile
.importer_name
);
179 browser_profile
->SetInteger("index", i
);
180 browser_profile
->SetBoolean("history",
181 (browser_services
& importer::HISTORY
) != 0);
182 browser_profile
->SetBoolean("favorites",
183 (browser_services
& importer::FAVORITES
) != 0);
184 browser_profile
->SetBoolean("passwords",
185 (browser_services
& importer::PASSWORDS
) != 0);
186 browser_profile
->SetBoolean("search",
187 (browser_services
& importer::SEARCH_ENGINES
) != 0);
189 browser_profile
->SetBoolean("show_bottom_bar",
190 #if defined(OS_MACOSX)
191 source_profile
.importer_type
== importer::TYPE_SAFARI
);
196 browser_profiles
.Append(browser_profile
);
199 web_ui()->CallJavascriptFunction("ImportDataOverlay.updateSupportedBrowsers",
203 void ImportDataHandler::ImportStarted() {
204 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
207 void ImportDataHandler::ImportItemStarted(importer::ImportItem item
) {
208 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
210 // TODO(csilv): show progress detail in the web view.
213 void ImportDataHandler::ImportItemEnded(importer::ImportItem item
) {
214 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
216 // TODO(csilv): show progress detail in the web view.
217 import_did_succeed_
= true;
220 void ImportDataHandler::ImportEnded() {
221 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
223 importer_host_
->set_observer(NULL
);
224 importer_host_
= NULL
;
226 if (import_did_succeed_
) {
227 web_ui()->CallJavascriptFunction("ImportDataOverlay.confirmSuccess");
229 base::FundamentalValue
state(false);
230 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
232 web_ui()->CallJavascriptFunction("ImportDataOverlay.dismiss");
236 void ImportDataHandler::FileSelected(const base::FilePath
& path
,
239 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
241 importer::SourceProfile source_profile
;
242 source_profile
.importer_type
= importer::TYPE_BOOKMARKS_FILE
;
243 source_profile
.source_path
= path
;
245 StartImport(source_profile
, importer::FAVORITES
);
248 void ImportDataHandler::HandleChooseBookmarksFile(const base::ListValue
* args
) {
249 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
251 DCHECK(args
&& args
->empty());
252 select_file_dialog_
= ui::SelectFileDialog::Create(
253 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents()));
255 ui::SelectFileDialog::FileTypeInfo file_type_info
;
256 file_type_info
.extensions
.resize(1);
257 file_type_info
.extensions
[0].push_back(FILE_PATH_LITERAL("html"));
260 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
262 select_file_dialog_
->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE
,
267 base::FilePath::StringType(),
268 browser
->window()->GetNativeWindow(),
272 } // namespace options