Home | History | Annotate | Download | only in options
      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/web_ui.h"
     28 #include "grit/chromium_strings.h"
     29 #include "grit/generated_resources.h"
     30 #include "ui/base/l10n/l10n_util.h"
     31 
     32 namespace options {
     33 
     34 ImportDataHandler::ImportDataHandler() : importer_host_(NULL),
     35                                          import_did_succeed_(false) {
     36 }
     37 
     38 ImportDataHandler::~ImportDataHandler() {
     39   if (importer_list_)
     40     importer_list_->set_observer(NULL);
     41 
     42   if (importer_host_)
     43     importer_host_->set_observer(NULL);
     44 
     45   if (select_file_dialog_)
     46     select_file_dialog_->ListenerDestroyed();
     47 }
     48 
     49 void ImportDataHandler::GetLocalizedValues(DictionaryValue* localized_strings) {
     50   DCHECK(localized_strings);
     51 
     52   static OptionsStringResource resources[] = {
     53     { "importFromLabel", IDS_IMPORT_FROM_LABEL },
     54     { "importLoading", IDS_IMPORT_LOADING_PROFILES },
     55     { "importDescription", IDS_IMPORT_ITEMS_LABEL },
     56     { "importHistory", IDS_IMPORT_HISTORY_CHKBOX },
     57     { "importFavorites", IDS_IMPORT_FAVORITES_CHKBOX },
     58     { "importSearch", IDS_IMPORT_SEARCH_ENGINES_CHKBOX },
     59     { "importPasswords", IDS_IMPORT_PASSWORDS_CHKBOX },
     60     { "importChooseFile", IDS_IMPORT_CHOOSE_FILE },
     61     { "importCommit", IDS_IMPORT_COMMIT },
     62     { "noProfileFound", IDS_IMPORT_NO_PROFILE_FOUND },
     63     { "importSucceeded", IDS_IMPORT_SUCCEEDED },
     64     { "findYourImportedBookmarks", IDS_IMPORT_FIND_YOUR_BOOKMARKS },
     65     { "macPasswordKeychain", IDS_IMPORT_PASSWORD_KEYCHAIN_WARNING },
     66   };
     67 
     68   RegisterStrings(localized_strings, resources, arraysize(resources));
     69   RegisterTitle(localized_strings, "importDataOverlay",
     70                 IDS_IMPORT_SETTINGS_TITLE);
     71 }
     72 
     73 void ImportDataHandler::InitializeHandler() {
     74   importer_list_ = new ImporterList();
     75   importer_list_->DetectSourceProfiles(
     76       g_browser_process->GetApplicationLocale(), this);
     77 }
     78 
     79 void ImportDataHandler::RegisterMessages() {
     80   web_ui()->RegisterMessageCallback(
     81       "importData",
     82       base::Bind(&ImportDataHandler::ImportData, base::Unretained(this)));
     83   web_ui()->RegisterMessageCallback(
     84       "chooseBookmarksFile",
     85       base::Bind(&ImportDataHandler::HandleChooseBookmarksFile,
     86                  base::Unretained(this)));
     87 }
     88 
     89 void ImportDataHandler::StartImport(
     90     const importer::SourceProfile& source_profile,
     91     uint16 imported_items) {
     92   if (!imported_items)
     93     return;
     94 
     95   // If another import is already ongoing, let it finish silently.
     96   if (importer_host_)
     97     importer_host_->set_observer(NULL);
     98 
     99   base::FundamentalValue importing(true);
    100   web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
    101                                    importing);
    102   import_did_succeed_ = false;
    103 
    104   importer_host_ = new ExternalProcessImporterHost();
    105   importer_host_->set_observer(this);
    106   Profile* profile = Profile::FromWebUI(web_ui());
    107   importer_host_->StartImportSettings(source_profile, profile,
    108                                       imported_items,
    109                                       new ProfileWriter(profile));
    110 
    111   importer::LogImporterUseToMetrics("ImportDataHandler",
    112                                     source_profile.importer_type);
    113 }
    114 
    115 void ImportDataHandler::ImportData(const ListValue* args) {
    116   std::string string_value;
    117 
    118   int browser_index;
    119   if (!args->GetString(0, &string_value) ||
    120       !base::StringToInt(string_value, &browser_index)) {
    121     NOTREACHED();
    122     return;
    123   }
    124 
    125   uint16 selected_items = importer::NONE;
    126   if (args->GetString(1, &string_value) && string_value == "true") {
    127     selected_items |= importer::HISTORY;
    128   }
    129   if (args->GetString(2, &string_value) && string_value == "true") {
    130     selected_items |= importer::FAVORITES;
    131   }
    132   if (args->GetString(3, &string_value) && string_value == "true") {
    133     selected_items |= importer::PASSWORDS;
    134   }
    135   if (args->GetString(4, &string_value) && string_value == "true") {
    136     selected_items |= importer::SEARCH_ENGINES;
    137   }
    138 
    139   const importer::SourceProfile& source_profile =
    140       importer_list_->GetSourceProfileAt(browser_index);
    141   uint16 supported_items = source_profile.services_supported;
    142 
    143   uint16 imported_items = (selected_items & supported_items);
    144   if (imported_items) {
    145     StartImport(source_profile, imported_items);
    146   } else {
    147     LOG(WARNING) << "There were no settings to import from '"
    148         << source_profile.importer_name << "'.";
    149   }
    150 }
    151 
    152 void ImportDataHandler::OnSourceProfilesLoaded() {
    153   InitializePage();
    154 }
    155 
    156 void ImportDataHandler::InitializePage() {
    157   if (!importer_list_->source_profiles_loaded())
    158     return;
    159 
    160   ListValue browser_profiles;
    161   for (size_t i = 0; i < importer_list_->count(); ++i) {
    162     const importer::SourceProfile& source_profile =
    163         importer_list_->GetSourceProfileAt(i);
    164     uint16 browser_services = source_profile.services_supported;
    165 
    166     DictionaryValue* browser_profile = new DictionaryValue();
    167     browser_profile->SetString("name", source_profile.importer_name);
    168     browser_profile->SetInteger("index", i);
    169     browser_profile->SetBoolean("history",
    170         (browser_services & importer::HISTORY) != 0);
    171     browser_profile->SetBoolean("favorites",
    172         (browser_services & importer::FAVORITES) != 0);
    173     browser_profile->SetBoolean("passwords",
    174         (browser_services & importer::PASSWORDS) != 0);
    175     browser_profile->SetBoolean("search",
    176         (browser_services & importer::SEARCH_ENGINES) != 0);
    177 
    178     browser_profile->SetBoolean("show_bottom_bar",
    179 #if defined(OS_MACOSX)
    180         source_profile.importer_type == importer::TYPE_SAFARI);
    181 #else
    182         false);
    183 #endif
    184 
    185     browser_profiles.Append(browser_profile);
    186   }
    187 
    188   web_ui()->CallJavascriptFunction("ImportDataOverlay.updateSupportedBrowsers",
    189                                    browser_profiles);
    190 }
    191 
    192 void ImportDataHandler::ImportStarted() {
    193 }
    194 
    195 void ImportDataHandler::ImportItemStarted(importer::ImportItem item) {
    196   // TODO(csilv): show progress detail in the web view.
    197 }
    198 
    199 void ImportDataHandler::ImportItemEnded(importer::ImportItem item) {
    200   // TODO(csilv): show progress detail in the web view.
    201   import_did_succeed_ = true;
    202 }
    203 
    204 void ImportDataHandler::ImportEnded() {
    205   importer_host_->set_observer(NULL);
    206   importer_host_ = NULL;
    207 
    208   if (import_did_succeed_) {
    209     web_ui()->CallJavascriptFunction("ImportDataOverlay.confirmSuccess");
    210   } else {
    211     base::FundamentalValue state(false);
    212     web_ui()->CallJavascriptFunction("ImportDataOverlay.setImportingState",
    213                                      state);
    214     web_ui()->CallJavascriptFunction("ImportDataOverlay.dismiss");
    215   }
    216 }
    217 
    218 void ImportDataHandler::FileSelected(const base::FilePath& path,
    219                                      int /*index*/,
    220                                      void* /*params*/) {
    221 
    222   importer::SourceProfile source_profile;
    223   source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
    224   source_profile.source_path = path;
    225 
    226   StartImport(source_profile, importer::FAVORITES);
    227 }
    228 
    229 void ImportDataHandler::HandleChooseBookmarksFile(const base::ListValue* args) {
    230   DCHECK(args && args->empty());
    231   select_file_dialog_ = ui::SelectFileDialog::Create(
    232       this, new ChromeSelectFilePolicy(web_ui()->GetWebContents()));
    233 
    234   ui::SelectFileDialog::FileTypeInfo file_type_info;
    235   file_type_info.extensions.resize(1);
    236   file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html"));
    237 
    238   Browser* browser =
    239       chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
    240 
    241   select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
    242                                   base::string16(),
    243                                   base::FilePath(),
    244                                   &file_type_info,
    245                                   0,
    246                                   base::FilePath::StringType(),
    247                                   browser->window()->GetNativeWindow(),
    248                                   NULL);
    249 }
    250 
    251 }  // namespace options
    252