summaryrefslogtreecommitdiff
path: root/chromium/chrome/common/chrome_paths_lacros.cc
blob: 1545a49ec56ee5c2c64e910910b3cb2a878e1f30 (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
// Copyright 2020 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_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"

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;
};

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) {
  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;
}

void SetLacrosDefaultPathsFromInitParams(
    const crosapi::mojom::BrowserInitParams* init_params) {
  CHECK(init_params);
  // default_paths may be null on browser_tests and individual components may be
  // empty due to version skew between ash and lacros.
  if (init_params->default_paths) {
    base::FilePath drivefs_dir;
    if (init_params->default_paths->drivefs.has_value())
      drivefs_dir = init_params->default_paths->drivefs.value();
    base::FilePath removable_media_dir;
    if (init_params->default_paths->removable_media.has_value())
      removable_media_dir = init_params->default_paths->removable_media.value();
    base::FilePath android_files_dir;
    if (init_params->default_paths->android_files.has_value())
      android_files_dir = init_params->default_paths->android_files.value();
    base::FilePath linux_files_dir;
    if (init_params->default_paths->linux_files.has_value())
      linux_files_dir = init_params->default_paths->linux_files.value();
    chrome::SetLacrosDefaultPaths(init_params->default_paths->documents,
                                  init_params->default_paths->downloads,
                                  drivefs_dir, removable_media_dir,
                                  android_files_dir, linux_files_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;
}

}  // namespace chrome