#ifndef CHROME_BROWSER_ASH_API_TASKS_TASKS_CLIENT_IMPL_H_
#define CHROME_BROWSER_ASH_API_TASKS_TASKS_CLIENT_IMPL_H_
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "ash/api/tasks/tasks_client.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/types/expected.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "components/signin/public/base/oauth_consumer_id.h"
#include "google_apis/tasks/tasks_api_requests.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "ui/base/models/list_model.h"
class PolicyBlocklistService;
class PrefService;
namespace base {
class Time;
}
namespace google_apis {
class RequestSender;
namespace tasks {
class TaskLists;
class Tasks;
}
}
namespace ash::api {
struct Task;
struct TaskList;
class TasksClientImpl : public TasksClient {
public:
using CreateRequestSenderCallback =
base::RepeatingCallback<std::unique_ptr<google_apis::RequestSender>(
signin::OAuthConsumerId oauth_consumer_id,
const net::NetworkTrafficAnnotationTag& traffic_annotation_tag)>;
TasksClientImpl(
PrefService* pref_service,
apps::AppServiceProxy* app_service_proxy,
PolicyBlocklistService* policy_blocklist_service,
const CreateRequestSenderCallback& create_request_sender_callback,
net::NetworkTrafficAnnotationTag traffic_annotation_tag);
TasksClientImpl(const TasksClientImpl&) = delete;
TasksClientImpl& operator=(const TasksClientImpl&) = delete;
~TasksClientImpl() override;
bool IsDisabledByAdmin() const override;
const ui::ListModel<api::TaskList>* GetCachedTaskLists() override;
void GetTaskLists(bool force_fetch,
TasksClient::GetTaskListsCallback callback) override;
const ui::ListModel<api::Task>* GetCachedTasksInTaskList(
const std::string& task_list_id) override;
void GetTasks(const std::string& task_list_id,
bool force_fetch,
TasksClient::GetTasksCallback callback) override;
void MarkAsCompleted(const std::string& task_list_id,
const std::string& task_id,
bool completed) override;
void AddTask(const std::string& task_list_id,
const std::string& title,
TasksClient::OnTaskSavedCallback callback) override;
void UpdateTask(const std::string& task_list_id,
const std::string& task_id,
const std::string& title,
bool completed,
TasksClient::OnTaskSavedCallback callback) override;
void InvalidateCache() override;
std::optional<base::Time> GetTasksLastUpdateTime(
const std::string& task_list_id) const override;
void OnGlanceablesBubbleClosed(base::OnceClosure callback) override;
using TaskListsRequestCallback =
base::RepeatingCallback<void(const std::string& page_token)>;
void set_task_lists_request_callback_for_testing(
const TaskListsRequestCallback& callback) {
task_lists_request_callback_ = callback;
}
using TasksRequestCallback =
base::RepeatingCallback<void(const std::string& task_list_id,
const std::string& page_token)>;
void set_tasks_request_callback_for_testing(
const TasksRequestCallback& callback) {
tasks_request_callback_ = callback;
}
private:
enum class FetchStatus { kNotFresh, kRefreshing, kFresh };
struct TaskListsFetchState {
TaskListsFetchState();
~TaskListsFetchState();
FetchStatus status = FetchStatus::kNotFresh;
std::vector<TasksClient::GetTaskListsCallback> callbacks;
};
struct TasksFetchState {
TasksFetchState();
~TasksFetchState();
FetchStatus status = FetchStatus::kNotFresh;
std::optional<base::Time> last_updated_time = std::nullopt;
std::vector<TasksClient::GetTasksCallback> callbacks;
};
void FetchTaskListsPage(
const std::string& page_token,
int page_number,
std::vector<std::unique_ptr<google_apis::tasks::TaskList>>
accumulated_raw_task_lists);
void OnTaskListsPageFetched(
const base::Time& request_start_time,
int page_number,
std::vector<std::unique_ptr<google_apis::tasks::TaskList>>
accumulated_raw_task_lists,
base::expected<std::unique_ptr<google_apis::tasks::TaskLists>,
google_apis::ApiErrorCode> result);
void FetchTasksPage(const std::string& task_list_id,
const std::string& page_token,
int page_number,
std::vector<std::unique_ptr<google_apis::tasks::Task>>
accumulated_raw_tasks);
void OnTasksPageFetched(
const std::string& task_list_id,
std::vector<std::unique_ptr<google_apis::tasks::Task>>
accumulated_raw_tasks,
const base::Time& request_start_time,
int page_number,
base::expected<std::unique_ptr<google_apis::tasks::Tasks>,
google_apis::ApiErrorCode> result);
void OnMarkedAsCompleted(
const base::Time& request_start_time,
base::RepeatingClosure on_done,
base::expected<std::unique_ptr<google_apis::tasks::Task>,
google_apis::ApiErrorCode> result);
void OnTaskAdded(const std::string& task_list_id,
const base::Time& request_start_time,
TasksClient::OnTaskSavedCallback callback,
base::expected<std::unique_ptr<google_apis::tasks::Task>,
google_apis::ApiErrorCode> result);
void OnTaskUpdated(const std::string& task_list_id,
const base::Time& request_start_time,
TasksClient::OnTaskSavedCallback callback,
base::expected<std::unique_ptr<google_apis::tasks::Task>,
google_apis::ApiErrorCode> result);
void RunGetTaskListsCallbacks(
FetchStatus final_fetch_status,
std::optional<google_apis::ApiErrorCode> http_error,
std::vector<std::unique_ptr<google_apis::tasks::TaskList>>
accumulated_raw_task_lists);
void RunGetTasksCallbacks(
const std::string& task_list_id,
FetchStatus final_fetch_status,
std::optional<google_apis::ApiErrorCode> http_error,
std::vector<std::unique_ptr<google_apis::tasks::Task>>
accumulated_raw_tasks);
std::map<std::string, std::set<std::string>> pending_completed_tasks_;
google_apis::RequestSender* GetRequestSender();
const raw_ptr<PrefService> pref_service_;
const raw_ptr<apps::AppServiceProxy> app_service_proxy_;
const raw_ptr<PolicyBlocklistService> policy_blocklist_service_;
const CreateRequestSenderCallback create_request_sender_callback_;
std::unique_ptr<google_apis::RequestSender> request_sender_;
TaskListsFetchState task_lists_fetch_state_;
ui::ListModel<TaskList> task_lists_;
std::map<std::string, ui::ListModel<Task>> tasks_in_task_lists_;
std::map<std::string, std::unique_ptr<TasksFetchState>> tasks_fetch_state_;
ui::ListModel<Task> stub_task_list_;
TaskListsRequestCallback task_lists_request_callback_;
TasksRequestCallback tasks_request_callback_;
const net::NetworkTrafficAnnotationTag traffic_annotation_tag_;
base::WeakPtrFactory<TasksClientImpl> weak_factory_{this};
};
}
#endif