import_data_handler.cc revision 010d83a9304c5a91596085d917d248abff47903a
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. 4 5#include "chrome/browser/ui/webui/options/import_data_handler.h" 6 7#include <string> 8 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" 32 33using content::BrowserThread; 34 35namespace options { 36 37ImportDataHandler::ImportDataHandler() 38 : importer_host_(NULL), 39 import_did_succeed_(false) { 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); 41} 42 43ImportDataHandler::~ImportDataHandler() { 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); 45 46 if (importer_host_) 47 importer_host_->set_observer(NULL); 48 49 if (select_file_dialog_) 50 select_file_dialog_->ListenerDestroyed(); 51} 52 53void ImportDataHandler::GetLocalizedValues( 54 base::DictionaryValue* localized_strings) { 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); 56 DCHECK(localized_strings); 57 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#if defined(OS_MACOSX) 72 { "macPasswordKeychain", IDS_IMPORT_PASSWORD_KEYCHAIN_WARNING }, 73#endif 74 }; 75 76 RegisterStrings(localized_strings, resources, arraysize(resources)); 77 RegisterTitle(localized_strings, "importDataOverlay", 78 IDS_IMPORT_SETTINGS_TITLE); 79} 80 81void ImportDataHandler::InitializeHandler() { 82 DCHECK_CURRENTLY_ON(BrowserThread::UI); 83 84 importer_list_.reset(new ImporterList()); 85 importer_list_->DetectSourceProfiles( 86 g_browser_process->GetApplicationLocale(), 87 true, // include_interactive_profiles? 88 base::Bind(&ImportDataHandler::InitializePage, base::Unretained(this))); 89} 90 91void ImportDataHandler::RegisterMessages() { 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); 93 94 web_ui()->RegisterMessageCallback( 95 "importData", 96 base::Bind(&ImportDataHandler::ImportData, base::Unretained(this))); 97 web_ui()->RegisterMessageCallback( 98 "chooseBookmarksFile", 99 base::Bind(&ImportDataHandler::HandleChooseBookmarksFile, 100 base::Unretained(this))); 101} 102 103void ImportDataHandler::StartImport( 104 const importer::SourceProfile& source_profile, 105 uint16 imported_items) { 106 DCHECK_CURRENTLY_ON(BrowserThread::UI); 107 108 if (!imported_items) 109 return; 110 111 // If another import is already ongoing, let it finish silently. 112 if (importer_host_) 113 importer_host_->set_observer(NULL); 114 115 base::FundamentalValue importing(true); 116 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState", 117 importing); 118 import_did_succeed_ = false; 119 120 importer_host_ = new ExternalProcessImporterHost(); 121 importer_host_->set_observer(this); 122 Profile* profile = Profile::FromWebUI(web_ui()); 123 importer_host_->StartImportSettings(source_profile, profile, 124 imported_items, 125 new ProfileWriter(profile)); 126 127 importer::LogImporterUseToMetrics("ImportDataHandler", 128 source_profile.importer_type); 129} 130 131void ImportDataHandler::ImportData(const base::ListValue* args) { 132 DCHECK_CURRENTLY_ON(BrowserThread::UI); 133 134 std::string string_value; 135 136 int browser_index; 137 if (!args->GetString(0, &string_value) || 138 !base::StringToInt(string_value, &browser_index)) { 139 NOTREACHED(); 140 return; 141 } 142 143 uint16 selected_items = importer::NONE; 144 if (args->GetString(1, &string_value) && string_value == "true") { 145 selected_items |= importer::HISTORY; 146 } 147 if (args->GetString(2, &string_value) && string_value == "true") { 148 selected_items |= importer::FAVORITES; 149 } 150 if (args->GetString(3, &string_value) && string_value == "true") { 151 selected_items |= importer::PASSWORDS; 152 } 153 if (args->GetString(4, &string_value) && string_value == "true") { 154 selected_items |= importer::SEARCH_ENGINES; 155 } 156 157 const importer::SourceProfile& source_profile = 158 importer_list_->GetSourceProfileAt(browser_index); 159 uint16 supported_items = source_profile.services_supported; 160 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 << "'."; 167 } 168} 169 170void ImportDataHandler::InitializePage() { 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); 172 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; 178 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 191 browser_profile->SetBoolean("show_bottom_bar", 192#if defined(OS_MACOSX) 193 source_profile.importer_type == importer::TYPE_SAFARI); 194#else 195 false); 196#endif 197 198 browser_profiles.Append(browser_profile); 199 } 200 201 web_ui()->CallJavascriptFunction("ImportDataOverlay.updateSupportedBrowsers", 202 browser_profiles); 203} 204 205void ImportDataHandler::ImportStarted() { 206 DCHECK_CURRENTLY_ON(BrowserThread::UI); 207} 208 209void ImportDataHandler::ImportItemStarted(importer::ImportItem item) { 210 DCHECK_CURRENTLY_ON(BrowserThread::UI); 211 212 // TODO(csilv): show progress detail in the web view. 213} 214 215void ImportDataHandler::ImportItemEnded(importer::ImportItem item) { 216 DCHECK_CURRENTLY_ON(BrowserThread::UI); 217 218 // TODO(csilv): show progress detail in the web view. 219 import_did_succeed_ = true; 220} 221 222void ImportDataHandler::ImportEnded() { 223 DCHECK_CURRENTLY_ON(BrowserThread::UI); 224 225 importer_host_->set_observer(NULL); 226 importer_host_ = NULL; 227 228 if (import_did_succeed_) { 229 web_ui()->CallJavascriptFunction("ImportDataOverlay.confirmSuccess"); 230 } else { 231 base::FundamentalValue state(false); 232 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState", 233 state); 234 web_ui()->CallJavascriptFunction("ImportDataOverlay.dismiss"); 235 } 236} 237 238void ImportDataHandler::FileSelected(const base::FilePath& path, 239 int /*index*/, 240 void* /*params*/) { 241 DCHECK_CURRENTLY_ON(BrowserThread::UI); 242 243 importer::SourceProfile source_profile; 244 source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE; 245 source_profile.source_path = path; 246 247 StartImport(source_profile, importer::FAVORITES); 248} 249 250void ImportDataHandler::HandleChooseBookmarksFile(const base::ListValue* args) { 251 DCHECK_CURRENTLY_ON(BrowserThread::UI); 252 253 DCHECK(args && args->empty()); 254 select_file_dialog_ = ui::SelectFileDialog::Create( 255 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); 256 257 ui::SelectFileDialog::FileTypeInfo file_type_info; 258 file_type_info.extensions.resize(1); 259 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html")); 260 261 Browser* browser = 262 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents()); 263 264 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE, 265 base::string16(), 266 base::FilePath(), 267 &file_type_info, 268 0, 269 base::FilePath::StringType(), 270 browser->window()->GetNativeWindow(), 271 NULL); 272} 273 274} // namespace options 275