2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-06-07 19:45:11 +00:00
|
|
|
|
|
|
|
#include "nsIDirectoryService.h"
|
2009-11-07 07:19:44 +00:00
|
|
|
#include "DirectoryProvider.h"
|
2005-06-07 19:45:11 +00:00
|
|
|
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2005-08-08 13:37:51 +00:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2006-08-02 13:58:31 +00:00
|
|
|
#include "nsArrayEnumerator.h"
|
|
|
|
#include "nsEnumeratorUtils.h"
|
2005-06-07 19:45:11 +00:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2006-06-12 18:58:37 +00:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2005-06-07 19:45:11 +00:00
|
|
|
#include "nsCategoryManagerUtils.h"
|
2006-11-13 17:53:01 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2005-06-07 19:45:11 +00:00
|
|
|
#include "nsCOMArray.h"
|
2006-11-13 17:53:01 +00:00
|
|
|
#include "nsDirectoryServiceUtils.h"
|
2010-06-10 18:11:40 +00:00
|
|
|
#include "mozilla/ModuleUtils.h"
|
2006-11-13 17:53:01 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStringAPI.h"
|
2005-06-07 19:45:11 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-03-08 07:39:17 +00:00
|
|
|
#include "nsIPrefLocalizedString.h"
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2009-11-07 07:19:44 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace browser {
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(DirectoryProvider,
|
|
|
|
nsIDirectoryServiceProvider,
|
|
|
|
nsIDirectoryServiceProvider2)
|
2005-06-07 19:45:11 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
DirectoryProvider::GetFile(const char *aKey, bool *aPersist, nsIFile* *aResult)
|
2005-06-07 19:45:11 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
2006-06-12 18:58:37 +00:00
|
|
|
|
2005-06-07 19:45:11 +00:00
|
|
|
// NOTE: This function can be reentrant through the NS_GetSpecialDirectory
|
|
|
|
// call, so be careful not to cause infinite recursion.
|
|
|
|
|
2005-08-08 13:37:51 +00:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
char const* leafName = nullptr;
|
2005-08-08 13:37:51 +00:00
|
|
|
|
|
|
|
if (!strcmp(aKey, NS_APP_BOOKMARKS_50_FILE)) {
|
2005-06-08 12:18:17 +00:00
|
|
|
leafName = "bookmarks.html";
|
2005-08-08 13:37:51 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (prefs) {
|
2006-11-13 17:53:01 +00:00
|
|
|
nsCString path;
|
2005-08-08 13:37:51 +00:00
|
|
|
rv = prefs->GetCharPref("browser.bookmarks.file", getter_Copies(path));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-06-06 02:08:30 +00:00
|
|
|
NS_NewNativeLocalFile(path, true, getter_AddRefs(file));
|
2005-08-08 13:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-07 19:45:11 +00:00
|
|
|
else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2005-08-08 13:37:51 +00:00
|
|
|
nsDependentCString leafstr(leafName);
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2005-08-08 13:37:51 +00:00
|
|
|
nsCOMPtr<nsIFile> parentDir;
|
|
|
|
if (file) {
|
|
|
|
rv = file->GetParent(getter_AddRefs(parentDir));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(parentDir));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2005-08-08 13:37:51 +00:00
|
|
|
rv = parentDir->Clone(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
file->AppendNative(leafstr);
|
|
|
|
}
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*aPersist = true;
|
2005-06-07 19:45:11 +00:00
|
|
|
NS_ADDREF(*aResult = file);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-08-02 13:58:31 +00:00
|
|
|
static void
|
|
|
|
AppendFileKey(const char *key, nsIProperties* aDirSvc,
|
|
|
|
nsCOMArray<nsIFile> &array)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = aDirSvc->Get(key, NS_GET_IID(nsIFile), getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists;
|
2006-08-02 13:58:31 +00:00
|
|
|
rv = file->Exists(&exists);
|
|
|
|
if (NS_FAILED(rv) || !exists)
|
|
|
|
return;
|
|
|
|
|
|
|
|
array.AppendObject(file);
|
|
|
|
}
|
|
|
|
|
2007-09-11 16:09:15 +00:00
|
|
|
// Appends the distribution-specific search engine directories to the
|
|
|
|
// array. The directory structure is as follows:
|
|
|
|
|
|
|
|
// appdir/
|
|
|
|
// \- distribution/
|
|
|
|
// \- searchplugins/
|
|
|
|
// |- common/
|
|
|
|
// \- locale/
|
|
|
|
// |- <locale 1>/
|
|
|
|
// ...
|
|
|
|
// \- <locale N>/
|
|
|
|
|
|
|
|
// common engines are loaded for all locales. If there is no locale
|
|
|
|
// directory for the current locale, there is a pref:
|
|
|
|
// "distribution.searchplugins.defaultLocale"
|
|
|
|
// which specifies a default locale to use.
|
|
|
|
|
|
|
|
static void
|
|
|
|
AppendDistroSearchDirs(nsIProperties* aDirSvc, nsCOMArray<nsIFile> &array)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> searchPlugins;
|
2014-09-29 18:51:51 +00:00
|
|
|
nsresult rv = aDirSvc->Get(XRE_APP_DISTRIBUTION_DIR,
|
2007-09-11 16:09:15 +00:00
|
|
|
NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(searchPlugins));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
searchPlugins->AppendNative(NS_LITERAL_CSTRING("searchplugins"));
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists;
|
2007-09-11 16:09:15 +00:00
|
|
|
rv = searchPlugins->Exists(&exists);
|
|
|
|
if (NS_FAILED(rv) || !exists)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> commonPlugins;
|
|
|
|
rv = searchPlugins->Clone(getter_AddRefs(commonPlugins));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
commonPlugins->AppendNative(NS_LITERAL_CSTRING("common"));
|
|
|
|
rv = commonPlugins->Exists(&exists);
|
|
|
|
if (NS_SUCCEEDED(rv) && exists)
|
|
|
|
array.AppendObject(commonPlugins);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (prefs) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> localePlugins;
|
|
|
|
rv = searchPlugins->Clone(getter_AddRefs(localePlugins));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
localePlugins->AppendNative(NS_LITERAL_CSTRING("locale"));
|
|
|
|
|
|
|
|
nsCString locale;
|
2012-03-08 07:39:17 +00:00
|
|
|
nsCOMPtr<nsIPrefLocalizedString> prefString;
|
|
|
|
rv = prefs->GetComplexValue("general.useragent.locale",
|
|
|
|
NS_GET_IID(nsIPrefLocalizedString),
|
|
|
|
getter_AddRefs(prefString));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsAutoString wLocale;
|
|
|
|
prefString->GetData(getter_Copies(wLocale));
|
|
|
|
CopyUTF16toUTF8(wLocale, locale);
|
|
|
|
} else {
|
|
|
|
rv = prefs->GetCharPref("general.useragent.locale", getter_Copies(locale));
|
|
|
|
}
|
|
|
|
|
2007-09-11 16:09:15 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> curLocalePlugins;
|
|
|
|
rv = localePlugins->Clone(getter_AddRefs(curLocalePlugins));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
|
|
|
curLocalePlugins->AppendNative(locale);
|
|
|
|
rv = curLocalePlugins->Exists(&exists);
|
|
|
|
if (NS_SUCCEEDED(rv) && exists) {
|
|
|
|
array.AppendObject(curLocalePlugins);
|
|
|
|
return; // all done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we didn't append the locale dir - try the default one
|
|
|
|
nsCString defLocale;
|
|
|
|
rv = prefs->GetCharPref("distribution.searchplugins.defaultLocale",
|
|
|
|
getter_Copies(defLocale));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> defLocalePlugins;
|
|
|
|
rv = localePlugins->Clone(getter_AddRefs(defLocalePlugins));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
|
|
|
defLocalePlugins->AppendNative(defLocale);
|
|
|
|
rv = defLocalePlugins->Exists(&exists);
|
|
|
|
if (NS_SUCCEEDED(rv) && exists)
|
|
|
|
array.AppendObject(defLocalePlugins);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-07 19:45:11 +00:00
|
|
|
NS_IMETHODIMP
|
2009-11-07 07:19:44 +00:00
|
|
|
DirectoryProvider::GetFiles(const char *aKey, nsISimpleEnumerator* *aResult)
|
2005-06-07 19:45:11 +00:00
|
|
|
{
|
2015-06-15 16:32:09 +00:00
|
|
|
/**
|
|
|
|
* We want to preserve the following order, since the search service loads
|
|
|
|
* engines in first-loaded-wins order.
|
|
|
|
* - distro search plugin locations (Loaded by the search service using
|
|
|
|
* NS_APP_DISTRIBUTION_SEARCH_DIR_LIST)
|
|
|
|
*
|
|
|
|
* - engines shipped in chrome (Loaded from jar files by the search
|
|
|
|
* service)
|
|
|
|
*
|
|
|
|
* Then other locations, from NS_APP_SEARCH_DIR_LIST:
|
|
|
|
* - extension search plugin locations (prepended below using
|
|
|
|
* NS_NewUnionEnumerator)
|
|
|
|
* - user search plugin locations (profile)
|
|
|
|
*/
|
|
|
|
|
2005-06-07 19:45:11 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2015-06-15 16:32:09 +00:00
|
|
|
if (!strcmp(aKey, NS_APP_DISTRIBUTION_SEARCH_DIR_LIST)) {
|
|
|
|
nsCOMPtr<nsIProperties> dirSvc
|
|
|
|
(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
|
|
|
|
if (!dirSvc)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMArray<nsIFile> distroFiles;
|
|
|
|
AppendDistroSearchDirs(dirSvc, distroFiles);
|
|
|
|
|
|
|
|
return NS_NewArrayEnumerator(aResult, distroFiles);
|
|
|
|
}
|
|
|
|
|
2005-06-07 19:45:11 +00:00
|
|
|
if (!strcmp(aKey, NS_APP_SEARCH_DIR_LIST)) {
|
|
|
|
nsCOMPtr<nsIProperties> dirSvc
|
|
|
|
(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
|
|
|
|
if (!dirSvc)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2006-08-02 13:58:31 +00:00
|
|
|
nsCOMArray<nsIFile> baseFiles;
|
|
|
|
|
|
|
|
AppendFileKey(NS_APP_USER_SEARCH_DIR, dirSvc, baseFiles);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> baseEnum;
|
|
|
|
rv = NS_NewArrayEnumerator(getter_AddRefs(baseEnum), baseFiles);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2005-06-07 19:45:11 +00:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> list;
|
|
|
|
rv = dirSvc->Get(XRE_EXTENSIONS_DIR_LIST,
|
|
|
|
NS_GET_IID(nsISimpleEnumerator), getter_AddRefs(list));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
static char const *const kAppendSPlugins[] = {"searchplugins", nullptr};
|
2005-06-07 19:45:11 +00:00
|
|
|
|
2006-08-02 13:58:31 +00:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> extEnum =
|
|
|
|
new AppendingEnumerator(list, kAppendSPlugins);
|
|
|
|
if (!extEnum)
|
2005-06-07 19:45:11 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2006-08-02 13:58:31 +00:00
|
|
|
return NS_NewUnionEnumerator(aResult, extEnum, baseEnum);
|
2005-06-07 19:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(DirectoryProvider::AppendingEnumerator, nsISimpleEnumerator)
|
2005-06-07 19:45:11 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
DirectoryProvider::AppendingEnumerator::HasMoreElements(bool *aResult)
|
2005-06-07 19:45:11 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*aResult = mNext ? true : false;
|
2005-06-07 19:45:11 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-07 07:19:44 +00:00
|
|
|
DirectoryProvider::AppendingEnumerator::GetNext(nsISupports* *aResult)
|
2005-06-07 19:45:11 +00:00
|
|
|
{
|
|
|
|
if (aResult)
|
|
|
|
NS_ADDREF(*aResult = mNext);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mNext = nullptr;
|
2005-06-07 19:45:11 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Ignore all errors
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool more;
|
2005-06-07 19:45:11 +00:00
|
|
|
while (NS_SUCCEEDED(mBase->HasMoreElements(&more)) && more) {
|
|
|
|
nsCOMPtr<nsISupports> nextbasesupp;
|
|
|
|
mBase->GetNext(getter_AddRefs(nextbasesupp));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> nextbase(do_QueryInterface(nextbasesupp));
|
|
|
|
if (!nextbase)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nextbase->Clone(getter_AddRefs(mNext));
|
|
|
|
if (!mNext)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char const *const * i = mAppendList;
|
|
|
|
while (*i) {
|
|
|
|
mNext->AppendNative(nsDependentCString(*i));
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists;
|
2005-06-07 19:45:11 +00:00
|
|
|
rv = mNext->Exists(&exists);
|
|
|
|
if (NS_SUCCEEDED(rv) && exists)
|
|
|
|
break;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mNext = nullptr;
|
2005-06-07 19:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-07 07:19:44 +00:00
|
|
|
DirectoryProvider::AppendingEnumerator::AppendingEnumerator
|
2005-06-07 19:45:11 +00:00
|
|
|
(nsISimpleEnumerator* aBase,
|
|
|
|
char const *const *aAppendList) :
|
|
|
|
mBase(aBase),
|
|
|
|
mAppendList(aAppendList)
|
|
|
|
{
|
|
|
|
// Initialize mNext to begin.
|
2012-07-30 14:20:58 +00:00
|
|
|
GetNext(nullptr);
|
2005-06-07 19:45:11 +00:00
|
|
|
}
|
2009-11-07 07:19:44 +00:00
|
|
|
|
|
|
|
} // namespace browser
|
|
|
|
} // namespace mozilla
|