12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef BASE_PATH_SERVICE_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define BASE_PATH_SERVICE_H_
77dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include <string>
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/base_export.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/base_paths.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/gtest_prod_util.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "build/build_config.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ScopedPathOverride;
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// The path service is a global table mapping keys to file system paths.  It is
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// OK to use this service from multiple threads.
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class BASE_EXPORT PathService {
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Retrieves a path to a special directory or file and places it into the
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // string pointed to by 'path'. If you ask for a directory it is guaranteed
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // to NOT have a path separator at the end. For example, "c:\windows\temp"
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Directories are also guaranteed to exist when this function succeeds.
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if the directory or file was successfully retrieved. On
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // failure, 'path' will not be changed.
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static bool Get(int key, base::FilePath* path);
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Overrides the path to a special directory or file.  This cannot be used to
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // change the value of DIR_CURRENT, but that should be obvious.  Also, if the
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // path specifies a directory that does not exist, the directory will be
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // created by this method.  This method returns true if successful.
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If the given path is relative, then it will be resolved against
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // DIR_CURRENT.
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // WARNING: Consumers of PathService::Get may expect paths to be constant
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // over the lifetime of the app, so this method should be used with caution.
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Unit tests generally should use ScopedPathOverride instead. Overrides from
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // one test should not carry over to another.
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static bool Override(int key, const base::FilePath& path);
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This function does the same as PathService::Override but it takes extra
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // parameters:
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - |is_absolute| indicates that |path| has already been expanded into an
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // absolute path, otherwise MakeAbsoluteFilePath() will be used. This is
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // useful to override paths that may not exist yet, since MakeAbsoluteFilePath
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // fails for those. Note that MakeAbsoluteFilePath also expands symbolic
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // links, even if path.IsAbsolute() is already true.
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // - |create| guides whether the directory to be overriden must
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // be created in case it doesn't exist already.
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool OverrideAndCreateIfNeeded(int key,
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        const base::FilePath& path,
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        bool is_absolute,
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        bool create);
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // To extend the set of supported keys, you can register a path provider,
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // which is just a function mirroring PathService::Get.  The ProviderFunc
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // returns false if it cannot provide a non-empty path for the given key.
66ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Otherwise, true is returned.
67ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  //
68ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // WARNING: This function could be called on any thread from which the
69ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
70ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  //
71ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  typedef bool (*ProviderFunc)(int, base::FilePath*);
72ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
73ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Call to register a path provider.  You must specify the range "[key_start,
74ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // key_end)" of supported path keys.
75ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void RegisterProvider(ProviderFunc provider,
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               int key_start,
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               int key_end);
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Disable internal cache.
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void DisableCache();
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class base::ScopedPathOverride;
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride);
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removes an override for a special directory or file. Returns true if there
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // was an override to remove or false if none was present.
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // NOTE: This function is intended to be used by tests only!
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool RemoveOverride(int key);
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // BASE_PATH_SERVICE_H_
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)