OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/callback.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/file_path.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/scoped_vector.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/synchronization/condition_variable.h" | |
18 #include "base/synchronization/lock.h" | |
19 | |
20 namespace leveldb { | |
21 class DB; | |
22 } | |
23 | |
24 namespace contacts { | |
25 | |
26 class Contact; | |
27 typedef std::vector<const Contact*> ContactPointers; | |
28 | |
29 // Interface for classes providing persistent storage of Contact objects. | |
30 class ContactDatabaseInterface { | |
31 public: | |
32 // The parameter is true on success and false on failure. | |
33 typedef base::Callback<void(bool)> InitCallback; | |
satorux1
2012/07/30 17:15:07
Having a parameter name would make it a bit more i
Daniel Erat
2012/07/30 22:12:09
Thanks, didn't know that this works! Done.
| |
34 | |
35 // The parameter is true on success and false on failure. | |
36 typedef base::Callback<void(bool)> SaveCallback; | |
37 | |
38 // The first parameter is true on success and false on failure. | |
39 typedef base::Callback<void(bool, scoped_ptr<ScopedVector<Contact> >)> | |
40 LoadCallback; | |
41 | |
42 ContactDatabaseInterface() {} | |
43 | |
44 // Destruction blocks until all outstanding file operations are complete. | |
45 virtual ~ContactDatabaseInterface() {} | |
46 | |
47 // Asynchronously initializes the object. |callback| will be invoked on the | |
48 // UI thread when complete. | |
49 virtual void Init(const FilePath& database_dir, InitCallback callback) = 0; | |
50 | |
51 // Asynchronously saves |contacts| to the database. If |is_full_update| is | |
52 // true, all existing contacts in the database not present in |contacts| will | |
53 // be removed. |callback| will be invoked on the UI thread when complete. | |
54 // The caller must not make changes to the underlying passed-in Contact | |
55 // objects until the callback has been invoked. | |
56 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, | |
57 bool is_full_update, | |
58 SaveCallback callback) = 0; | |
59 | |
60 // Asynchronously loads all contacts from the database and invokes |callback| | |
61 // when complete. | |
62 virtual void LoadContacts(LoadCallback callback) = 0; | |
63 | |
64 private: | |
65 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseInterface); | |
66 }; | |
67 | |
68 class ContactDatabase : public ContactDatabaseInterface, | |
69 public base::SupportsWeakPtr<ContactDatabase> { | |
70 public: | |
71 ContactDatabase(); | |
72 virtual ~ContactDatabase(); | |
73 | |
74 // ContactDatabaseInterface implementation. | |
75 virtual void Init(const FilePath& database_dir, | |
76 InitCallback callback) OVERRIDE; | |
77 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, | |
78 bool is_full_update, | |
79 SaveCallback callback) OVERRIDE; | |
80 virtual void LoadContacts(LoadCallback callback) OVERRIDE; | |
81 | |
82 private: | |
83 // Initializes the database in |database_dir| and posts |callback| on the UI | |
84 // thread with the result. | |
85 void InitOnFileThread(const FilePath& database_dir, InitCallback callback); | |
86 | |
87 // Helper method called by InitOnFileThread(). Returns true on success. | |
88 bool InitInternal(const FilePath& database_dir); | |
89 | |
90 // Saves |contacts| to disk and invokes |callback| on the UI thread when | |
91 // complete. | |
92 void SaveContactsOnFileThread(scoped_ptr<ContactPointers> contacts, | |
93 bool is_full_update, | |
94 SaveCallback callback); | |
95 | |
96 // Loads contacts from disk and passes them to |callback| on the UI thread | |
97 // when complete. | |
98 void LoadContactsOnFileThread(LoadCallback callback); | |
99 | |
100 // Helper method called by LoadContactsOnFileThread(). Returns true on | |
101 // success. | |
102 bool LoadContactsInternal(ScopedVector<Contact>* contacts); | |
103 | |
104 // Increments or decrements |num_outstanding_file_operations_|. | |
105 void IncrementOutstandingFileOperations(); | |
106 void DecrementOutstandingFileOperations(); | |
107 | |
108 scoped_ptr<leveldb::DB> db_; | |
109 | |
110 // Protects |num_outstanding_file_operations_|. | |
111 base::Lock lock_; | |
112 | |
113 // Signals on |lock_| when a file operation completes. | |
114 base::ConditionVariable condition_variable_; | |
satorux1
2012/07/30 17:15:07
I'm very wary of Lock and friends. Can you go with
Daniel Erat
2012/07/30 22:12:09
Done.
| |
115 | |
116 // Number of pending or in-progress operations on the file thread. | |
117 int num_outstanding_file_operations_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(ContactDatabase); | |
120 }; | |
121 | |
122 } // namespace contacts | |
123 | |
124 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | |
OLD | NEW |