diff options
author | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-09 10:50:10 +0000 |
---|---|---|
committer | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-09 10:50:10 +0000 |
commit | cb571e752bc8e06610e1d2af8f1d522fc7f22e88 (patch) | |
tree | e1b63897cdbcbf3bca122fd9af48477fd7db9ec0 /base | |
parent | b4a7d490cb89cad10a89c50af6019061c67c8907 (diff) | |
download | chromium_src-cb571e752bc8e06610e1d2af8f1d522fc7f22e88.zip chromium_src-cb571e752bc8e06610e1d2af8f1d522fc7f22e88.tar.gz chromium_src-cb571e752bc8e06610e1d2af8f1d522fc7f22e88.tar.bz2 |
Add new method OverrideAndCreateIfNeeded to PathService.
This is prerequisite for adding support for running chrome on network
shared location
BUG=120388
TEST=base_unittests:PathServiceTest.Override
Review URL: https://chromiumcodereview.appspot.com/10388027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@136017 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/path_service.cc | 25 | ||||
-rw-r--r-- | base/path_service.h | 9 | ||||
-rw-r--r-- | base/path_service_unittest.cc | 27 |
3 files changed, 52 insertions, 9 deletions
diff --git a/base/path_service.cc b/base/path_service.cc index 46f394c..a3b882c 100644 --- a/base/path_service.cc +++ b/base/path_service.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -222,18 +222,29 @@ bool PathService::Get(int key, FilePath* result) { } bool PathService::Override(int key, const FilePath& path) { + // Just call the full function with true for the value of |create|. + return OverrideAndCreateIfNeeded(key, path, true); +} + +bool PathService::OverrideAndCreateIfNeeded(int key, + const FilePath& path, + bool create) { PathData* path_data = GetPathData(); DCHECK(path_data); DCHECK_GT(key, base::DIR_CURRENT) << "invalid path key"; FilePath file_path = path; - // Make sure the directory exists. We need to do this before we translate - // this to the absolute path because on POSIX, AbsolutePath fails if called - // on a non-existent path. - if (!file_util::PathExists(file_path) && - !file_util::CreateDirectory(file_path)) - return false; + // For some locations this will fail if called from inside the sandbox there- + // fore we protect this call with a flag. + if (create) { + // Make sure the directory exists. We need to do this before we translate + // this to the absolute path because on POSIX, AbsolutePath fails if called + // on a non-existent path. + if (!file_util::PathExists(file_path) && + !file_util::CreateDirectory(file_path)) + return false; + } // We need to have an absolute path, as extensions and plugins don't like // relative paths, and will gladly crash the browser in CHECK()s if they get a diff --git a/base/path_service.h b/base/path_service.h index 4b29738..03e4b44 100644 --- a/base/path_service.h +++ b/base/path_service.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -40,6 +40,13 @@ class BASE_EXPORT PathService { // over the lifetime of the app, so this method should be used with caution. static bool Override(int key, const FilePath& path); + // This function does the same as PathService::Override but it takes an extra + // parameter |create| which guides whether the directory to be overriden must + // be created in case it doesn't exist already. + static bool OverrideAndCreateIfNeeded(int key, + const FilePath& path, + bool create); + // To extend the set of supported keys, you can register a path provider, // which is just a function mirroring PathService::Get. The ProviderFunc // returns false if it cannot provide a non-empty path for the given key. diff --git a/base/path_service_unittest.cc b/base/path_service_unittest.cc index ec9cf28..294f8d8 100644 --- a/base/path_service_unittest.cc +++ b/base/path_service_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,6 +7,7 @@ #include "base/basictypes.h" #include "base/file_util.h" #include "base/file_path.h" +#include "base/scoped_temp_dir.h" #if defined(OS_WIN) #include "base/win/windows_version.h" #endif @@ -74,3 +75,27 @@ TEST_F(PathServiceTest, Get) { } #endif } + +// test that all versions of the Override function of PathService do what they +// are supposed to do. +TEST_F(PathServiceTest, Override) { + int my_special_key = 666; + ScopedTempDir temp_dir; + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); + FilePath fake_cache_dir(temp_dir.path().AppendASCII("cache")); + // PathService::Override should always create the path provided if it doesn't + // exist. + EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir)); + EXPECT_TRUE(file_util::PathExists(fake_cache_dir)); + + FilePath fake_cache_dir2(temp_dir.path().AppendASCII("cache2")); + // PathService::OverrideAndCreateIfNeeded should obey the |create| parameter. + PathService::OverrideAndCreateIfNeeded(my_special_key, + fake_cache_dir2, + false); + EXPECT_FALSE(file_util::PathExists(fake_cache_dir2)); + EXPECT_TRUE(PathService::OverrideAndCreateIfNeeded(my_special_key, + fake_cache_dir2, + true)); + EXPECT_TRUE(file_util::PathExists(fake_cache_dir2)); +} |