Home | History | Annotate | Download | only in common
      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.
      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 "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"
     21 
     22 namespace chrome {
     23 
     24 namespace {
     25 
     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];
     34   path_buf[0] = 0;
     35   if (FAILED(SHGetFolderPath(NULL, csidl_folder, NULL,
     36                              SHGFP_TYPE_CURRENT, path_buf))) {
     37     return false;
     38   }
     39   *result = base::FilePath(path_buf);
     40   return true;
     41 }
     42 
     43 }  // namespace
     44 
     45 bool GetDefaultUserDataDirectory(base::FilePath* result) {
     46   if (!PathService::Get(base::DIR_LOCAL_APP_DATA, result))
     47     return false;
     48   BrowserDistribution* dist = BrowserDistribution::GetDistribution();
     49   *result = result->Append(dist->GetInstallSubDir());
     50   *result = result->Append(chrome::kUserDataDirname);
     51   return true;
     52 }
     53 
     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;
     58 }
     59 
     60 bool GetUserDocumentsDirectory(base::FilePath* result) {
     61   return GetUserDirectory(CSIDL_MYDOCUMENTS, result);
     62 }
     63 
     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
     67 // can be changed.
     68 bool GetUserDownloadsDirectorySafe(base::FilePath* result) {
     69   if (!GetUserDocumentsDirectory(result))
     70     return false;
     71 
     72   *result = result->Append(L"Downloads");
     73   return true;
     74 }
     75 
     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));
     87     return true;
     88   }
     89   return GetUserDownloadsDirectorySafe(result);
     90 }
     91 
     92 bool GetUserMusicDirectory(base::FilePath* result) {
     93   return GetUserDirectory(CSIDL_MYMUSIC, result);
     94 }
     95 
     96 bool GetUserPicturesDirectory(base::FilePath* result) {
     97   return GetUserDirectory(CSIDL_MYPICTURES, result);
     98 }
     99 
    100 bool GetUserVideosDirectory(base::FilePath* result) {
    101   return GetUserDirectory(CSIDL_MYVIDEO, result);
    102 }
    103 
    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.
    108 
    109   if (process_type.empty() || process_type == switches::kServiceProcess)
    110     return true;
    111 
    112 #if !defined(DISABLE_NACL)
    113   if (process_type == switches::kNaClBrokerProcess ||
    114       process_type == switches::kNaClLoaderProcess) {
    115     return true;
    116   }
    117 #endif
    118 
    119   return false;
    120 }
    121 
    122 }  // namespace chrome
    123