summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/common/chrome_paths_lacros.cc
blob: 3f117cf80c7308bdf5272ade10d0f2aefcd7e6d5 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2020 The Chromium Authors
// 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_lacros.h"

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/system/sys_info.h"
#include "chrome/common/chrome_paths.h"
#include "chromeos/crosapi/cpp/crosapi_constants.h"
#include "chromeos/crosapi/mojom/crosapi.mojom.h"
#include "chromeos/lacros/lacros_paths.h"

namespace chrome {
namespace {

struct DefaultPaths {
  base::FilePath documents_dir;
  base::FilePath downloads_dir;
  // |drivefs| is empty if Drive is not enabled in Ash.
  base::FilePath drivefs;
  base::FilePath removable_media_dir;
  base::FilePath android_files_dir;
  base::FilePath linux_files_dir;
  base::FilePath share_cache_dir;
  base::FilePath preinstalled_web_app_config_dir;
  base::FilePath preinstalled_web_app_extra_config_dir;
};

DefaultPaths& GetDefaultPaths() {
  static base::NoDestructor<DefaultPaths> lacros_paths;
  return *lacros_paths;
}

}  // namespace

void SetLacrosDefaultPaths(
    const base::FilePath& documents_dir,
    const base::FilePath& downloads_dir,
    const base::FilePath& drivefs,
    const base::FilePath& removable_media_dir,
    const base::FilePath& android_files_dir,
    const base::FilePath& linux_files_dir,
    const base::FilePath& ash_resources_dir,
    const base::FilePath& share_cache_dir,
    const base::FilePath& preinstalled_web_app_config_dir,
    const base::FilePath& preinstalled_web_app_extra_config_dir) {
  DCHECK(!documents_dir.empty());
  DCHECK(documents_dir.IsAbsolute());
  GetDefaultPaths().documents_dir = documents_dir;

  DCHECK(!downloads_dir.empty());
  DCHECK(downloads_dir.IsAbsolute());
  GetDefaultPaths().downloads_dir = downloads_dir;

  GetDefaultPaths().drivefs = drivefs;
  GetDefaultPaths().removable_media_dir = removable_media_dir;
  GetDefaultPaths().android_files_dir = android_files_dir;
  GetDefaultPaths().linux_files_dir = linux_files_dir;

  // As for ash resources path, set to chromeos::lacros_paths.
  chromeos::lacros_paths::SetAshResourcesPath(ash_resources_dir);

  GetDefaultPaths().share_cache_dir = share_cache_dir;
  GetDefaultPaths().preinstalled_web_app_config_dir =
      preinstalled_web_app_config_dir;
  GetDefaultPaths().preinstalled_web_app_extra_config_dir =
      preinstalled_web_app_extra_config_dir;
}

void SetLacrosDefaultPathsFromInitParams(
    const crosapi::mojom::DefaultPaths* default_paths) {
  // default_paths may be null on browser_tests and individual components may be
  // empty due to version skew between ash and lacros.
  if (default_paths) {
    base::FilePath drivefs_dir;
    if (default_paths->drivefs.has_value())
      drivefs_dir = default_paths->drivefs.value();
    base::FilePath removable_media_dir;
    if (default_paths->removable_media.has_value())
      removable_media_dir = default_paths->removable_media.value();
    base::FilePath android_files_dir;
    if (default_paths->android_files.has_value())
      android_files_dir = default_paths->android_files.value();
    base::FilePath linux_files_dir;
    if (default_paths->linux_files.has_value())
      linux_files_dir = default_paths->linux_files.value();
    base::FilePath ash_resources_dir;
    if (default_paths->ash_resources.has_value())
      ash_resources_dir = default_paths->ash_resources.value();
    base::FilePath share_cache_dir;
    if (default_paths->share_cache.has_value())
      share_cache_dir = default_paths->share_cache.value();
    base::FilePath preinstalled_web_app_config_dir;
    if (default_paths->preinstalled_web_app_config.has_value()) {
      preinstalled_web_app_config_dir =
          default_paths->preinstalled_web_app_config.value();
    }
    base::FilePath preinstalled_web_app_extra_config_dir;
    if (default_paths->preinstalled_web_app_extra_config.has_value()) {
      preinstalled_web_app_extra_config_dir =
          default_paths->preinstalled_web_app_extra_config.value();
    }

    chrome::SetLacrosDefaultPaths(
        default_paths->documents, default_paths->downloads, drivefs_dir,
        removable_media_dir, android_files_dir, linux_files_dir,
        ash_resources_dir, share_cache_dir, preinstalled_web_app_config_dir,
        preinstalled_web_app_extra_config_dir);
  }
}

void SetDriveFsMountPointPath(const base::FilePath& drivefs) {
  GetDefaultPaths().drivefs = drivefs;
}

bool GetDefaultUserDataDirectory(base::FilePath* result) {
  if (base::SysInfo::IsRunningOnChromeOS()) {
    *result = base::FilePath(crosapi::kLacrosUserDataPath);
  } else {
    // For developers on Linux desktop, just pick a reasonable default. Most
    // developers will pass --user-data-dir and override this value anyway.
    *result = base::GetHomeDir().Append(".config").Append("lacros");
  }
  return true;
}

void GetUserCacheDirectory(const base::FilePath& profile_dir,
                           base::FilePath* result) {
  // Chrome OS doesn't allow special cache overrides like desktop Linux.
  *result = profile_dir;
}

bool GetUserDocumentsDirectory(base::FilePath* result) {
  // NOTE: Lacros overrides the path with a value from ash early in startup. See
  // crosapi::mojom::LacrosInitParams.
  CHECK(!GetDefaultPaths().documents_dir.empty());
  *result = GetDefaultPaths().documents_dir;
  return true;
}

bool GetUserDownloadsDirectorySafe(base::FilePath* result) {
  // NOTE: Lacros overrides the path with a value from ash early in startup. See
  // crosapi::mojom::LacrosInitParams.
  CHECK(!GetDefaultPaths().downloads_dir.empty());
  *result = GetDefaultPaths().downloads_dir;
  return true;
}

bool GetUserDownloadsDirectory(base::FilePath* result) {
  return GetUserDownloadsDirectorySafe(result);
}

bool GetUserMusicDirectory(base::FilePath* result) {
  // Chrome OS does not support custom media directories.
  return false;
}

bool GetUserPicturesDirectory(base::FilePath* result) {
  // Chrome OS does not support custom media directories.
  return false;
}

bool GetUserVideosDirectory(base::FilePath* result) {
  // Chrome OS does not support custom media directories.
  return false;
}

bool ProcessNeedsProfileDir(const std::string& process_type) {
  // We have no reason to forbid this on Chrome OS as we don't have roaming
  // profile troubles there.
  return true;
}

bool GetDriveFsMountPointPath(base::FilePath* result) {
  // NOTE: Lacros overrides the path with a value from ash early in startup. See
  // crosapi::mojom::LacrosInitParams.
  if (GetDefaultPaths().drivefs.empty())
    return false;
  *result = GetDefaultPaths().drivefs;
  return true;
}

bool GetRemovableMediaPath(base::FilePath* result) {
  if (GetDefaultPaths().removable_media_dir.empty())
    return false;
  *result = GetDefaultPaths().removable_media_dir;
  return true;
}

bool GetAndroidFilesPath(base::FilePath* result) {
  if (GetDefaultPaths().android_files_dir.empty())
    return false;
  *result = GetDefaultPaths().android_files_dir;
  return true;
}

bool GetLinuxFilesPath(base::FilePath* result) {
  if (GetDefaultPaths().linux_files_dir.empty())
    return false;
  *result = GetDefaultPaths().linux_files_dir;
  return true;
}

bool GetShareCachePath(base::FilePath* result) {
  if (GetDefaultPaths().share_cache_dir.empty())
    return false;
  *result = GetDefaultPaths().share_cache_dir;
  return true;
}

bool GetPreinstalledWebAppConfigPath(base::FilePath* result) {
  if (GetDefaultPaths().preinstalled_web_app_config_dir.empty())
    return false;
  *result = GetDefaultPaths().preinstalled_web_app_config_dir;
  return true;
}

bool GetPreinstalledWebAppExtraConfigPath(base::FilePath* result) {
  if (GetDefaultPaths().preinstalled_web_app_extra_config_dir.empty())
    return false;
  *result = GetDefaultPaths().preinstalled_web_app_extra_config_dir;
  return true;
}

}  // namespace chrome