Home | History | Annotate | Download | only in base
      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 
     10 #include <string>
     11 
     12 #include "base/base_paths.h"
     13 
     14 class FilePath;
     15 
     16 // The path service is a global table mapping keys to file system paths.  It is
     17 // OK to use this service from multiple threads.
     18 //
     19 class PathService {
     20  public:
     21   // Retrieves a path to a special directory or file and places it into the
     22   // string pointed to by 'path'. If you ask for a directory it is guaranteed
     23   // to NOT have a path separator at the end. For example, "c:\windows\temp"
     24   // Directories are also guaranteed to exist when this function succeeds.
     25   //
     26   // Returns true if the directory or file was successfully retrieved. On
     27   // failure, 'path' will not be changed.
     28   static bool Get(int key, FilePath* path);
     29 #if defined(OS_WIN)
     30   // This version, producing a wstring, is deprecated and only kept around
     31   // until we can fix all callers.
     32   static bool Get(int key, std::wstring* path);
     33 #endif
     34 
     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   //
     40   // If the given path is relative, then it will be resolved against
     41   // DIR_CURRENT.
     42   //
     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.
     45   static bool Override(int key, const FilePath& path);
     46 
     47   // Return whether a path was overridden.
     48   static bool IsOverridden(int key);
     49 
     50   // To extend the set of supported keys, you can register a path provider,
     51   // which is just a function mirroring PathService::Get.  The ProviderFunc
     52   // returns false if it cannot provide a non-empty path for the given key.
     53   // Otherwise, true is returned.
     54   //
     55   // WARNING: This function could be called on any thread from which the
     56   // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
     57   //
     58   typedef bool (*ProviderFunc)(int, FilePath*);
     59 
     60   // Call to register a path provider.  You must specify the range "[key_start,
     61   // key_end)" of supported path keys.
     62   static void RegisterProvider(ProviderFunc provider,
     63                                int key_start,
     64                                int key_end);
     65  private:
     66   static bool GetFromCache(int key, FilePath* path);
     67   static void AddToCache(int key, const FilePath& path);
     68 };
     69 
     70 #endif  // BASE_PATH_SERVICE_H_
     71