michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_PATH_SERVICE_H_ michael@0: #define BASE_PATH_SERVICE_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/base_paths.h" michael@0: #include "base/gtest_prod_util.h" michael@0: #include "build/build_config.h" michael@0: michael@0: namespace base { michael@0: class FilePath; michael@0: class ScopedPathOverride; michael@0: } // namespace michael@0: michael@0: // The path service is a global table mapping keys to file system paths. It is michael@0: // OK to use this service from multiple threads. michael@0: // michael@0: class BASE_EXPORT PathService { michael@0: public: michael@0: // Retrieves a path to a special directory or file and places it into the michael@0: // string pointed to by 'path'. If you ask for a directory it is guaranteed michael@0: // to NOT have a path separator at the end. For example, "c:\windows\temp" michael@0: // Directories are also guaranteed to exist when this function succeeds. michael@0: // michael@0: // Returns true if the directory or file was successfully retrieved. On michael@0: // failure, 'path' will not be changed. michael@0: static bool Get(int key, base::FilePath* path); michael@0: michael@0: // Overrides the path to a special directory or file. This cannot be used to michael@0: // change the value of DIR_CURRENT, but that should be obvious. Also, if the michael@0: // path specifies a directory that does not exist, the directory will be michael@0: // created by this method. This method returns true if successful. michael@0: // michael@0: // If the given path is relative, then it will be resolved against michael@0: // DIR_CURRENT. michael@0: // michael@0: // WARNING: Consumers of PathService::Get may expect paths to be constant michael@0: // over the lifetime of the app, so this method should be used with caution. michael@0: static bool Override(int key, const base::FilePath& path); michael@0: michael@0: // This function does the same as PathService::Override but it takes an extra michael@0: // parameter |create| which guides whether the directory to be overriden must michael@0: // be created in case it doesn't exist already. michael@0: static bool OverrideAndCreateIfNeeded(int key, michael@0: const base::FilePath& path, michael@0: bool create); michael@0: michael@0: // To extend the set of supported keys, you can register a path provider, michael@0: // which is just a function mirroring PathService::Get. The ProviderFunc michael@0: // returns false if it cannot provide a non-empty path for the given key. michael@0: // Otherwise, true is returned. michael@0: // michael@0: // WARNING: This function could be called on any thread from which the michael@0: // PathService is used, so a the ProviderFunc MUST BE THREADSAFE. michael@0: // michael@0: typedef bool (*ProviderFunc)(int, base::FilePath*); michael@0: michael@0: // Call to register a path provider. You must specify the range "[key_start, michael@0: // key_end)" of supported path keys. michael@0: static void RegisterProvider(ProviderFunc provider, michael@0: int key_start, michael@0: int key_end); michael@0: michael@0: // Disable internal cache. michael@0: static void DisableCache(); michael@0: michael@0: private: michael@0: friend class base::ScopedPathOverride; michael@0: FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride); michael@0: michael@0: // Removes an override for a special directory or file. Returns true if there michael@0: // was an override to remove or false if none was present. michael@0: // NOTE: This function is intended to be used by tests only! michael@0: static bool RemoveOverride(int key); michael@0: }; michael@0: michael@0: #endif // BASE_PATH_SERVICE_H_