Chromium Code Reviews| 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" | |
|
satorux1
2012/07/31 07:26:30
is this needed?
Daniel Erat
2012/07/31 16:44:12
Yes, for scoped_refptr. :-)
satorux1
2012/07/31 17:34:26
oh you are right. It's used for the task runner.
| |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/scoped_vector.h" | |
| 17 | |
| 18 namespace base { | |
| 19 class SequencedTaskRunner; | |
| 20 } | |
| 21 | |
| 22 namespace leveldb { | |
| 23 class DB; | |
| 24 } | |
| 25 | |
| 26 namespace contacts { | |
| 27 | |
| 28 class Contact; | |
| 29 typedef std::vector<const Contact*> ContactPointers; | |
| 30 | |
| 31 // Interface for classes providing persistent storage of Contact objects. | |
| 32 class ContactDatabaseInterface { | |
| 33 public: | |
| 34 typedef base::Callback<void(bool success)> InitCallback; | |
| 35 typedef base::Callback<void(bool success)> SaveCallback; | |
| 36 typedef base::Callback<void(bool success, scoped_ptr<ScopedVector<Contact> >)> | |
| 37 LoadCallback; | |
| 38 | |
| 39 ContactDatabaseInterface() {} | |
| 40 | |
| 41 // Asynchronously destroys the object after all in-progress file operations | |
| 42 // have completed. | |
| 43 virtual void DestroyOnUIThread() {} | |
| 44 | |
| 45 // Asynchronously initializes the object. |callback| will be invoked on the | |
| 46 // UI thread when complete. | |
| 47 virtual void Init(const FilePath& database_dir, InitCallback callback) = 0; | |
| 48 | |
| 49 // Asynchronously saves |contacts| to the database. If |is_full_update| is | |
| 50 // true, all existing contacts in the database not present in |contacts| will | |
| 51 // be removed. |callback| will be invoked on the UI thread when complete. | |
| 52 // The caller must not make changes to the underlying passed-in Contact | |
| 53 // objects until the callback has been invoked. | |
| 54 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, | |
| 55 bool is_full_update, | |
| 56 SaveCallback callback) = 0; | |
| 57 | |
| 58 // Asynchronously loads all contacts from the database and invokes |callback| | |
| 59 // when complete. | |
| 60 virtual void LoadContacts(LoadCallback callback) = 0; | |
| 61 | |
| 62 protected: | |
| 63 virtual ~ContactDatabaseInterface() {} | |
| 64 | |
| 65 private: | |
| 66 DISALLOW_COPY_AND_ASSIGN(ContactDatabaseInterface); | |
| 67 }; | |
| 68 | |
| 69 class ContactDatabase : public ContactDatabaseInterface { | |
| 70 public: | |
| 71 ContactDatabase(); | |
| 72 | |
| 73 // ContactDatabaseInterface implementation. | |
| 74 virtual void DestroyOnUIThread() OVERRIDE; | |
| 75 virtual void Init(const FilePath& database_dir, | |
| 76 InitCallback callback) OVERRIDE; | |
| 77 virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, | |
| 78 bool is_full_update, | |
| 79 SaveCallback callback) OVERRIDE; | |
| 80 virtual void LoadContacts(LoadCallback callback) OVERRIDE; | |
| 81 | |
| 82 protected: | |
| 83 virtual ~ContactDatabase(); | |
| 84 | |
| 85 private: | |
| 86 // Are we currently being run by |task_runner_|? | |
| 87 bool IsRunByTaskRunner() const; | |
| 88 | |
| 89 // Deletes |this|. | |
| 90 void DestroyFromTaskRunner(); | |
| 91 | |
| 92 // Initializes the database in |database_dir| and posts |callback| on the UI | |
| 93 // thread with the result. | |
| 94 void InitFromTaskRunner(const FilePath& database_dir, InitCallback callback); | |
| 95 | |
| 96 // Helper method called by InitFromTaskRunner(). Returns true on success. | |
| 97 bool InitInternal(const FilePath& database_dir); | |
| 98 | |
| 99 // Saves |contacts| to disk and invokes |callback| on the UI thread when | |
| 100 // complete. | |
| 101 void SaveContactsFromTaskRunner(scoped_ptr<ContactPointers> contacts, | |
| 102 bool is_full_update, | |
| 103 SaveCallback callback); | |
| 104 | |
| 105 // Loads contacts from disk and passes them to |callback| on the UI thread | |
| 106 // when complete. | |
| 107 void LoadContactsFromTaskRunner(LoadCallback callback); | |
| 108 | |
| 109 // Helper method called by LoadContactsFromTaskRunner(). Returns true on | |
| 110 // success. | |
| 111 bool LoadContactsInternal(ScopedVector<Contact>* contacts); | |
| 112 | |
| 113 // Used to run blocking tasks in-order. | |
| 114 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 115 | |
| 116 scoped_ptr<leveldb::DB> db_; | |
| 117 | |
| 118 DISALLOW_COPY_AND_ASSIGN(ContactDatabase); | |
| 119 }; | |
| 120 | |
| 121 } // namespace contacts | |
| 122 | |
| 123 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | |
| OLD | NEW |