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, |
| 34 | FetchResultCallback entries_callback); |
| 35 | |
| 36 | // Starts the service to fetch history data. |
| 37 | void StartFetching(); |
| 38 | |
| 39 | private: |
| 40 | friend class base::RefCounted<FetchAndRankHelper>; |
| 41 | |
| 42 | ~FetchAndRankHelper(); |
| 43 | |
| 44 | // Continuing after StartFetching()'s call to FetchURLVisitAggregates(). |
| 45 | void OnFetched( |
| 46 | visited_url_ranking::ResultStatus status, |
| 47 | visited_url_ranking::URLVisitsMetadata url_visits_metadata, |
| 48 | std::vector<visited_url_ranking::URLVisitAggregate> aggregates); |
| 49 | |
| 50 | // Continuing after OnFetched()'s call to RankVisitAggregates(). |
| 51 | void OnRanked(visited_url_ranking::URLVisitsMetadata url_visits_metadata, |
| 52 | visited_url_ranking::ResultStatus status, |
| 53 | std::vector<visited_url_ranking::URLVisitAggregate> aggregates); |
| 54 | |
| 55 | private: |
| 56 | raw_ptr<visited_url_ranking::VisitedURLRankingService> ranking_service_; |
| 57 | FetchResultCallback entries_callback_; |
| 58 | const visited_url_ranking::FetchOptions fetch_options_; |
| 59 | const visited_url_ranking::Config config_; |
| 60 | }; |
| 61 | |
| 62 | #endif // CHROME_BROWSER_AUXILIARY_SEARCH_FETCH_AND_RANK_HELPER_H_ |