| |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| |
2 // Use of this source code is governed by a BSD-style license that can be |
| |
3 // found in the LICENSE file. |
| |
4 |
| |
5 #ifndef BASE_PATH_SERVICE_H__ |
| |
6 #define BASE_PATH_SERVICE_H__ |
| |
7 |
| |
8 #include "build/build_config.h" |
| |
9 #ifdef OS_WIN |
| |
10 // TODO(erikkay): this should be removable, but because SetCurrentDirectory |
| |
11 // is the name of a Windows function, it gets macro-ized to SetCurrentDirectoryW |
| |
12 // by windows.h, which leads to a different name in the header vs. the impl. |
| |
13 // Even if we could fix that, it would still hose all callers of the function. |
| |
14 // The right thing is likely to rename. |
| |
15 #include <windows.h> |
| |
16 #endif |
| |
17 |
| |
18 #include <string> |
| |
19 |
| |
20 #include "base/base_paths.h" |
| |
21 |
| |
22 class FilePath; |
| |
23 |
| |
24 // The path service is a global table mapping keys to file system paths. It is |
| |
25 // OK to use this service from multiple threads. |
| |
26 // |
| |
27 class PathService { |
| |
28 public: |
| |
29 // Retrieves a path to a special directory or file and places it into the |
| |
30 // string pointed to by 'path'. If you ask for a directory it is guaranteed |
| |
31 // to NOT have a path separator at the end. For example, "c:\windows\temp" |
| |
32 // Directories are also guaranteed to exist when this function succeeds. |
| |
33 // |
| |
34 // Returns true if the directory or file was successfully retrieved. On |
| |
35 // failure, 'path' will not be changed. |
| |
36 static bool Get(int key, FilePath* path); |
| |
37 // This version, producing a wstring, is deprecated and only kept around |
| |
38 // until we can fix all callers. |
| |
39 static bool Get(int key, std::wstring* path); |
| |
40 |
| |
41 // Overrides the path to a special directory or file. This cannot be used to |
| |
42 // change the value of DIR_CURRENT, but that should be obvious. Also, if the |
| |
43 // path specifies a directory that does not exist, the directory will be |
| |
44 // created by this method. This method returns true if successful. |
| |
45 // |
| |
46 // If the given path is relative, then it will be resolved against |
| |
47 // DIR_CURRENT. |
| |
48 // |
| |
49 // WARNING: Consumers of PathService::Get may expect paths to be constant |
| |
50 // over the lifetime of the app, so this method should be used with caution. |
| |
51 static bool Override(int key, const std::wstring& path); |
| |
52 |
| |
53 // Return whether a path was overridden. |
| |
54 static bool IsOverridden(int key); |
| |
55 |
| |
56 // Sets the current directory. |
| |
57 static bool SetCurrentDirectory(const std::wstring& current_directory); |
| |
58 |
| |
59 // To extend the set of supported keys, you can register a path provider, |
| |
60 // which is just a function mirroring PathService::Get. The ProviderFunc |
| |
61 // returns false if it cannot provide a non-empty path for the given key. |
| |
62 // Otherwise, true is returned. |
| |
63 // |
| |
64 // WARNING: This function could be called on any thread from which the |
| |
65 // PathService is used, so the ProviderFunc MUST BE THREADSAFE. |
| |
66 // |
| |
67 typedef bool (*ProviderFunc)(int, FilePath*); |
| |
68 |
| |
69 // Call to register a path provider. You must specify the range "[key_start, |
| |
70 // key_end)" of supported path keys. |
| |
71 static void RegisterProvider(ProviderFunc provider, |
| |
72 int key_start, |
| |
73 int key_end); |
| |
74 private: |
| |
75 static bool GetFromCache(int key, FilePath* path); |
| |
76 static void AddToCache(int key, const FilePath& path); |
| |
77 |
| |
78 }; |
| |
79 |
| |
80 #endif // BASE_PATH_SERVICE_H__ |