File: quick_insert_file_suggester.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 (183 lines) | stat: -rw-r--r-- 6,891 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
// Copyright 2024 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/ui/ash/quick_insert/quick_insert_file_suggester.h"

#include <optional>
#include <string>
#include <utility>

#include "base/barrier_callback.h"
#include "base/containers/to_vector.h"
#include "base/time/time.h"
#include "chrome/browser/ash/app_list/search/files/file_title.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_factory.h"
#include "chrome/browser/ash/fileapi/recent_file.h"
#include "chrome/browser/ash/fileapi/recent_model.h"
#include "chrome/browser/ash/fileapi/recent_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "content/public/browser/storage_partition.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/common/file_system/file_system_types.h"

namespace {

using LocalFile = QuickInsertFileSuggester::LocalFile;
using DriveFile = QuickInsertFileSuggester::DriveFile;
namespace fmp = extensions::api::file_manager_private;

constexpr base::TimeDelta kMaxDriveFileRecencyDelta = base::Days(30);
constexpr base::TimeDelta kScanTimeout = base::Seconds(1);

storage::FileSystemContext* GetFileSystemContextForProfile(Profile* profile) {
  content::StoragePartition* storage = profile->GetDefaultStoragePartition();
  return storage->GetFileSystemContext();
}

void GetRecentFiles(Profile* profile,
                    ash::RecentSource::FileType file_type,
                    fmp::VolumeType volume_type,
                    size_t max_files,
                    base::TimeDelta now_delta,
                    ash::RecentModel::GetRecentFilesCallback callback) {
  const scoped_refptr<storage::FileSystemContext> file_system_context =
      GetFileSystemContextForProfile(profile);
  if (!file_system_context) {
    return;
  }

  ash::RecentModel* model = ash::RecentModelFactory::GetForProfile(profile);
  if (!model) {
    return;
  }
  ash::RecentModelOptions options;
  options.now_delta = now_delta;
  options.file_type = file_type;
  options.scan_timeout = kScanTimeout;
  options.max_files = max_files;
  options.source_specs = {
      ash::RecentSourceSpec{.volume_type = volume_type},
  };

  model->GetRecentFiles(file_system_context.get(), GURL(), /*query=*/"",
                        options, std::move(callback));
}

void GetDriveFileMetadata(
    drive::DriveIntegrationService* drive_integration,
    const ash::RecentFile& file,
    base::OnceCallback<void(std::optional<DriveFile>)> callback) {
  const storage::FileSystemURL& url = file.url();
  if (url.type() != storage::kFileSystemTypeDriveFs) {
    std::move(callback).Run(std::nullopt);
    return;
  }

  const base::FilePath& path = url.path();
  drive_integration->GetMetadata(
      path, base::BindOnce(
                [](const base::FilePath& path, drive::FileError error,
                   drivefs::mojom::FileMetadataPtr metadata)
                    -> std::optional<DriveFile> {
                  if (error != drive::FILE_ERROR_OK) {
                    return std::nullopt;
                  }
                  return DriveFile(metadata->item_id,
                                   app_list::GetFileTitle(path), path,
                                   GURL(metadata->alternate_url));
                },
                path)
                .Then(std::move(callback)));
}

std::vector<DriveFile> FilterDriveFiles(
    std::vector<std::optional<DriveFile>> files) {
  std::vector<DriveFile> filtered;
  filtered.reserve(files.size());
  for (std::optional<DriveFile>& file : files) {
    if (file.has_value()) {
      filtered.push_back(std::move(*file));
    }
  }
  filtered.shrink_to_fit();
  return filtered;
}

}  // namespace

QuickInsertFileSuggester::DriveFile::DriveFile(std::optional<std::string> id,
                                               std::u16string title,
                                               base::FilePath local_path,
                                               GURL url)
    : id(std::move(id)),
      title(std::move(title)),
      local_path(std::move(local_path)),
      url(std::move(url)) {}

QuickInsertFileSuggester::DriveFile::~DriveFile() = default;

QuickInsertFileSuggester::DriveFile::DriveFile(const DriveFile&) = default;
QuickInsertFileSuggester::DriveFile::DriveFile(DriveFile&&) = default;
QuickInsertFileSuggester::DriveFile&
QuickInsertFileSuggester::DriveFile::operator=(const DriveFile&) = default;
QuickInsertFileSuggester::DriveFile&
QuickInsertFileSuggester::DriveFile::operator=(DriveFile&&) = default;

QuickInsertFileSuggester::QuickInsertFileSuggester(Profile* profile)
    : profile_(profile) {}

QuickInsertFileSuggester::~QuickInsertFileSuggester() = default;

void QuickInsertFileSuggester::GetRecentLocalImages(
    size_t max_files,
    base::TimeDelta now_delta,
    RecentLocalImagesCallback callback) {
  GetRecentFiles(
      profile_, ash::RecentSource::FileType::kImage,
      fmp::VolumeType::kDownloads, max_files, now_delta,
      base::BindOnce(&QuickInsertFileSuggester::OnGetRecentLocalImages,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void QuickInsertFileSuggester::GetRecentDriveFiles(
    size_t max_files,
    RecentDriveFilesCallback callback) {
  GetRecentFiles(
      profile_, ash::RecentSource::FileType::kAll, fmp::VolumeType::kDrive,
      max_files, kMaxDriveFileRecencyDelta,
      base::BindOnce(&QuickInsertFileSuggester::OnGetRecentDriveFiles,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void QuickInsertFileSuggester::OnGetRecentLocalImages(
    RecentLocalImagesCallback callback,
    const std::vector<ash::RecentFile>& recent_files) {
  std::move(callback).Run(
      base::ToVector(recent_files, [](const ash::RecentFile& recent_file) {
        const base::FilePath& path = recent_file.url().path();
        return LocalFile{.title = app_list::GetFileTitle(path), .path = path};
      }));
}

void QuickInsertFileSuggester::OnGetRecentDriveFiles(
    RecentDriveFilesCallback callback,
    const std::vector<ash::RecentFile>& recent_files) {
  drive::DriveIntegrationService* drive_integration =
      drive::DriveIntegrationServiceFactory::FindForProfile(profile_);
  if (!drive_integration) {
    std::move(callback).Run({});
  }

  auto barrier_callback = base::BarrierCallback<std::optional<DriveFile>>(
      /*num_callbacks=*/recent_files.size(),
      /*done_callback=*/base::BindOnce(FilterDriveFiles)
          .Then(std::move(callback)));

  for (const ash::RecentFile& recent_file : recent_files) {
    GetDriveFileMetadata(drive_integration, recent_file, barrier_callback);
  }
}