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" | |
| 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(const bool* success)> InitCallback; | |
|
satorux1
2012/07/31 19:56:57
It may be awkward for the client of this class to
Daniel Erat
2012/07/31 21:10:56
Done.
| |
| 36 typedef base::Callback<void(const bool* success)> SaveCallback; | |
| 37 typedef base::Callback<void(const bool* success, | |
| 38 scoped_ptr<ScopedVector<Contact> >)> 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 // Runs |closure| after being called on the UI thread. Used for replies sent | |
| 94 // in response to |task_runner_|'s tasks, so that weak_ptrs can be used to | |
| 95 // avoid running the replies after the ContactDatabase has been deleted. | |
| 96 void RunCallback(base::Closure closure); | |
| 97 | |
| 98 // Initializes the database in |database_dir| and updates |success|. | |
| 99 void InitFromTaskRunner(const FilePath& database_dir, bool* success); | |
| 100 | |
| 101 // Saves |contacts| to disk and updates |success|. | |
| 102 void SaveContactsFromTaskRunner(scoped_ptr<ContactPointers> contacts, | |
| 103 bool is_full_update, | |
| 104 bool* success); | |
| 105 | |
| 106 // Loads contacts from disk and updates |success|. | |
| 107 void LoadContactsFromTaskRunner(bool* success, | |
| 108 ScopedVector<Contact>* contacts_out); | |
| 109 | |
| 110 // Used to run blocking tasks in-order. | |
| 111 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 112 | |
| 113 base::WeakPtrFactory<ContactDatabase> weak_ptr_factory_; | |
|
satorux1
2012/07/31 19:56:57
This is very subtle but my understanding is that t
satorux1
2012/07/31 19:58:03
This may not matter here as InvalidateWeakPtrs() i
Daniel Erat
2012/07/31 21:10:56
Done.
| |
| 114 | |
| 115 scoped_ptr<leveldb::DB> db_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(ContactDatabase); | |
| 118 }; | |
| 119 | |
| 120 } // namespace contacts | |
| 121 | |
| 122 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ | |
| OLD | NEW |