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.
5 #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
6 #define COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/ref_counted_delete_on_message_loop.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "components/webdata/common/webdata_export.h"
14 #include "sql/init_status.h"
17 class WebDatabaseService
;
18 class WebDatabaseTable
;
24 // Base for WebDataService class hierarchy.
25 // WebDataServiceBase is destroyed on the UI thread.
26 class WEBDATA_EXPORT WebDataServiceBase
27 : public base::RefCountedDeleteOnMessageLoop
<WebDataServiceBase
> {
29 // All requests return an opaque handle of the following type.
32 // Users of this class may provide a callback to handle errors
33 // (e.g. by showing a UI). The callback is called only on error, and
34 // takes a single parameter, the sql::InitStatus value from trying
35 // to open the database.
36 // TODO(joi): Should we combine this with WebDatabaseService::InitCallback?
37 typedef base::Callback
<void(sql::InitStatus
)> ProfileErrorCallback
;
39 typedef base::Closure DBLoadedCallback
;
41 // |callback| will only be invoked on error, and only if
42 // |callback.is_null()| evaluates to false.
44 // The ownership of |wdbs| is shared, with the primary owner being the
45 // WebDataServiceWrapper, and secondary owners being subclasses of
46 // WebDataServiceBase, which receive |wdbs| upon construction. The
47 // WebDataServiceWrapper handles the initializing and shutting down and of
49 // WebDataServiceBase is destroyed on |ui_thread|.
50 WebDataServiceBase(scoped_refptr
<WebDatabaseService
> wdbs
,
51 const ProfileErrorCallback
& callback
,
52 const scoped_refptr
<base::MessageLoopProxy
>& ui_thread
);
54 // Cancel any pending request. You need to call this method if your
55 // WebDataServiceConsumer is about to be deleted.
56 virtual void CancelRequest(Handle h
);
58 // Shutdown the web data service. The service can no longer be used after this
60 virtual void ShutdownOnUIThread();
62 // Initializes the web data service.
65 // Unloads the database and shuts down service.
66 void ShutdownDatabase();
68 // Register a callback to be notified that the database has loaded. Multiple
69 // callbacks may be registered, and each will be called at most once
70 // (following a successful database load), then cleared.
71 // Note: if the database load is already complete, then the callback will NOT
72 // be stored or called.
73 virtual void RegisterDBLoadedCallback(const DBLoadedCallback
& callback
);
75 // Returns true if the database load has completetd successfully, and
76 // ShutdownOnUIThread has not yet been called.
77 virtual bool IsDatabaseLoaded();
79 // Returns a pointer to the DB (used by SyncableServices). May return NULL if
80 // the database is not loaded or otherwise unavailable. Must be called on
82 virtual WebDatabase
* GetDatabase();
85 friend class base::RefCountedDeleteOnMessageLoop
<WebDataServiceBase
>;
86 friend class base::DeleteHelper
<WebDataServiceBase
>;
88 virtual ~WebDataServiceBase();
90 // Our database service.
91 scoped_refptr
<WebDatabaseService
> wdbs_
;
94 ProfileErrorCallback profile_error_callback_
;
96 DISALLOW_COPY_AND_ASSIGN(WebDataServiceBase
);
99 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_