OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_ | 5 #ifndef WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_ |
6 #define WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_ | 6 #define WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/string16.h" | 12 #include "base/string16.h.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "webkit/storage/webkit_storage_export.h" | 14 #include "webkit/storage/webkit_storage_export.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class MessageLoopProxy; | 17 class MessageLoopProxy; |
18 } | 18 } |
19 | 19 |
20 namespace webkit_database { | 20 namespace webkit_database { |
21 | 21 |
22 class WEBKIT_STORAGE_EXPORT DatabaseConnections { | 22 class WEBKIT_STORAGE_EXPORT DatabaseConnections { |
23 public: | 23 public: |
24 DatabaseConnections(); | 24 DatabaseConnections(); |
25 ~DatabaseConnections(); | 25 ~DatabaseConnections(); |
26 | 26 |
27 bool IsEmpty() const; | 27 bool IsEmpty() const; |
28 bool IsDatabaseOpened(const string16& origin_identifier, | 28 bool IsDatabaseOpened(const base::string16& origin_identifier, |
29 const string16& database_name) const; | 29 const base::string16& database_name) const; |
30 bool IsOriginUsed(const string16& origin_identifier) const; | 30 bool IsOriginUsed(const base::string16& origin_identifier) const; |
31 | 31 |
32 // Returns true if this is the first connection. | 32 // Returns true if this is the first connection. |
33 bool AddConnection(const string16& origin_identifier, | 33 bool AddConnection(const base::string16& origin_identifier, |
34 const string16& database_name); | 34 const base::string16& database_name); |
35 | 35 |
36 // Returns true if the last connection was removed. | 36 // Returns true if the last connection was removed. |
37 bool RemoveConnection(const string16& origin_identifier, | 37 bool RemoveConnection(const base::string16& origin_identifier, |
38 const string16& database_name); | 38 const base::string16& database_name); |
39 | 39 |
40 void RemoveAllConnections(); | 40 void RemoveAllConnections(); |
41 void RemoveConnections( | 41 void RemoveConnections( |
42 const DatabaseConnections& connections, | 42 const DatabaseConnections& connections, |
43 std::vector<std::pair<string16, string16> >* closed_dbs); | 43 std::vector<std::pair<base::string16, base::string16> >* closed_dbs); |
44 | 44 |
45 // Database sizes can be kept only if IsDatabaseOpened returns true. | 45 // Database sizes can be kept only if IsDatabaseOpened returns true. |
46 int64 GetOpenDatabaseSize(const string16& origin_identifier, | 46 int64 GetOpenDatabaseSize(const base::string16& origin_identifier, |
47 const string16& database_name) const; | 47 const base::string16& database_name) const; |
48 void SetOpenDatabaseSize(const string16& origin_identifier, | 48 void SetOpenDatabaseSize(const base::string16& origin_identifier, |
49 const string16& database_name, | 49 const base::string16& database_name, |
50 int64 size); | 50 int64 size); |
51 | 51 |
52 // Returns a list of the connections, <origin_id, name>. | 52 // Returns a list of the connections, <origin_id, name>. |
53 void ListConnections( | 53 void ListConnections( |
54 std::vector<std::pair<string16, string16> > *list) const; | 54 std::vector<std::pair<base::string16, base::string16> > *list) const; |
55 | 55 |
56 private: | 56 private: |
57 // Mapping from name to <openCount, size> | 57 // Mapping from name to <openCount, size> |
58 typedef std::map<string16, std::pair<int, int64> > DBConnections; | 58 typedef std::map<base::string16, std::pair<int, int64> > DBConnections; |
59 typedef std::map<string16, DBConnections> OriginConnections; | 59 typedef std::map<base::string16, DBConnections> OriginConnections; |
60 mutable OriginConnections connections_; // mutable for GetOpenDatabaseSize | 60 mutable OriginConnections connections_; // mutable for GetOpenDatabaseSize |
61 | 61 |
62 // Returns true if the last connection was removed. | 62 // Returns true if the last connection was removed. |
63 bool RemoveConnectionsHelper(const string16& origin_identifier, | 63 bool RemoveConnectionsHelper(const base::string16& origin_identifier, |
64 const string16& database_name, | 64 const base::string16& database_name, |
65 int num_connections); | 65 int num_connections); |
66 }; | 66 }; |
67 | 67 |
68 // A wrapper class that provides thread-safety and the | 68 // A wrapper class that provides thread-safety and the |
69 // ability to wait until all connections have closed. | 69 // ability to wait until all connections have closed. |
70 // Intended for use in renderer processes. | 70 // Intended for use in renderer processes. |
71 class WEBKIT_STORAGE_EXPORT DatabaseConnectionsWrapper | 71 class WEBKIT_STORAGE_EXPORT DatabaseConnectionsWrapper |
72 : public base::RefCountedThreadSafe<DatabaseConnectionsWrapper> { | 72 : public base::RefCountedThreadSafe<DatabaseConnectionsWrapper> { |
73 public: | 73 public: |
74 DatabaseConnectionsWrapper(); | 74 DatabaseConnectionsWrapper(); |
75 | 75 |
76 // The Wait and Has methods should only be called on the | 76 // The Wait and Has methods should only be called on the |
77 // main thread (the thread on which the wrapper is constructed). | 77 // main thread (the thread on which the wrapper is constructed). |
78 void WaitForAllDatabasesToClose(); | 78 void WaitForAllDatabasesToClose(); |
79 bool HasOpenConnections(); | 79 bool HasOpenConnections(); |
80 | 80 |
81 // Add and Remove may be called on any thread. | 81 // Add and Remove may be called on any thread. |
82 void AddOpenConnection(const string16& origin_identifier, | 82 void AddOpenConnection(const base::string16& origin_identifier, |
83 const string16& database_name); | 83 const base::string16& database_name); |
84 void RemoveOpenConnection(const string16& origin_identifier, | 84 void RemoveOpenConnection(const base::string16& origin_identifier, |
85 const string16& database_name); | 85 const base::string16& database_name); |
86 private: | 86 private: |
87 ~DatabaseConnectionsWrapper(); | 87 ~DatabaseConnectionsWrapper(); |
88 friend class base::RefCountedThreadSafe<DatabaseConnectionsWrapper>; | 88 friend class base::RefCountedThreadSafe<DatabaseConnectionsWrapper>; |
89 | 89 |
90 bool waiting_for_dbs_to_close_; | 90 bool waiting_for_dbs_to_close_; |
91 base::Lock open_connections_lock_; | 91 base::Lock open_connections_lock_; |
92 DatabaseConnections open_connections_; | 92 DatabaseConnections open_connections_; |
93 scoped_refptr<base::MessageLoopProxy> main_thread_; | 93 scoped_refptr<base::MessageLoopProxy> main_thread_; |
94 }; | 94 }; |
95 | 95 |
96 } // namespace webkit_database | 96 } // namespace webkit_database |
97 | 97 |
98 #endif // WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_ | 98 #endif // WEBKIT_DATABASE_DATABASE_CONNECTIONS_H_ |
OLD | NEW |