Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / import_data_handler.cc
blobe0df00696398d4f2e8cd6d899cd957ccfd239f54
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"
7 #include <string>
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 "chrome/grit/chromium_strings.h"
28 #include "chrome/grit/generated_resources.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/web_ui.h"
32 using content::BrowserThread;
34 namespace options {
36 ImportDataHandler::ImportDataHandler()
37 : importer_host_(NULL),
38 import_did_succeed_(false) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42 ImportDataHandler::~ImportDataHandler() {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI);
45 if (importer_host_)
46 importer_host_->set_observer(NULL);
48 if (select_file_dialog_.get())
49 select_file_dialog_->ListenerDestroyed();
52 void ImportDataHandler::GetLocalizedValues(
53 base::DictionaryValue* localized_strings) {
54 DCHECK_CURRENTLY_ON(BrowserThread::UI);
55 DCHECK(localized_strings);
57 static OptionsStringResource resources[] = {
58 {"importFromLabel", IDS_IMPORT_FROM_LABEL},
59 {"importLoading", IDS_IMPORT_LOADING_PROFILES},
60 {"importDescription", IDS_IMPORT_ITEMS_LABEL},
61 {"importHistory", IDS_IMPORT_HISTORY_CHKBOX},
62 {"importFavorites", IDS_IMPORT_FAVORITES_CHKBOX},
63 {"importSearch", IDS_IMPORT_SEARCH_ENGINES_CHKBOX},
64 {"importPasswords", IDS_IMPORT_PASSWORDS_CHKBOX},
65 {"importAutofillFormData", IDS_IMPORT_AUTOFILL_FORM_DATA_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},
73 RegisterStrings(localized_strings, resources, arraysize(resources));
74 RegisterTitle(localized_strings, "importDataOverlay",
75 IDS_IMPORT_SETTINGS_TITLE);
78 void ImportDataHandler::InitializeHandler() {
79 DCHECK_CURRENTLY_ON(BrowserThread::UI);
81 importer_list_.reset(new ImporterList());
82 importer_list_->DetectSourceProfiles(
83 g_browser_process->GetApplicationLocale(),
84 true, // include_interactive_profiles?
85 base::Bind(&ImportDataHandler::InitializePage, base::Unretained(this)));
88 void ImportDataHandler::RegisterMessages() {
89 DCHECK_CURRENTLY_ON(BrowserThread::UI);
91 web_ui()->RegisterMessageCallback(
92 "importData",
93 base::Bind(&ImportDataHandler::ImportData, base::Unretained(this)));
94 web_ui()->RegisterMessageCallback(
95 "chooseBookmarksFile",
96 base::Bind(&ImportDataHandler::HandleChooseBookmarksFile,
97 base::Unretained(this)));
100 void ImportDataHandler::StartImport(
101 const importer::SourceProfile& source_profile,
102 uint16 imported_items) {
103 DCHECK_CURRENTLY_ON(BrowserThread::UI);
105 if (!imported_items)
106 return;
108 // If another import is already ongoing, let it finish silently.
109 if (importer_host_)
110 importer_host_->set_observer(NULL);
112 base::FundamentalValue importing(true);
113 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
114 importing);
115 import_did_succeed_ = false;
117 importer_host_ = new ExternalProcessImporterHost();
118 importer_host_->set_observer(this);
119 Profile* profile = Profile::FromWebUI(web_ui());
120 importer_host_->StartImportSettings(source_profile, profile,
121 imported_items,
122 new ProfileWriter(profile));
124 importer::LogImporterUseToMetrics("ImportDataHandler",
125 source_profile.importer_type);
128 void ImportDataHandler::ImportData(const base::ListValue* args) {
129 DCHECK_CURRENTLY_ON(BrowserThread::UI);
131 std::string string_value;
133 int browser_index;
134 if (!args->GetString(0, &string_value) ||
135 !base::StringToInt(string_value, &browser_index)) {
136 NOTREACHED();
137 return;
140 uint16 selected_items = importer::NONE;
141 if (args->GetString(1, &string_value) && string_value == "true") {
142 selected_items |= importer::HISTORY;
144 if (args->GetString(2, &string_value) && string_value == "true") {
145 selected_items |= importer::FAVORITES;
147 if (args->GetString(3, &string_value) && string_value == "true") {
148 selected_items |= importer::PASSWORDS;
150 if (args->GetString(4, &string_value) && string_value == "true") {
151 selected_items |= importer::SEARCH_ENGINES;
153 if (args->GetString(5, &string_value) && string_value == "true") {
154 selected_items |= importer::AUTOFILL_FORM_DATA;
157 const importer::SourceProfile& source_profile =
158 importer_list_->GetSourceProfileAt(browser_index);
159 uint16 supported_items = source_profile.services_supported;
161 uint16 imported_items = (selected_items & supported_items);
162 if (imported_items) {
163 StartImport(source_profile, imported_items);
164 } else {
165 LOG(WARNING) << "There were no settings to import from '"
166 << source_profile.importer_name << "'.";
170 void ImportDataHandler::InitializePage() {
171 DCHECK_CURRENTLY_ON(BrowserThread::UI);
173 base::ListValue browser_profiles;
174 for (size_t i = 0; i < importer_list_->count(); ++i) {
175 const importer::SourceProfile& source_profile =
176 importer_list_->GetSourceProfileAt(i);
177 uint16 browser_services = source_profile.services_supported;
179 base::DictionaryValue* browser_profile = new base::DictionaryValue();
180 browser_profile->SetString("name", source_profile.importer_name);
181 browser_profile->SetInteger("index", i);
182 browser_profile->SetBoolean("history",
183 (browser_services & importer::HISTORY) != 0);
184 browser_profile->SetBoolean("favorites",
185 (browser_services & importer::FAVORITES) != 0);
186 browser_profile->SetBoolean("passwords",
187 (browser_services & importer::PASSWORDS) != 0);
188 browser_profile->SetBoolean("search",
189 (browser_services & importer::SEARCH_ENGINES) != 0);
190 browser_profile->SetBoolean("autofill-form-data",
191 (browser_services & importer::AUTOFILL_FORM_DATA) != 0);
193 browser_profiles.Append(browser_profile);
196 web_ui()->CallJavascriptFunction("ImportDataOverlay.updateSupportedBrowsers",
197 browser_profiles);
200 void ImportDataHandler::ImportStarted() {
201 DCHECK_CURRENTLY_ON(BrowserThread::UI);
204 void ImportDataHandler::ImportItemStarted(importer::ImportItem item) {
205 DCHECK_CURRENTLY_ON(BrowserThread::UI);
207 // TODO(csilv): show progress detail in the web view.
210 void ImportDataHandler::ImportItemEnded(importer::ImportItem item) {
211 DCHECK_CURRENTLY_ON(BrowserThread::UI);
213 // TODO(csilv): show progress detail in the web view.
214 import_did_succeed_ = true;
217 void ImportDataHandler::ImportEnded() {
218 DCHECK_CURRENTLY_ON(BrowserThread::UI);
220 importer_host_->set_observer(NULL);
221 importer_host_ = NULL;
223 if (import_did_succeed_) {
224 web_ui()->CallJavascriptFunction("ImportDataOverlay.confirmSuccess");
225 } else {
226 base::FundamentalValue state(false);
227 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
228 state);
229 web_ui()->CallJavascriptFunction("ImportDataOverlay.dismiss");
233 void ImportDataHandler::FileSelected(const base::FilePath& path,
234 int /*index*/,
235 void* /*params*/) {
236 DCHECK_CURRENTLY_ON(BrowserThread::UI);
238 importer::SourceProfile source_profile;
239 source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
240 source_profile.source_path = path;
242 StartImport(source_profile, importer::FAVORITES);
245 void ImportDataHandler::HandleChooseBookmarksFile(const base::ListValue* args) {
246 DCHECK_CURRENTLY_ON(BrowserThread::UI);
248 DCHECK(args && args->empty());
249 select_file_dialog_ = ui::SelectFileDialog::Create(
250 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents()));
252 ui::SelectFileDialog::FileTypeInfo file_type_info;
253 file_type_info.extensions.resize(1);
254 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html"));
256 Browser* browser =
257 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
259 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
260 base::string16(),
261 base::FilePath(),
262 &file_type_info,
264 base::FilePath::StringType(),
265 browser->window()->GetNativeWindow(),
266 NULL);
269 } // namespace options