michael@0: // Copyright (c) 2006-2008 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 "build/build_config.h" michael@0: #ifdef OS_WIN michael@0: // TODO(erikkay): this should be removable, but because SetCurrentDirectory michael@0: // is the name of a Windows function, it gets macro-ized to SetCurrentDirectoryW michael@0: // by windows.h, which leads to a different name in the header vs. the impl. michael@0: // Even if we could fix that, it would still hose all callers of the function. michael@0: // The right thing is likely to rename. michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #include "base/base_paths.h" michael@0: michael@0: class FilePath; 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 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, FilePath* path); michael@0: // This version, producing a wstring, is deprecated and only kept around michael@0: // until we can fix all callers. michael@0: static bool Get(int key, std::wstring* 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 std::wstring& path); michael@0: michael@0: // Return whether a path was overridden. michael@0: static bool IsOverridden(int key); michael@0: michael@0: // Sets the current directory. michael@0: static bool SetCurrentDirectory(const std::wstring& current_directory); 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 the ProviderFunc MUST BE THREADSAFE. michael@0: // michael@0: typedef bool (*ProviderFunc)(int, 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: private: michael@0: static bool GetFromCache(int key, FilePath* path); michael@0: static void AddToCache(int key, const FilePath& path); michael@0: michael@0: }; michael@0: michael@0: #endif // BASE_PATH_SERVICE_H__