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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
// 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.
#include "rlz/chromeos/lib/rlz_value_store_chromeos.h"
#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "rlz/lib/lib_values.h"
#include "rlz/lib/recursive_cross_process_lock_posix.h"
#include "rlz/lib/rlz_lib.h"
namespace rlz_lib {
namespace {
// Key names.
const char kPingTimeKey[] = "ping_time";
const char kAccessPointKey[] = "access_points";
const char kProductEventKey[] = "product_events";
const char kStatefulEventKey[] = "stateful_events";
// Brand name used when there is no supplementary brand name.
const char kNoSupplementaryBrand[] = "_";
// RLZ store filename.
const base::FilePath::CharType kRLZDataFileName[] =
FILE_PATH_LITERAL("RLZ Data");
// RLZ store lock filename
const base::FilePath::CharType kRLZLockFileName[] =
FILE_PATH_LITERAL("RLZ Data.lock");
// RLZ store path for testing.
base::FilePath g_testing_rlz_store_path_;
// Returns file path of the RLZ storage.
base::FilePath GetRlzStorePath() {
base::FilePath homedir;
PathService::Get(base::DIR_HOME, &homedir);
return g_testing_rlz_store_path_.empty() ?
homedir.Append(kRLZDataFileName) :
g_testing_rlz_store_path_.Append(kRLZDataFileName);
}
// Returns file path of the RLZ storage lock file.
base::FilePath GetRlzStoreLockPath() {
base::FilePath homedir;
PathService::Get(base::DIR_HOME, &homedir);
return g_testing_rlz_store_path_.empty() ?
homedir.Append(kRLZLockFileName) :
g_testing_rlz_store_path_.Append(kRLZLockFileName);
}
// Returns the dictionary key for storing access point-related prefs.
std::string GetKeyName(std::string key, AccessPoint access_point) {
std::string brand = SupplementaryBranding::GetBrand();
if (brand.empty())
brand = kNoSupplementaryBrand;
return key + "." + GetAccessPointName(access_point) + "." + brand;
}
// Returns the dictionary key for storing product-related prefs.
std::string GetKeyName(std::string key, Product product) {
std::string brand = SupplementaryBranding::GetBrand();
if (brand.empty())
brand = kNoSupplementaryBrand;
return key + "." + GetProductName(product) + "." + brand;
}
} // namespace
RlzValueStoreChromeOS::RlzValueStoreChromeOS(const base::FilePath& store_path)
: rlz_store_(new base::DictionaryValue),
store_path_(store_path),
read_only_(true) {
ReadStore();
}
RlzValueStoreChromeOS::~RlzValueStoreChromeOS() {
WriteStore();
}
bool RlzValueStoreChromeOS::HasAccess(AccessType type) {
DCHECK(CalledOnValidThread());
return type == kReadAccess || !read_only_;
}
bool RlzValueStoreChromeOS::WritePingTime(Product product, int64 time) {
DCHECK(CalledOnValidThread());
rlz_store_->SetString(GetKeyName(kPingTimeKey, product),
base::Int64ToString(time));
return true;
}
bool RlzValueStoreChromeOS::ReadPingTime(Product product, int64* time) {
DCHECK(CalledOnValidThread());
std::string ping_time;
return rlz_store_->GetString(GetKeyName(kPingTimeKey, product), &ping_time) &&
base::StringToInt64(ping_time, time);
}
bool RlzValueStoreChromeOS::ClearPingTime(Product product) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kPingTimeKey, product), NULL);
return true;
}
bool RlzValueStoreChromeOS::WriteAccessPointRlz(AccessPoint access_point,
const char* new_rlz) {
DCHECK(CalledOnValidThread());
rlz_store_->SetString(
GetKeyName(kAccessPointKey, access_point), new_rlz);
return true;
}
bool RlzValueStoreChromeOS::ReadAccessPointRlz(AccessPoint access_point,
char* rlz,
size_t rlz_size) {
DCHECK(CalledOnValidThread());
std::string rlz_value;
rlz_store_->GetString(GetKeyName(kAccessPointKey, access_point), &rlz_value);
if (rlz_value.size() < rlz_size) {
strncpy(rlz, rlz_value.c_str(), rlz_size);
return true;
}
if (rlz_size > 0)
*rlz = '\0';
return false;
}
bool RlzValueStoreChromeOS::ClearAccessPointRlz(AccessPoint access_point) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kAccessPointKey, access_point), NULL);
return true;
}
bool RlzValueStoreChromeOS::AddProductEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
return AddValueToList(GetKeyName(kProductEventKey, product),
new base::StringValue(event_rlz));
}
bool RlzValueStoreChromeOS::ReadProductEvents(
Product product,
std::vector<std::string>* events) {
DCHECK(CalledOnValidThread());
base::ListValue* events_list = NULL; ;
if (!rlz_store_->GetList(GetKeyName(kProductEventKey, product), &events_list))
return false;
events->clear();
for (size_t i = 0; i < events_list->GetSize(); ++i) {
std::string event;
if (events_list->GetString(i, &event))
events->push_back(event);
}
return true;
}
bool RlzValueStoreChromeOS::ClearProductEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
base::StringValue event_value(event_rlz);
return RemoveValueFromList(GetKeyName(kProductEventKey, product),
event_value);
}
bool RlzValueStoreChromeOS::ClearAllProductEvents(Product product) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kProductEventKey, product), NULL);
return true;
}
bool RlzValueStoreChromeOS::AddStatefulEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
return AddValueToList(GetKeyName(kStatefulEventKey, product),
new base::StringValue(event_rlz));
}
bool RlzValueStoreChromeOS::IsStatefulEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
base::StringValue event_value(event_rlz);
base::ListValue* events_list = NULL;
return rlz_store_->GetList(GetKeyName(kStatefulEventKey, product),
&events_list) &&
events_list->Find(event_value) != events_list->end();
}
bool RlzValueStoreChromeOS::ClearAllStatefulEvents(Product product) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kStatefulEventKey, product), NULL);
return true;
}
void RlzValueStoreChromeOS::CollectGarbage() {
DCHECK(CalledOnValidThread());
NOTIMPLEMENTED();
}
void RlzValueStoreChromeOS::ReadStore() {
int error_code = 0;
std::string error_msg;
JSONFileValueDeserializer deserializer(store_path_);
scoped_ptr<base::Value> value(
deserializer.Deserialize(&error_code, &error_msg));
switch (error_code) {
case JSONFileValueDeserializer::JSON_NO_SUCH_FILE:
read_only_ = false;
break;
case JSONFileValueDeserializer::JSON_NO_ERROR:
read_only_ = false;
rlz_store_.reset(static_cast<base::DictionaryValue*>(value.release()));
break;
default:
LOG(ERROR) << "Error reading RLZ store: " << error_msg;
}
}
void RlzValueStoreChromeOS::WriteStore() {
std::string json_data;
JSONStringValueSerializer serializer(&json_data);
serializer.set_pretty_print(true);
scoped_ptr<base::DictionaryValue> copy(
rlz_store_->DeepCopyWithoutEmptyChildren());
if (!serializer.Serialize(*copy.get())) {
LOG(ERROR) << "Failed to serialize RLZ data";
NOTREACHED();
return;
}
if (!base::ImportantFileWriter::WriteFileAtomically(store_path_, json_data))
LOG(ERROR) << "Error writing RLZ store";
}
bool RlzValueStoreChromeOS::AddValueToList(std::string list_name,
base::Value* value) {
base::ListValue* list_value = NULL;
if (!rlz_store_->GetList(list_name, &list_value)) {
list_value = new base::ListValue;
rlz_store_->Set(list_name, list_value);
}
list_value->AppendIfNotPresent(value);
return true;
}
bool RlzValueStoreChromeOS::RemoveValueFromList(std::string list_name,
const base::Value& value) {
base::ListValue* list_value = NULL;
if (!rlz_store_->GetList(list_name, &list_value))
return false;
size_t index;
list_value->Remove(value, &index);
return true;
}
namespace {
// RlzValueStoreChromeOS keeps its data in memory and only writes it to disk
// when ScopedRlzValueStoreLock goes out of scope. Hence, if several
// ScopedRlzValueStoreLocks are nested, they all need to use the same store
// object.
RecursiveCrossProcessLock g_recursive_lock =
RECURSIVE_CROSS_PROCESS_LOCK_INITIALIZER;
// This counts the nesting depth of |ScopedRlzValueStoreLock|.
int g_lock_depth = 0;
// This is the shared store object. Non-|NULL| only when |g_lock_depth > 0|.
RlzValueStoreChromeOS* g_store = NULL;
} // namespace
ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() {
bool got_cross_process_lock =
g_recursive_lock.TryGetCrossProcessLock(GetRlzStoreLockPath());
// At this point, we hold the in-process lock, no matter the value of
// |got_cross_process_lock|.
++g_lock_depth;
if (!got_cross_process_lock) {
// Acquiring cross-process lock failed, so simply return here.
// In-process lock will be released in dtor.
DCHECK(!g_store);
return;
}
if (g_lock_depth > 1) {
// Reuse the already existing store object.
DCHECK(g_store);
store_.reset(g_store);
return;
}
// This is the topmost lock, create a new store object.
DCHECK(!g_store);
g_store = new RlzValueStoreChromeOS(GetRlzStorePath());
store_.reset(g_store);
}
ScopedRlzValueStoreLock::~ScopedRlzValueStoreLock() {
--g_lock_depth;
DCHECK(g_lock_depth >= 0);
if (g_lock_depth > 0) {
// Other locks are still using store_, so don't free it yet.
ignore_result(store_.release());
return;
}
g_store = NULL;
g_recursive_lock.ReleaseLock();
}
RlzValueStore* ScopedRlzValueStoreLock::GetStore() {
return store_.get();
}
namespace testing {
void SetRlzStoreDirectory(const base::FilePath& directory) {
g_testing_rlz_store_path_ = directory;
}
std::string RlzStoreFilenameStr() {
return GetRlzStorePath().value();
}
} // namespace testing
} // namespace rlz_lib
|