File: file_system_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (334 lines) | stat: -rw-r--r-- 11,030 bytes parent folder | download | duplicates (3)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2012 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/browser/ash/drive/file_system_util.h"

#include <stddef.h>

#include <memory>
#include <string>
#include <type_traits>
#include <vector>

#include "ash/constants/ash_constants.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#include "base/containers/fixed_flat_set.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#include "base/system/sys_info.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_factory.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_paths_internal.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/components/network/managed_state.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "components/drive/drive_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/gaia/gaia_auth_util.h"

using content::BrowserThread;

namespace drive::util {

using user_manager::User;
using user_manager::UserManager;

namespace {

ConnectionStatus GetDeviceOnlineStatus() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  using enum ConnectionStatus;

  if (!ash::NetworkHandler::IsInitialized()) {
    VLOG(1) << "GetDeviceOnlineStatus: no network handler";
    return kNoNetwork;
  }

  ash::NetworkStateHandler* const handler =
      ash::NetworkHandler::Get()->network_state_handler();
  DCHECK(handler);

  const ash::NetworkState* const network = handler->DefaultNetwork();
  if (!network) {
    VLOG(1) << "GetDeviceOnlineStatus: no network";
    return kNoNetwork;
  }

  if (!network->IsOnline()) {
    VLOG(1) << "GetDeviceOnlineStatus: not ready: network is "
            << network->connection_state();
    return kNotReady;
  }

  using PortalState = ash::NetworkState::PortalState;
  if (const PortalState portal_state = network->portal_state();
      portal_state != PortalState::kOnline) {
    VLOG(1) << "GetDeviceOnlineStatus: not ready: portal is " << portal_state;
    return kNotReady;
  }

  if (handler->default_network_is_metered()) {
    VLOG(1) << "GetDeviceOnlineStatus: metered";
    return kMetered;
  }

  return kConnected;
}

}  // namespace

DriveIntegrationService* GetIntegrationServiceByProfile(Profile* profile) {
  DriveIntegrationService* service =
      DriveIntegrationServiceFactory::FindForProfile(profile);
  if (!service || !service->IsMounted()) {
    return nullptr;
  }
  return service;
}

bool IsUnderDriveMountPoint(const base::FilePath& path) {
  std::vector<base::FilePath::StringType> components = path.GetComponents();
  if (components.size() < 4) {
    return false;
  }
  if (components[0] != FILE_PATH_LITERAL("/")) {
    return false;
  }
  if (components[1] != FILE_PATH_LITERAL("media")) {
    return false;
  }
  if (components[2] != FILE_PATH_LITERAL("fuse")) {
    return false;
  }
  static const base::FilePath::CharType kPrefix[] =
      FILE_PATH_LITERAL("drivefs");
  if (components[3].compare(0, std::size(kPrefix) - 1, kPrefix) != 0) {
    return false;
  }

  return true;
}

base::FilePath GetCacheRootPath(const Profile* const profile) {
  base::FilePath cache_base_path;
  chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path);
  base::FilePath cache_root_path =
      cache_base_path.Append(ash::kDriveCacheDirname);
  static const base::FilePath::CharType kFileCacheVersionDir[] =
      FILE_PATH_LITERAL("v1");
  return cache_root_path.Append(kFileCacheVersionDir);
}

DriveAvailability CheckDriveAvailabilityForProfile(
    const Profile* const profile) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // Disable Drive for non-Gaia accounts.
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          ash::switches::kDisableGaiaServices)) {
    return DriveAvailability::kNotAvailableForAccountType;
  }
  if (!ash::LoginState::IsInitialized()) {
    return DriveAvailability::kNotAvailableForUninitialisedLoginState;
  }
  // Disable Drive for incognito profiles.
  if (profile->IsOffTheRecord()) {
    return DriveAvailability::kNotAvailableInIncognito;
  }
  const User* const user = ash::ProfileHelper::Get()->GetUserByProfile(profile);
  if (!user || !user->HasGaiaAccount()) {
    return DriveAvailability::kNotAvailableForAccountType;
  }

  // Disable Drive if the flag has been passed and it is a test image.
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          ash::switches::kDisableDriveFsForTesting)) {
    base::SysInfo::CrashIfChromeOSNonTestImage();
    return DriveAvailability::kNotAvailableForTestImage;
  }

  return DriveAvailability::kAvailable;
}

bool IsDriveAvailableForProfile(const Profile* const profile) {
  return CheckDriveAvailabilityForProfile(profile) ==
         DriveAvailability::kAvailable;
}

DriveAvailability CheckDriveEnabledAndDriveAvailabilityForProfile(
    const Profile* const profile) {
  // Disable Drive if preference is set. This can happen with commandline flag
  // --disable-drive or enterprise policy, or with user settings.
  if (profile->GetPrefs()->GetBoolean(prefs::kDisableDrive)) {
    return DriveAvailability::kNotAvailableWhenDisableDrivePreferenceSet;
  }

  return CheckDriveAvailabilityForProfile(profile);
}

bool IsDriveEnabledForProfile(const Profile* const profile) {
  return CheckDriveEnabledAndDriveAvailabilityForProfile(profile) ==
         DriveAvailability::kAvailable;
}

bool IsDriveFsBulkPinningAvailable(const Profile* const profile) {
  // Check the "drivefs.bulk_pinning.visible" boolean pref. If this pref is
  // false, then it probably means that it has been turned down by an enterprise
  // policy, and the bulk-pinning feature should not be available.
  if (profile &&
      !profile->GetPrefs()->GetBoolean(prefs::kDriveFsBulkPinningVisible)) {
    return false;
  }

  // For Googlers, the bulk-pinning feature is available on any kind of device.
  if (UserManager::IsInitialized()) {
    if (const User* const user = UserManager::Get()->GetActiveUser();
        user && gaia::IsGoogleInternalAccountEmail(
                    user->GetAccountId().GetUserEmail())) {
      return true;
    }
  }

  // For other users (non-Googlers), the bulk-pinning feature is available on
  // suitable devices, as controlled by the
  // "FeatureManagementDriveFsBulkPinning" Chrome feature.
  return base::FeatureList::IsEnabled(
      ash::features::kFeatureManagementDriveFsBulkPinning);
}

bool IsDriveFsBulkPinningAvailable() {
  return IsDriveFsBulkPinningAvailable(ProfileManager::GetActiveUserProfile());
}

bool IsOobeDrivePinningAvailable(const Profile* const profile) {
  const bool b = IsOobeDrivePinningScreenEnabled() &&
                 IsDriveFsBulkPinningAvailable(profile);
  VLOG(1) << "IsOobeDrivePinningAvailable() returned " << b;
  return b;
}

bool IsOobeDrivePinningAvailable() {
  return IsOobeDrivePinningAvailable(ProfileManager::GetActiveUserProfile());
}

// To ensure that the DrivePinningScreen is always available to the wizard,
// regardless of the current user profile, check this to add the
// DrivePinningScreen to the screen_manager when initializing the
// wizardController.
bool IsOobeDrivePinningScreenEnabled() {
  return base::FeatureList::IsEnabled(ash::features::kOobeDrivePinning) &&
         ash::features::IsOobeChoobeEnabled();
}

bool IsDriveFsMirrorSyncAvailable(const Profile* const profile) {
  return base::FeatureList::IsEnabled(ash::features::kDriveFsMirroring);
}

std::ostream& operator<<(std::ostream& out, const ConnectionStatus status) {
  switch (status) {
#define PRINT(s)               \
  case ConnectionStatus::k##s: \
    return out << #s;
    PRINT(NoService)
    PRINT(NoNetwork)
    PRINT(NotReady)
    PRINT(Metered)
    PRINT(Connected)
#undef PRINT
  }

  return out << "ConnectionStatus("
             << static_cast<std::underlying_type_t<ConnectionStatus>>(status)
             << ")";
}

// For testing.
static ConnectionStatus connection_status_for_testing;
static bool has_connection_status_for_testing = false;

void SetDriveConnectionStatusForTesting(const ConnectionStatus status) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  VLOG(1) << "SetDriveConnectionStatusForTesting: " << status;
  connection_status_for_testing = status;
  has_connection_status_for_testing = true;
}

ConnectionStatus GetDriveConnectionStatus(Profile* const profile,
                                          bool* is_online) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  using enum ConnectionStatus;

  if (has_connection_status_for_testing) {
    VLOG(1) << "GetDriveConnectionStatus: for testing: "
            << connection_status_for_testing;
    return connection_status_for_testing;
  }

  ConnectionStatus online_status = GetDeviceOnlineStatus();
  if (is_online != nullptr) {
    *is_online = online_status == kConnected || online_status == kMetered;
  }

  if (!GetIntegrationServiceByProfile(profile)) {
    VLOG(1) << "GetDriveConnectionStatus: no Drive integration service";
    return kNoService;
  }

  DCHECK(profile);
  if (online_status == kMetered &&
      !profile->GetPrefs()->GetBoolean(prefs::kDisableDriveOverCellular)) {
    VLOG(1) << "GetDriveConnectionStatus: metered, but still enabled";
    return kConnected;
  }

  VLOG(1) << "GetDriveConnectionStatus: " << online_status;
  return online_status;
}

bool IsPinnableGDocMimeType(const std::string& mime_type) {
  constexpr auto kPinnableGDocMimeTypes =
      base::MakeFixedFlatSet<std::string_view>({
          "application/vnd.google-apps.document",
          "application/vnd.google-apps.drawing",
          "application/vnd.google-apps.presentation",
          "application/vnd.google-apps.spreadsheet",
      });

  return kPinnableGDocMimeTypes.contains(mime_type);
}

int64_t ComputeDriveFsContentCacheSize(const base::FilePath& path) {
  int64_t blocks = 0;

  using base::FileEnumerator;
  FileEnumerator it(path, true, FileEnumerator::FILES);
  while (!it.Next().empty()) {
    const FileEnumerator::FileInfo& info = it.GetInfo();

    // Skip the `chunks.db*` files.
    if (base::StartsWith(info.GetName().value(), "chunks.db")) {
      continue;
    }

    blocks += info.stat().st_blocks;
  }

  const int64_t size = blocks << 9;
  VLOG(1) << "DriveFs cache: " << (size >> 20) << " M";
  return size;
}

}  // namespace drive::util