1 // Copyright (c) 2011 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 WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_
6 #define WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/string16.h"
13 #include "base/synchronization/lock.h"
14 #include "webkit/storage/webkit_storage_export.h"
17 class MessageLoopProxy
;
20 namespace webkit_database
{
22 class WEBKIT_STORAGE_EXPORT DatabaseConnections
{
24 DatabaseConnections();
25 ~DatabaseConnections();
28 bool IsDatabaseOpened(const string16
& origin_identifier
,
29 const string16
& database_name
) const;
30 bool IsOriginUsed(const string16
& origin_identifier
) const;
32 // Returns true if this is the first connection.
33 bool AddConnection(const string16
& origin_identifier
,
34 const string16
& database_name
);
36 // Returns true if the last connection was removed.
37 bool RemoveConnection(const string16
& origin_identifier
,
38 const string16
& database_name
);
40 void RemoveAllConnections();
41 void RemoveConnections(
42 const DatabaseConnections
& connections
,
43 std::vector
<std::pair
<string16
, string16
> >* closed_dbs
);
45 // Database sizes can be kept only if IsDatabaseOpened returns true.
46 int64
GetOpenDatabaseSize(const string16
& origin_identifier
,
47 const string16
& database_name
) const;
48 void SetOpenDatabaseSize(const string16
& origin_identifier
,
49 const string16
& database_name
,
52 // Returns a list of the connections, <origin_id, name>.
54 std::vector
<std::pair
<string16
, string16
> > *list
) const;
57 // Mapping from name to <openCount, size>
58 typedef std::map
<string16
, std::pair
<int, int64
> > DBConnections
;
59 typedef std::map
<string16
, DBConnections
> OriginConnections
;
60 mutable OriginConnections connections_
; // mutable for GetOpenDatabaseSize
62 // Returns true if the last connection was removed.
63 bool RemoveConnectionsHelper(const string16
& origin_identifier
,
64 const string16
& database_name
,
68 // A wrapper class that provides thread-safety and the
69 // ability to wait until all connections have closed.
70 // Intended for use in renderer processes.
71 class WEBKIT_STORAGE_EXPORT DatabaseConnectionsWrapper
72 : public base::RefCountedThreadSafe
<DatabaseConnectionsWrapper
> {
74 DatabaseConnectionsWrapper();
76 // The Wait and Has methods should only be called on the
77 // main thread (the thread on which the wrapper is constructed).
78 void WaitForAllDatabasesToClose();
79 bool HasOpenConnections();
81 // Add and Remove may be called on any thread.
82 void AddOpenConnection(const string16
& origin_identifier
,
83 const string16
& database_name
);
84 void RemoveOpenConnection(const string16
& origin_identifier
,
85 const string16
& database_name
);
87 ~DatabaseConnectionsWrapper();
88 friend class base::RefCountedThreadSafe
<DatabaseConnectionsWrapper
>;
90 bool waiting_for_dbs_to_close_
;
91 base::Lock open_connections_lock_
;
92 DatabaseConnections open_connections_
;
93 scoped_refptr
<base::MessageLoopProxy
> main_thread_
;
96 } // namespace webkit_database
98 #endif // WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_