michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/base_paths_win.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/file_path.h" michael@0: #include "base/file_util.h" michael@0: #include "base/path_service.h" michael@0: #include "base/win_util.h" michael@0: michael@0: // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx michael@0: extern "C" IMAGE_DOS_HEADER __ImageBase; michael@0: michael@0: namespace base { michael@0: michael@0: bool PathProviderWin(int key, FilePath* result) { michael@0: michael@0: // We need to go compute the value. It would be nice to support paths with michael@0: // names longer than MAX_PATH, but the system functions don't seem to be michael@0: // designed for it either, with the exception of GetTempPath (but other michael@0: // things will surely break if the temp path is too long, so we don't bother michael@0: // handling it. michael@0: wchar_t system_buffer[MAX_PATH]; michael@0: system_buffer[0] = 0; michael@0: michael@0: FilePath cur; michael@0: std::wstring wstring_path; michael@0: switch (key) { michael@0: case base::FILE_EXE: michael@0: GetModuleFileName(NULL, system_buffer, MAX_PATH); michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::FILE_MODULE: { michael@0: // the resource containing module is assumed to be the one that michael@0: // this code lives in, whether that's a dll or exe michael@0: HMODULE this_module = reinterpret_cast(&__ImageBase); michael@0: GetModuleFileName(this_module, system_buffer, MAX_PATH); michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: } michael@0: case base::DIR_WINDOWS: michael@0: GetWindowsDirectory(system_buffer, MAX_PATH); michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_SYSTEM: michael@0: GetSystemDirectory(system_buffer, MAX_PATH); michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_PROGRAM_FILES: michael@0: if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, michael@0: SHGFP_TYPE_CURRENT, system_buffer))) michael@0: return false; michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_IE_INTERNET_CACHE: michael@0: if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL, michael@0: SHGFP_TYPE_CURRENT, system_buffer))) michael@0: return false; michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_COMMON_START_MENU: michael@0: if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL, michael@0: SHGFP_TYPE_CURRENT, system_buffer))) michael@0: return false; michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_START_MENU: michael@0: if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, michael@0: SHGFP_TYPE_CURRENT, system_buffer))) michael@0: return false; michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_APP_DATA: michael@0: if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, michael@0: system_buffer))) michael@0: return false; michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_LOCAL_APP_DATA_LOW: michael@0: if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) { michael@0: return false; michael@0: } michael@0: // TODO(nsylvain): We should use SHGetKnownFolderPath instead. Bug 1281128 michael@0: if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, michael@0: system_buffer))) michael@0: return false; michael@0: wstring_path = system_buffer; michael@0: file_util::UpOneDirectory(&wstring_path); michael@0: file_util::AppendToPath(&wstring_path, L"LocalLow"); michael@0: cur = FilePath(wstring_path); michael@0: break; michael@0: case base::DIR_LOCAL_APP_DATA: michael@0: if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, michael@0: SHGFP_TYPE_CURRENT, system_buffer))) michael@0: return false; michael@0: cur = FilePath(system_buffer); michael@0: break; michael@0: case base::DIR_SOURCE_ROOT: michael@0: // On Windows, unit tests execute two levels deep from the source root. michael@0: // For example: chrome/{Debug|Release}/ui_tests.exe michael@0: PathService::Get(base::DIR_EXE, &wstring_path); michael@0: file_util::UpOneDirectory(&wstring_path); michael@0: file_util::UpOneDirectory(&wstring_path); michael@0: cur = FilePath(wstring_path); michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: *result = cur; michael@0: return true; michael@0: } michael@0: michael@0: } // namespace base