Chromium Code Reviews| Index: chrome/browser/chromeos/contacts/contact_database.h |
| diff --git a/chrome/browser/chromeos/contacts/contact_database.h b/chrome/browser/chromeos/contacts/contact_database.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8be700085c57f482562b2a4a13263b4c1fcd8deb |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/contacts/contact_database.h |
| @@ -0,0 +1,122 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |
| +#define CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/callback.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/file_path.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/memory/weak_ptr.h" |
| + |
| +namespace base { |
| +class SequencedTaskRunner; |
| +} |
| + |
| +namespace leveldb { |
| +class DB; |
| +} |
| + |
| +namespace contacts { |
| + |
| +class Contact; |
| +typedef std::vector<const Contact*> ContactPointers; |
| + |
| +// Interface for classes providing persistent storage of Contact objects. |
| +class ContactDatabaseInterface { |
| + public: |
| + 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.
|
| + typedef base::Callback<void(const bool* success)> SaveCallback; |
| + typedef base::Callback<void(const bool* success, |
| + scoped_ptr<ScopedVector<Contact> >)> LoadCallback; |
| + |
| + ContactDatabaseInterface() {} |
| + |
| + // Asynchronously destroys the object after all in-progress file operations |
| + // have completed. |
| + virtual void DestroyOnUIThread() {} |
| + |
| + // Asynchronously initializes the object. |callback| will be invoked on the |
| + // UI thread when complete. |
| + virtual void Init(const FilePath& database_dir, InitCallback callback) = 0; |
| + |
| + // Asynchronously saves |contacts| to the database. If |is_full_update| is |
| + // true, all existing contacts in the database not present in |contacts| will |
| + // be removed. |callback| will be invoked on the UI thread when complete. |
| + // The caller must not make changes to the underlying passed-in Contact |
| + // objects until the callback has been invoked. |
| + virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, |
| + bool is_full_update, |
| + SaveCallback callback) = 0; |
| + |
| + // Asynchronously loads all contacts from the database and invokes |callback| |
| + // when complete. |
| + virtual void LoadContacts(LoadCallback callback) = 0; |
| + |
| + protected: |
| + virtual ~ContactDatabaseInterface() {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ContactDatabaseInterface); |
| +}; |
| + |
| +class ContactDatabase : public ContactDatabaseInterface { |
| + public: |
| + ContactDatabase(); |
| + |
| + // ContactDatabaseInterface implementation. |
| + virtual void DestroyOnUIThread() OVERRIDE; |
| + virtual void Init(const FilePath& database_dir, |
| + InitCallback callback) OVERRIDE; |
| + virtual void SaveContacts(scoped_ptr<ContactPointers> contacts, |
| + bool is_full_update, |
| + SaveCallback callback) OVERRIDE; |
| + virtual void LoadContacts(LoadCallback callback) OVERRIDE; |
| + |
| + protected: |
| + virtual ~ContactDatabase(); |
| + |
| + private: |
| + // Are we currently being run by |task_runner_|? |
| + bool IsRunByTaskRunner() const; |
| + |
| + // Deletes |this|. |
| + void DestroyFromTaskRunner(); |
| + |
| + // Runs |closure| after being called on the UI thread. Used for replies sent |
| + // in response to |task_runner_|'s tasks, so that weak_ptrs can be used to |
| + // avoid running the replies after the ContactDatabase has been deleted. |
| + void RunCallback(base::Closure closure); |
| + |
| + // Initializes the database in |database_dir| and updates |success|. |
| + void InitFromTaskRunner(const FilePath& database_dir, bool* success); |
| + |
| + // Saves |contacts| to disk and updates |success|. |
| + void SaveContactsFromTaskRunner(scoped_ptr<ContactPointers> contacts, |
| + bool is_full_update, |
| + bool* success); |
| + |
| + // Loads contacts from disk and updates |success|. |
| + void LoadContactsFromTaskRunner(bool* success, |
| + ScopedVector<Contact>* contacts_out); |
| + |
| + // Used to run blocking tasks in-order. |
| + scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| + |
| + 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.
|
| + |
| + scoped_ptr<leveldb::DB> db_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContactDatabase); |
| +}; |
| + |
| +} // namespace contacts |
| + |
| +#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |