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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
// 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_GDATA_GDATA_CACHE_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_
#pragma once
#include <map>
#include <string>
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/platform_file.h"
#include "base/threading/sequenced_worker_pool.h"
class Profile;
namespace gdata {
class GDataCacheMetadata;
// Callback for SetMountedStateOnUIThread.
typedef base::Callback<void(base::PlatformFileError error,
const FilePath& file_path)> SetMountedStateCallback;
// Callback for completion of cache operation.
typedef base::Callback<void(base::PlatformFileError error,
const std::string& resource_id,
const std::string& md5)> CacheOperationCallback;
// Callback for GetFileFromCache.
typedef base::Callback<void(base::PlatformFileError error,
const std::string& resource_id,
const std::string& md5,
const FilePath& cache_file_path)>
GetFileFromCacheCallback;
// Callback for GetResourceIdsOfBacklogOnUIThread.
// |to_fetch| is for resource IDs of pinned-but-not-fetched files.
// |to_upload| is for resource IDs of dirty-but-not-uploaded files.
typedef base::Callback<void(const std::vector<std::string>& to_fetch,
const std::vector<std::string>& to_upload)>
GetResourceIdsCallback;
// GDataCache is used to maintain cache states of GDataFileSystem.
//
// All non-static public member functions, unless mentioned otherwise (see
// GetCacheFilePath() for example), should be called from the sequenced
// worker pool with the sequence token set by CreateGDataCacheOnUIThread(). This
// threading model is enforced by AssertOnSequencedWorkerPool().
//
// TODO(hashimoto): Change threading model of this class to make public methods
// being called on UI thread unless mentioned otherwise. crbug.com/132926
class GDataCache {
public:
// Enum defining GCache subdirectory location.
// This indexes into |GDataCache::cache_paths_| vector.
enum CacheSubDirectoryType {
CACHE_TYPE_META = 0, // Downloaded feeds.
CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are
// pinned, or to /dev/null for non-existent
// files.
CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to
// be uploaded.
CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally,
// not evictable, hopefully.
CACHE_TYPE_TMP, // Files that don't meet criteria to be in
// persistent dir, and hence evictable.
CACHE_TYPE_TMP_DOWNLOADS, // Downloaded files.
CACHE_TYPE_TMP_DOCUMENTS, // Temporary JSON files for hosted documents.
NUM_CACHE_TYPES, // This must be at the end.
};
// This is used as a bitmask for the cache state.
enum CacheState {
CACHE_STATE_NONE = 0x0,
CACHE_STATE_PINNED = 0x1 << 0,
CACHE_STATE_PRESENT = 0x1 << 1,
CACHE_STATE_DIRTY = 0x1 << 2,
CACHE_STATE_MOUNTED = 0x1 << 3,
};
// Enum defining origin of a cached file.
enum CachedFileOrigin {
CACHED_FILE_FROM_SERVER = 0,
CACHED_FILE_LOCALLY_MODIFIED,
CACHED_FILE_MOUNTED,
};
// Enum defining type of file operation e.g. copy or move, etc.
enum FileOperationType {
FILE_OPERATION_MOVE = 0,
FILE_OPERATION_COPY,
};
// Used to notify events. All events are notified on UI thread.
class Observer {
public:
// Triggered when a file has been pinned successfully.
virtual void OnCachePinned(const std::string& resource_id,
const std::string& md5) {}
// Triggered when a file has been unpinned successfully.
virtual void OnCacheUnpinned(const std::string& resource_id,
const std::string& md5) {}
// Triggered when a dirty file has been committed (saved) successfully.
virtual void OnCacheCommitted(const std::string& resource_id) {}
protected:
virtual ~Observer() {}
};
// Structure to store information of an existing cache file.
struct CacheEntry {
CacheEntry() : sub_dir_type(CACHE_TYPE_META),
cache_state(0) {}
CacheEntry(const std::string& md5,
CacheSubDirectoryType sub_dir_type,
int cache_state)
: md5(md5),
sub_dir_type(sub_dir_type),
cache_state(cache_state) {
}
bool IsPresent() const { return IsCachePresent(cache_state); }
bool IsPinned() const { return IsCachePinned(cache_state); }
bool IsDirty() const { return IsCacheDirty(cache_state); }
bool IsMounted() const { return IsCacheMounted(cache_state); }
// For debugging purposes.
std::string ToString() const;
std::string md5;
CacheSubDirectoryType sub_dir_type;
int cache_state;
};
// Callback for GetCacheEntryOnUIThread.
// |success| indicates if the operation was successful.
// |cache_entry| is the obtained cache entry.
//
// TODO(satorux): Unlike other callback types, this has to be defined
// inside GDataCache as CacheEntry is inside GDataCache. We should get them
// outside of GDataCache.
typedef base::Callback<void(bool success, const CacheEntry& cache_entry)>
GetCacheEntryCallback;
static bool IsCachePresent(int cache_state) {
return cache_state & CACHE_STATE_PRESENT;
}
static bool IsCachePinned(int cache_state) {
return cache_state & CACHE_STATE_PINNED;
}
static bool IsCacheDirty(int cache_state) {
return cache_state & CACHE_STATE_DIRTY;
}
static bool IsCacheMounted(int cache_state) {
return cache_state & CACHE_STATE_MOUNTED;
}
static int SetCachePresent(int cache_state) {
return cache_state |= CACHE_STATE_PRESENT;
}
static int SetCachePinned(int cache_state) {
return cache_state |= CACHE_STATE_PINNED;
}
static int SetCacheDirty(int cache_state) {
return cache_state |= CACHE_STATE_DIRTY;
}
static int SetCacheMounted(int cache_state) {
return cache_state |= CACHE_STATE_MOUNTED;
}
static int ClearCachePresent(int cache_state) {
return cache_state &= ~CACHE_STATE_PRESENT;
}
static int ClearCachePinned(int cache_state) {
return cache_state &= ~CACHE_STATE_PINNED;
}
static int ClearCacheDirty(int cache_state) {
return cache_state &= ~CACHE_STATE_DIRTY;
}
static int ClearCacheMounted(int cache_state) {
return cache_state &= ~CACHE_STATE_MOUNTED;
}
// Returns the sub-directory under gdata cache directory for the given sub
// directory type. Example: <user_profile_dir>/GCache/v1/tmp
//
// Can be called on any thread.
FilePath GetCacheDirectoryPath(CacheSubDirectoryType sub_dir_type) const;
// Returns absolute path of the file if it were cached or to be cached.
//
// Can be called on any thread.
FilePath GetCacheFilePath(const std::string& resource_id,
const std::string& md5,
CacheSubDirectoryType sub_dir_type,
CachedFileOrigin file_orign) const;
// Returns true if the given path is under gdata cache directory, i.e.
// <user_profile_dir>/GCache/v1
//
// Can be called on any thread.
bool IsUnderGDataCacheDirectory(const FilePath& path) const;
// Adds observer.
// Must be called on UI thread.
void AddObserver(Observer* observer);
// Removes observer.
// Must be called on UI thread.
void RemoveObserver(Observer* observer);
// Gets the cache entry by the given resource ID and MD5.
// See also GetCacheEntry().
//
// Must be called on UI thread. |callback| is run on UI thread.
void GetCacheEntryOnUIThread(
const std::string& resource_id,
const std::string& md5,
const GetCacheEntryCallback& callback);
// Gets the resource IDs of pinned-but-not-fetched files and
// dirty-but-not-uploaded files.
//
// Must be called on UI thread. |callback| is run on UI thread.
void GetResourceIdsOfBacklogOnUIThread(
const GetResourceIdsCallback& callback);
// Frees up disk space to store the given number of bytes, while keeping
// kMinFreSpace bytes on the disk, if needed. |has_enough_space| is
// updated to indicate if we have enough space.
void FreeDiskSpaceIfNeededFor(int64 num_bytes,
bool* has_enough_space);
// Checks if file corresponding to |resource_id| and |md5| exists in cache.
void GetFileOnUIThread(const std::string& resource_id,
const std::string& md5,
const GetFileFromCacheCallback& callback);
// Modifies cache state, which involves the following:
// - moves or copies (per |file_operation_type|) |source_path|
// to |dest_path| in the cache dir
// - if necessary, creates symlink
// - deletes stale cached versions of |resource_id| in
// |dest_path|'s directory.
void StoreOnUIThread(const std::string& resource_id,
const std::string& md5,
const FilePath& source_path,
FileOperationType file_operation_type,
const CacheOperationCallback& callback);
// Modifies cache state, which involves the following:
// - moves |source_path| to |dest_path| in persistent dir if
// file is not dirty
// - creates symlink in pinned dir that references downloaded or locally
// modified file
void PinOnUIThread(const std::string& resource_id,
const std::string& md5,
const CacheOperationCallback& callback);
// Modifies cache state, which involves the following:
// - moves |source_path| to |dest_path| in tmp dir if file is not dirty
// - deletes symlink from pinned dir
void UnpinOnUIThread(const std::string& resource_id,
const std::string& md5,
const CacheOperationCallback& callback);
// Modifies cache state, which involves the following:
// - moves |source_path| to |dest_path|, where
// if we're mounting: |source_path| is the unmounted path and has .<md5>
// extension, and |dest_path| is the mounted path in persistent dir
// and has .<md5>.mounted extension;
// if we're unmounting: the opposite is true for the two paths, i.e.
// |dest_path| is the mounted path and |source_path| the unmounted path.
void SetMountedStateOnUIThread(const FilePath& file_path,
bool to_mount,
const SetMountedStateCallback& callback);
// Modifies cache state, which involves the following:
// - moves |source_path| to |dest_path| in persistent dir, where
// |source_path| has .<md5> extension and |dest_path| has .local extension
// - if file is pinned, updates symlink in pinned dir to reference dirty file
void MarkDirtyOnUIThread(const std::string& resource_id,
const std::string& md5,
const GetFileFromCacheCallback& callback);
// Modifies cache state, i.e. creates symlink in outgoing
// dir to reference dirty file in persistent dir.
void CommitDirtyOnUIThread(const std::string& resource_id,
const std::string& md5,
const CacheOperationCallback& callback);
// Modifies cache state, which involves the following:
// - moves |source_path| to |dest_path| in persistent dir if
// file is pinned or tmp dir otherwise, where |source_path| has .local
// extension and |dest_path| has .<md5> extension
// - deletes symlink in outgoing dir
// - if file is pinned, updates symlink in pinned dir to reference
// |dest_path|
void ClearDirtyOnUIThread(const std::string& resource_id,
const std::string& md5,
const CacheOperationCallback& callback);
// Does the following:
// - remove all delete stale cache versions corresponding to |resource_id| in
// persistent, tmp and pinned directories
// - remove entry corresponding to |resource_id| from cache map.
void RemoveOnUIThread(const std::string& resource_id,
const CacheOperationCallback& callback);
// Utility method to call Initialize on UI thread.
void RequestInitializeOnUIThread();
// Returns the cache entry for file corresponding to |resource_id| and |md5|
// if entry exists in cache map. Otherwise, returns NULL.
// |md5| can be empty if only matching |resource_id| is desired, which may
// happen when looking for pinned entries where symlinks' filenames have no
// extension and hence no md5.
scoped_ptr<CacheEntry> GetCacheEntry(const std::string& resource_id,
const std::string& md5);
// Factory methods for GDataCache.
// |pool| and |sequence_token| are used to assert that the functions are
// called on the right sequenced worker pool with the right sequence token.
//
// For testing, the thread assertion can be disabled by passing NULL and
// the default value of SequenceToken.
static GDataCache* CreateGDataCacheOnUIThread(
const FilePath& cache_root_path,
base::SequencedWorkerPool* pool,
const base::SequencedWorkerPool::SequenceToken& sequence_token);
// Deletes the cache.
void DestroyOnUIThread();
// Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the
// profile.
// TODO(satorux): Write a unit test for this.
static FilePath GetCacheRootPath(Profile* profile);
private:
GDataCache(
const FilePath& cache_root_path,
base::SequencedWorkerPool* pool_,
const base::SequencedWorkerPool::SequenceToken& sequence_token);
virtual ~GDataCache();
// Checks whether the current thread is on the right sequenced worker pool
// with the right sequence ID. If not, DCHECK will fail.
void AssertOnSequencedWorkerPool();
// Initializes the cache.
void Initialize();
// Deletes the cache.
void Destroy();
// Used to implement GetResourceIdsOfBacklogOnUIThread.
void GetResourceIdsOfBacklog(
std::vector<std::string>* to_fetch,
std::vector<std::string>* to_upload);
// Used to implement GetFileOnUIThread.
void GetFile(const std::string& resource_id,
const std::string& md5,
base::PlatformFileError* error,
FilePath* cache_file_path);
// Used to implement StoreOnUIThread.
void Store(const std::string& resource_id,
const std::string& md5,
const FilePath& source_path,
FileOperationType file_operation_type,
base::PlatformFileError* error);
// Used to implement PinOnUIThread.
void Pin(const std::string& resource_id,
const std::string& md5,
FileOperationType file_operation_type,
base::PlatformFileError* error);
// Used to implement UnpinOnUIThread.
void Unpin(const std::string& resource_id,
const std::string& md5,
FileOperationType file_operation_type,
base::PlatformFileError* error);
// Used to implement SetMountedStateOnUIThread.
void SetMountedState(const FilePath& file_path,
bool to_mount,
base::PlatformFileError* error,
FilePath* cache_file_path);
// Used to implement MarkDirtyOnUIThread.
void MarkDirty(const std::string& resource_id,
const std::string& md5,
FileOperationType file_operation_type,
base::PlatformFileError* error,
FilePath* cache_file_path);
// Used to implement CommitDirtyOnUIThread.
void CommitDirty(const std::string& resource_id,
const std::string& md5,
FileOperationType file_operation_type,
base::PlatformFileError* error);
// Used to implement ClearDirtyOnUIThread.
void ClearDirty(const std::string& resource_id,
const std::string& md5,
FileOperationType file_operation_type,
base::PlatformFileError* error);
// Used to implement RemoveOnUIThread.
void Remove(const std::string& resource_id,
base::PlatformFileError* error);
// Runs callback and notifies the observers when file is pinned.
void OnPinned(base::PlatformFileError* error,
const std::string& resource_id,
const std::string& md5,
const CacheOperationCallback& callback);
// Runs callback and notifies the observers when file is unpinned.
void OnUnpinned(base::PlatformFileError* error,
const std::string& resource_id,
const std::string& md5,
const CacheOperationCallback& callback);
// Runs callback and notifies the observers when file is committed.
void OnCommitDirty(base::PlatformFileError* error,
const std::string& resource_id,
const std::string& md5,
const CacheOperationCallback& callback);
// Helper function to implement GetCacheEntryOnUIThread().
void GetCacheEntryHelper(const std::string& resource_id,
const std::string& md5,
bool* success,
GDataCache::CacheEntry* cache_entry);
// The root directory of the cache (i.e. <user_profile_dir>/GCache/v1).
const FilePath cache_root_path_;
// Paths for all subdirectories of GCache, one for each
// GDataCache::CacheSubDirectoryType enum.
const std::vector<FilePath> cache_paths_;
base::SequencedWorkerPool* pool_;
const base::SequencedWorkerPool::SequenceToken sequence_token_;
// The cache state data. This member must be access only on the blocking pool.
scoped_ptr<GDataCacheMetadata> metadata_;
// WeakPtrFactory and WeakPtr bound to the UI thread.
base::WeakPtrFactory<GDataCache> ui_weak_ptr_factory_;
base::WeakPtr<GDataCache> ui_weak_ptr_;
// List of observers, this member must be accessed on UI thread.
ObserverList<Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(GDataCache);
};
// The minimum free space to keep. GDataFileSystem::GetFileByPath() returns
// base::PLATFORM_FILE_ERROR_NO_SPACE if the available space is smaller than
// this value.
//
// Copied from cryptohome/homedirs.h.
// TODO(satorux): Share the constant.
const int64 kMinFreeSpace = 512 * 1LL << 20;
// Interface class used for getting the free disk space. Only for testing.
class FreeDiskSpaceGetterInterface {
public:
virtual ~FreeDiskSpaceGetterInterface() {}
virtual int64 AmountOfFreeDiskSpace() const = 0;
};
// Sets the free disk space getter for testing.
// The existing getter is deleted.
void SetFreeDiskSpaceGetterForTesting(
FreeDiskSpaceGetterInterface* getter);
} // namespace gdata
#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_
|