| OLD | NEW |
| (Empty) |
| 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/options2/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/string16.h" | |
| 14 #include "base/string_number_conversions.h" | |
| 15 #include "base/string_util.h" | |
| 16 #include "base/threading/thread_restrictions.h" | |
| 17 #include "base/utf_string_conversions.h" | |
| 18 #include "base/values.h" | |
| 19 #include "chrome/browser/importer/external_process_importer_host.h" | |
| 20 #include "chrome/browser/importer/importer_host.h" | |
| 21 #include "chrome/browser/importer/importer_list.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | |
| 23 #include "chrome/browser/ui/browser_finder.h" | |
| 24 #include "content/public/browser/web_ui.h" | |
| 25 #include "grit/chromium_strings.h" | |
| 26 #include "grit/generated_resources.h" | |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 | |
| 29 namespace options { | |
| 30 | |
| 31 ImportDataHandler::ImportDataHandler() : importer_host_(NULL), | |
| 32 import_did_succeed_(false) { | |
| 33 } | |
| 34 | |
| 35 ImportDataHandler::~ImportDataHandler() { | |
| 36 if (importer_list_) | |
| 37 importer_list_->SetObserver(NULL); | |
| 38 | |
| 39 if (importer_host_) | |
| 40 importer_host_->SetObserver(NULL); | |
| 41 } | |
| 42 | |
| 43 void ImportDataHandler::GetLocalizedValues(DictionaryValue* localized_strings) { | |
| 44 DCHECK(localized_strings); | |
| 45 | |
| 46 static OptionsStringResource resources[] = { | |
| 47 { "importFromLabel", IDS_IMPORT_FROM_LABEL }, | |
| 48 { "importLoading", IDS_IMPORT_LOADING_PROFILES }, | |
| 49 { "importDescription", IDS_IMPORT_ITEMS_LABEL }, | |
| 50 { "importHistory", IDS_IMPORT_HISTORY_CHKBOX }, | |
| 51 { "importFavorites", IDS_IMPORT_FAVORITES_CHKBOX }, | |
| 52 { "importSearch", IDS_IMPORT_SEARCH_ENGINES_CHKBOX }, | |
| 53 { "importPasswords", IDS_IMPORT_PASSWORDS_CHKBOX }, | |
| 54 { "importCommit", IDS_IMPORT_COMMIT }, | |
| 55 { "noProfileFound", IDS_IMPORT_NO_PROFILE_FOUND }, | |
| 56 { "importSucceeded", IDS_IMPORT_SUCCEEDED }, | |
| 57 { "findYourImportedBookmarks", IDS_IMPORT_FIND_YOUR_BOOKMARKS }, | |
| 58 }; | |
| 59 | |
| 60 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 61 RegisterTitle(localized_strings, "importDataOverlay", | |
| 62 IDS_IMPORT_SETTINGS_TITLE); | |
| 63 } | |
| 64 | |
| 65 void ImportDataHandler::InitializeHandler() { | |
| 66 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 67 importer_list_ = new ImporterList(profile->GetRequestContext()); | |
| 68 importer_list_->DetectSourceProfiles(this); | |
| 69 } | |
| 70 | |
| 71 void ImportDataHandler::RegisterMessages() { | |
| 72 web_ui()->RegisterMessageCallback("importData", | |
| 73 base::Bind(&ImportDataHandler::ImportData, base::Unretained(this))); | |
| 74 } | |
| 75 | |
| 76 void ImportDataHandler::ImportData(const ListValue* args) { | |
| 77 std::string string_value; | |
| 78 | |
| 79 int browser_index; | |
| 80 if (!args->GetString(0, &string_value) || | |
| 81 !base::StringToInt(string_value, &browser_index)) { | |
| 82 NOTREACHED(); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 uint16 selected_items = importer::NONE; | |
| 87 if (args->GetString(1, &string_value) && string_value == "true") { | |
| 88 selected_items |= importer::HISTORY; | |
| 89 } | |
| 90 if (args->GetString(2, &string_value) && string_value == "true") { | |
| 91 selected_items |= importer::FAVORITES; | |
| 92 } | |
| 93 if (args->GetString(3, &string_value) && string_value == "true") { | |
| 94 selected_items |= importer::PASSWORDS; | |
| 95 } | |
| 96 if (args->GetString(4, &string_value) && string_value == "true") { | |
| 97 selected_items |= importer::SEARCH_ENGINES; | |
| 98 } | |
| 99 | |
| 100 const importer::SourceProfile& source_profile = | |
| 101 importer_list_->GetSourceProfileAt(browser_index); | |
| 102 uint16 supported_items = source_profile.services_supported; | |
| 103 | |
| 104 uint16 import_services = (selected_items & supported_items); | |
| 105 if (import_services) { | |
| 106 base::FundamentalValue state(true); | |
| 107 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState", | |
| 108 state); | |
| 109 import_did_succeed_ = false; | |
| 110 | |
| 111 // TODO(csilv): Out-of-process import has only been qualified on MacOS X, | |
| 112 // so we will only use it on that platform since it is required. Remove this | |
| 113 // conditional logic once oop import is qualified for Linux/Windows. | |
| 114 // http://crbug.com/22142 | |
| 115 #if defined(OS_MACOSX) | |
| 116 importer_host_ = new ExternalProcessImporterHost; | |
| 117 #else | |
| 118 importer_host_ = new ImporterHost; | |
| 119 #endif | |
| 120 importer_host_->SetObserver(this); | |
| 121 importer_host_->set_browser( | |
| 122 browser::FindBrowserWithWebContents(web_ui()->GetWebContents())); | |
| 123 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 124 importer_host_->StartImportSettings(source_profile, profile, | |
| 125 import_services, | |
| 126 new ProfileWriter(profile), false); | |
| 127 } else { | |
| 128 LOG(WARNING) << "There were no settings to import from '" | |
| 129 << source_profile.importer_name << "'."; | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 void ImportDataHandler::OnSourceProfilesLoaded() { | |
| 134 InitializePage(); | |
| 135 } | |
| 136 | |
| 137 void ImportDataHandler::InitializePage() { | |
| 138 if (!importer_list_->source_profiles_loaded()) | |
| 139 return; | |
| 140 | |
| 141 ListValue browser_profiles; | |
| 142 for (size_t i = 0; i < importer_list_->count(); ++i) { | |
| 143 const importer::SourceProfile& source_profile = | |
| 144 importer_list_->GetSourceProfileAt(i); | |
| 145 uint16 browser_services = source_profile.services_supported; | |
| 146 | |
| 147 DictionaryValue* browser_profile = new DictionaryValue(); | |
| 148 browser_profile->SetString("name", source_profile.importer_name); | |
| 149 browser_profile->SetInteger("index", i); | |
| 150 browser_profile->SetBoolean("history", | |
| 151 (browser_services & importer::HISTORY) != 0); | |
| 152 browser_profile->SetBoolean("favorites", | |
| 153 (browser_services & importer::FAVORITES) != 0); | |
| 154 browser_profile->SetBoolean("passwords", | |
| 155 (browser_services & importer::PASSWORDS) != 0); | |
| 156 browser_profile->SetBoolean("search", | |
| 157 (browser_services & importer::SEARCH_ENGINES) != 0); | |
| 158 | |
| 159 browser_profiles.Append(browser_profile); | |
| 160 } | |
| 161 | |
| 162 web_ui()->CallJavascriptFunction("ImportDataOverlay.updateSupportedBrowsers", | |
| 163 browser_profiles); | |
| 164 } | |
| 165 | |
| 166 void ImportDataHandler::ImportStarted() { | |
| 167 } | |
| 168 | |
| 169 void ImportDataHandler::ImportItemStarted(importer::ImportItem item) { | |
| 170 // TODO(csilv): show progress detail in the web view. | |
| 171 } | |
| 172 | |
| 173 void ImportDataHandler::ImportItemEnded(importer::ImportItem item) { | |
| 174 // TODO(csilv): show progress detail in the web view. | |
| 175 import_did_succeed_ = true; | |
| 176 } | |
| 177 | |
| 178 void ImportDataHandler::ImportEnded() { | |
| 179 importer_host_->SetObserver(NULL); | |
| 180 importer_host_ = NULL; | |
| 181 | |
| 182 if (import_did_succeed_) { | |
| 183 web_ui()->CallJavascriptFunction("ImportDataOverlay.confirmSuccess"); | |
| 184 } else { | |
| 185 base::FundamentalValue state(false); | |
| 186 web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState", | |
| 187 state); | |
| 188 web_ui()->CallJavascriptFunction("ImportDataOverlay.dismiss"); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 } // namespace options | |
| OLD | NEW |