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 <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" |
| 14 #include "base/file_path.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/synchronization/condition_variable.h" |
| 18 #include "base/synchronization/lock.h" |
| 19 #include "sql/connection.h" |
| 20 #include "sql/init_status.h" |
| 21 |
| 22 namespace sql { |
| 23 class Connection; |
| 24 class MetaTable; |
| 25 } |
| 26 |
| 27 namespace contacts { |
| 28 |
| 29 struct Contact; |
| 30 typedef std::vector<const Contact*> ContactPointers; |
| 31 |
| 32 // Interface for classes providing persistent storage of Contact objects. |
| 33 class ContactDatabaseInterface { |
| 34 public: |
| 35 // The parameter is true on success and false on failure. |
| 36 typedef base::Callback<void(bool)> InitCallback; |
| 37 |
| 38 // The parameter is true on success and false on failure. |
| 39 typedef base::Callback<void(bool)> SaveCallback; |
| 40 |
| 41 // The first parameter is true on success and false on failure. |
| 42 typedef base::Callback<void(bool, scoped_ptr<ScopedVector<Contact> >)> |
| 43 LoadCallback; |
| 44 |
| 45 ContactDatabaseInterface() {} |
| 46 virtual ~ContactDatabaseInterface() {} |
| 47 |
| 48 // Asynchronously initializes the object. |callback| will be invoked on the |
| 49 // UI thread when complete with a true parameter if initialization succeeded. |
| 50 virtual void Init(const FilePath& database_path, InitCallback callback) = 0; |
| 51 |
| 52 // Asynchronously saves |contacts| to the database. |callback| will be |
| 53 // invoked on the UI thread when complete. The caller must not make changes |
| 54 // to the underlying passed-in contacts until the callback has been invoked. |
| 55 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 56 bool is_full_update, |
| 57 SaveCallback callback) = 0; |
| 58 |
| 59 // Asynchronously loads all contacts from the database and invokes |callback| |
| 60 // when complete. |
| 61 virtual void LoadContacts(LoadCallback callback) = 0; |
| 62 |
| 63 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseInterface); |
| 65 }; |
| 66 |
| 67 class ContactDatabase : public ContactDatabaseInterface { |
| 68 public: |
| 69 ContactDatabase(); |
| 70 virtual ~ContactDatabase(); |
| 71 |
| 72 // ContactDatabaseInterface implementation. |
| 73 virtual void Init(const FilePath& database_path, |
| 74 InitCallback callback) OVERRIDE; |
| 75 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, |
| 76 bool is_full_update, |
| 77 SaveCallback callback) OVERRIDE; |
| 78 virtual void LoadContacts(LoadCallback callback) OVERRIDE; |
| 79 |
| 80 private: |
| 81 // Initializes the database in |database_dir| and posts |callback| on the UI |
| 82 // thread with the result. |
| 83 void InitOnFileThread(const FilePath& database_path, InitCallback callback); |
| 84 |
| 85 // Helper method called by InitOnFileThread(). |
| 86 sql::InitStatus InitInternal(const FilePath& database_path); |
| 87 |
| 88 // Creates our set of tables in |db| if they don't already exist. |
| 89 // Called by InitInternal(). Returns false on failure. |
| 90 bool CreateTablesIfNeeded(); |
| 91 |
| 92 // Queries the database to find the |contact_id| field for |contact|. |
| 93 // Returns -1 if it's not present or if an error occurs. |
| 94 int64 GetContactId(const Contact& contact); |
| 95 |
| 96 // Saves |contacts| to disk and invokes |callback| on the UI thread when |
| 97 // complete. Ownership of |contacts| remains with the caller (or with the |
| 98 // callback itself, in this case). |
| 99 void SaveContactsOnFileThread(scoped_ptr<ContactPointers> contacts, |
| 100 bool is_full_update, |
| 101 SaveCallback callback); |
| 102 |
| 103 // Helper method called by SaveContactsOnFileThread(). Returns true on |
| 104 // success. |
| 105 bool SaveContactsInternal(const ContactPointers& contacts, |
| 106 bool is_full_update); |
| 107 |
| 108 // Clears all contact-related information from the database. |
| 109 bool ClearContacts(); |
| 110 |
| 111 // Saves |contact| to the database, returning true on success. This method |
| 112 // performs multiple queries, so a transaction must be open before it's |
| 113 // called. If |possibly_exists| is false, we don't bother checking to see if |
| 114 // there's an existing row to update (i.e. we're doing a full update and have |
| 115 // already cleared the tables). |
| 116 bool SaveContact(const Contact& contact, bool possibly_exists); |
| 117 |
| 118 // Loads contacts from disk and passes them to |callback| on the UI thread |
| 119 // when complete. |
| 120 void LoadContactsOnFileThread(LoadCallback callback); |
| 121 |
| 122 // Helper method called by LoadContactsOnFileThread(). Returns true on |
| 123 // success. |
| 124 bool LoadContactsInternal(ScopedVector<Contact>* contacts); |
| 125 |
| 126 // Increments or decrements |num_outstanding_file_operations_|. |
| 127 void IncrementOutstandingFileOperations(); |
| 128 void DecrementOutstandingFileOperations(); |
| 129 |
| 130 scoped_ptr<sql::Connection> db_; |
| 131 scoped_ptr<sql::MetaTable> meta_table_; |
| 132 |
| 133 // Protects |num_outstanding_file_operations_|. |
| 134 base::Lock lock_; |
| 135 |
| 136 // Signals on |lock_| when a file operation completes. |
| 137 base::ConditionVariable condition_variable_; |
| 138 |
| 139 // Number of pending or in-progress operations on the file thread. |
| 140 int num_outstanding_file_operations_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(ContactDatabase); |
| 143 }; |
| 144 |
| 145 } // namespace contacts |
| 146 |
| 147 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |
OLD | NEW |