blob: 94b0db34246bfb657c18180c4b3d8e492d9deabf [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]640517f2008-10-30 23:54:0415class FilePath;
16
[email protected]d3e6d252012-09-27 00:45:3617namespace base {
18class ScopedPathOverride;
19} // namespace
20
initial.commitd7cae122008-07-26 21:49:3821// The path service is a global table mapping keys to file system paths. It is
22// OK to use this service from multiple threads.
23//
[email protected]0bea7252011-08-05 15:34:0024class BASE_EXPORT PathService {
initial.commitd7cae122008-07-26 21:49:3825 public:
26 // Retrieves a path to a special directory or file and places it into the
27 // string pointed to by 'path'. If you ask for a directory it is guaranteed
28 // to NOT have a path separator at the end. For example, "c:\windows\temp"
29 // Directories are also guaranteed to exist when this function succeeds.
30 //
31 // Returns true if the directory or file was successfully retrieved. On
32 // failure, 'path' will not be changed.
[email protected]640517f2008-10-30 23:54:0433 static bool Get(int key, FilePath* path);
initial.commitd7cae122008-07-26 21:49:3834
35 // Overrides the path to a special directory or file. This cannot be used to
36 // change the value of DIR_CURRENT, but that should be obvious. Also, if the
37 // path specifies a directory that does not exist, the directory will be
38 // created by this method. This method returns true if successful.
39 //
[email protected]52a261f2009-03-03 15:01:1240 // If the given path is relative, then it will be resolved against
41 // DIR_CURRENT.
initial.commitd7cae122008-07-26 21:49:3842 //
43 // WARNING: Consumers of PathService::Get may expect paths to be constant
44 // over the lifetime of the app, so this method should be used with caution.
[email protected]eca6a4f2009-06-25 17:29:0945 static bool Override(int key, const FilePath& path);
initial.commitd7cae122008-07-26 21:49:3846
[email protected]cb571e752012-05-09 10:50:1047 // This function does the same as PathService::Override but it takes an extra
48 // parameter |create| which guides whether the directory to be overriden must
49 // be created in case it doesn't exist already.
50 static bool OverrideAndCreateIfNeeded(int key,
51 const FilePath& path,
52 bool create);
53
initial.commitd7cae122008-07-26 21:49:3854 // To extend the set of supported keys, you can register a path provider,
55 // which is just a function mirroring PathService::Get. The ProviderFunc
56 // returns false if it cannot provide a non-empty path for the given key.
57 // Otherwise, true is returned.
58 //
59 // WARNING: This function could be called on any thread from which the
60 // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
61 //
[email protected]4792a262008-11-19 16:50:0362 typedef bool (*ProviderFunc)(int, FilePath*);
initial.commitd7cae122008-07-26 21:49:3863
64 // Call to register a path provider. You must specify the range "[key_start,
65 // key_end)" of supported path keys.
66 static void RegisterProvider(ProviderFunc provider,
67 int key_start,
68 int key_end);
[email protected]d6b3af92012-09-26 19:05:1269
[email protected]1265917f2008-08-12 17:33:5270 private:
[email protected]d3e6d252012-09-27 00:45:3671 friend class base::ScopedPathOverride;
[email protected]d6b3af92012-09-26 19:05:1272 FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride);
73
74 // Removes an override for a special directory or file. Returns true if there
75 // was an override to remove or false if none was present.
76 // NOTE: This function is intended to be used by tests only!
77 static bool RemoveOverride(int key);
initial.commitd7cae122008-07-26 21:49:3878};
79
[email protected]057a85532009-09-02 17:18:2280#endif // BASE_PATH_SERVICE_H_