1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// 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/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;
class UpdateMetadata;
typedef std::vector<const Contact*> ContactPointers;
// Interface for classes providing persistent storage of Contact objects.
class ContactDatabaseInterface {
public:
typedef std::vector<std::string> ContactIds;
typedef base::Callback<void(bool success)> InitCallback;
typedef base::Callback<void(bool success)> SaveCallback;
typedef base::Callback<void(bool success,
scoped_ptr<ScopedVector<Contact> >,
scoped_ptr<UpdateMetadata>)>
LoadCallback;
// 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 base::FilePath& database_dir,
InitCallback callback) = 0;
// Asynchronously saves |contacts_to_save| and |metadata| to the database and
// removes contacts with IDs contained in |contact_ids_to_delete|. If
// |is_full_update| is true, all existing contacts in the database not present
// in |contacts_to_save| 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_to_save,
scoped_ptr<ContactIds> contact_ids_to_delete,
scoped_ptr<UpdateMetadata> metadata,
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() {}
};
class ContactDatabase : public ContactDatabaseInterface {
public:
ContactDatabase();
// ContactDatabaseInterface implementation.
virtual void DestroyOnUIThread() OVERRIDE;
virtual void Init(const base::FilePath& database_dir,
InitCallback callback) OVERRIDE;
virtual void SaveContacts(scoped_ptr<ContactPointers> contacts_to_save,
scoped_ptr<ContactIds> contact_ids_to_delete,
scoped_ptr<UpdateMetadata> metadata,
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,
scoped_ptr<UpdateMetadata> metadata);
// Initializes the database in |database_dir| and updates |success|.
void InitFromTaskRunner(const base::FilePath& database_dir, bool* success);
// Saves data to disk and updates |success|.
void SaveContactsFromTaskRunner(scoped_ptr<ContactPointers> contacts_to_save,
scoped_ptr<ContactIds> contact_ids_to_delete,
scoped_ptr<UpdateMetadata> metadata,
bool is_full_update,
bool* success);
// Loads contacts from disk and updates |success|.
void LoadContactsFromTaskRunner(bool* success,
ScopedVector<Contact>* contacts,
UpdateMetadata* metadata);
// 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_
|