Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/base_api.h"
     12 #include "base/base_paths.h"
     13 #include "build/build_config.h"
     14 
     15 class FilePath;
     16 
     17 // The path service is a global table mapping keys to file system paths.  It is
     18 // OK to use this service from multiple threads.
     19 //
     20 class BASE_API PathService {
     21  public:
     22   // Retrieves a path to a special directory or file and places it into the
     23   // string pointed to by 'path'. If you ask for a directory it is guaranteed
     24   // to NOT have a path separator at the end. For example, "c:\windows\temp"
     25   // Directories are also guaranteed to exist when this function succeeds.
     26   //
     27   // Returns true if the directory or file was successfully retrieved. On
     28   // failure, 'path' will not be changed.
     29   static bool Get(int key, FilePath* path);
     30 
     31   // Overrides the path to a special directory or file.  This cannot be used to
     32   // change the value of DIR_CURRENT, but that should be obvious.  Also, if the
     33   // path specifies a directory that does not exist, the directory will be
     34   // created by this method.  This method returns true if successful.
     35   //
     36   // If the given path is relative, then it will be resolved against
     37   // DIR_CURRENT.
     38   //
     39   // WARNING: Consumers of PathService::Get may expect paths to be constant
     40   // over the lifetime of the app, so this method should be used with caution.
     41   static bool Override(int key, const FilePath& path);
     42 
     43   // To extend the set of supported keys, you can register a path provider,
     44   // which is just a function mirroring PathService::Get.  The ProviderFunc
     45   // returns false if it cannot provide a non-empty path for the given key.
     46   // Otherwise, true is returned.
     47   //
     48   // WARNING: This function could be called on any thread from which the
     49   // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
     50   //
     51   typedef bool (*ProviderFunc)(int, FilePath*);
     52 
     53   // Call to register a path provider.  You must specify the range "[key_start,
     54   // key_end)" of supported path keys.
     55   static void RegisterProvider(ProviderFunc provider,
     56                                int key_start,
     57                                int key_end);
     58  private:
     59   static bool GetFromCache(int key, FilePath* path);
     60   static bool GetFromOverrides(int key, FilePath* path);
     61   static void AddToCache(int key, const FilePath& path);
     62 };
     63 
     64 #endif  // BASE_PATH_SERVICE_H_
     65