nsAppFileLocationProvider.cpp 13.3 KB
Newer Older
1
2
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
4
5
/* 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/. */
6

7
#include "nsAppFileLocationProvider.h"
8
9
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceDefs.h"
10
#include "nsEnumeratorUtils.h"
11
#include "nsAtom.h"
12
#include "nsIDirectoryService.h"
13
#include "nsIFile.h"
dougt%netscape.com's avatar
dougt%netscape.com committed
14
#include "nsString.h"
15
#include "nsSimpleEnumerator.h"
16
#include "prenv.h"
17
#include "nsCRT.h"
18
#if defined(MOZ_WIDGET_COCOA)
19
20
#  include <Carbon/Carbon.h>
#  include "nsILocalFileMac.h"
seawood%netscape.com's avatar
seawood%netscape.com committed
21
#elif defined(XP_WIN)
22
23
#  include <windows.h>
#  include <shlobj.h>
dougt%netscape.com's avatar
dougt%netscape.com committed
24
#elif defined(XP_UNIX)
25
26
27
#  include <unistd.h>
#  include <stdlib.h>
#  include <sys/param.h>
28
#endif
ccarlen%netscape.com's avatar
ccarlen%netscape.com committed
29

30
31
32
// WARNING: These hard coded names need to go away. They need to
// come from localizable resources

33
#if defined(MOZ_WIDGET_COCOA)
34
35
#  define APP_REGISTRY_NAME "Application Registry"_ns
#  define ESSENTIAL_FILES "Essential Files"_ns
36
#elif defined(XP_WIN)
37
#  define APP_REGISTRY_NAME "registry.dat"_ns
38
#else
39
#  define APP_REGISTRY_NAME "appreg"_ns
40
#endif
41
42

// define default product directory
43
#define DEFAULT_PRODUCT_DIR nsLiteralCString(MOZ_USER_DIR)
44

45
// Locally defined keys used by nsAppDirectoryEnumerator
46
#define NS_USER_PLUGINS_DIR "UserPlugins"
47

48
#ifdef MOZ_WIDGET_COCOA
49
50
#  define NS_MACOSX_USER_PLUGIN_DIR "OSXUserPlugins"
#  define NS_MACOSX_LOCAL_PLUGIN_DIR "OSXLocalPlugins"
51
#elif XP_UNIX
52
#  define NS_SYSTEM_PLUGINS_DIR "SysPlugins"
53
54
#endif

55
56
57
58
59
#define DEFAULTS_DIR_NAME "defaults"_ns
#define DEFAULTS_PREF_DIR_NAME "pref"_ns
#define RES_DIR_NAME "res"_ns
#define CHROME_DIR_NAME "chrome"_ns
#define PLUGINS_DIR_NAME "plugins"_ns
60
61
62

//*****************************************************************************
// nsAppFileLocationProvider::Constructor/Destructor
63
//*****************************************************************************
64

65
nsAppFileLocationProvider::nsAppFileLocationProvider() = default;
66
67
68

//*****************************************************************************
// nsAppFileLocationProvider::nsISupports
69
//*****************************************************************************
70

71
NS_IMPL_ISUPPORTS(nsAppFileLocationProvider, nsIDirectoryServiceProvider,
72
                  nsIDirectoryServiceProvider2)
73
74
75

//*****************************************************************************
// nsAppFileLocationProvider::nsIDirectoryServiceProvider
76
//*****************************************************************************
77
78

NS_IMETHODIMP
79
nsAppFileLocationProvider::GetFile(const char* aProp, bool* aPersistent,
80
                                   nsIFile** aResult) {
81
  if (NS_WARN_IF(!aProp)) {
82
    return NS_ERROR_INVALID_ARG;
83
  }
84

85
  nsCOMPtr<nsIFile> localFile;
86
  nsresult rv = NS_ERROR_FAILURE;
87

88
89
  *aResult = nullptr;
  *aPersistent = true;
90

91
#ifdef MOZ_WIDGET_COCOA
92
93
  FSRef fileRef;
  nsCOMPtr<nsILocalFileMac> macFile;
94
#endif
95

96
  if (nsCRT::strcmp(aProp, NS_APP_APPLICATION_REGISTRY_DIR) == 0) {
97
    rv = GetProductDirectory(getter_AddRefs(localFile));
98
  } else if (nsCRT::strcmp(aProp, NS_APP_APPLICATION_REGISTRY_FILE) == 0) {
99
    rv = GetProductDirectory(getter_AddRefs(localFile));
100
    if (NS_SUCCEEDED(rv)) {
101
      rv = localFile->AppendNative(APP_REGISTRY_NAME);
102
103
    }
  } else if (nsCRT::strcmp(aProp, NS_APP_DEFAULTS_50_DIR) == 0) {
104
    rv = CloneMozBinDirectory(getter_AddRefs(localFile));
105
    if (NS_SUCCEEDED(rv)) {
106
      rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
107
108
    }
  } else if (nsCRT::strcmp(aProp, NS_APP_PREF_DEFAULTS_50_DIR) == 0) {
109
110
111
    rv = CloneMozBinDirectory(getter_AddRefs(localFile));
    if (NS_SUCCEEDED(rv)) {
      rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
112
      if (NS_SUCCEEDED(rv)) {
113
        rv = localFile->AppendRelativeNativePath(DEFAULTS_PREF_DIR_NAME);
114
      }
115
    }
116
  } else if (nsCRT::strcmp(aProp, NS_APP_USER_PROFILES_ROOT_DIR) == 0) {
117
    rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile));
118
  } else if (nsCRT::strcmp(aProp, NS_APP_USER_PROFILES_LOCAL_ROOT_DIR) == 0) {
119
    rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile), true);
120
  } else if (nsCRT::strcmp(aProp, NS_APP_RES_DIR) == 0) {
121
    rv = CloneMozBinDirectory(getter_AddRefs(localFile));
122
    if (NS_SUCCEEDED(rv)) {
123
      rv = localFile->AppendRelativeNativePath(RES_DIR_NAME);
124
125
    }
  } else if (nsCRT::strcmp(aProp, NS_APP_CHROME_DIR) == 0) {
126
    rv = CloneMozBinDirectory(getter_AddRefs(localFile));
127
    if (NS_SUCCEEDED(rv)) {
128
      rv = localFile->AppendRelativeNativePath(CHROME_DIR_NAME);
129
    }
130
  }
131
#ifdef MOZ_WIDGET_COCOA
132
  else if (nsCRT::strcmp(aProp, NS_MACOSX_USER_PLUGIN_DIR) == 0) {
133
134
    if (::FSFindFolder(kUserDomain, kInternetPlugInFolderType, false,
                       &fileRef) == noErr) {
135
      rv = NS_NewLocalFileWithFSRef(&fileRef, true, getter_AddRefs(macFile));
136
      if (NS_SUCCEEDED(rv)) {
137
        localFile = macFile;
138
      }
139
    }
140
  } else if (nsCRT::strcmp(aProp, NS_MACOSX_LOCAL_PLUGIN_DIR) == 0) {
141
142
    if (::FSFindFolder(kLocalDomain, kInternetPlugInFolderType, false,
                       &fileRef) == noErr) {
143
      rv = NS_NewLocalFileWithFSRef(&fileRef, true, getter_AddRefs(macFile));
144
      if (NS_SUCCEEDED(rv)) {
145
        localFile = macFile;
146
      }
147
    }
148
  }
149
#else
150
  else if (nsCRT::strcmp(aProp, NS_USER_PLUGINS_DIR) == 0) {
151
#  ifdef ENABLE_SYSTEM_EXTENSION_DIRS
152
    rv = GetProductDirectory(getter_AddRefs(localFile));
153
    if (NS_SUCCEEDED(rv)) {
154
      rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
155
    }
156
#  else
157
    rv = NS_ERROR_FAILURE;
158
#  endif
159
  }
160
#  ifdef XP_UNIX
161
  else if (nsCRT::strcmp(aProp, NS_SYSTEM_PLUGINS_DIR) == 0) {
162
#    ifdef ENABLE_SYSTEM_EXTENSION_DIRS
163
    static const char* const sysLPlgDir =
164
#      if defined(HAVE_USR_LIB64_DIR) && defined(__LP64__)
165
        "/usr/lib64/mozilla/plugins";
166
#      elif defined(__OpenBSD__) || defined(__FreeBSD__)
167
        "/usr/local/lib/mozilla/plugins";
168
#      else
169
        "/usr/lib/mozilla/plugins";
170
#      endif
171
172
    rv = NS_NewNativeLocalFile(nsDependentCString(sysLPlgDir), false,
                               getter_AddRefs(localFile));
173
#    else
174
    rv = NS_ERROR_FAILURE;
175
#    endif
176
  }
177
#  endif
178
#endif
179
  else if (nsCRT::strcmp(aProp, NS_APP_INSTALL_CLEANUP_DIR) == 0) {
180
181
182
183
184
    // This is cloned so that embeddors will have a hook to override
    // with their own cleanup dir.  See bugzilla bug #105087
    rv = CloneMozBinDirectory(getter_AddRefs(localFile));
  }

185
  if (localFile && NS_SUCCEEDED(rv)) {
186
187
    localFile.forget(aResult);
    return NS_OK;
188
  }
189
190

  return rv;
191
192
}

193
nsresult nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile** aLocalFile) {
194
  if (NS_WARN_IF(!aLocalFile)) {
195
    return NS_ERROR_INVALID_ARG;
196
  }
197
198
  nsresult rv;

199
  if (!mMozBinDirectory) {
200
201
202
203
    // Get the mozilla bin directory
    // 1. Check the directory service first for NS_XPCOM_CURRENT_PROCESS_DIR
    //    This will be set if a directory was passed to NS_InitXPCOM
    // 2. If that doesn't work, set it to be the current process directory
204
205
    nsCOMPtr<nsIProperties> directoryService(
        do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
206
    if (NS_FAILED(rv)) {
207
      return rv;
208
    }
209

210
211
212
    rv =
        directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
                              getter_AddRefs(mMozBinDirectory));
213
    if (NS_FAILED(rv)) {
214
215
216
      rv = directoryService->Get(NS_OS_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
                                 getter_AddRefs(mMozBinDirectory));
      if (NS_FAILED(rv)) {
217
        return rv;
218
      }
219
    }
220
  }
221

222
223
  nsCOMPtr<nsIFile> aFile;
  rv = mMozBinDirectory->Clone(getter_AddRefs(aFile));
224
  if (NS_FAILED(rv)) {
225
    return rv;
226
  }
227

228
229
  NS_IF_ADDREF(*aLocalFile = aFile);
  return NS_OK;
230
231
232
}

//----------------------------------------------------------------------------------------
233
234
// GetProductDirectory - Gets the directory which contains the application data
// folder
235
236
//
// UNIX   : ~/.mozilla/
237
// WIN    : <Application Data folder on user's machine>\Mozilla
238
// Mac    : :Documents:Mozilla:
239
//----------------------------------------------------------------------------------------
240
241
nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
                                                        bool aLocal) {
242
  if (NS_WARN_IF(!aLocalFile)) {
243
    return NS_ERROR_INVALID_ARG;
244
  }
245

246
247
248
  nsresult rv;
  bool exists;
  nsCOMPtr<nsIFile> localDir;
249

250
#if defined(MOZ_WIDGET_COCOA)
251
  FSRef fsRef;
252
253
  OSType folderType =
      aLocal ? (OSType)kCachedDataFolderType : (OSType)kDomainLibraryFolderType;
254
  OSErr err = ::FSFindFolder(kUserDomain, folderType, kCreateFolder, &fsRef);
255
256
257
  if (err) {
    return NS_ERROR_FAILURE;
  }
258
  NS_NewLocalFile(EmptyString(), true, getter_AddRefs(localDir));
259
260
261
  if (!localDir) {
    return NS_ERROR_FAILURE;
  }
262
263
  nsCOMPtr<nsILocalFileMac> localDirMac(do_QueryInterface(localDir));
  rv = localDirMac->InitWithFSRef(&fsRef);
264
265
266
  if (NS_FAILED(rv)) {
    return rv;
  }
seawood%netscape.com's avatar
seawood%netscape.com committed
267
#elif defined(XP_WIN)
268
  nsCOMPtr<nsIProperties> directoryService =
269
      do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
270
271
272
  if (NS_FAILED(rv)) {
    return rv;
  }
273
  const char* prop = aLocal ? NS_WIN_LOCAL_APPDATA_DIR : NS_WIN_APPDATA_DIR;
274
275
  rv = directoryService->Get(prop, NS_GET_IID(nsIFile),
                             getter_AddRefs(localDir));
276
277
278
  if (NS_FAILED(rv)) {
    return rv;
  }
279
#elif defined(XP_UNIX)
280
281
  rv = NS_NewNativeLocalFile(nsDependentCString(PR_GetEnv("HOME")), true,
                             getter_AddRefs(localDir));
282
283
284
  if (NS_FAILED(rv)) {
    return rv;
  }
285
#else
286
#  error dont_know_how_to_get_product_dir_on_your_platform
287
288
#endif

289
  rv = localDir->AppendRelativeNativePath(DEFAULT_PRODUCT_DIR);
290
291
292
  if (NS_FAILED(rv)) {
    return rv;
  }
293
  rv = localDir->Exists(&exists);
294

295
  if (NS_SUCCEEDED(rv) && !exists) {
296
    rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
297
  }
298

299
300
301
  if (NS_FAILED(rv)) {
    return rv;
  }
302

303
  localDir.forget(aLocalFile);
304

305
  return rv;
306
307
308
}

//----------------------------------------------------------------------------------------
309
310
// GetDefaultUserProfileRoot - Gets the directory which contains each user
// profile dir
311
312
//
// UNIX   : ~/.mozilla/
313
// WIN    : <Application Data folder on user's machine>\Mozilla\Profiles
314
// Mac    : :Documents:Mozilla:Profiles:
315
//----------------------------------------------------------------------------------------
316
317
nsresult nsAppFileLocationProvider::GetDefaultUserProfileRoot(
    nsIFile** aLocalFile, bool aLocal) {
318
  if (NS_WARN_IF(!aLocalFile)) {
319
    return NS_ERROR_INVALID_ARG;
320
  }
321

322
323
  nsresult rv;
  nsCOMPtr<nsIFile> localDir;
324

325
  rv = GetProductDirectory(getter_AddRefs(localDir), aLocal);
326
327
328
  if (NS_FAILED(rv)) {
    return rv;
  }
329

330
#if defined(MOZ_WIDGET_COCOA) || defined(XP_WIN)
331
  // These 3 platforms share this part of the path - do them as one
332
  rv = localDir->AppendRelativeNativePath("Profiles"_ns);
333
334
335
  if (NS_FAILED(rv)) {
    return rv;
  }
336
337
338

  bool exists;
  rv = localDir->Exists(&exists);
339
  if (NS_SUCCEEDED(rv) && !exists) {
340
    rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
341
342
343
344
  }
  if (NS_FAILED(rv)) {
    return rv;
  }
345
346
#endif

347
  localDir.forget(aLocalFile);
348

349
  return rv;
350
351
}

352
353
354
355
//*****************************************************************************
// nsAppFileLocationProvider::nsIDirectoryServiceProvider2
//*****************************************************************************

356
357
class nsAppDirectoryEnumerator : public nsSimpleEnumerator {
 public:
358
  /**
359
360
   * aKeyList is a null-terminated list of properties which are provided by
   * aProvider They do not need to be publicly defined keys.
361
   */
362
  nsAppDirectoryEnumerator(nsIDirectoryServiceProvider* aProvider,
363
364
                           const char* aKeyList[])
      : mProvider(aProvider), mCurrentKey(aKeyList) {}
365

366
367
  const nsID& DefaultInterface() override { return NS_GET_IID(nsIFile); }

368
  NS_IMETHOD HasMoreElements(bool* aResult) override {
369
    while (!mNext && *mCurrentKey) {
370
371
      bool dontCare;
      nsCOMPtr<nsIFile> testFile;
372
373
      (void)mProvider->GetFile(*mCurrentKey++, &dontCare,
                               getter_AddRefs(testFile));
374
      mNext = testFile;
375
    }
376
    *aResult = mNext != nullptr;
377
378
    return NS_OK;
  }
379

380
  NS_IMETHOD GetNext(nsISupports** aResult) override {
381
    if (NS_WARN_IF(!aResult)) {
382
      return NS_ERROR_INVALID_ARG;
383
384
    }
    *aResult = nullptr;
385

386
387
    bool hasMore;
    HasMoreElements(&hasMore);
388
    if (!hasMore) {
389
      return NS_ERROR_FAILURE;
390
    }
391

392
393
    *aResult = mNext;
    NS_IF_ADDREF(*aResult);
394
    mNext = nullptr;
395

396
    return *aResult ? NS_OK : NS_ERROR_FAILURE;
397
398
  }

399
 protected:
400
  nsCOMPtr<nsIDirectoryServiceProvider> mProvider;
401
402
  const char** mCurrentKey;
  nsCOMPtr<nsIFile> mNext;
403
404
405
};

NS_IMETHODIMP
406
nsAppFileLocationProvider::GetFiles(const char* aProp,
407
                                    nsISimpleEnumerator** aResult) {
408
  if (NS_WARN_IF(!aResult)) {
409
    return NS_ERROR_INVALID_ARG;
410
411
  }
  *aResult = nullptr;
412
413
  nsresult rv = NS_ERROR_FAILURE;

414
  if (!nsCRT::strcmp(aProp, NS_APP_PLUGINS_DIR_LIST)) {
415
#ifdef MOZ_WIDGET_COCOA
416
    static const char* keys[] = {NS_MACOSX_USER_PLUGIN_DIR,
417
                                 NS_MACOSX_LOCAL_PLUGIN_DIR, nullptr};
418
#else
419
#  ifdef XP_UNIX
420
    static const char* keys[] = {NS_USER_PLUGINS_DIR, NS_SYSTEM_PLUGINS_DIR,
421
                                 nullptr};
422
#  else
423
    static const char* keys[] = {NS_USER_PLUGINS_DIR, nullptr};
424
#  endif
425
#endif
426
    *aResult = new nsAppDirectoryEnumerator(this, keys);
427
428
    NS_ADDREF(*aResult);
    rv = NS_OK;
429
430
  }
  return rv;
431
}