sql_callback_task.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_OFFLINE_PAGES_TASK_SQL_CALLBACK_TASK_H_
  5. #define COMPONENTS_OFFLINE_PAGES_TASK_SQL_CALLBACK_TASK_H_
  6. #include "base/callback.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "components/offline_pages/task/task.h"
  10. namespace sql {
  11. class Database;
  12. }
  13. namespace offline_pages {
  14. class SqlStoreBase;
  15. // A simple task that calls store->Execute() with the provided |exec_callback|
  16. // and completes. |done_callback|, if provided, is called with the result. This
  17. // class can be used if there are no UI thread actions that need done.
  18. class SqlCallbackTask : public Task {
  19. public:
  20. typedef base::OnceCallback<bool(sql::Database* db)> ExecuteCallback;
  21. typedef base::OnceCallback<void(bool)> DoneCallback;
  22. SqlCallbackTask(SqlStoreBase* store,
  23. ExecuteCallback exec_callback,
  24. DoneCallback done_callback = {});
  25. ~SqlCallbackTask() override;
  26. void Run() override;
  27. private:
  28. base::WeakPtr<SqlCallbackTask> GetWeakPtr() {
  29. return weak_ptr_factory_.GetWeakPtr();
  30. }
  31. void Done(bool result);
  32. raw_ptr<SqlStoreBase> store_;
  33. ExecuteCallback exec_callback_;
  34. DoneCallback done_callback_;
  35. base::WeakPtrFactory<SqlCallbackTask> weak_ptr_factory_{this};
  36. };
  37. } // namespace offline_pages
  38. #endif // COMPONENTS_OFFLINE_PAGES_TASK_SQL_CALLBACK_TASK_H_