Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/settings/settings_import_data_handler.h" | 5 #include "chrome/browser/ui/webui/settings/settings_import_data_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #include "chrome/browser/ui/chrome_select_file_policy.h" | 28 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 29 #include "chrome/grit/chromium_strings.h" | 29 #include "chrome/grit/chromium_strings.h" |
| 30 #include "chrome/grit/generated_resources.h" | 30 #include "chrome/grit/generated_resources.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/web_ui.h" | 32 #include "content/public/browser/web_ui.h" |
| 33 | 33 |
| 34 using content::BrowserThread; | 34 using content::BrowserThread; |
| 35 | 35 |
| 36 namespace settings { | 36 namespace settings { |
| 37 | 37 |
| 38 namespace { | |
| 39 const char kImportStatusInProgress[] = "inProgress"; | |
| 40 const char kImportStatusSucceeded[] = "succeeded"; | |
| 41 const char kImportStatusFailed[] = "failed"; | |
| 42 } | |
| 43 | |
| 38 ImportDataHandler::ImportDataHandler() | 44 ImportDataHandler::ImportDataHandler() |
| 39 : importer_host_(NULL), import_did_succeed_(false) { | 45 : importer_host_(NULL), import_did_succeed_(false) { |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 41 } | 47 } |
| 42 | 48 |
| 43 ImportDataHandler::~ImportDataHandler() { | 49 ImportDataHandler::~ImportDataHandler() { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 50 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 | 51 |
| 46 if (importer_host_) | 52 if (importer_host_) |
| 47 importer_host_->set_observer(NULL); | 53 importer_host_->set_observer(NULL); |
| 48 | 54 |
| 49 if (select_file_dialog_.get()) | 55 if (select_file_dialog_.get()) |
| 50 select_file_dialog_->ListenerDestroyed(); | 56 select_file_dialog_->ListenerDestroyed(); |
| 51 } | 57 } |
| 52 | 58 |
| 53 void ImportDataHandler::InitializeDialog(const base::ListValue* args) { | |
| 54 AllowJavascript(); | |
| 55 | |
| 56 importer_list_.reset(new ImporterList()); | |
| 57 importer_list_->DetectSourceProfiles( | |
| 58 g_browser_process->GetApplicationLocale(), | |
| 59 true, // include_interactive_profiles? | |
| 60 base::Bind(&ImportDataHandler::SendBrowserProfileData, | |
| 61 base::Unretained(this))); | |
| 62 } | |
| 63 | |
| 64 void ImportDataHandler::RegisterMessages() { | 59 void ImportDataHandler::RegisterMessages() { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 66 | 61 |
| 67 web_ui()->RegisterMessageCallback( | 62 web_ui()->RegisterMessageCallback( |
| 68 "initializeImportDialog", | 63 "initializeImportDialog", |
| 69 base::Bind(&ImportDataHandler::InitializeDialog, base::Unretained(this))); | 64 base::Bind(&ImportDataHandler::InitializeDialog, base::Unretained(this))); |
| 70 web_ui()->RegisterMessageCallback( | 65 web_ui()->RegisterMessageCallback( |
| 71 "importData", | 66 "importData", |
| 72 base::Bind(&ImportDataHandler::ImportData, base::Unretained(this))); | 67 base::Bind(&ImportDataHandler::ImportData, base::Unretained(this))); |
| 73 web_ui()->RegisterMessageCallback( | 68 web_ui()->RegisterMessageCallback( |
| 74 "chooseBookmarksFile", | 69 "importFromBookmarksFile", |
| 75 base::Bind(&ImportDataHandler::HandleChooseBookmarksFile, | 70 base::Bind(&ImportDataHandler::HandleChooseBookmarksFile, |
| 76 base::Unretained(this))); | 71 base::Unretained(this))); |
| 77 } | 72 } |
| 78 | 73 |
| 79 void ImportDataHandler::OnJavascriptDisallowed() { | 74 void ImportDataHandler::OnJavascriptDisallowed() { |
| 80 // Cancels outstanding profile list detections. | 75 // Cancels outstanding profile list detections. |
| 81 importer_list_.reset(); | 76 importer_list_.reset(); |
| 82 | 77 |
| 83 // Stops listening to updates from any ongoing imports. | 78 // Stops listening to updates from any ongoing imports. |
| 84 if (importer_host_) | 79 if (importer_host_) |
| 85 importer_host_->set_observer(NULL); | 80 importer_host_->set_observer(NULL); |
| 86 } | 81 } |
| 87 | 82 |
| 88 void ImportDataHandler::StartImport( | 83 void ImportDataHandler::StartImport( |
| 89 const importer::SourceProfile& source_profile, | 84 const importer::SourceProfile& source_profile, |
| 90 uint16_t imported_items) { | 85 uint16_t imported_items) { |
| 91 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 86 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 92 | 87 |
| 93 if (!imported_items) | 88 if (!imported_items) |
| 94 return; | 89 return; |
| 95 | 90 |
| 96 // If another import is already ongoing, let it finish silently. | 91 // If another import is already ongoing, let it finish silently. |
| 97 if (importer_host_) | 92 if (importer_host_) |
| 98 importer_host_->set_observer(NULL); | 93 importer_host_->set_observer(NULL); |
| 99 | 94 |
| 100 base::FundamentalValue importing(true); | 95 CallJavascriptFunction("cr.webUIListenerCallback", |
| 101 web_ui()->CallJavascriptFunctionUnsafe("ImportDataOverlay.setImportingState", | 96 base::StringValue("import-data-status-changed"), |
| 102 importing); | 97 base::StringValue(kImportStatusInProgress)); |
| 103 import_did_succeed_ = false; | 98 import_did_succeed_ = false; |
| 104 | 99 |
| 105 importer_host_ = new ExternalProcessImporterHost(); | 100 importer_host_ = new ExternalProcessImporterHost(); |
| 106 importer_host_->set_observer(this); | 101 importer_host_->set_observer(this); |
| 107 Profile* profile = Profile::FromWebUI(web_ui()); | 102 Profile* profile = Profile::FromWebUI(web_ui()); |
| 108 importer_host_->StartImportSettings(source_profile, profile, | 103 importer_host_->StartImportSettings(source_profile, profile, |
| 109 imported_items, | 104 imported_items, |
| 110 new ProfileWriter(profile)); | 105 new ProfileWriter(profile)); |
| 111 | 106 |
| 112 importer::LogImporterUseToMetrics("ImportDataHandler", | 107 importer::LogImporterUseToMetrics("ImportDataHandler", |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 | 143 |
| 149 uint16_t imported_items = (selected_items & supported_items); | 144 uint16_t imported_items = (selected_items & supported_items); |
| 150 if (imported_items) { | 145 if (imported_items) { |
| 151 StartImport(source_profile, imported_items); | 146 StartImport(source_profile, imported_items); |
| 152 } else { | 147 } else { |
| 153 LOG(WARNING) << "There were no settings to import from '" | 148 LOG(WARNING) << "There were no settings to import from '" |
| 154 << source_profile.importer_name << "'."; | 149 << source_profile.importer_name << "'."; |
| 155 } | 150 } |
| 156 } | 151 } |
| 157 | 152 |
| 158 void ImportDataHandler::SendBrowserProfileData() { | 153 void ImportDataHandler::InitializeDialog(const base::ListValue* args) { |
| 154 AllowJavascript(); | |
| 155 | |
| 156 CHECK_EQ(1U, args->GetSize()); | |
| 157 std::string callback_id; | |
| 158 CHECK(args->GetString(0, &callback_id)); | |
| 159 | |
| 160 importer_list_.reset(new ImporterList()); | |
| 161 importer_list_->DetectSourceProfiles( | |
| 162 g_browser_process->GetApplicationLocale(), | |
| 163 true, // include_interactive_profiles? | |
|
dpapad
2016/11/03 23:40:43
Maybe remove question mark at the end. Makes it so
tommycli
2016/11/03 23:46:45
Done.
| |
| 164 base::Bind(&ImportDataHandler::SendBrowserProfileData, | |
| 165 base::Unretained(this), callback_id)); | |
| 166 } | |
| 167 | |
| 168 void ImportDataHandler::SendBrowserProfileData(const std::string& callback_id) { | |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 169 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 160 | 170 |
| 161 base::ListValue browser_profiles; | 171 base::ListValue browser_profiles; |
| 162 for (size_t i = 0; i < importer_list_->count(); ++i) { | 172 for (size_t i = 0; i < importer_list_->count(); ++i) { |
| 163 const importer::SourceProfile& source_profile = | 173 const importer::SourceProfile& source_profile = |
| 164 importer_list_->GetSourceProfileAt(i); | 174 importer_list_->GetSourceProfileAt(i); |
| 165 uint16_t browser_services = source_profile.services_supported; | 175 uint16_t browser_services = source_profile.services_supported; |
| 166 | 176 |
| 167 std::unique_ptr<base::DictionaryValue> browser_profile( | 177 std::unique_ptr<base::DictionaryValue> browser_profile( |
| 168 new base::DictionaryValue()); | 178 new base::DictionaryValue()); |
| 169 browser_profile->SetString("name", source_profile.importer_name); | 179 browser_profile->SetString("name", source_profile.importer_name); |
| 170 browser_profile->SetInteger("index", i); | 180 browser_profile->SetInteger("index", i); |
| 171 browser_profile->SetBoolean("history", | 181 browser_profile->SetBoolean("history", |
| 172 (browser_services & importer::HISTORY) != 0); | 182 (browser_services & importer::HISTORY) != 0); |
| 173 browser_profile->SetBoolean("favorites", | 183 browser_profile->SetBoolean("favorites", |
| 174 (browser_services & importer::FAVORITES) != 0); | 184 (browser_services & importer::FAVORITES) != 0); |
| 175 browser_profile->SetBoolean("passwords", | 185 browser_profile->SetBoolean("passwords", |
| 176 (browser_services & importer::PASSWORDS) != 0); | 186 (browser_services & importer::PASSWORDS) != 0); |
| 177 browser_profile->SetBoolean("search", | 187 browser_profile->SetBoolean("search", |
| 178 (browser_services & importer::SEARCH_ENGINES) != 0); | 188 (browser_services & importer::SEARCH_ENGINES) != 0); |
| 179 browser_profile->SetBoolean("autofill-form-data", | 189 browser_profile->SetBoolean( |
| 190 "autofillFormData", | |
| 180 (browser_services & importer::AUTOFILL_FORM_DATA) != 0); | 191 (browser_services & importer::AUTOFILL_FORM_DATA) != 0); |
| 181 | 192 |
| 182 browser_profiles.Append(std::move(browser_profile)); | 193 browser_profiles.Append(std::move(browser_profile)); |
| 183 } | 194 } |
| 184 | 195 |
| 185 web_ui()->CallJavascriptFunctionUnsafe( | 196 ResolveJavascriptCallback(base::StringValue(callback_id), browser_profiles); |
| 186 "ImportDataOverlay.updateSupportedBrowsers", browser_profiles); | |
| 187 } | 197 } |
| 188 | 198 |
| 189 void ImportDataHandler::ImportStarted() { | 199 void ImportDataHandler::ImportStarted() { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 200 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 191 } | 201 } |
| 192 | 202 |
| 193 void ImportDataHandler::ImportItemStarted(importer::ImportItem item) { | 203 void ImportDataHandler::ImportItemStarted(importer::ImportItem item) { |
| 194 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 204 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 195 | 205 |
| 196 // TODO(csilv): show progress detail in the web view. | 206 // TODO(csilv): show progress detail in the web view. |
| 197 } | 207 } |
| 198 | 208 |
| 199 void ImportDataHandler::ImportItemEnded(importer::ImportItem item) { | 209 void ImportDataHandler::ImportItemEnded(importer::ImportItem item) { |
| 200 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 210 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 201 | 211 |
| 202 // TODO(csilv): show progress detail in the web view. | 212 // TODO(csilv): show progress detail in the web view. |
| 203 import_did_succeed_ = true; | 213 import_did_succeed_ = true; |
| 204 } | 214 } |
| 205 | 215 |
| 206 void ImportDataHandler::ImportEnded() { | 216 void ImportDataHandler::ImportEnded() { |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 208 | 218 |
| 209 importer_host_->set_observer(NULL); | 219 importer_host_->set_observer(NULL); |
| 210 importer_host_ = NULL; | 220 importer_host_ = NULL; |
| 211 | 221 |
| 212 if (import_did_succeed_) { | 222 CallJavascriptFunction( |
| 213 web_ui()->CallJavascriptFunctionUnsafe("ImportDataOverlay.confirmSuccess"); | 223 "cr.webUIListenerCallback", |
| 214 } else { | 224 base::StringValue("import-data-status-changed"), |
| 215 base::FundamentalValue state(false); | 225 base::StringValue(import_did_succeed_ ? kImportStatusSucceeded |
| 216 web_ui()->CallJavascriptFunctionUnsafe( | 226 : kImportStatusFailed)); |
| 217 "ImportDataOverlay.setImportingState", state); | |
| 218 web_ui()->CallJavascriptFunctionUnsafe("ImportDataOverlay.dismiss"); | |
| 219 } | |
| 220 } | 227 } |
| 221 | 228 |
| 222 void ImportDataHandler::FileSelected(const base::FilePath& path, | 229 void ImportDataHandler::FileSelected(const base::FilePath& path, |
| 223 int /*index*/, | 230 int /*index*/, |
| 224 void* /*params*/) { | 231 void* /*params*/) { |
| 225 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 232 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 226 | 233 |
| 227 importer::SourceProfile source_profile; | 234 importer::SourceProfile source_profile; |
| 228 source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE; | 235 source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE; |
| 229 source_profile.source_path = path; | 236 source_profile.source_path = path; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 249 base::string16(), | 256 base::string16(), |
| 250 base::FilePath(), | 257 base::FilePath(), |
| 251 &file_type_info, | 258 &file_type_info, |
| 252 0, | 259 0, |
| 253 base::FilePath::StringType(), | 260 base::FilePath::StringType(), |
| 254 browser->window()->GetNativeWindow(), | 261 browser->window()->GetNativeWindow(), |
| 255 NULL); | 262 NULL); |
| 256 } | 263 } |
| 257 | 264 |
| 258 } // namespace settings | 265 } // namespace settings |
| OLD | NEW |