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..501b3353ef5ddd176e05dda275a1a1db81f06a6d |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/contacts/contact_database.h |
| @@ -0,0 +1,123 @@ |
| +// 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" |
|
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.
|
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.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(bool success)> InitCallback; |
| + typedef base::Callback<void(bool success)> SaveCallback; |
| + typedef base::Callback<void(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(); |
| + |
| + // Initializes the database in |database_dir| and posts |callback| on the UI |
| + // thread with the result. |
| + void InitFromTaskRunner(const FilePath& database_dir, InitCallback callback); |
| + |
| + // Helper method called by InitFromTaskRunner(). Returns true on success. |
| + bool InitInternal(const FilePath& database_dir); |
| + |
| + // Saves |contacts| to disk and invokes |callback| on the UI thread when |
| + // complete. |
| + void SaveContactsFromTaskRunner(scoped_ptr<ContactPointers> contacts, |
| + bool is_full_update, |
| + SaveCallback callback); |
| + |
| + // Loads contacts from disk and passes them to |callback| on the UI thread |
| + // when complete. |
| + void LoadContactsFromTaskRunner(LoadCallback callback); |
| + |
| + // Helper method called by LoadContactsFromTaskRunner(). Returns true on |
| + // success. |
| + bool LoadContactsInternal(ScopedVector<Contact>* contacts); |
| + |
| + // Used to run blocking tasks in-order. |
| + scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| + |
| + scoped_ptr<leveldb::DB> db_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContactDatabase); |
| +}; |
| + |
| +} // namespace contacts |
| + |
| +#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |