Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Yeunjoo Choi | ef8a4a4 | 2022-11-08 04:21:05 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_ |
| 6 | #define CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_ |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Avi Drissman | 02e49e58 | 2023-01-07 01:23:18 | [diff] [blame] | 11 | #include "base/functional/callback_forward.h" |
Bo Majewski | d5190897 | 2023-10-30 04:40:01 | [diff] [blame] | 12 | #include "base/i18n/string_search.h" |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/time/time.h" |
Bo Majewski | 6f33bc8 | 2024-05-28 02:24:00 | [diff] [blame] | 15 | #include "chrome/common/extensions/api/file_manager_private.h" |
DongJun Kim | febb3c2 | 2019-10-21 02:08:06 | [diff] [blame] | 16 | #include "storage/browser/file_system/file_system_context.h" |
| 17 | #include "storage/browser/file_system/file_system_url.h" |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 18 | #include "url/gurl.h" |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 19 | |
Yeunjoo Choi | 3d9ed38a | 2022-11-10 02:51:24 | [diff] [blame] | 20 | namespace ash { |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 21 | |
Shuhei Takahashi | 257ee53 | 2017-08-21 09:06:22 | [diff] [blame] | 22 | class RecentFile; |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 23 | |
| 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 Takahashi | 2436d23c1 | 2017-08-18 05:20:14 | [diff] [blame] | 30 | // Note: If a source implementation depends on KeyedServices, remember to add |
| 31 | // dependencies in RecentModelFactory. |
| 32 | // |
Shuhei Takahashi | 3291693 | 2017-08-09 08:35:16 | [diff] [blame] | 33 | // All member functions must be called on the UI thread. |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 34 | class RecentSource { |
| 35 | public: |
Bo Majewski | a50b657 | 2023-11-15 00:03:26 | [diff] [blame] | 36 | typedef base::OnceCallback<void(std::vector<RecentFile> files)> |
| 37 | GetRecentFilesCallback; |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 38 | |
Naoki Fukino | de99484 | 2020-01-09 04:01:41 | [diff] [blame] | 39 | // File types to filter the results of GetRecentFiles(). |
| 40 | enum class FileType { |
| 41 | kAll, |
| 42 | kAudio, |
Wenbo Jie | a78b844 | 2022-04-07 08:00:57 | [diff] [blame] | 43 | kDocument, |
Naoki Fukino | de99484 | 2020-01-09 04:01:41 | [diff] [blame] | 44 | kImage, |
| 45 | kVideo, |
| 46 | }; |
| 47 | |
Bo Majewski | b5b1880 | 2023-11-22 06:04:53 | [diff] [blame] | 48 | // Parameters passed to GetRecentFiles(). May be copied. |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 49 | class Params { |
| 50 | public: |
| 51 | Params(storage::FileSystemContext* file_system_context, |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 52 | int32_t call_id, |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 53 | const GURL& origin, |
Bo Majewski | d5190897 | 2023-10-30 04:40:01 | [diff] [blame] | 54 | const std::string& query, |
Bo Majewski | 3fbd2440 | 2024-05-16 01:06:14 | [diff] [blame] | 55 | const size_t max_files, |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 56 | const base::Time& cutoff_time, |
Bo Majewski | 27fde47 | 2023-09-06 05:16:55 | [diff] [blame] | 57 | const base::TimeTicks& end_time, |
Bo Majewski | b5b1880 | 2023-11-22 06:04:53 | [diff] [blame] | 58 | FileType file_type); |
| 59 | Params(const Params& params); |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 60 | |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 61 | ~Params(); |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 62 | |
| 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 Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 68 | int32_t call_id() const { return call_id_; } |
| 69 | |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 70 | // Origin of external file system URLs. |
| 71 | // E.g. "chrome-extension://<extension-ID>/" |
| 72 | const GURL& origin() const { return origin_; } |
| 73 | |
Bo Majewski | d5190897 | 2023-10-30 04:40:01 | [diff] [blame] | 74 | // 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 Majewski | 3fbd2440 | 2024-05-16 01:06:14 | [diff] [blame] | 78 | // The maximum number of files to be returned by this source. |
| 79 | size_t max_files() const { return max_files_; } |
| 80 | |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 81 | // 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 Fukino | de99484 | 2020-01-09 04:01:41 | [diff] [blame] | 86 | // 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 Majewski | 27fde47 | 2023-09-06 05:16:55 | [diff] [blame] | 90 | // 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 Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 99 | private: |
| 100 | scoped_refptr<storage::FileSystemContext> file_system_context_; |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 101 | const int32_t call_id_; |
Bo Majewski | b5b1880 | 2023-11-22 06:04:53 | [diff] [blame] | 102 | const GURL origin_; |
| 103 | const std::string query_; |
Bo Majewski | 3fbd2440 | 2024-05-16 01:06:14 | [diff] [blame] | 104 | const size_t max_files_; |
Bo Majewski | b5b1880 | 2023-11-22 06:04:53 | [diff] [blame] | 105 | const base::Time cutoff_time_; |
| 106 | const FileType file_type_; |
Bo Majewski | 27fde47 | 2023-09-06 05:16:55 | [diff] [blame] | 107 | const base::TimeTicks end_time_; |
Shuhei Takahashi | f6d8747 | 2017-08-22 07:07:04 | [diff] [blame] | 108 | }; |
| 109 | |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 110 | virtual ~RecentSource(); |
| 111 | |
| 112 | // Retrieves a list of recent files from this source. |
| 113 | // |
Shuhei Takahashi | 2436d23c1 | 2017-08-18 05:20:14 | [diff] [blame] | 114 | // You can assume that, once this function is called, it is not called again |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 115 | // until the callback is invoked. This means that you can safely save internal |
| 116 | // states to compute recent files in member variables. |
Bo Majewski | 3e5a6bd | 2024-02-29 03:06:23 | [diff] [blame] | 117 | virtual void GetRecentFiles(const Params& params, |
Bo Majewski | b5b1880 | 2023-11-22 06:04:53 | [diff] [blame] | 118 | GetRecentFilesCallback callback) = 0; |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 119 | |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 120 | // 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 Majewski | 6f33bc8 | 2024-05-28 02:24:00 | [diff] [blame] | 127 | // 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 Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 132 | protected: |
Bo Majewski | 6f33bc8 | 2024-05-28 02:24:00 | [diff] [blame] | 133 | // 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 Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 139 | }; |
| 140 | |
Bo Majewski | d5190897 | 2023-10-30 04:40:01 | [diff] [blame] | 141 | // 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. |
| 144 | inline 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 Choi | 3d9ed38a | 2022-11-10 02:51:24 | [diff] [blame] | 150 | } // namespace ash |
Shuhei Takahashi | 975f86a4 | 2017-08-04 08:54:58 | [diff] [blame] | 151 | |
Yeunjoo Choi | ef8a4a4 | 2022-11-08 04:21:05 | [diff] [blame] | 152 | #endif // CHROME_BROWSER_ASH_FILEAPI_RECENT_SOURCE_H_ |