File: recent_drive_source.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (193 lines) | stat: -rw-r--r-- 6,867 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 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/fileapi/recent_drive_source.h"

#include <iterator>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/fileapi/recent_file.h"
#include "chromeos/ash/components/drivefs/drivefs_util.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "recent_drive_source.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_operation_runner.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/common/file_system/file_system_types.h"
#include "ui/file_manager/file_types_data.h"
#include "url/origin.h"

using content::BrowserThread;

namespace ash {

const char RecentDriveSource::kLoadHistogramName[] =
    "FileBrowser.Recent.LoadDrive";

const char kAudioMimeType[] = "audio";
const char kImageMimeType[] = "image";
const char kVideoMimeType[] = "video";

RecentDriveSource::CallContext::CallContext(GetRecentFilesCallback callback)
    : callback(std::move(callback)), build_start_time(base::TimeTicks::Now()) {}

RecentDriveSource::CallContext::CallContext(CallContext&& context)
    : callback(std::move(context.callback)),
      build_start_time(context.build_start_time),
      files(std::move(context.files)),
      search_query(std::move(context.search_query)) {}

RecentDriveSource::CallContext::~CallContext() = default;

RecentDriveSource::RecentDriveSource(Profile* profile)
    : RecentSource(extensions::api::file_manager_private::VolumeType::kDrive),
      profile_(profile) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

RecentDriveSource::~RecentDriveSource() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

std::vector<std::string> RecentDriveSource::CreateTypeFilters(
    RecentSource::FileType file_type) {
  std::vector<std::string> type_filters;
  switch (file_type) {
    case FileType::kAudio:
      type_filters.push_back(kAudioMimeType);
      break;
    case FileType::kImage:
      type_filters.push_back(kImageMimeType);
      break;
    case FileType::kVideo:
      type_filters.push_back(kVideoMimeType);
      break;
    case FileType::kDocument: {
      type_filters.insert(type_filters.end(),
                          file_types_data::kDocumentMIMETypes.begin(),
                          file_types_data::kDocumentMIMETypes.end());
      break;
    }
    default:
      // Leave the filters empty.
      break;
  }
  return type_filters;
}

void RecentDriveSource::GetRecentFiles(const Params& params,
                                       GetRecentFilesCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  auto context = std::make_unique<CallContext>(std::move(callback));
  CallContext* context_ptr = context.get();
  context_map_.AddWithID(std::move(context), params.call_id());

  auto* integration_service =
      drive::util::GetIntegrationServiceByProfile(profile_);
  if (!integration_service) {
    // |integration_service| is nullptr if Drive is disabled.
    OnComplete(params.call_id());
    return;
  }

  auto query_params = drivefs::mojom::QueryParameters::New();
  query_params->page_size = params.max_files();
  query_params->query_source =
      drivefs::mojom::QueryParameters::QuerySource::kLocalOnly;
  query_params->sort_field =
      drivefs::mojom::QueryParameters::SortField::kLastViewedByMe;
  query_params->sort_direction =
      drivefs::mojom::QueryParameters::SortDirection::kDescending;
  std::vector<std::string> type_filters =
      RecentDriveSource::CreateTypeFilters(params.file_type());
  query_params->viewed_time = params.cutoff_time();
  query_params->title = params.query();
  query_params->viewed_time_operator =
      drivefs::mojom::QueryParameters::DateComparisonOperator::kGreaterThan;
  if (type_filters.size() == 1) {
    query_params->mime_type = type_filters.front();
  } else if (type_filters.size() > 1) {
    query_params->mime_types = std::move(type_filters);
  }
  integration_service->GetDriveFsInterface()->StartSearchQuery(
      context_ptr->search_query.BindNewPipeAndPassReceiver(),
      std::move(query_params));
  context_ptr->search_query->GetNextPage(
      base::BindOnce(&RecentDriveSource::GotSearchResults,
                     weak_ptr_factory_.GetWeakPtr(), params));
}

std::vector<RecentFile> RecentDriveSource::Stop(const int32_t call_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  CallContext* context = context_map_.Lookup(call_id);
  if (context == nullptr) {
    return {};
  }
  std::vector<RecentFile> files = std::move(context->files);
  context_map_.Remove(call_id);
  return files;
}

void RecentDriveSource::OnComplete(const int32_t call_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  CallContext* context = context_map_.Lookup(call_id);
  if (context == nullptr) {
    return;
  }
  DCHECK(!context->build_start_time.is_null());

  UMA_HISTOGRAM_TIMES(kLoadHistogramName,
                      base::TimeTicks::Now() - context->build_start_time);
  std::move(context->callback).Run(std::move(context->files));
  context_map_.Remove(call_id);
}

void RecentDriveSource::GotSearchResults(
    const Params& params,
    drive::FileError error,
    std::optional<std::vector<drivefs::mojom::QueryItemPtr>> results) {
  CallContext* context = context_map_.Lookup(params.call_id());
  if (context == nullptr) {
    return;
  }

  auto* integration_service =
      drive::util::GetIntegrationServiceByProfile(profile_);
  if (!results || !integration_service) {
    OnComplete(params.call_id());
    return;
  }

  context->files.reserve(results->size());
  for (auto& result : *results) {
    if (!drivefs::IsAFile(result->metadata->type)) {
      continue;
    }
    base::FilePath path = integration_service->GetMountPointPath().BaseName();
    if (!base::FilePath("/").AppendRelativePath(result->path, &path)) {
      path = path.Append(result->path);
    }
    context->files.emplace_back(
        params.file_system_context()->CreateCrackedFileSystemURL(
            blink::StorageKey::CreateFirstParty(
                url::Origin::Create(params.origin())),
            storage::kFileSystemTypeExternal, path),
        // Do not use "modification_time" field here because that will cause
        // files modified by others recently (e.g. Shared with me) being
        // treated as recent files.
        result->metadata->last_viewed_by_me_time);
  }
  OnComplete(params.call_id());
}

}  // namespace ash