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 STORAGE_COMMON_DATABASE_DATABASE_CONNECTIONS_H_
6 #define STORAGE_COMMON_DATABASE_DATABASE_CONNECTIONS_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "base/synchronization/lock.h"
15 #include "storage/common/storage_common_export.h"
18 class SingleThreadTaskRunner
;
23 class STORAGE_COMMON_EXPORT DatabaseConnections
{
25 DatabaseConnections();
26 ~DatabaseConnections();
29 bool IsDatabaseOpened(const std::string
& origin_identifier
,
30 const base::string16
& database_name
) const;
31 bool IsOriginUsed(const std::string
& origin_identifier
) const;
33 // Returns true if this is the first connection.
34 bool AddConnection(const std::string
& origin_identifier
,
35 const base::string16
& database_name
);
37 // Returns true if the last connection was removed.
38 bool RemoveConnection(const std::string
& origin_identifier
,
39 const base::string16
& database_name
);
41 void RemoveAllConnections();
42 void RemoveConnections(
43 const DatabaseConnections
& connections
,
44 std::vector
<std::pair
<std::string
, base::string16
> >* closed_dbs
);
46 // Database sizes can be kept only if IsDatabaseOpened returns true.
47 int64
GetOpenDatabaseSize(const std::string
& origin_identifier
,
48 const base::string16
& database_name
) const;
49 void SetOpenDatabaseSize(const std::string
& origin_identifier
,
50 const base::string16
& database_name
,
53 // Returns a list of the connections, <origin_id, name>.
55 std::vector
<std::pair
<std::string
, base::string16
> > *list
) const;
58 // Mapping from name to <openCount, size>
59 typedef std::map
<base::string16
, std::pair
<int, int64
> > DBConnections
;
60 typedef std::map
<std::string
, DBConnections
> OriginConnections
;
61 mutable OriginConnections connections_
; // mutable for GetOpenDatabaseSize
63 // Returns true if the last connection was removed.
64 bool RemoveConnectionsHelper(const std::string
& origin_identifier
,
65 const base::string16
& database_name
,
69 // A wrapper class that provides thread-safety and the
70 // ability to wait until all connections have closed.
71 // Intended for use in renderer processes.
72 class STORAGE_COMMON_EXPORT DatabaseConnectionsWrapper
73 : public base::RefCountedThreadSafe
<DatabaseConnectionsWrapper
> {
75 DatabaseConnectionsWrapper();
77 // The Wait and Has methods should only be called on the
78 // main thread (the thread on which the wrapper is constructed).
79 void WaitForAllDatabasesToClose();
80 bool HasOpenConnections();
82 // Add and Remove may be called on any thread.
83 void AddOpenConnection(const std::string
& origin_identifier
,
84 const base::string16
& database_name
);
85 void RemoveOpenConnection(const std::string
& origin_identifier
,
86 const base::string16
& database_name
);
88 ~DatabaseConnectionsWrapper();
89 friend class base::RefCountedThreadSafe
<DatabaseConnectionsWrapper
>;
91 bool waiting_for_dbs_to_close_
;
92 base::Lock open_connections_lock_
;
93 DatabaseConnections open_connections_
;
94 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_
;
97 } // namespace storage
99 #endif // STORAGE_COMMON_DATABASE_DATABASE_CONNECTIONS_H_