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 without actually shutting down the service. This can
66 // be used to temporarily reduce the browser process' memory footprint.
67 void UnloadDatabase();
69 // Unloads the database permanently and shuts down service.
70 void ShutdownDatabase();
72 // Register a callback to be notified that the database has loaded. Multiple
73 // callbacks may be registered, and each will be called at most once
74 // (following a successful database load), then cleared.
75 // Note: if the database load is already complete, then the callback will NOT
76 // be stored or called.
77 virtual void RegisterDBLoadedCallback(const DBLoadedCallback& callback);
79 // Returns true if the database load has completetd successfully, and
80 // ShutdownOnUIThread has not yet been called.
81 virtual bool IsDatabaseLoaded();
83 // Returns a pointer to the DB (used by SyncableServices). May return NULL if
84 // the database is not loaded or otherwise unavailable. Must be called on
86 virtual WebDatabase* GetDatabase();
89 friend class base::RefCountedDeleteOnMessageLoop<WebDataServiceBase>;
90 friend class base::DeleteHelper<WebDataServiceBase>;
92 virtual ~WebDataServiceBase();
94 // Our database service.
95 scoped_refptr<WebDatabaseService> wdbs_;
98 ProfileErrorCallback profile_error_callback_;
100 DISALLOW_COPY_AND_ASSIGN(WebDataServiceBase);
103 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_