blob: 50179deadbff4a8bd31ffe2846715f35463ac314 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2017 The Chromium Authors
Shuhei Takahashib1f9a9c2017-08-17 07:44:102// 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_DRIVE_SOURCE_H_
6#define CHROME_BROWSER_ASH_FILEAPI_RECENT_DRIVE_SOURCE_H_
Shuhei Takahashib1f9a9c2017-08-17 07:44:107
8#include <memory>
Mohamed Mansour5cf92142023-12-08 23:12:129#include <optional>
Shuhei Takahashi257ee532017-08-21 09:06:2210#include <vector>
Shuhei Takahashib1f9a9c2017-08-17 07:44:1011
Bo Majewski65e328462024-02-12 06:15:0412#include "base/containers/id_map.h"
Shuhei Takahashi257ee532017-08-21 09:06:2213#include "base/files/file.h"
Arthur Sonzogni39396932023-04-24 09:41:3314#include "base/memory/raw_ptr.h"
Shuhei Takahashib1f9a9c2017-08-17 07:44:1015#include "base/memory/weak_ptr.h"
Shuhei Takahashi1b95b4cc2017-08-21 03:02:4516#include "base/time/time.h"
Yeunjoo Choief8a4a42022-11-08 04:21:0517#include "chrome/browser/ash/fileapi/recent_source.h"
Henrique Ferreiro197812962022-08-22 23:51:1018#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"
Shuhei Takahashib1f9a9c2017-08-17 07:44:1019#include "components/drive/file_errors.h"
Henrique Ferreiro3279d132019-10-15 12:29:5720#include "mojo/public/cpp/bindings/remote.h"
Shuhei Takahashib1f9a9c2017-08-17 07:44:1021
22class Profile;
23
Yeunjoo Choi3d9ed38a2022-11-10 02:51:2424namespace ash {
Shuhei Takahashib1f9a9c2017-08-17 07:44:1025
Shuhei Takahashi257ee532017-08-21 09:06:2226class RecentFile;
27
Bo Majewski65e328462024-02-12 06:15:0428// RecentSource implementation for Google Drive files.
Shuhei Takahashib1f9a9c2017-08-17 07:44:1029//
30// All member functions must be called on the UI thread.
Shuhei Takahashi1b95b4cc2017-08-21 03:02:4531//
32// TODO(nya): Write unit tests.
Shuhei Takahashib1f9a9c2017-08-17 07:44:1033class RecentDriveSource : public RecentSource {
34 public:
Bo Majewski3fbd24402024-05-16 01:06:1435 explicit RecentDriveSource(Profile* profile);
Peter Boström53c6c5952021-09-17 09:41:2636
37 RecentDriveSource(const RecentDriveSource&) = delete;
38 RecentDriveSource& operator=(const RecentDriveSource&) = delete;
39
Shuhei Takahashib1f9a9c2017-08-17 07:44:1040 ~RecentDriveSource() override;
41
42 // RecentSource overrides:
Bo Majewski3e5a6bd2024-02-29 03:06:2343 void GetRecentFiles(const Params& params,
44 GetRecentFilesCallback callback) override;
Shuhei Takahashib1f9a9c2017-08-17 07:44:1045
Bo Majewski65e328462024-02-12 06:15:0446 // Overrides the Stop method to implement search interruption.
47 std::vector<RecentFile> Stop(const int32_t call_id) override;
48
Bo Majewski965f1c72023-02-06 01:51:4449 // 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 Takahashib1f9a9c2017-08-17 07:44:1054 private:
Shuhei Takahashi1b95b4cc2017-08-21 03:02:4555 static const char kLoadHistogramName[];
56
Bo Majewski65e328462024-02-12 06:15:0457 // 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 Takahashib1f9a9c2017-08-17 07:44:1080
Sam McNallye4088582018-09-21 06:15:1481 void GotSearchResults(
Munira Tursunovab67fa752024-11-01 08:49:5882 const Params& params,
Sam McNallye4088582018-09-21 06:15:1483 drive::FileError error,
Mohamed Mansour5cf92142023-12-08 23:12:1284 std::optional<std::vector<drivefs::mojom::QueryItemPtr>> results);
Sam McNallye4088582018-09-21 06:15:1485
Bo Majewski65e328462024-02-12 06:15:0486 // 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 Nowierski8f579342024-01-08 06:45:1589 const raw_ptr<Profile> profile_;
Shuhei Takahashib1f9a9c2017-08-17 07:44:1090
Bo Majewski65e328462024-02-12 06:15:0491 // A map from call_id to the call context.
92 base::IDMap<std::unique_ptr<CallContext>> context_map_;
Shuhei Takahashi257ee532017-08-21 09:06:2293
Jeremy Roman47d432e2019-08-20 14:24:0094 base::WeakPtrFactory<RecentDriveSource> weak_ptr_factory_{this};
Shuhei Takahashib1f9a9c2017-08-17 07:44:1095};
96
Yeunjoo Choi3d9ed38a2022-11-10 02:51:2497} // namespace ash
Shuhei Takahashib1f9a9c2017-08-17 07:44:1098
Yeunjoo Choief8a4a42022-11-08 04:21:0599#endif // CHROME_BROWSER_ASH_FILEAPI_RECENT_DRIVE_SOURCE_H_