OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |
6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
21 } | 21 } |
22 | 22 |
23 namespace leveldb { | 23 namespace leveldb { |
24 class DB; | 24 class DB; |
25 } | 25 } |
26 | 26 |
27 namespace contacts { | 27 namespace contacts { |
28 | 28 |
29 class Contact; | 29 class Contact; |
| 30 class UpdateMetadata; |
30 typedef std::vector<const Contact*> ContactPointers; | 31 typedef std::vector<const Contact*> ContactPointers; |
31 | 32 |
32 // Interface for classes providing persistent storage of Contact objects. | 33 // Interface for classes providing persistent storage of Contact objects. |
33 class ContactDatabaseInterface { | 34 class ContactDatabaseInterface { |
34 public: | 35 public: |
35 typedef base::Callback<void(bool success)> InitCallback; | 36 typedef base::Callback<void(bool success)> InitCallback; |
36 typedef base::Callback<void(bool success)> SaveCallback; | 37 typedef base::Callback<void(bool success)> SaveCallback; |
37 typedef base::Callback<void(bool success, scoped_ptr<ScopedVector<Contact> >)> | 38 typedef base::Callback<void(bool success, |
| 39 scoped_ptr<ScopedVector<Contact> >, |
| 40 scoped_ptr<UpdateMetadata>)> |
38 LoadCallback; | 41 LoadCallback; |
39 | 42 |
40 ContactDatabaseInterface() {} | 43 ContactDatabaseInterface() {} |
41 | 44 |
42 // Asynchronously destroys the object after all in-progress file operations | 45 // Asynchronously destroys the object after all in-progress file operations |
43 // have completed. | 46 // have completed. |
44 virtual void DestroyOnUIThread() {} | 47 virtual void DestroyOnUIThread() {} |
45 | 48 |
46 // Asynchronously initializes the object. |callback| will be invoked on the | 49 // Asynchronously initializes the object. |callback| will be invoked on the |
47 // UI thread when complete. | 50 // UI thread when complete. |
48 virtual void Init(const FilePath& database_dir, InitCallback callback) = 0; | 51 virtual void Init(const FilePath& database_dir, InitCallback callback) = 0; |
49 | 52 |
50 // Asynchronously saves |contacts| to the database. If |is_full_update| is | 53 // Asynchronously saves |contacts| and |metadata| to the database. If |
51 // true, all existing contacts in the database not present in |contacts| will | 54 // |is_full_update| is true, all existing contacts in the database not present |
52 // be removed. |callback| will be invoked on the UI thread when complete. | 55 // in |contacts| will be removed. |callback| will be invoked on the UI thread |
53 // The caller must not make changes to the underlying passed-in Contact | 56 // when complete. The caller must not make changes to the underlying |
54 // objects until the callback has been invoked. | 57 // passed-in Contact objects until the callback has been invoked. |
55 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, | 58 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 59 scoped_ptr<UpdateMetadata> metadata, |
56 bool is_full_update, | 60 bool is_full_update, |
57 SaveCallback callback) = 0; | 61 SaveCallback callback) = 0; |
58 | 62 |
59 // Asynchronously loads all contacts from the database and invokes |callback| | 63 // Asynchronously loads all contacts from the database and invokes |callback| |
60 // when complete. | 64 // when complete. |
61 virtual void LoadContacts(LoadCallback callback) = 0; | 65 virtual void LoadContacts(LoadCallback callback) = 0; |
62 | 66 |
63 protected: | 67 protected: |
64 virtual ~ContactDatabaseInterface() {} | 68 virtual ~ContactDatabaseInterface() {} |
65 | 69 |
66 private: | 70 private: |
67 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseInterface); | 71 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseInterface); |
68 }; | 72 }; |
69 | 73 |
70 class ContactDatabase : public ContactDatabaseInterface { | 74 class ContactDatabase : public ContactDatabaseInterface { |
71 public: | 75 public: |
72 ContactDatabase(); | 76 ContactDatabase(); |
73 | 77 |
74 // ContactDatabaseInterface implementation. | 78 // ContactDatabaseInterface implementation. |
75 virtual void DestroyOnUIThread() OVERRIDE; | 79 virtual void DestroyOnUIThread() OVERRIDE; |
76 virtual void Init(const FilePath& database_dir, | 80 virtual void Init(const FilePath& database_dir, |
77 InitCallback callback) OVERRIDE; | 81 InitCallback callback) OVERRIDE; |
78 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, | 82 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 83 scoped_ptr<UpdateMetadata> metadata, |
79 bool is_full_update, | 84 bool is_full_update, |
80 SaveCallback callback) OVERRIDE; | 85 SaveCallback callback) OVERRIDE; |
81 virtual void LoadContacts(LoadCallback callback) OVERRIDE; | 86 virtual void LoadContacts(LoadCallback callback) OVERRIDE; |
82 | 87 |
83 protected: | 88 protected: |
84 virtual ~ContactDatabase(); | 89 virtual ~ContactDatabase(); |
85 | 90 |
86 private: | 91 private: |
87 // Are we currently being run by |task_runner_|? | 92 // Are we currently being run by |task_runner_|? |
88 bool IsRunByTaskRunner() const; | 93 bool IsRunByTaskRunner() const; |
89 | 94 |
90 // Deletes |this|. | 95 // Deletes |this|. |
91 void DestroyFromTaskRunner(); | 96 void DestroyFromTaskRunner(); |
92 | 97 |
93 // Passes the supplied parameters to |callback| after being called on the UI | 98 // Passes the supplied parameters to |callback| after being called on the UI |
94 // thread. Used for replies sent in response to |task_runner_|'s tasks, so | 99 // thread. Used for replies sent in response to |task_runner_|'s tasks, so |
95 // that weak_ptrs can be used to avoid running the replies after the | 100 // that weak_ptrs can be used to avoid running the replies after the |
96 // ContactDatabase has been deleted. | 101 // ContactDatabase has been deleted. |
97 void RunInitCallback(InitCallback callback, const bool* success); | 102 void RunInitCallback(InitCallback callback, const bool* success); |
98 void RunSaveCallback(SaveCallback callback, const bool* success); | 103 void RunSaveCallback(SaveCallback callback, const bool* success); |
99 void RunLoadCallback(LoadCallback callback, | 104 void RunLoadCallback(LoadCallback callback, |
100 const bool* success, | 105 const bool* success, |
101 scoped_ptr<ScopedVector<Contact> > contacts); | 106 scoped_ptr<ScopedVector<Contact> > contacts, |
| 107 scoped_ptr<UpdateMetadata> metadata); |
102 | 108 |
103 // Initializes the database in |database_dir| and updates |success|. | 109 // Initializes the database in |database_dir| and updates |success|. |
104 void InitFromTaskRunner(const FilePath& database_dir, bool* success); | 110 void InitFromTaskRunner(const FilePath& database_dir, bool* success); |
105 | 111 |
106 // Saves |contacts| to disk and updates |success|. | 112 // Saves |contacts| to disk and updates |success|. |
107 void SaveContactsFromTaskRunner(scoped_ptr<ContactPointers> contacts, | 113 void SaveContactsFromTaskRunner(scoped_ptr<ContactPointers> contacts, |
| 114 scoped_ptr<UpdateMetadata> metadata, |
108 bool is_full_update, | 115 bool is_full_update, |
109 bool* success); | 116 bool* success); |
110 | 117 |
111 // Loads contacts from disk and updates |success|. | 118 // Loads contacts from disk and updates |success|. |
112 void LoadContactsFromTaskRunner(bool* success, | 119 void LoadContactsFromTaskRunner(bool* success, |
113 ScopedVector<Contact>* contacts_out); | 120 ScopedVector<Contact>* contacts, |
| 121 UpdateMetadata* metadata); |
114 | 122 |
115 // Used to run blocking tasks in-order. | 123 // Used to run blocking tasks in-order. |
116 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 124 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
117 | 125 |
118 scoped_ptr<leveldb::DB> db_; | 126 scoped_ptr<leveldb::DB> db_; |
119 | 127 |
120 // Note: This should remain the last member so it'll be destroyed and | 128 // Note: This should remain the last member so it'll be destroyed and |
121 // invalidate its weak pointers before any other members are destroyed. | 129 // invalidate its weak pointers before any other members are destroyed. |
122 base::WeakPtrFactory<ContactDatabase> weak_ptr_factory_; | 130 base::WeakPtrFactory<ContactDatabase> weak_ptr_factory_; |
123 | 131 |
124 DISALLOW_COPY_AND_ASSIGN(ContactDatabase); | 132 DISALLOW_COPY_AND_ASSIGN(ContactDatabase); |
125 }; | 133 }; |
126 | 134 |
127 } // namespace contacts | 135 } // namespace contacts |
128 | 136 |
129 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | 137 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |
OLD | NEW |