summaryrefslogtreecommitdiffstats
path: root/chrome/common/chrome_paths_linux.cc
blob: d3ce20788b53f37e7f8dc79d758723ac0b7f0cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/chrome_paths_internal.h"

#include <glib.h>
#include <stdlib.h>

#include "base/file_path.h"
#include "base/path_service.h"
#include "chrome/third_party/xdg_user_dirs/xdg_user_dir_lookup.h"

namespace {

FilePath GetHomeDir() {
  const char *home_dir = getenv("HOME");

  if (home_dir && home_dir[0])
    return FilePath(home_dir);

  home_dir = g_get_home_dir();
  if (home_dir && home_dir[0])
    return FilePath(home_dir);

  FilePath rv;
  if (PathService::Get(base::DIR_TEMP, &rv))
    return rv;

  /* last resort */
  return FilePath("/tmp/");
}

// Wrapper around xdg_user_dir_lookup() from
// src/chrome/third_party/xdg-user-dirs
FilePath GetXDGUserDirectory(const char* env_name, const char* fallback_dir) {
  char* xdg_dir = xdg_user_dir_lookup(env_name);
  if (xdg_dir) {
    FilePath rv(xdg_dir);
    free(xdg_dir);
    return rv;
  }
  return GetHomeDir().Append(fallback_dir);
}

// |env_name| is the name of an environment variable that we want to use to get
// a directory path. |fallback_dir| is the directory relative to $HOME that we
// use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL.
FilePath GetXDGDirectory(const char* env_name, const char* fallback_dir) {
  const char* env_value = getenv(env_name);
  if (env_value && env_value[0])
    return FilePath(env_value);
  return GetHomeDir().Append(fallback_dir);
}

}  // namespace

namespace chrome {

// See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
// for a spec on where config files go.  The net effect for most
// systems is we use ~/.config/chromium/ for Chromium and
// ~/.config/google-chrome/ for official builds.
// (This also helps us sidestep issues with other apps grabbing ~/.chromium .)
bool GetDefaultUserDataDirectory(FilePath* result) {
  FilePath config_dir(GetXDGDirectory("XDG_CONFIG_HOME", ".config"));
#if defined(GOOGLE_CHROME_BUILD)
  *result = config_dir.Append("google-chrome");
#else
  *result = config_dir.Append("chromium");
#endif
  return true;
}

bool GetUserDocumentsDirectory(FilePath* result) {
  *result = GetXDGUserDirectory("DOCUMENTS", "Documents");
  return true;
}

// We respect the user's preferred download location, unless it is
// ~ or their desktop directory, in which case we default to ~/Downloads.
bool GetUserDownloadsDirectory(FilePath* result) {
  *result = GetXDGUserDirectory("DOWNLOAD", "Downloads");

  FilePath home = GetHomeDir();
  if (*result == home) {
    *result = home.Append("Downloads");
    return true;
  }

  FilePath desktop;
  GetUserDesktop(&desktop);
  if (*result == desktop) {
    *result = home.Append("Downloads");
  }

  return true;
}

bool GetUserDesktop(FilePath* result) {
  *result = GetXDGUserDirectory("DESKTOP", "Desktop");
  return true;
}

}  // namespace chrome