[AppIntegration] Move FetchAndRankHelper out of the Provider.

This is refactoring CL without any behavior changes:
1) Move the FetchAndRankHelper to a separate file, and add
   FetchAndRankHelper.java to handle the Jni call.
2) Refactor FetchAndRankHelper#OnRank() without passing a list from
   Java.
This is a preparation for adding top sides fetching code in the
AuxiliarySearchProvider.

Bug: 397457989
Change-Id: I191b206810025ee230f4568e3c59ddd53c6e87ca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6357751
Reviewed-by: Siddhartha S <[email protected]>
Commit-Queue: Xi Han <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1434302}
diff --git a/chrome/browser/auxiliary_search/fetch_and_rank_helper.h b/chrome/browser/auxiliary_search/fetch_and_rank_helper.h
new file mode 100644
index 0000000..9041f3cb
--- /dev/null
+++ b/chrome/browser/auxiliary_search/fetch_and_rank_helper.h
@@ -0,0 +1,62 @@
+// Copyright 2025 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_AUXILIARY_SEARCH_FETCH_AND_RANK_HELPER_H_
+#define CHROME_BROWSER_AUXILIARY_SEARCH_FETCH_AND_RANK_HELPER_H_
+
+#include <jni.h>
+
+#include "base/android/scoped_java_ref.h"
+#include "base/memory/raw_ptr.h"
+#include "base/memory/ref_counted.h"
+#include "components/visited_url_ranking/public/visited_url_ranking_service.h"
+
+namespace visited_url_ranking {
+class VisitedURLRankingService;
+struct URLVisitsMetadata;
+struct URLVisitAggregate;
+struct FetchOptions;
+struct Config;
+}  // namespace visited_url_ranking
+
+// Class to manage history data fetch and rank flow, containing required
+// parameters and states, and to create java objects and run the callback.
+class FetchAndRankHelper : public base::RefCounted<FetchAndRankHelper> {
+ public:
+  using FetchResultCallback = base::OnceCallback<void(
+      std::vector<jni_zero::ScopedJavaLocalRef<jobject>>)>;
+
+  // The |entries_callback| is called when history data is fetched and ranked.
+  // It passes a List<AuxiliarySearchDataEntry> to Java.
+  FetchAndRankHelper(
+      visited_url_ranking::VisitedURLRankingService* ranking_service,
+      FetchResultCallback entries_callback);
+
+  // Starts the service to fetch history data.
+  void StartFetching();
+
+ private:
+  friend class base::RefCounted<FetchAndRankHelper>;
+
+  ~FetchAndRankHelper();
+
+  // Continuing after StartFetching()'s call to FetchURLVisitAggregates().
+  void OnFetched(
+      visited_url_ranking::ResultStatus status,
+      visited_url_ranking::URLVisitsMetadata url_visits_metadata,
+      std::vector<visited_url_ranking::URLVisitAggregate> aggregates);
+
+  // Continuing after OnFetched()'s call to RankVisitAggregates().
+  void OnRanked(visited_url_ranking::URLVisitsMetadata url_visits_metadata,
+                visited_url_ranking::ResultStatus status,
+                std::vector<visited_url_ranking::URLVisitAggregate> aggregates);
+
+ private:
+  raw_ptr<visited_url_ranking::VisitedURLRankingService> ranking_service_;
+  FetchResultCallback entries_callback_;
+  const visited_url_ranking::FetchOptions fetch_options_;
+  const visited_url_ranking::Config config_;
+};
+
+#endif  // CHROME_BROWSER_AUXILIARY_SEARCH_FETCH_AND_RANK_HELPER_H_