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