initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifndef BASE_PATH_SERVICE_H__ |
| 31 | #define BASE_PATH_SERVICE_H__ |
| 32 | |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 33 | #include "build/build_config.h" |
| 34 | #ifdef OS_WIN |
| 35 | // TODO(erikkay): this should be removable, but because SetCurrentDirectory |
| 36 | // is the name of a Windows function, it gets macro-ized to SetCurrentDirectoryW |
| 37 | // by windows.h, which leads to a different name in the header vs. the impl. |
| 38 | // Even if we could fix that, it would still hose all callers of the function. |
| 39 | // The right thing is likely to rename. |
| 40 | #include <windows.h> |
| 41 | #endif |
| 42 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | #include <string> |
| 44 | |
| 45 | #include "base/base_paths.h" |
| 46 | |
| 47 | // The path service is a global table mapping keys to file system paths. It is |
| 48 | // OK to use this service from multiple threads. |
| 49 | // |
| 50 | class PathService { |
| 51 | public: |
| 52 | // Retrieves a path to a special directory or file and places it into the |
| 53 | // string pointed to by 'path'. If you ask for a directory it is guaranteed |
| 54 | // to NOT have a path separator at the end. For example, "c:\windows\temp" |
| 55 | // Directories are also guaranteed to exist when this function succeeds. |
| 56 | // |
| 57 | // Returns true if the directory or file was successfully retrieved. On |
| 58 | // failure, 'path' will not be changed. |
| 59 | static bool Get(int key, std::wstring* path); |
| 60 | |
| 61 | // Overrides the path to a special directory or file. This cannot be used to |
| 62 | // change the value of DIR_CURRENT, but that should be obvious. Also, if the |
| 63 | // path specifies a directory that does not exist, the directory will be |
| 64 | // created by this method. This method returns true if successful. |
| 65 | // |
| 66 | // If the given path is relative, then it will be resolved against DIR_CURRENT. |
| 67 | // |
| 68 | // WARNING: Consumers of PathService::Get may expect paths to be constant |
| 69 | // over the lifetime of the app, so this method should be used with caution. |
| 70 | static bool Override(int key, const std::wstring& path); |
| 71 | |
| 72 | // Return whether a path was overridden. |
| 73 | static bool IsOverridden(int key); |
| 74 | |
| 75 | // Sets the current directory. |
| 76 | static bool SetCurrentDirectory(const std::wstring& current_directory); |
| 77 | |
| 78 | // To extend the set of supported keys, you can register a path provider, |
| 79 | // which is just a function mirroring PathService::Get. The ProviderFunc |
| 80 | // returns false if it cannot provide a non-empty path for the given key. |
| 81 | // Otherwise, true is returned. |
| 82 | // |
| 83 | // WARNING: This function could be called on any thread from which the |
| 84 | // PathService is used, so a the ProviderFunc MUST BE THREADSAFE. |
| 85 | // |
| 86 | typedef bool (*ProviderFunc)(int, std::wstring*); |
| 87 | |
| 88 | // Call to register a path provider. You must specify the range "[key_start, |
| 89 | // key_end)" of supported path keys. |
| 90 | static void RegisterProvider(ProviderFunc provider, |
| 91 | int key_start, |
| 92 | int key_end); |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame^] | 93 | private: |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 94 | static bool GetFromCache(int key, std::wstring* path); |
| 95 | static void AddToCache(int key, const std::wstring& path); |
| 96 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | #endif // BASE_PATH_SERVICE_H__ |