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