Ensure that Firefox search engines are imported correctly.
It also makes sure that engines which have been removed from Firefox
are not imported into Chrome.

BUG= http://crbug.com/12245
TEST= Install firefox, and do not change the default search engines.
Start Chrome, and import search engines from Firefox.  Default Firefox
engines should now be part of Chrome.


Review URL: http://codereview.chromium.org/115895

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17207 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/importer/firefox3_importer.cc b/chrome/browser/importer/firefox3_importer.cc
index 28ea6bdb..60be97c 100644
--- a/chrome/browser/importer/firefox3_importer.cc
+++ b/chrome/browser/importer/firefox3_importer.cc
@@ -325,7 +325,11 @@
   scoped_ptr_malloc<sqlite3, DBClose> db(sqlite);
 
   SQLStatement s;
-  const char* stmt = "SELECT engineid FROM engine_data ORDER BY value ASC";
+  const char* stmt = "SELECT engineid FROM engine_data "
+                     "WHERE engineid NOT IN "
+                     "(SELECT engineid FROM engine_data "
+                     "WHERE name='hidden') "
+                     "ORDER BY value ASC";
 
   if (s.prepare(db.get(), stmt) != SQLITE_OK)
     return;
@@ -335,31 +339,48 @@
   std::wstring profile_path = source_path_;
   file_util::AppendToPath(&profile_path, L"searchplugins");
 
-  const std::wstring kAppPrefix = L"[app]/";
-  const std::wstring kProfilePrefix = L"[profile]/";
-  while (s.step() == SQLITE_ROW && !cancelled()) {
-    std::wstring file;
-    std::wstring engine = UTF8ToWide(s.column_string(0));
-    // The string contains [app]/<name>.xml or [profile]/<name>.xml where the
-    // [app] and [profile] need to be replaced with the actual app or profile
-    // path.
-    size_t index = engine.find(kAppPrefix);
-    if (index != std::wstring::npos) {
-      file = app_path;
-      file_util::AppendToPath(
-          &file,
-          engine.substr(index + kAppPrefix.length()));  // Remove '[app]/'.
-    } else if ((index = engine.find(kProfilePrefix)) != std::wstring::npos) {
-      file = profile_path;
-      file_util::AppendToPath(
-          &file,
-          engine.substr(index + kProfilePrefix.length()));  // Remove
-                                                            // '[profile]/'.
-    } else {
-      NOTREACHED() << "Unexpected Firefox 3 search engine id";
-      continue;
+  // Firefox doesn't store any search engines in its sqlite database unless
+  // the user has changed the standard set of engines.  If we find that the
+  // database is empty, get the standard Firefox set from the app folder.
+  if (s.step() != SQLITE_ROW) {
+    file_util::FileEnumerator engines(FilePath::FromWStringHack(app_path),
+                                      false,
+                                      file_util::FileEnumerator::FILES);
+    for (FilePath engine_path = engines.Next(); !engine_path.value().empty();
+         engine_path = engines.Next()) {
+      std::wstring enginew = engine_path.ToWStringHack();
+      files->push_back(enginew);
     }
-    files->push_back(file);
+  } else {
+    const std::wstring kAppPrefix = L"[app]/";
+    const std::wstring kProfilePrefix = L"[profile]/";
+    do {
+      std::wstring file;
+      std::wstring engine = UTF8ToWide(s.column_string(0));
+
+      // The string contains [app]/<name>.xml or [profile]/<name>.xml where
+      // the [app] and [profile] need to be replaced with the actual app or
+      // profile path.
+      size_t index = engine.find(kAppPrefix);
+      if (index != std::wstring::npos) {
+        // Remove '[app]/'.
+        file = app_path;
+        file_util::AppendToPath(
+            &file,
+            engine.substr(index + kAppPrefix.length()));
+      } else if ((index = engine.find(kProfilePrefix)) !=
+                  std::wstring::npos) {
+        // Remove '[profile]/'.
+        file = profile_path;
+        file_util::AppendToPath(
+            &file,
+            engine.substr(index + kProfilePrefix.length()));
+      } else {
+        NOTREACHED() << "Unexpected Firefox 3 search engine id";
+        continue;
+      }
+      files->push_back(file);
+    } while (s.step() == SQLITE_ROW && !cancelled());
   }
 }