Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [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_DRIVE_SOURCE_H_ |
| 6 | #define CHROME_BROWSER_ASH_FILEAPI_RECENT_DRIVE_SOURCE_H_ |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Mohamed Mansour | 5cf9214 | 2023-12-08 23:12:12 | [diff] [blame] | 9 | #include <optional> |
Shuhei Takahashi | 257ee53 | 2017-08-21 09:06:22 | [diff] [blame] | 10 | #include <vector> |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 11 | |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 12 | #include "base/containers/id_map.h" |
Shuhei Takahashi | 257ee53 | 2017-08-21 09:06:22 | [diff] [blame] | 13 | #include "base/files/file.h" |
Arthur Sonzogni | 3939693 | 2023-04-24 09:41:33 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Shuhei Takahashi | 1b95b4cc | 2017-08-21 03:02:45 | [diff] [blame] | 16 | #include "base/time/time.h" |
Yeunjoo Choi | ef8a4a4 | 2022-11-08 04:21:05 | [diff] [blame] | 17 | #include "chrome/browser/ash/fileapi/recent_source.h" |
Henrique Ferreiro | 19781296 | 2022-08-22 23:51:10 | [diff] [blame] | 18 | #include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h" |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 19 | #include "components/drive/file_errors.h" |
Henrique Ferreiro | 3279d13 | 2019-10-15 12:29:57 | [diff] [blame] | 20 | #include "mojo/public/cpp/bindings/remote.h" |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 21 | |
| 22 | class Profile; |
| 23 | |
Yeunjoo Choi | 3d9ed38a | 2022-11-10 02:51:24 | [diff] [blame] | 24 | namespace ash { |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 25 | |
Shuhei Takahashi | 257ee53 | 2017-08-21 09:06:22 | [diff] [blame] | 26 | class RecentFile; |
| 27 | |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 28 | // RecentSource implementation for Google Drive files. |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 29 | // |
| 30 | // All member functions must be called on the UI thread. |
Shuhei Takahashi | 1b95b4cc | 2017-08-21 03:02:45 | [diff] [blame] | 31 | // |
| 32 | // TODO(nya): Write unit tests. |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 33 | class RecentDriveSource : public RecentSource { |
| 34 | public: |
Bo Majewski | 3fbd2440 | 2024-05-16 01:06:14 | [diff] [blame] | 35 | explicit RecentDriveSource(Profile* profile); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 36 | |
| 37 | RecentDriveSource(const RecentDriveSource&) = delete; |
| 38 | RecentDriveSource& operator=(const RecentDriveSource&) = delete; |
| 39 | |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 40 | ~RecentDriveSource() override; |
| 41 | |
| 42 | // RecentSource overrides: |
Bo Majewski | 3e5a6bd | 2024-02-29 03:06:23 | [diff] [blame] | 43 | void GetRecentFiles(const Params& params, |
| 44 | GetRecentFilesCallback callback) override; |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 45 | |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 46 | // Overrides the Stop method to implement search interruption. |
| 47 | std::vector<RecentFile> Stop(const int32_t call_id) override; |
| 48 | |
Bo Majewski | 965f1c7 | 2023-02-06 01:51:44 | [diff] [blame] | 49 | // Generates type filters based on the file_type parameter. This is done so |
| 50 | // that this code can be shared between recent files and file search. |
| 51 | static std::vector<std::string> CreateTypeFilters( |
| 52 | RecentSource::FileType file_type); |
| 53 | |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 54 | private: |
Shuhei Takahashi | 1b95b4cc | 2017-08-21 03:02:45 | [diff] [blame] | 55 | static const char kLoadHistogramName[]; |
| 56 | |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 57 | // The context for a single GetRecentFiles call. Multiple, parallel calls can |
| 58 | // be issued and each receives its unique context, stored in the map using the |
| 59 | // call_id parameter. |
| 60 | struct CallContext { |
| 61 | explicit CallContext(GetRecentFilesCallback callback); |
| 62 | // Move constructor necessary due to move-only callback type. |
| 63 | CallContext(CallContext&& callcontext); |
| 64 | ~CallContext(); |
| 65 | |
| 66 | // The callback on which the results are delivered. |
| 67 | GetRecentFilesCallback callback; |
| 68 | |
| 69 | // The time the GetRecentFiles request was started. |
| 70 | base::TimeTicks build_start_time; |
| 71 | |
| 72 | // The list of files built up by the request. |
| 73 | std::vector<RecentFile> files; |
| 74 | |
| 75 | // The mojo remote that performs Drive search. |
| 76 | mojo::Remote<drivefs::mojom::SearchQuery> search_query; |
| 77 | }; |
| 78 | |
| 79 | void OnComplete(const int32_t call_id); |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 80 | |
Sam McNally | e408858 | 2018-09-21 06:15:14 | [diff] [blame] | 81 | void GotSearchResults( |
Munira Tursunova | b67fa75 | 2024-11-01 08:49:58 | [diff] [blame] | 82 | const Params& params, |
Sam McNally | e408858 | 2018-09-21 06:15:14 | [diff] [blame] | 83 | drive::FileError error, |
Mohamed Mansour | 5cf9214 | 2023-12-08 23:12:12 | [diff] [blame] | 84 | std::optional<std::vector<drivefs::mojom::QueryItemPtr>> results); |
Sam McNally | e408858 | 2018-09-21 06:15:14 | [diff] [blame] | 85 | |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 86 | // The current profile for which this source was constructed. This class does |
| 87 | // not own the Profile object. Instead it uses it to fetch a drive integration |
| 88 | // service for fetching matched files. |
Bartek Nowierski | 8f57934 | 2024-01-08 06:45:15 | [diff] [blame] | 89 | const raw_ptr<Profile> profile_; |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 90 | |
Bo Majewski | 65e32846 | 2024-02-12 06:15:04 | [diff] [blame] | 91 | // A map from call_id to the call context. |
| 92 | base::IDMap<std::unique_ptr<CallContext>> context_map_; |
Shuhei Takahashi | 257ee53 | 2017-08-21 09:06:22 | [diff] [blame] | 93 | |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 94 | base::WeakPtrFactory<RecentDriveSource> weak_ptr_factory_{this}; |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 95 | }; |
| 96 | |
Yeunjoo Choi | 3d9ed38a | 2022-11-10 02:51:24 | [diff] [blame] | 97 | } // namespace ash |
Shuhei Takahashi | b1f9a9c | 2017-08-17 07:44:10 | [diff] [blame] | 98 | |
Yeunjoo Choi | ef8a4a4 | 2022-11-08 04:21:05 | [diff] [blame] | 99 | #endif // CHROME_BROWSER_ASH_FILEAPI_RECENT_DRIVE_SOURCE_H_ |