chrome_paths_win.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2006-2008 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#include "chrome/common/chrome_paths_internal.h"
6
7#include <windows.h>
8#include <knownfolders.h>
9#include <shellapi.h>
10#include <shlobj.h>
11#include <shobjidl.h>
12
13#include "app/win/scoped_co_mem.h"
14#include "base/file_path.h"
15#include "base/path_service.h"
16#include "chrome/common/chrome_constants.h"
17#include "chrome/installer/util/browser_distribution.h"
18#include "chrome/installer/util/master_preferences.h"
19
20namespace chrome {
21
22bool GetDefaultUserDataDirectory(FilePath* result) {
23  if (!PathService::Get(base::DIR_LOCAL_APP_DATA, result))
24    return false;
25  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
26  *result = result->Append(dist->GetInstallSubDir());
27  *result = result->Append(chrome::kUserDataDirname);
28  return true;
29}
30
31bool GetChromeFrameUserDataDirectory(FilePath* result) {
32  if (!PathService::Get(base::DIR_LOCAL_APP_DATA, result))
33    return false;
34  BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
35      BrowserDistribution::CHROME_FRAME,
36      installer::MasterPreferences::ForCurrentProcess());
37  *result = result->Append(dist->GetInstallSubDir());
38  *result = result->Append(chrome::kUserDataDirname);
39  return true;
40}
41
42void GetUserCacheDirectory(const FilePath& profile_dir, FilePath* result) {
43  // This function does more complicated things on Mac/Linux.
44  *result = profile_dir;
45}
46
47bool GetUserDocumentsDirectory(FilePath* result) {
48  wchar_t path_buf[MAX_PATH];
49  if (FAILED(SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL,
50                             SHGFP_TYPE_CURRENT, path_buf)))
51    return false;
52  *result = FilePath(path_buf);
53  return true;
54}
55
56// Return a default path for downloads that is safe.
57// We just use 'Downloads' under DIR_USER_DOCUMENTS. Localizing
58// 'downloads' is not a good idea because Chrome's UI language
59// can be changed.
60bool GetUserDownloadsDirectorySafe(FilePath* result) {
61  if (!GetUserDocumentsDirectory(result))
62    return false;
63
64  *result = result->Append(L"Downloads");
65  return true;
66}
67
68// On Vista and higher, use the downloads known folder. Since it can be
69// relocated to point to a "dangerous" folder, callers should validate that the
70// returned path is not dangerous before using it.
71bool GetUserDownloadsDirectory(FilePath* result) {
72  typedef HRESULT (WINAPI *GetKnownFolderPath)(
73      REFKNOWNFOLDERID, DWORD, HANDLE, PWSTR*);
74  GetKnownFolderPath f = reinterpret_cast<GetKnownFolderPath>(
75      GetProcAddress(GetModuleHandle(L"shell32.dll"), "SHGetKnownFolderPath"));
76  app::win::ScopedCoMem<wchar_t> path_buf;
77  if (f && SUCCEEDED(f(FOLDERID_Downloads, 0, NULL, &path_buf))) {
78    *result = FilePath(std::wstring(path_buf));
79    return true;
80  }
81  return GetUserDownloadsDirectorySafe(result);
82}
83
84bool GetUserDesktop(FilePath* result) {
85  // We need to go compute the value. It would be nice to support paths
86  // with names longer than MAX_PATH, but the system functions don't seem
87  // to be designed for it either, with the exception of GetTempPath
88  // (but other things will surely break if the temp path is too long,
89  // so we don't bother handling it.
90  wchar_t system_buffer[MAX_PATH];
91  system_buffer[0] = 0;
92  if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
93                             SHGFP_TYPE_CURRENT, system_buffer)))
94    return false;
95  *result = FilePath(system_buffer);
96  return true;
97}
98
99}  // namespace chrome
100