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/web_ui.h"
28 #include "grit/chromium_strings.h"
29 #include "grit/generated_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
34 ImportDataHandler::ImportDataHandler() : importer_host_(NULL
),
35 import_did_succeed_(false) {
38 ImportDataHandler::~ImportDataHandler() {
40 importer_list_
->set_observer(NULL
);
43 importer_host_
->set_observer(NULL
);
45 if (select_file_dialog_
)
46 select_file_dialog_
->ListenerDestroyed();
49 void ImportDataHandler::GetLocalizedValues(
50 base::DictionaryValue
* localized_strings
) {
51 DCHECK(localized_strings
);
53 static OptionsStringResource resources
[] = {
54 { "importFromLabel", IDS_IMPORT_FROM_LABEL
},
55 { "importLoading", IDS_IMPORT_LOADING_PROFILES
},
56 { "importDescription", IDS_IMPORT_ITEMS_LABEL
},
57 { "importHistory", IDS_IMPORT_HISTORY_CHKBOX
},
58 { "importFavorites", IDS_IMPORT_FAVORITES_CHKBOX
},
59 { "importSearch", IDS_IMPORT_SEARCH_ENGINES_CHKBOX
},
60 { "importPasswords", IDS_IMPORT_PASSWORDS_CHKBOX
},
61 { "importChooseFile", IDS_IMPORT_CHOOSE_FILE
},
62 { "importCommit", IDS_IMPORT_COMMIT
},
63 { "noProfileFound", IDS_IMPORT_NO_PROFILE_FOUND
},
64 { "importSucceeded", IDS_IMPORT_SUCCEEDED
},
65 { "findYourImportedBookmarks", IDS_IMPORT_FIND_YOUR_BOOKMARKS
},
66 { "macPasswordKeychain", IDS_IMPORT_PASSWORD_KEYCHAIN_WARNING
},
69 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
70 RegisterTitle(localized_strings
, "importDataOverlay",
71 IDS_IMPORT_SETTINGS_TITLE
);
74 void ImportDataHandler::InitializeHandler() {
75 importer_list_
= new ImporterList();
76 importer_list_
->DetectSourceProfiles(
77 g_browser_process
->GetApplicationLocale(), true, this);
80 void ImportDataHandler::RegisterMessages() {
81 web_ui()->RegisterMessageCallback(
83 base::Bind(&ImportDataHandler::ImportData
, base::Unretained(this)));
84 web_ui()->RegisterMessageCallback(
85 "chooseBookmarksFile",
86 base::Bind(&ImportDataHandler::HandleChooseBookmarksFile
,
87 base::Unretained(this)));
90 void ImportDataHandler::StartImport(
91 const importer::SourceProfile
& source_profile
,
92 uint16 imported_items
) {
96 // If another import is already ongoing, let it finish silently.
98 importer_host_
->set_observer(NULL
);
100 base::FundamentalValue
importing(true);
101 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
103 import_did_succeed_
= false;
105 importer_host_
= new ExternalProcessImporterHost();
106 importer_host_
->set_observer(this);
107 Profile
* profile
= Profile::FromWebUI(web_ui());
108 importer_host_
->StartImportSettings(source_profile
, profile
,
110 new ProfileWriter(profile
));
112 importer::LogImporterUseToMetrics("ImportDataHandler",
113 source_profile
.importer_type
);
116 void ImportDataHandler::ImportData(const base::ListValue
* args
) {
117 std::string string_value
;
120 if (!args
->GetString(0, &string_value
) ||
121 !base::StringToInt(string_value
, &browser_index
)) {
126 uint16 selected_items
= importer::NONE
;
127 if (args
->GetString(1, &string_value
) && string_value
== "true") {
128 selected_items
|= importer::HISTORY
;
130 if (args
->GetString(2, &string_value
) && string_value
== "true") {
131 selected_items
|= importer::FAVORITES
;
133 if (args
->GetString(3, &string_value
) && string_value
== "true") {
134 selected_items
|= importer::PASSWORDS
;
136 if (args
->GetString(4, &string_value
) && string_value
== "true") {
137 selected_items
|= importer::SEARCH_ENGINES
;
140 const importer::SourceProfile
& source_profile
=
141 importer_list_
->GetSourceProfileAt(browser_index
);
142 uint16 supported_items
= source_profile
.services_supported
;
144 uint16 imported_items
= (selected_items
& supported_items
);
145 if (imported_items
) {
146 StartImport(source_profile
, imported_items
);
148 LOG(WARNING
) << "There were no settings to import from '"
149 << source_profile
.importer_name
<< "'.";
153 void ImportDataHandler::OnSourceProfilesLoaded() {
157 void ImportDataHandler::InitializePage() {
158 if (!importer_list_
->source_profiles_loaded())
161 base::ListValue browser_profiles
;
162 for (size_t i
= 0; i
< importer_list_
->count(); ++i
) {
163 const importer::SourceProfile
& source_profile
=
164 importer_list_
->GetSourceProfileAt(i
);
165 uint16 browser_services
= source_profile
.services_supported
;
167 base::DictionaryValue
* browser_profile
= new base::DictionaryValue();
168 browser_profile
->SetString("name", source_profile
.importer_name
);
169 browser_profile
->SetInteger("index", i
);
170 browser_profile
->SetBoolean("history",
171 (browser_services
& importer::HISTORY
) != 0);
172 browser_profile
->SetBoolean("favorites",
173 (browser_services
& importer::FAVORITES
) != 0);
174 browser_profile
->SetBoolean("passwords",
175 (browser_services
& importer::PASSWORDS
) != 0);
176 browser_profile
->SetBoolean("search",
177 (browser_services
& importer::SEARCH_ENGINES
) != 0);
179 browser_profile
->SetBoolean("show_bottom_bar",
180 #if defined(OS_MACOSX)
181 source_profile
.importer_type
== importer::TYPE_SAFARI
);
186 browser_profiles
.Append(browser_profile
);
189 web_ui()->CallJavascriptFunction("ImportDataOverlay.updateSupportedBrowsers",
193 void ImportDataHandler::ImportStarted() {
196 void ImportDataHandler::ImportItemStarted(importer::ImportItem item
) {
197 // TODO(csilv): show progress detail in the web view.
200 void ImportDataHandler::ImportItemEnded(importer::ImportItem item
) {
201 // TODO(csilv): show progress detail in the web view.
202 import_did_succeed_
= true;
205 void ImportDataHandler::ImportEnded() {
206 importer_host_
->set_observer(NULL
);
207 importer_host_
= NULL
;
209 if (import_did_succeed_
) {
210 web_ui()->CallJavascriptFunction("ImportDataOverlay.confirmSuccess");
212 base::FundamentalValue
state(false);
213 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
215 web_ui()->CallJavascriptFunction("ImportDataOverlay.dismiss");
219 void ImportDataHandler::FileSelected(const base::FilePath
& path
,
223 importer::SourceProfile source_profile
;
224 source_profile
.importer_type
= importer::TYPE_BOOKMARKS_FILE
;
225 source_profile
.source_path
= path
;
227 StartImport(source_profile
, importer::FAVORITES
);
230 void ImportDataHandler::HandleChooseBookmarksFile(const base::ListValue
* args
) {
231 DCHECK(args
&& args
->empty());
232 select_file_dialog_
= ui::SelectFileDialog::Create(
233 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents()));
235 ui::SelectFileDialog::FileTypeInfo file_type_info
;
236 file_type_info
.extensions
.resize(1);
237 file_type_info
.extensions
[0].push_back(FILE_PATH_LITERAL("html"));
240 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
242 select_file_dialog_
->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE
,
247 base::FilePath::StringType(),
248 browser
->window()->GetNativeWindow(),
252 } // namespace options