blob: decff705941dca8834f37c3fd65e7078f9f0a7b9 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2017 The Chromium Authors
Shuhei Takahashi975f86a42017-08-04 08:54:582// 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_SOURCE_H_
6#define CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_
Shuhei Takahashi975f86a42017-08-04 08:54:587
8#include <memory>
Shuhei Takahashi975f86a42017-08-04 08:54:589#include <vector>
10
Avi Drissman02e49e582023-01-07 01:23:1811#include "base/functional/callback_forward.h"
Bo Majewskid51908972023-10-30 04:40:0112#include "base/i18n/string_search.h"
Shuhei Takahashif6d87472017-08-22 07:07:0413#include "base/memory/ref_counted.h"
14#include "base/time/time.h"
Bo Majewski6f33bc82024-05-28 02:24:0015#include "chrome/common/extensions/api/file_manager_private.h"
DongJun Kimfebb3c22019-10-21 02:08:0616#include "storage/browser/file_system/file_system_context.h"
17#include "storage/browser/file_system/file_system_url.h"
Shuhei Takahashif6d87472017-08-22 07:07:0418#include "url/gurl.h"
Shuhei Takahashi975f86a42017-08-04 08:54:5819
Yeunjoo Choi3d9ed38a2022-11-10 02:51:2420namespace ash {
Shuhei Takahashi975f86a42017-08-04 08:54:5821
Shuhei Takahashi257ee532017-08-21 09:06:2222class RecentFile;
Shuhei Takahashi975f86a42017-08-04 08:54:5823
24// Interface class for a source of recent files.
25//
26// Recent file system retrieves recent files from several sources such as
27// local directories and cloud storages. To provide files to Recent file
28// system, this interface should be implemented for each source.
29//
Shuhei Takahashi2436d23c12017-08-18 05:20:1430// Note: If a source implementation depends on KeyedServices, remember to add
31// dependencies in RecentModelFactory.
32//
Shuhei Takahashi32916932017-08-09 08:35:1633// All member functions must be called on the UI thread.
Shuhei Takahashi975f86a42017-08-04 08:54:5834class RecentSource {
35 public:
Bo Majewskia50b6572023-11-15 00:03:2636 typedef base::OnceCallback<void(std::vector<RecentFile> files)>
37 GetRecentFilesCallback;
Shuhei Takahashi975f86a42017-08-04 08:54:5838
Naoki Fukinode994842020-01-09 04:01:4139 // File types to filter the results of GetRecentFiles().
40 enum class FileType {
41 kAll,
42 kAudio,
Wenbo Jiea78b8442022-04-07 08:00:5743 kDocument,
Naoki Fukinode994842020-01-09 04:01:4144 kImage,
45 kVideo,
46 };
47
Bo Majewskib5b18802023-11-22 06:04:5348 // Parameters passed to GetRecentFiles(). May be copied.
Shuhei Takahashif6d87472017-08-22 07:07:0449 class Params {
50 public:
51 Params(storage::FileSystemContext* file_system_context,
Bo Majewski65e328462024-02-12 06:15:0452 int32_t call_id,
Shuhei Takahashif6d87472017-08-22 07:07:0453 const GURL& origin,
Bo Majewskid51908972023-10-30 04:40:0154 const std::string& query,
Bo Majewski3fbd24402024-05-16 01:06:1455 const size_t max_files,
Shuhei Takahashif6d87472017-08-22 07:07:0456 const base::Time& cutoff_time,
Bo Majewski27fde472023-09-06 05:16:5557 const base::TimeTicks& end_time,
Bo Majewskib5b18802023-11-22 06:04:5358 FileType file_type);
59 Params(const Params& params);
Shuhei Takahashif6d87472017-08-22 07:07:0460
Shuhei Takahashif6d87472017-08-22 07:07:0461 ~Params();
Shuhei Takahashif6d87472017-08-22 07:07:0462
63 // FileSystemContext that can be used for file system operations.
64 storage::FileSystemContext* file_system_context() const {
65 return file_system_context_.get();
66 }
67
Bo Majewski65e328462024-02-12 06:15:0468 int32_t call_id() const { return call_id_; }
69
Shuhei Takahashif6d87472017-08-22 07:07:0470 // Origin of external file system URLs.
71 // E.g. "chrome-extension://<extension-ID>/"
72 const GURL& origin() const { return origin_; }
73
Bo Majewskid51908972023-10-30 04:40:0174 // The query to be applied to recent files to further narrow the returned
75 // matches.
76 const std::string& query() const { return query_; }
77
Bo Majewski3fbd24402024-05-16 01:06:1478 // The maximum number of files to be returned by this source.
79 size_t max_files() const { return max_files_; }
80
Shuhei Takahashif6d87472017-08-22 07:07:0481 // Cut-off last modified time. RecentSource is expected to return files
82 // modified at this time or later. It is fine to return older files than
83 // requested here, but they will be filtered out by RecentModel.
84 const base::Time& cutoff_time() const { return cutoff_time_; }
85
Naoki Fukinode994842020-01-09 04:01:4186 // File type to filter the results from RecentSource. RecentSource is
87 // expected to return files which matches the specified file type.
88 FileType file_type() const { return file_type_; }
89
Bo Majewski27fde472023-09-06 05:16:5590 // Returns the time by which recent scan operation should terminate (with or
91 // without results).
92 base::TimeTicks end_time() const { return end_time_; }
93
94 // If maximum duration was set, this method checks if a recent source is
95 // late with delivery of recent files or still on schedule. If maximum
96 // duration was never set, this method always returns false.
97 bool IsLate() const;
98
Shuhei Takahashif6d87472017-08-22 07:07:0499 private:
100 scoped_refptr<storage::FileSystemContext> file_system_context_;
Bo Majewski65e328462024-02-12 06:15:04101 const int32_t call_id_;
Bo Majewskib5b18802023-11-22 06:04:53102 const GURL origin_;
103 const std::string query_;
Bo Majewski3fbd24402024-05-16 01:06:14104 const size_t max_files_;
Bo Majewskib5b18802023-11-22 06:04:53105 const base::Time cutoff_time_;
106 const FileType file_type_;
Bo Majewski27fde472023-09-06 05:16:55107 const base::TimeTicks end_time_;
Shuhei Takahashif6d87472017-08-22 07:07:04108 };
109
Shuhei Takahashi975f86a42017-08-04 08:54:58110 virtual ~RecentSource();
111
112 // Retrieves a list of recent files from this source.
113 //
Shuhei Takahashi2436d23c12017-08-18 05:20:14114 // You can assume that, once this function is called, it is not called again
Shuhei Takahashi975f86a42017-08-04 08:54:58115 // until the callback is invoked. This means that you can safely save internal
116 // states to compute recent files in member variables.
Bo Majewski3e5a6bd2024-02-29 03:06:23117 virtual void GetRecentFiles(const Params& params,
Bo Majewskib5b18802023-11-22 06:04:53118 GetRecentFilesCallback callback) = 0;
Shuhei Takahashi975f86a42017-08-04 08:54:58119
Bo Majewski65e328462024-02-12 06:15:04120 // Called by the RecentModel if it wants to interrupt search for recent files.
121 // The recent source may return whatever recent files it has collected so far
122 // as the response to this call. If the Stop method is called, the callback
123 // passed to GetRecentFiles is NEVER called. The `call_id` corresponds to one
124 // of the `call_id` passed in the `params` of the GetRecentFiles` method.
125 virtual std::vector<RecentFile> Stop(const int32_t call_id) = 0;
126
Bo Majewski6f33bc82024-05-28 02:24:00127 // Returns the volume type that is serviced by this recent source.
128 extensions::api::file_manager_private::VolumeType volume_type() const {
129 return volume_type_;
130 }
131
Shuhei Takahashi975f86a42017-08-04 08:54:58132 protected:
Bo Majewski6f33bc82024-05-28 02:24:00133 // Creates a new recent source that handles a volume of the given type.
134 explicit RecentSource(
135 extensions::api::file_manager_private::VolumeType volume_type);
136
137 private:
138 extensions::api::file_manager_private::VolumeType volume_type_;
Shuhei Takahashi975f86a42017-08-04 08:54:58139};
140
Bo Majewskid51908972023-10-30 04:40:01141// A common to all recent sources function for checking a file name against the
142// query. This function returns true if the query is contained in the given file
143// name. This function does case-insensitive, accent-insensitive comparison.
144inline bool FileNameMatches(const std::u16string& file_name,
145 const std::u16string& query) {
146 return query.empty() || base::i18n::StringSearchIgnoringCaseAndAccents(
147 query, file_name, nullptr, nullptr);
148}
149
Yeunjoo Choi3d9ed38a2022-11-10 02:51:24150} // namespace ash
Shuhei Takahashi975f86a42017-08-04 08:54:58151
Yeunjoo Choief8a4a42022-11-08 04:21:05152#endif // CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_