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..208377fc15a940727208c4b6d4b6744e1dc60734 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/contacts/contact_database.h |
| @@ -0,0 +1,124 @@ |
| +// 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/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/synchronization/condition_variable.h" |
| +#include "base/synchronization/lock.h" |
| + |
| +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: |
| + // The parameter is true on success and false on failure. |
| + typedef base::Callback<void(bool)> InitCallback; |
|
satorux1
2012/07/30 17:15:07
Having a parameter name would make it a bit more i
Daniel Erat
2012/07/30 22:12:09
Thanks, didn't know that this works! Done.
|
| + |
| + // The parameter is true on success and false on failure. |
| + typedef base::Callback<void(bool)> SaveCallback; |
| + |
| + // The first parameter is true on success and false on failure. |
| + typedef base::Callback<void(bool, scoped_ptr<ScopedVector<Contact> >)> |
| + LoadCallback; |
| + |
| + ContactDatabaseInterface() {} |
| + |
| + // Destruction blocks until all outstanding file operations are complete. |
| + virtual ~ContactDatabaseInterface() {} |
| + |
| + // 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; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ContactDatabaseInterface); |
| +}; |
| + |
| +class ContactDatabase : public ContactDatabaseInterface, |
| + public base::SupportsWeakPtr<ContactDatabase> { |
| + public: |
| + ContactDatabase(); |
| + virtual ~ContactDatabase(); |
| + |
| + // ContactDatabaseInterface implementation. |
| + 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; |
| + |
| + private: |
| + // Initializes the database in |database_dir| and posts |callback| on the UI |
| + // thread with the result. |
| + void InitOnFileThread(const FilePath& database_dir, InitCallback callback); |
| + |
| + // Helper method called by InitOnFileThread(). Returns true on success. |
| + bool InitInternal(const FilePath& database_dir); |
| + |
| + // Saves |contacts| to disk and invokes |callback| on the UI thread when |
| + // complete. |
| + void SaveContactsOnFileThread(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 LoadContactsOnFileThread(LoadCallback callback); |
| + |
| + // Helper method called by LoadContactsOnFileThread(). Returns true on |
| + // success. |
| + bool LoadContactsInternal(ScopedVector<Contact>* contacts); |
| + |
| + // Increments or decrements |num_outstanding_file_operations_|. |
| + void IncrementOutstandingFileOperations(); |
| + void DecrementOutstandingFileOperations(); |
| + |
| + scoped_ptr<leveldb::DB> db_; |
| + |
| + // Protects |num_outstanding_file_operations_|. |
| + base::Lock lock_; |
| + |
| + // Signals on |lock_| when a file operation completes. |
| + base::ConditionVariable condition_variable_; |
|
satorux1
2012/07/30 17:15:07
I'm very wary of Lock and friends. Can you go with
Daniel Erat
2012/07/30 22:12:09
Done.
|
| + |
| + // Number of pending or in-progress operations on the file thread. |
| + int num_outstanding_file_operations_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContactDatabase); |
| +}; |
| + |
| +} // namespace contacts |
| + |
| +#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_ |