Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3001)

Unified Diff: chrome/browser/chromeos/contacts/contact_database.h

Issue 10832064: contacts: Add contacts::ContactDatabase. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add callback-running methods Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8019e4a71ebc03ea997e98484247939666ccedda
--- /dev/null
+++ b/chrome/browser/chromeos/contacts/contact_database.h
@@ -0,0 +1,129 @@
+// 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(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();
+
+ // Passes the supplied parameters to |callback| 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 RunInitCallback(InitCallback callback, const bool* success);
+ void RunSaveCallback(SaveCallback callback, const bool* success);
+ void RunLoadCallback(LoadCallback callback,
+ const bool* success,
+ scoped_ptr<ScopedVector<Contact> > contacts);
+
+ // 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_;
+
+ scoped_ptr<leveldb::DB> db_;
+
+ // Note: This should remain the last member so it'll be destroyed and
+ // invalidate its weak pointers before any other members are destroyed.
+ base::WeakPtrFactory<ContactDatabase> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(ContactDatabase);
+};
+
+} // namespace contacts
+
+#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_CONTACT_DATABASE_H_

Powered by Google App Engine
This is Rietveld 408576698