importer_list.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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/importer/importer_list.h"
6
7#include "app/l10n_util.h"
8#include "base/file_util.h"
9#include "base/stl_util-inl.h"
10#include "base/values.h"
11#include "base/utf_string_conversions.h"
12#include "chrome/browser/first_run.h"
13#include "chrome/browser/importer/firefox2_importer.h"
14#include "chrome/browser/importer/firefox3_importer.h"
15#include "chrome/browser/importer/firefox_importer_utils.h"
16#include "chrome/browser/importer/importer_bridge.h"
17#include "chrome/browser/importer/toolbar_importer.h"
18#include "chrome/browser/shell_integration.h"
19#include "grit/generated_resources.h"
20
21#if defined(OS_WIN)
22#include "app/win_util.h"
23#include "chrome/browser/importer/ie_importer.h"
24#include "chrome/browser/password_manager/ie7_password.h"
25#endif
26#if defined(OS_MACOSX)
27#include "base/mac_util.h"
28#include "chrome/browser/importer/safari_importer.h"
29#endif
30
31ImporterList::ImporterList() {
32}
33
34ImporterList::~ImporterList() {
35  STLDeleteContainerPointers(source_profiles_.begin(), source_profiles_.end());
36}
37
38void ImporterList::DetectSourceProfiles() {
39#if defined(OS_WIN)
40  // The order in which detect is called determines the order
41  // in which the options appear in the dropdown combo-box
42  if (ShellIntegration::IsFirefoxDefaultBrowser()) {
43    DetectFirefoxProfiles();
44    DetectIEProfiles();
45  } else {
46    DetectIEProfiles();
47    DetectFirefoxProfiles();
48  }
49  // TODO(brg) : Current UI requires win_util.
50  DetectGoogleToolbarProfiles();
51#else
52#if defined(OS_MACOSX)
53  DetectSafariProfiles();
54#endif
55  DetectFirefoxProfiles();
56#endif
57}
58
59Importer* ImporterList::CreateImporterByType(importer::ProfileType type) {
60  switch (type) {
61#if defined(OS_WIN)
62    case importer::MS_IE:
63      return new IEImporter();
64#endif
65    case importer::BOOKMARKS_HTML:
66    case importer::FIREFOX2:
67      return new Firefox2Importer();
68    case importer::FIREFOX3:
69      return new Firefox3Importer();
70    case importer::GOOGLE_TOOLBAR5:
71      return new Toolbar5Importer();
72#if defined(OS_MACOSX)
73    case importer::SAFARI:
74      return new SafariImporter(mac_util::GetUserLibraryPath());
75#endif  // OS_MACOSX
76  }
77  NOTREACHED();
78  return NULL;
79}
80
81int ImporterList::GetAvailableProfileCount() const {
82  return static_cast<int>(source_profiles_.size());
83}
84
85std::wstring ImporterList::GetSourceProfileNameAt(int index) const {
86  DCHECK(index >=0 && index < GetAvailableProfileCount());
87  return source_profiles_[index]->description;
88}
89
90const importer::ProfileInfo& ImporterList::GetSourceProfileInfoAt(
91    int index) const {
92  DCHECK(index >=0 && index < GetAvailableProfileCount());
93  return *source_profiles_[index];
94}
95
96const importer::ProfileInfo& ImporterList::GetSourceProfileInfoForBrowserType(
97    int browser_type) const {
98  int count = GetAvailableProfileCount();
99  for (int i = 0; i < count; ++i) {
100    if (source_profiles_[i]->browser_type == browser_type)
101      return *source_profiles_[i];
102  }
103  NOTREACHED();
104  return *(new importer::ProfileInfo());
105}
106
107#if defined(OS_WIN)
108void ImporterList::DetectIEProfiles() {
109  // IE always exists and don't have multiple profiles.
110  ProfileInfo* ie = new ProfileInfo();
111  ie->description = l10n_util::GetString(IDS_IMPORT_FROM_IE);
112  ie->browser_type = importer::MS_IE;
113  ie->source_path.clear();
114  ie->app_path.clear();
115  ie->services_supported = importer::HISTORY | importer::FAVORITES |
116      importer::COOKIES | importer::PASSWORDS | importer::SEARCH_ENGINES;
117  source_profiles_.push_back(ie);
118}
119#endif
120
121void ImporterList::DetectFirefoxProfiles() {
122  FilePath profile_path = GetFirefoxProfilePath();
123  if (profile_path.empty())
124    return;
125
126  // Detects which version of Firefox is installed.
127  importer::ProfileType firefox_type;
128  FilePath app_path;
129  int version = 0;
130#if defined(OS_WIN)
131  version = GetCurrentFirefoxMajorVersionFromRegistry();
132#endif
133  if (version != 2 && version != 3)
134    GetFirefoxVersionAndPathFromProfile(profile_path, &version, &app_path);
135
136  if (version == 2) {
137    firefox_type = importer::FIREFOX2;
138  } else if (version == 3) {
139    firefox_type = importer::FIREFOX3;
140  } else {
141    // Ignores other versions of firefox.
142    return;
143  }
144
145  importer::ProfileInfo* firefox = new importer::ProfileInfo();
146  firefox->description = l10n_util::GetString(IDS_IMPORT_FROM_FIREFOX);
147  firefox->browser_type = firefox_type;
148  firefox->source_path = profile_path;
149#if defined(OS_WIN)
150  firefox->app_path = FilePath::FromWStringHack(
151      GetFirefoxInstallPathFromRegistry());
152#endif
153  if (firefox->app_path.empty())
154    firefox->app_path = app_path;
155  firefox->services_supported = importer::HISTORY | importer::FAVORITES |
156      importer::PASSWORDS | importer::SEARCH_ENGINES;
157  source_profiles_.push_back(firefox);
158}
159
160void ImporterList::DetectGoogleToolbarProfiles() {
161  if (!FirstRun::IsChromeFirstRun()) {
162    importer::ProfileInfo* google_toolbar = new importer::ProfileInfo();
163    google_toolbar->browser_type = importer::GOOGLE_TOOLBAR5;
164    google_toolbar->description = l10n_util::GetString(
165                                  IDS_IMPORT_FROM_GOOGLE_TOOLBAR);
166    google_toolbar->source_path.clear();
167    google_toolbar->app_path.clear();
168    google_toolbar->services_supported = importer::FAVORITES;
169    source_profiles_.push_back(google_toolbar);
170  }
171}
172
173#if defined(OS_MACOSX)
174void ImporterList::DetectSafariProfiles() {
175  uint16 items = importer::NONE;
176  if (SafariImporter::CanImport(mac_util::GetUserLibraryPath(), &items)) {
177    importer::ProfileInfo* safari = new importer::ProfileInfo();
178    safari->browser_type = importer::SAFARI;
179    safari->description = l10n_util::GetString(IDS_IMPORT_FROM_SAFARI);
180    safari->source_path.clear();
181    safari->app_path.clear();
182    safari->services_supported = items;
183    source_profiles_.push_back(safari);
184  }
185}
186#endif  // OS_MACOSX
187