Skip to content

Commit

Permalink
Move more code to using FilePath.
Browse files Browse the repository at this point in the history
Review URL: http://codereview.chromium.org/11252


git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5679 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
evanm@google.com committed Nov 19, 2008
1 parent aa075e7 commit 4792a26
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 38 deletions.
4 changes: 2 additions & 2 deletions base/base_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace base {

bool PathProvider(int key, std::wstring* result) {
bool PathProvider(int key, FilePath* result) {
// NOTE: DIR_CURRENT is a special cased in PathService::Get

FilePath cur;
Expand All @@ -31,7 +31,7 @@ bool PathProvider(int key, std::wstring* result) {
return false;
}

*result = cur.ToWStringHack();
*result = cur;
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions base/base_paths_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace base {

bool PathProviderLinux(int key, std::wstring* result) {
bool PathProviderLinux(int key, FilePath* result) {
FilePath path;
switch (key) {
case base::FILE_EXE:
Expand All @@ -27,7 +27,7 @@ bool PathProviderLinux(int key, std::wstring* result) {
return false;
}
bin_dir[bin_dir_size] = 0;
*result = base::SysNativeMBToWide(bin_dir);
*result = FilePath(bin_dir);
return true;
}
case base::DIR_SOURCE_ROOT:
Expand All @@ -37,7 +37,7 @@ bool PathProviderLinux(int key, std::wstring* result) {
return false;
path = path.Append(FilePath::kParentDirectory)
.Append(FilePath::kParentDirectory);
*result = path.ToWStringHack();
*result = path;
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions base/base_paths_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace base {

bool PathProviderMac(int key, std::wstring* result) {
bool PathProviderMac(int key, FilePath* result) {
std::wstring cur;
switch (key) {
case base::FILE_EXE:
Expand Down Expand Up @@ -49,7 +49,7 @@ bool PathProviderMac(int key, std::wstring* result) {
return false;
}

result->swap(cur);
*result = FilePath::FromWStringHack(cur);
return true;
}

Expand Down
42 changes: 23 additions & 19 deletions base/base_paths_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <windows.h>
#include <shlobj.h>

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/win_util.h"
Expand All @@ -16,7 +17,7 @@ extern "C" IMAGE_DOS_HEADER __ImageBase;

namespace base {

bool PathProviderWin(int key, std::wstring* result) {
bool PathProviderWin(int key, FilePath* result) {

// We need to go compute the value. It would be nice to support paths with
// names longer than MAX_PATH, but the system functions don't seem to be
Expand All @@ -26,57 +27,58 @@ bool PathProviderWin(int key, std::wstring* result) {
wchar_t system_buffer[MAX_PATH];
system_buffer[0] = 0;

std::wstring cur;
FilePath cur;
std::wstring wstring_path;
switch (key) {
case base::FILE_EXE:
GetModuleFileName(NULL, system_buffer, MAX_PATH);
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::FILE_MODULE: {
// the resource containing module is assumed to be the one that
// this code lives in, whether that's a dll or exe
HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
GetModuleFileName(this_module, system_buffer, MAX_PATH);
cur = system_buffer;
cur = FilePath(system_buffer);
break;
}
case base::DIR_WINDOWS:
GetWindowsDirectory(system_buffer, MAX_PATH);
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_SYSTEM:
GetSystemDirectory(system_buffer, MAX_PATH);
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_PROGRAM_FILES:
if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_IE_INTERNET_CACHE:
if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_COMMON_START_MENU:
if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_START_MENU:
if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_APP_DATA:
if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
system_buffer)))
return false;
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_LOCAL_APP_DATA_LOW:
if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) {
Expand All @@ -86,28 +88,30 @@ bool PathProviderWin(int key, std::wstring* result) {
if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
system_buffer)))
return false;
cur = system_buffer;
file_util::UpOneDirectory(&cur);
file_util::AppendToPath(&cur, L"LocalLow");
wstring_path = system_buffer;
file_util::UpOneDirectory(&wstring_path);
file_util::AppendToPath(&wstring_path, L"LocalLow");
cur = FilePath(wstring_path);
break;
case base::DIR_LOCAL_APP_DATA:
if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = system_buffer;
cur = FilePath(system_buffer);
break;
case base::DIR_SOURCE_ROOT:
// On Windows, unit tests execute two levels deep from the source root.
// For example: chrome/{Debug|Release}/ui_tests.exe
PathService::Get(base::DIR_EXE, &cur);
file_util::UpOneDirectory(&cur);
file_util::UpOneDirectory(&cur);
PathService::Get(base::DIR_EXE, &wstring_path);
file_util::UpOneDirectory(&wstring_path);
file_util::UpOneDirectory(&wstring_path);
cur = FilePath(wstring_path);
break;
default:
return false;
}

result->swap(cur);
*result = cur;
return true;
}

Expand Down
17 changes: 8 additions & 9 deletions base/path_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
#include "base/string_util.h"

namespace base {
bool PathProvider(int key, std::wstring* result);
bool PathProvider(int key, FilePath* result);
#if defined(OS_WIN)
bool PathProviderWin(int key, std::wstring* result);
bool PathProviderWin(int key, FilePath* result);
#elif defined(OS_MACOSX)
bool PathProviderMac(int key, std::wstring* result);
bool PathProviderMac(int key, FilePath* result);
#elif defined(OS_LINUX)
bool PathProviderLinux(int key, std::wstring* result);
bool PathProviderLinux(int key, FilePath* result);
#endif
}

Expand Down Expand Up @@ -166,23 +166,22 @@ bool PathService::Get(int key, FilePath* result) {
if (GetFromCache(key, result))
return true;

std::wstring path_string;
FilePath path;

// search providers for the requested path
// NOTE: it should be safe to iterate here without the lock
// since RegisterProvider always prepends.
Provider* provider = path_data->providers;
while (provider) {
if (provider->func(key, &path_string))
if (provider->func(key, &path))
break;
DCHECK(path_string.empty()) << "provider should not have modified path";
DCHECK(path.value().empty()) << "provider should not have modified path";
provider = provider->next;
}

if (path_string.empty())
if (path.value().empty())
return false;

FilePath path = FilePath::FromWStringHack(path_string);
AddToCache(key, path);

*result = path;
Expand Down
2 changes: 1 addition & 1 deletion base/path_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class PathService {
// WARNING: This function could be called on any thread from which the
// PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
//
typedef bool (*ProviderFunc)(int, std::wstring*);
typedef bool (*ProviderFunc)(int, FilePath*);

// Call to register a path provider. You must specify the range "[key_start,
// key_end)" of supported path keys.
Expand Down
5 changes: 3 additions & 2 deletions chrome/common/chrome_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "chrome/common/chrome_paths.h"

#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
Expand Down Expand Up @@ -52,7 +53,7 @@ bool GetGearsPluginPathFromCommandLine(std::wstring *path) {
#endif
}

bool PathProvider(int key, std::wstring* result) {
bool PathProvider(int key, FilePath* result) {
// Some keys are just aliases...
switch (key) {
case chrome::DIR_APP:
Expand Down Expand Up @@ -261,7 +262,7 @@ bool PathProvider(int key, std::wstring* result) {
if (!exists && !file_util::PathExists(cur) && !file_util::CreateDirectory(cur))
return false;

result->swap(cur);
*result = FilePath::FromWStringHack(cur);
return true;
}

Expand Down

0 comments on commit 4792a26

Please sign in to comment.