blob: 9224e59e6ad0a976af3bdea8066a1f9672ba9881 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2017 The Chromium Authors
Joel Hockeyc7649cc02019-03-08 00:38:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Yeunjoo Choief8a4a42022-11-08 04:21:055#ifndef CHROME_BROWSER_ASH_FILEAPI_RECENT_DISK_SOURCE_H_
6#define CHROME_BROWSER_ASH_FILEAPI_RECENT_DISK_SOURCE_H_
Joel Hockeyc7649cc02019-03-08 00:38:427
8#include <memory>
Joel Hockeyc7649cc02019-03-08 00:38:429#include <string>
10#include <vector>
11
Bo Majewski65e328462024-02-12 06:15:0412#include "base/containers/id_map.h"
Joel Hockeyc7649cc02019-03-08 00:38:4213#include "base/files/file.h"
14#include "base/files/file_path.h"
15#include "base/gtest_prod_util.h"
Joel Hockeyc7649cc02019-03-08 00:38:4216#include "base/memory/weak_ptr.h"
Joel Hockeyc7649cc02019-03-08 00:38:4217#include "base/time/time.h"
Bo Majewski32011912023-11-07 10:04:1718#include "chrome/browser/ash/fileapi/file_accumulator.h"
Yeunjoo Choief8a4a42022-11-08 04:21:0519#include "chrome/browser/ash/fileapi/recent_file.h"
20#include "chrome/browser/ash/fileapi/recent_model.h"
21#include "chrome/browser/ash/fileapi/recent_source.h"
DongJun Kimfebb3c22019-10-21 02:08:0622#include "storage/browser/file_system/file_system_operation.h"
Joel Hockeyc7649cc02019-03-08 00:38:4223
Yeunjoo Choi3d9ed38a2022-11-10 02:51:2424namespace ash {
Joel Hockeyc7649cc02019-03-08 00:38:4225
26// RecentSource implementation for local disks.
27// Used for Downloads and fuse-based Crostini.
28//
29// All member functions must be called on the UI thread.
30class RecentDiskSource : public RecentSource {
31 public:
Bo Majewskia50b6572023-11-15 00:03:2632 // Create a RecentDiskSource for the volume registered to `mount_point_name`.
33 // Does nothing if no volume is registered at `mount_point_name`.
34 // If `ignore_dotfiles` is true, recents will ignore directories and files
35 // starting with a dot. Set `max_depth` to zero for unlimited depth.
Bo Majewski6f33bc82024-05-28 02:24:0036 RecentDiskSource(
37 extensions::api::file_manager_private::VolumeType volume_type,
38 std::string mount_point_name,
39 bool ignore_dotfiles,
40 int max_depth,
41 std::string uma_histogram_name);
Peter Boström53c6c5952021-09-17 09:41:2642
43 RecentDiskSource(const RecentDiskSource&) = delete;
44 RecentDiskSource& operator=(const RecentDiskSource&) = delete;
45
Joel Hockeyc7649cc02019-03-08 00:38:4246 ~RecentDiskSource() override;
47
48 // RecentSource overrides:
Bo Majewski3e5a6bd2024-02-29 03:06:2349 void GetRecentFiles(const Params& params,
50 GetRecentFilesCallback callback) override;
Joel Hockeyc7649cc02019-03-08 00:38:4251
Bo Majewski65e328462024-02-12 06:15:0452 // Stops the recent files search. Returns any partial results already
53 // collected.
54 std::vector<RecentFile> Stop(const int32_t call_id) override;
55
Bo Majewski965f1c72023-02-06 01:51:4456 // Helper function that determines a match between file type inferred from the
57 // path and the desired file_type.
58 static bool MatchesFileType(const base::FilePath& path,
59 RecentSource::FileType file_type);
60
Joel Hockeyc7649cc02019-03-08 00:38:4261 private:
62 FRIEND_TEST_ALL_PREFIXES(RecentDiskSourceTest, GetRecentFiles_UmaStats);
63
64 static const char kLoadHistogramName[];
65
Bo Majewski3e5a6bd2024-02-29 03:06:2366 void ScanDirectory(const int32_t call_id,
Bo Majewskib5b18802023-11-22 06:04:5367 const base::FilePath& path,
68 int depth);
Bo Majewski3e5a6bd2024-02-29 03:06:2369 void OnReadDirectory(const int32_t call_id,
Bo Majewskib5b18802023-11-22 06:04:5370 const base::FilePath& path,
Joel Hockeyc7649cc02019-03-08 00:38:4271 int depth,
72 base::File::Error result,
73 storage::FileSystemOperation::FileEntryList entries,
74 bool has_more);
Bo Majewski3e5a6bd2024-02-29 03:06:2375 void OnGotMetadata(const int32_t call_id,
Bo Majewskib5b18802023-11-22 06:04:5376 const storage::FileSystemURL& url,
Joel Hockeyc7649cc02019-03-08 00:38:4277 base::File::Error result,
78 const base::File::Info& info);
Bo Majewski3e5a6bd2024-02-29 03:06:2379 void OnReadOrStatFinished(int32_t call_id);
Joel Hockeyc7649cc02019-03-08 00:38:4280
Bo Majewskib5b18802023-11-22 06:04:5381 storage::FileSystemURL BuildDiskURL(const Params& params,
82 const base::FilePath& path) const;
Joel Hockeyc7649cc02019-03-08 00:38:4283
84 const std::string mount_point_name_;
85 const bool ignore_dotfiles_;
86 const int max_depth_;
87 const std::string uma_histogram_name_;
88
Bo Majewski65e328462024-02-12 06:15:0489 // CallContext gather information for a single GetRecentFiles call. As
90 // GetRecentFiles call can take time, and some data is collected on IO thread,
91 // we cannot guarantee that two calls will not overlap. To solve this each
92 // call receives a unique call_id and its context is stored in the map. As the
93 // map is only accessed on the UI thread we do not need to use additional
94 // locks to guarantee its consistency.
95 struct CallContext {
Bo Majewski3fbd24402024-05-16 01:06:1496 CallContext(const Params& params, GetRecentFilesCallback callback);
Bo Majewski65e328462024-02-12 06:15:0497 // Move constructor; necessary as callback is a move-only type.
98 CallContext(CallContext&& context);
99
100 ~CallContext();
101
Bo Majewski3e5a6bd2024-02-29 03:06:23102 // The parameters of the GetRecentFiles call.
103 const Params params;
104
Bo Majewski65e328462024-02-12 06:15:04105 // The callback called when the files and their metadata is ready.
106 GetRecentFilesCallback callback;
107 // Time when the build started.
108 base::TimeTicks build_start_time;
109 // Number of ReadDirectory() calls in flight.
110 int inflight_readdirs = 0;
111 // Number of GetMetadata() calls in flight.
112 int inflight_stats = 0;
113 // Most recently modified files.
114 FileAccumulator accumulator;
115 };
116
117 // A map from call_id to the context of the call.
118 base::IDMap<std::unique_ptr<CallContext>> context_map_;
119
Jeremy Roman47d432e2019-08-20 14:24:00120 base::WeakPtrFactory<RecentDiskSource> weak_ptr_factory_{this};
Joel Hockeyc7649cc02019-03-08 00:38:42121};
122
Yeunjoo Choi3d9ed38a2022-11-10 02:51:24123} // namespace ash
Joel Hockeyc7649cc02019-03-08 00:38:42124
Yeunjoo Choief8a4a42022-11-08 04:21:05125#endif // CHROME_BROWSER_ASH_FILEAPI_RECENT_DISK_SOURCE_H_