blob: 9759754377f9a060b04e6503e12884d395b7217a [file] [log] [blame]
[email protected]cb571e752012-05-09 10:50:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]057a85532009-09-02 17:18:225#ifndef BASE_PATH_SERVICE_H_
6#define BASE_PATH_SERVICE_H_
initial.commitd7cae122008-07-26 21:49:387
initial.commitd7cae122008-07-26 21:49:388#include <string>
9
[email protected]0bea7252011-08-05 15:34:0010#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3811#include "base/base_paths.h"
[email protected]d6b3af92012-09-26 19:05:1212#include "base/gtest_prod_util.h"
[email protected]90509cb2011-03-25 18:46:3813#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3814
[email protected]d3e6d252012-09-27 00:45:3615namespace base {
[email protected]a3ef4832013-02-02 05:12:3316class FilePath;
[email protected]d3e6d252012-09-27 00:45:3617class ScopedPathOverride;
18} // namespace
19
initial.commitd7cae122008-07-26 21:49:3820// The path service is a global table mapping keys to file system paths. It is
21// OK to use this service from multiple threads.
22//
[email protected]0bea7252011-08-05 15:34:0023class BASE_EXPORT PathService {
initial.commitd7cae122008-07-26 21:49:3824 public:
25 // Retrieves a path to a special directory or file and places it into the
26 // string pointed to by 'path'. If you ask for a directory it is guaranteed
27 // to NOT have a path separator at the end. For example, "c:\windows\temp"
28 // Directories are also guaranteed to exist when this function succeeds.
29 //
30 // Returns true if the directory or file was successfully retrieved. On
31 // failure, 'path' will not be changed.
[email protected]a3ef4832013-02-02 05:12:3332 static bool Get(int key, base::FilePath* path);
initial.commitd7cae122008-07-26 21:49:3833
34 // Overrides the path to a special directory or file. This cannot be used to
35 // change the value of DIR_CURRENT, but that should be obvious. Also, if the
36 // path specifies a directory that does not exist, the directory will be
37 // created by this method. This method returns true if successful.
38 //
[email protected]52a261f2009-03-03 15:01:1239 // If the given path is relative, then it will be resolved against
40 // DIR_CURRENT.
initial.commitd7cae122008-07-26 21:49:3841 //
42 // WARNING: Consumers of PathService::Get may expect paths to be constant
43 // over the lifetime of the app, so this method should be used with caution.
[email protected]826ab452014-02-26 13:20:3644 //
45 // Unit tests generally should use ScopedPathOverride instead. Overrides from
46 // one test should not carry over to another.
[email protected]a3ef4832013-02-02 05:12:3347 static bool Override(int key, const base::FilePath& path);
initial.commitd7cae122008-07-26 21:49:3848
[email protected]cb571e752012-05-09 10:50:1049 // This function does the same as PathService::Override but it takes an extra
50 // parameter |create| which guides whether the directory to be overriden must
51 // be created in case it doesn't exist already.
52 static bool OverrideAndCreateIfNeeded(int key,
[email protected]a3ef4832013-02-02 05:12:3353 const base::FilePath& path,
[email protected]cb571e752012-05-09 10:50:1054 bool create);
55
initial.commitd7cae122008-07-26 21:49:3856 // To extend the set of supported keys, you can register a path provider,
57 // which is just a function mirroring PathService::Get. The ProviderFunc
58 // returns false if it cannot provide a non-empty path for the given key.
59 // Otherwise, true is returned.
60 //
61 // WARNING: This function could be called on any thread from which the
62 // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
63 //
[email protected]a3ef4832013-02-02 05:12:3364 typedef bool (*ProviderFunc)(int, base::FilePath*);
initial.commitd7cae122008-07-26 21:49:3865
66 // Call to register a path provider. You must specify the range "[key_start,
67 // key_end)" of supported path keys.
68 static void RegisterProvider(ProviderFunc provider,
69 int key_start,
70 int key_end);
[email protected]d6b3af92012-09-26 19:05:1271
[email protected]c5a726b32013-01-29 00:56:5672 // Disable internal cache.
73 static void DisableCache();
74
[email protected]1265917f2008-08-12 17:33:5275 private:
[email protected]d3e6d252012-09-27 00:45:3676 friend class base::ScopedPathOverride;
[email protected]d6b3af92012-09-26 19:05:1277 FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride);
78
79 // Removes an override for a special directory or file. Returns true if there
80 // was an override to remove or false if none was present.
81 // NOTE: This function is intended to be used by tests only!
82 static bool RemoveOverride(int key);
initial.commitd7cae122008-07-26 21:49:3883};
84
[email protected]057a85532009-09-02 17:18:2285#endif // BASE_PATH_SERVICE_H_