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