| 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..d45a53d2cd2bc7b6bc3694bc7e8fd813143bf961
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/contacts/contact_database.h
|
| @@ -0,0 +1,147 @@
|
| +// 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 <string>
|
| +#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/synchronization/condition_variable.h"
|
| +#include "base/synchronization/lock.h"
|
| +#include "sql/connection.h"
|
| +#include "sql/init_status.h"
|
| +
|
| +namespace sql {
|
| +class Connection;
|
| +class MetaTable;
|
| +}
|
| +
|
| +namespace contacts {
|
| +
|
| +struct 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;
|
| +
|
| + // 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() {}
|
| + virtual ~ContactDatabaseInterface() {}
|
| +
|
| + // Asynchronously initializes the object. |callback| will be invoked on the
|
| + // UI thread when complete with a true parameter if initialization succeeded.
|
| + virtual void Init(const FilePath& database_path, InitCallback callback) = 0;
|
| +
|
| + // Asynchronously saves |contacts| to the database. |callback| will be
|
| + // invoked on the UI thread when complete. The caller must not make changes
|
| + // to the underlying passed-in contacts 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:
|
| + ContactDatabase();
|
| + virtual ~ContactDatabase();
|
| +
|
| + // ContactDatabaseInterface implementation.
|
| + virtual void Init(const FilePath& database_path,
|
| + 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_path, InitCallback callback);
|
| +
|
| + // Helper method called by InitOnFileThread().
|
| + sql::InitStatus InitInternal(const FilePath& database_path);
|
| +
|
| + // Creates our set of tables in |db| if they don't already exist.
|
| + // Called by InitInternal(). Returns false on failure.
|
| + bool CreateTablesIfNeeded();
|
| +
|
| + // Queries the database to find the |contact_id| field for |contact|.
|
| + // Returns -1 if it's not present or if an error occurs.
|
| + int64 GetContactId(const Contact& contact);
|
| +
|
| + // Saves |contacts| to disk and invokes |callback| on the UI thread when
|
| + // complete. Ownership of |contacts| remains with the caller (or with the
|
| + // callback itself, in this case).
|
| + void SaveContactsOnFileThread(scoped_ptr<ContactPointers> contacts,
|
| + bool is_full_update,
|
| + SaveCallback callback);
|
| +
|
| + // Helper method called by SaveContactsOnFileThread(). Returns true on
|
| + // success.
|
| + bool SaveContactsInternal(const ContactPointers& contacts,
|
| + bool is_full_update);
|
| +
|
| + // Clears all contact-related information from the database.
|
| + bool ClearContacts();
|
| +
|
| + // Saves |contact| to the database, returning true on success. This method
|
| + // performs multiple queries, so a transaction must be open before it's
|
| + // called. If |possibly_exists| is false, we don't bother checking to see if
|
| + // there's an existing row to update (i.e. we're doing a full update and have
|
| + // already cleared the tables).
|
| + bool SaveContact(const Contact& contact, bool possibly_exists);
|
| +
|
| + // 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<sql::Connection> db_;
|
| + scoped_ptr<sql::MetaTable> meta_table_;
|
| +
|
| + // Protects |num_outstanding_file_operations_|.
|
| + base::Lock lock_;
|
| +
|
| + // Signals on |lock_| when a file operation completes.
|
| + base::ConditionVariable condition_variable_;
|
| +
|
| + // 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_
|
|
|