web_data_service_base.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2012 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_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
  5. #define COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
  6. #include "base/callback.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/ref_counted_delete_on_sequence.h"
  9. #include "components/webdata/common/webdata_export.h"
  10. #include "sql/init_status.h"
  11. class WebDatabase;
  12. class WebDatabaseService;
  13. namespace base {
  14. class SingleThreadTaskRunner;
  15. }
  16. // Base for WebDataService class hierarchy.
  17. // WebDataServiceBase is destroyed on the UI sequence.
  18. class WEBDATA_EXPORT WebDataServiceBase
  19. : public base::RefCountedDeleteOnSequence<WebDataServiceBase> {
  20. public:
  21. // All requests return an opaque handle of the following type.
  22. typedef int Handle;
  23. // Users of this class may provide a callback to handle errors
  24. // (e.g. by showing a UI). The callback is called only on error, and
  25. // takes a single parameter, the sql::InitStatus value from trying
  26. // to open the database.
  27. // TODO(joi): Should we combine this with WebDatabaseService::InitCallback?
  28. using ProfileErrorCallback =
  29. base::OnceCallback<void(sql::InitStatus, const std::string&)>;
  30. // |callback| will only be invoked on error, and only if
  31. // |callback.is_null()| evaluates to false.
  32. //
  33. // The ownership of |wdbs| is shared, with the primary owner being the
  34. // WebDataServiceWrapper, and secondary owners being subclasses of
  35. // WebDataServiceBase, which receive |wdbs| upon construction. The
  36. // WebDataServiceWrapper handles the initializing and shutting down and of
  37. // the |wdbs| object.
  38. // WebDataServiceBase is destroyed on the UI sequence.
  39. WebDataServiceBase(
  40. scoped_refptr<WebDatabaseService> wdbs,
  41. const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
  42. WebDataServiceBase(const WebDataServiceBase&) = delete;
  43. WebDataServiceBase& operator=(const WebDataServiceBase&) = delete;
  44. // Cancel any pending request. You need to call this method if your
  45. // WebDataServiceConsumer is about to be deleted.
  46. virtual void CancelRequest(Handle h);
  47. // Shutdown the web data service. The service can no longer be used after this
  48. // call.
  49. virtual void ShutdownOnUISequence();
  50. // Initializes the web data service.
  51. virtual void Init(ProfileErrorCallback callback);
  52. // Unloads the database and shuts down service.
  53. void ShutdownDatabase();
  54. // Returns a pointer to the DB (used by SyncableServices). May return NULL if
  55. // the database is unavailable. Must be called on DB sequence.
  56. virtual WebDatabase* GetDatabase();
  57. protected:
  58. friend class base::RefCountedDeleteOnSequence<WebDataServiceBase>;
  59. friend class base::DeleteHelper<WebDataServiceBase>;
  60. virtual ~WebDataServiceBase();
  61. // Our database service.
  62. scoped_refptr<WebDatabaseService> wdbs_;
  63. };
  64. #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_