Xi Han | 04499eb | 2025-03-18 18:44:23 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_AUXILIARY_SEARCH_FETCH_AND_RANK_HELPER_H_ |
| 6 | #define CHROME_BROWSER_AUXILIARY_SEARCH_FETCH_AND_RANK_HELPER_H_ |
| 7 | |
| 8 | #include <jni.h> |
| 9 | |
| 10 | #include "base/android/scoped_java_ref.h" |
| 11 | #include "base/memory/raw_ptr.h" |
| 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "components/visited_url_ranking/public/visited_url_ranking_service.h" |
| 14 | |
| 15 | namespace visited_url_ranking { |
| 16 | class VisitedURLRankingService; |
| 17 | struct URLVisitsMetadata; |
| 18 | struct URLVisitAggregate; |
| 19 | struct FetchOptions; |
| 20 | struct Config; |
| 21 | } // namespace visited_url_ranking |
| 22 | |
| 23 | // Class to manage history data fetch and rank flow, containing required |
| 24 | // parameters and states, and to create java objects and run the callback. |
| 25 | class FetchAndRankHelper : public base::RefCounted<FetchAndRankHelper> { |
| 26 | public: |
| 27 | using FetchResultCallback = base::OnceCallback<void( |
| 28 | std::vector<jni_zero::ScopedJavaLocalRef<jobject>>)>; |
| 29 | |
| 30 | // The |entries_callback| is called when history data is fetched and ranked. |
| 31 | // It passes a List<AuxiliarySearchDataEntry> to Java. |
| 32 | FetchAndRankHelper( |
| 33 | visited_url_ranking::VisitedURLRankingService* ranking_service, |
Xi Han | a459af7 | 2025-05-12 18:38:48 | [diff] [blame] | 34 | FetchResultCallback entries_callback, |
Xi Han | 304c80a | 2025-05-16 22:16:11 | [diff] [blame] | 35 | const std::optional<GURL>& custom_tab_url = std::nullopt, |
Xi Han | a459af7 | 2025-05-12 18:38:48 | [diff] [blame] | 36 | std::optional<base::Time> begin_time = std::nullopt); |
Xi Han | 04499eb | 2025-03-18 18:44:23 | [diff] [blame] | 37 | |
| 38 | // Starts the service to fetch history data. |
| 39 | void StartFetching(); |
| 40 | |
| 41 | private: |
| 42 | friend class base::RefCounted<FetchAndRankHelper>; |
| 43 | |
| 44 | ~FetchAndRankHelper(); |
| 45 | |
| 46 | // Continuing after StartFetching()'s call to FetchURLVisitAggregates(). |
| 47 | void OnFetched( |
| 48 | visited_url_ranking::ResultStatus status, |
| 49 | visited_url_ranking::URLVisitsMetadata url_visits_metadata, |
| 50 | std::vector<visited_url_ranking::URLVisitAggregate> aggregates); |
| 51 | |
| 52 | // Continuing after OnFetched()'s call to RankVisitAggregates(). |
| 53 | void OnRanked(visited_url_ranking::URLVisitsMetadata url_visits_metadata, |
| 54 | visited_url_ranking::ResultStatus status, |
| 55 | std::vector<visited_url_ranking::URLVisitAggregate> aggregates); |
| 56 | |
| 57 | private: |
| 58 | raw_ptr<visited_url_ranking::VisitedURLRankingService> ranking_service_; |
| 59 | FetchResultCallback entries_callback_; |
Xi Han | 304c80a | 2025-05-16 22:16:11 | [diff] [blame] | 60 | std::optional<GURL> custom_tab_url_; |
Xi Han | 04499eb | 2025-03-18 18:44:23 | [diff] [blame] | 61 | const visited_url_ranking::FetchOptions fetch_options_; |
| 62 | const visited_url_ranking::Config config_; |
| 63 | }; |
| 64 | |
| 65 | #endif // CHROME_BROWSER_AUXILIARY_SEARCH_FETCH_AND_RANK_HELPER_H_ |