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