blob: 40da55a2e21e1f36413b376930e19110249e831a [file] [log] [blame]
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
jcampan@chromium.org36fe2212009-09-03 02:18:22 +09005#ifndef BASE_PATH_SERVICE_H_
6#define BASE_PATH_SERVICE_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <string>
9
darin@chromium.orge585bed2011-08-06 00:34:00 +090010#include "base/base_export.h"
initial.commit3f4a7322008-07-27 06:49:38 +090011#include "base/base_paths.h"
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +090012#include "base/gtest_prod_util.h"
rvargas@google.com4891e7d2011-03-26 03:46:38 +090013#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090014
evanm@google.com874d1672008-10-31 08:54:04 +090015class FilePath;
16
initial.commit3f4a7322008-07-27 06:49:38 +090017// The path service is a global table mapping keys to file system paths. It is
18// OK to use this service from multiple threads.
19//
darin@chromium.orge585bed2011-08-06 00:34:00 +090020class BASE_EXPORT PathService {
initial.commit3f4a7322008-07-27 06:49:38 +090021 public:
22 // Retrieves a path to a special directory or file and places it into the
23 // string pointed to by 'path'. If you ask for a directory it is guaranteed
24 // to NOT have a path separator at the end. For example, "c:\windows\temp"
25 // Directories are also guaranteed to exist when this function succeeds.
26 //
27 // Returns true if the directory or file was successfully retrieved. On
28 // failure, 'path' will not be changed.
evanm@google.com874d1672008-10-31 08:54:04 +090029 static bool Get(int key, FilePath* path);
initial.commit3f4a7322008-07-27 06:49:38 +090030
31 // Overrides the path to a special directory or file. This cannot be used to
32 // change the value of DIR_CURRENT, but that should be obvious. Also, if the
33 // path specifies a directory that does not exist, the directory will be
34 // created by this method. This method returns true if successful.
35 //
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +090036 // If the given path is relative, then it will be resolved against
37 // DIR_CURRENT.
initial.commit3f4a7322008-07-27 06:49:38 +090038 //
39 // WARNING: Consumers of PathService::Get may expect paths to be constant
40 // over the lifetime of the app, so this method should be used with caution.
jcampan@chromium.orgcce10e72009-06-26 02:29:09 +090041 static bool Override(int key, const FilePath& path);
initial.commit3f4a7322008-07-27 06:49:38 +090042
pastarmovj@chromium.org8f452092012-05-09 19:50:10 +090043 // This function does the same as PathService::Override but it takes an extra
44 // parameter |create| which guides whether the directory to be overriden must
45 // be created in case it doesn't exist already.
46 static bool OverrideAndCreateIfNeeded(int key,
47 const FilePath& path,
48 bool create);
49
initial.commit3f4a7322008-07-27 06:49:38 +090050 // To extend the set of supported keys, you can register a path provider,
51 // which is just a function mirroring PathService::Get. The ProviderFunc
52 // returns false if it cannot provide a non-empty path for the given key.
53 // Otherwise, true is returned.
54 //
55 // WARNING: This function could be called on any thread from which the
56 // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
57 //
evanm@google.com3e9d5d72008-11-20 01:50:03 +090058 typedef bool (*ProviderFunc)(int, FilePath*);
initial.commit3f4a7322008-07-27 06:49:38 +090059
60 // Call to register a path provider. You must specify the range "[key_start,
61 // key_end)" of supported path keys.
62 static void RegisterProvider(ProviderFunc provider,
63 int key_start,
64 int key_end);
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +090065
erikkay@google.coma3ff2752008-08-13 02:33:52 +090066 private:
pastarmovj@chromium.org5dce41a2012-09-27 04:05:12 +090067 FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride);
68
69 // Removes an override for a special directory or file. Returns true if there
70 // was an override to remove or false if none was present.
71 // NOTE: This function is intended to be used by tests only!
72 static bool RemoveOverride(int key);
initial.commit3f4a7322008-07-27 06:49:38 +090073};
74
jcampan@chromium.org36fe2212009-09-03 02:18:22 +090075#endif // BASE_PATH_SERVICE_H_