1 // Copyright 2013 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_DATABASE_BACKEND_H_
6 #define COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_BACKEND_H_
8 #include "base/basictypes.h"
9 #include "base/callback_forward.h"
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/ref_counted_delete_on_message_loop.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "components/webdata/common/web_database_service.h"
17 #include "components/webdata/common/webdata_export.h"
20 class WebDatabaseTable
;
22 class WebDataRequestManager
;
24 namespace tracked_objects
{
28 // WebDatabaseBackend handles all database tasks posted by
29 // WebDatabaseService. It is refcounted to allow asynchronous destruction on the
32 class WEBDATA_EXPORT WebDatabaseBackend
33 : public base::RefCountedDeleteOnMessageLoop
<WebDatabaseBackend
> {
37 virtual ~Delegate() {}
39 // Invoked when the backend has finished loading the db.
40 virtual void DBLoaded(sql::InitStatus status
) = 0;
43 WebDatabaseBackend(const base::FilePath
& path
,
45 const scoped_refptr
<base::MessageLoopProxy
>& db_thread
);
47 // Must call only before InitDatabaseWithCallback.
48 void AddTable(scoped_ptr
<WebDatabaseTable
> table
);
50 // Initializes the database and notifies caller via callback when complete.
51 // Callback is called synchronously.
54 // Opens the database file from the profile path if an init has not yet been
55 // attempted. Separated from the constructor to ease construction/destruction
56 // of this object on one thread but database access on the DB thread. Returns
57 // the status of the database.
58 sql::InitStatus
LoadDatabaseIfNecessary();
60 // Shuts down the database.
61 void ShutdownDatabase();
63 // Task wrappers to update requests and and notify |request_manager_|. These
64 // are used in cases where the request is being made from the UI thread and an
65 // asyncronous callback is required to notify the client of |request|'s
67 void DBWriteTaskWrapper(const WebDatabaseService::WriteTask
& task
,
68 scoped_ptr
<WebDataRequest
> request
);
69 void DBReadTaskWrapper(const WebDatabaseService::ReadTask
& task
,
70 scoped_ptr
<WebDataRequest
> request
);
72 // Task runners to run database tasks.
73 void ExecuteWriteTask(const WebDatabaseService::WriteTask
& task
);
74 scoped_ptr
<WDTypedResult
> ExecuteReadTask(
75 const WebDatabaseService::ReadTask
& task
);
77 const scoped_refptr
<WebDataRequestManager
>& request_manager() {
78 return request_manager_
;
81 WebDatabase
* database() { return db_
.get(); }
84 friend class base::RefCountedDeleteOnMessageLoop
<WebDatabaseBackend
>;
85 friend class base::DeleteHelper
<WebDatabaseBackend
>;
87 virtual ~WebDatabaseBackend();
90 // Commit the current transaction.
93 // Path to database file.
94 base::FilePath db_path_
;
96 // The tables that participate in managing the database. These are
97 // owned here but other than that this class does nothing with
98 // them. Their initialization is in whatever factory creates
99 // WebDatabaseService, and lookup by type is provided by the
100 // WebDatabase class. The tables need to be owned by this refcounted
101 // object, or they themselves would need to be refcounted. Owning
102 // them here rather than having WebDatabase own them makes for
103 // easier unit testing of WebDatabase.
104 ScopedVector
<WebDatabaseTable
> tables_
;
106 scoped_ptr
<WebDatabase
> db_
;
108 // Keeps track of all pending requests made to the db.
109 scoped_refptr
<WebDataRequestManager
> request_manager_
;
111 // State of database initialization. Used to prevent the executing of tasks
112 // before the db is ready.
113 sql::InitStatus init_status_
;
115 // True if an attempt has been made to load the database (even if the attempt
116 // fails), used to avoid continually trying to reinit if the db init fails.
119 // Delegate. See the class definition above for more information.
120 scoped_ptr
<Delegate
> delegate_
;
122 DISALLOW_COPY_AND_ASSIGN(WebDatabaseBackend
);
125 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_BACKEND_H_