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