1 // Copyright (c) 2012 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.
5 #include "chrome/common/chrome_paths_internal.h"
8 #include <knownfolders.h>
13 #include "base/files/file_path.h"
14 #include "base/path_service.h"
15 #include "base/win/metro.h"
16 #include "base/win/scoped_co_mem.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/installer/util/browser_distribution.h"
20 #include "components/nacl/common/nacl_switches.h"
26 // Generic function to call SHGetFolderPath().
27 bool GetUserDirectory(int csidl_folder
, base::FilePath
* result
) {
28 // We need to go compute the value. It would be nice to support paths
29 // with names longer than MAX_PATH, but the system functions don't seem
30 // to be designed for it either, with the exception of GetTempPath
31 // (but other things will surely break if the temp path is too long,
32 // so we don't bother handling it.
33 wchar_t path_buf
[MAX_PATH
];
35 if (FAILED(SHGetFolderPath(NULL
, csidl_folder
, NULL
,
36 SHGFP_TYPE_CURRENT
, path_buf
))) {
39 *result
= base::FilePath(path_buf
);
45 bool GetDefaultUserDataDirectory(base::FilePath
* result
) {
46 if (!PathService::Get(base::DIR_LOCAL_APP_DATA
, result
))
48 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
49 *result
= result
->Append(dist
->GetInstallSubDir());
50 *result
= result
->Append(chrome::kUserDataDirname
);
54 void GetUserCacheDirectory(const base::FilePath
& profile_dir
,
55 base::FilePath
* result
) {
56 // This function does more complicated things on Mac/Linux.
57 *result
= profile_dir
;
60 bool GetUserDocumentsDirectory(base::FilePath
* result
) {
61 return GetUserDirectory(CSIDL_MYDOCUMENTS
, result
);
64 // Return a default path for downloads that is safe.
65 // We just use 'Downloads' under DIR_USER_DOCUMENTS. Localizing
66 // 'downloads' is not a good idea because Chrome's UI language
68 bool GetUserDownloadsDirectorySafe(base::FilePath
* result
) {
69 if (!GetUserDocumentsDirectory(result
))
72 *result
= result
->Append(L
"Downloads");
76 // On Vista and higher, use the downloads known folder. Since it can be
77 // relocated to point to a "dangerous" folder, callers should validate that the
78 // returned path is not dangerous before using it.
79 bool GetUserDownloadsDirectory(base::FilePath
* result
) {
80 typedef HRESULT (WINAPI
*GetKnownFolderPath
)(
81 REFKNOWNFOLDERID
, DWORD
, HANDLE
, PWSTR
*);
82 GetKnownFolderPath f
= reinterpret_cast<GetKnownFolderPath
>(
83 GetProcAddress(GetModuleHandle(L
"shell32.dll"), "SHGetKnownFolderPath"));
84 base::win::ScopedCoMem
<wchar_t> path_buf
;
85 if (f
&& SUCCEEDED(f(FOLDERID_Downloads
, 0, NULL
, &path_buf
))) {
86 *result
= base::FilePath(std::wstring(path_buf
));
89 return GetUserDownloadsDirectorySafe(result
);
92 bool GetUserMusicDirectory(base::FilePath
* result
) {
93 return GetUserDirectory(CSIDL_MYMUSIC
, result
);
96 bool GetUserPicturesDirectory(base::FilePath
* result
) {
97 return GetUserDirectory(CSIDL_MYPICTURES
, result
);
100 bool GetUserVideosDirectory(base::FilePath
* result
) {
101 return GetUserDirectory(CSIDL_MYVIDEO
, result
);
104 bool ProcessNeedsProfileDir(const std::string
& process_type
) {
105 // On windows we don't want subprocesses other than the browser process and
106 // service processes to be able to use the profile directory because if it
107 // lies on a network share the sandbox will prevent us from accessing it.
109 if (process_type
.empty() || process_type
== switches::kServiceProcess
)
112 #if !defined(DISABLE_NACL)
113 if (process_type
== switches::kNaClBrokerProcess
||
114 process_type
== switches::kNaClLoaderProcess
) {
122 } // namespace chrome