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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_cache.h

Issue 10877005: Rename GDataCache* to DriveCache* (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase. Created 8 years, 4 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/callback.h"
13 #include "base/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/platform_file.h"
18 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h"
19
20 class Profile;
21
22 namespace base {
23
24 class SequencedTaskRunner;
25
26 } // namespace base
27
28 namespace gdata {
29
30 class DriveCacheEntry;
31 class GDataCacheMetadata;
32
33 // Callback for SetMountedStateOnUIThread and ClearAllOnUIThread.
34 typedef base::Callback<void(GDataFileError error,
35 const FilePath& file_path)>
36 ChangeCacheStateCallback;
37
38 // Callback for completion of cache operation.
39 typedef base::Callback<void(GDataFileError error,
40 const std::string& resource_id,
41 const std::string& md5)> CacheOperationCallback;
42
43 // Callback for GetFileFromCache.
44 typedef base::Callback<void(GDataFileError error,
45 const std::string& resource_id,
46 const std::string& md5,
47 const FilePath& cache_file_path)>
48 GetFileFromCacheCallback;
49
50 // Callback for GetResourceIdsOfBacklogOnUIThread.
51 // |to_fetch| is for resource IDs of pinned-but-not-fetched files.
52 // |to_upload| is for resource IDs of dirty-but-not-uploaded files.
53 typedef base::Callback<void(const std::vector<std::string>& to_fetch,
54 const std::vector<std::string>& to_upload)>
55 GetResourceIdsOfBacklogCallback;
56
57 // Callback for GetResourceIdsOfExistingPinnedFilesOnUIThread.
58 typedef base::Callback<void(const std::vector<std::string>& resource_ids)>
59 GetResourceIdsCallback;
60
61 // Callback for GetCacheEntryOnUIThread.
62 // |success| indicates if the operation was successful.
63 // |cache_entry| is the obtained cache entry. On failure, |cache_state| is
64 // set to TEST_CACHE_STATE_NONE.
65 typedef base::Callback<void(bool success, const DriveCacheEntry& cache_entry)>
66 GetCacheEntryCallback;
67
68 // GDataCache is used to maintain cache states of GDataFileSystem.
69 //
70 // All non-static public member functions, unless mentioned otherwise (see
71 // GetCacheFilePath() for example), should be called from the sequenced
72 // worker pool with the sequence token set by CreateGDataCacheOnUIThread(). This
73 // threading model is enforced by AssertOnSequencedWorkerPool().
74 //
75 // TODO(hashimoto): Change threading model of this class to make public methods
76 // being called on UI thread unless mentioned otherwise. crbug.com/132926
77 class GDataCache {
78 public:
79 // Enum defining GCache subdirectory location.
80 // This indexes into |GDataCache::cache_paths_| vector.
81 enum CacheSubDirectoryType {
82 CACHE_TYPE_META = 0, // Downloaded feeds.
83 CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are
84 // pinned, or to /dev/null for non-existent
85 // files.
86 CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to
87 // be uploaded.
88 CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally,
89 // not evictable, hopefully.
90 CACHE_TYPE_TMP, // Files that don't meet criteria to be in
91 // persistent dir, and hence evictable.
92 CACHE_TYPE_TMP_DOWNLOADS, // Downloaded files.
93 CACHE_TYPE_TMP_DOCUMENTS, // Temporary JSON files for hosted documents.
94 NUM_CACHE_TYPES, // This must be at the end.
95 };
96
97 // Enum defining origin of a cached file.
98 enum CachedFileOrigin {
99 CACHED_FILE_FROM_SERVER = 0,
100 CACHED_FILE_LOCALLY_MODIFIED,
101 CACHED_FILE_MOUNTED,
102 };
103
104 // Enum defining type of file operation e.g. copy or move, etc.
105 enum FileOperationType {
106 FILE_OPERATION_MOVE = 0,
107 FILE_OPERATION_COPY,
108 };
109
110 // Used to notify events. All events are notified on UI thread.
111 class Observer {
112 public:
113 // Triggered when a file has been pinned successfully.
114 virtual void OnCachePinned(const std::string& resource_id,
115 const std::string& md5) {}
116
117 // Triggered when a file has been unpinned successfully.
118 virtual void OnCacheUnpinned(const std::string& resource_id,
119 const std::string& md5) {}
120
121 // Triggered when a dirty file has been committed (saved) successfully.
122 virtual void OnCacheCommitted(const std::string& resource_id) {}
123
124 protected:
125 virtual ~Observer() {}
126 };
127
128 // Returns the sub-directory under gdata cache directory for the given sub
129 // directory type. Example: <user_profile_dir>/GCache/v1/tmp
130 //
131 // Can be called on any thread.
132 FilePath GetCacheDirectoryPath(CacheSubDirectoryType sub_dir_type) const;
133
134 // Returns absolute path of the file if it were cached or to be cached.
135 //
136 // Can be called on any thread.
137 FilePath GetCacheFilePath(const std::string& resource_id,
138 const std::string& md5,
139 CacheSubDirectoryType sub_dir_type,
140 CachedFileOrigin file_orign) const;
141
142 // Returns true if the given path is under gdata cache directory, i.e.
143 // <user_profile_dir>/GCache/v1
144 //
145 // Can be called on any thread.
146 bool IsUnderGDataCacheDirectory(const FilePath& path) const;
147
148 // Adds observer.
149 // Must be called on UI thread.
150 void AddObserver(Observer* observer);
151
152 // Removes observer.
153 // Must be called on UI thread.
154 void RemoveObserver(Observer* observer);
155
156 // Gets the cache entry by the given resource ID and MD5.
157 // See also GetCacheEntry().
158 //
159 // Must be called on UI thread. |callback| is run on UI thread.
160 void GetCacheEntryOnUIThread(
161 const std::string& resource_id,
162 const std::string& md5,
163 const GetCacheEntryCallback& callback);
164
165 // Gets the resource IDs of pinned-but-not-fetched files and
166 // dirty-but-not-uploaded files.
167 //
168 // Must be called on UI thread. |callback| is run on UI thread.
169 void GetResourceIdsOfBacklogOnUIThread(
170 const GetResourceIdsOfBacklogCallback& callback);
171
172 // Gets the resource IDs of all existing (i.e. cached locally) pinned
173 // files, including pinned dirty files.
174 //
175 // Must be called on UI thread. |callback| is run on UI thread.
176 void GetResourceIdsOfExistingPinnedFilesOnUIThread(
177 const GetResourceIdsCallback& callback);
178
179 // Gets the resource IDs of all files in the cache.
180 //
181 // Must be called on UI thread. |callback| is run on UI thread.
182 void GetResourceIdsOfAllFilesOnUIThread(
183 const GetResourceIdsCallback& callback);
184
185 // Frees up disk space to store the given number of bytes, while keeping
186 // kMinFreSpace bytes on the disk, if needed. |has_enough_space| is
187 // updated to indicate if we have enough space.
188 void FreeDiskSpaceIfNeededFor(int64 num_bytes,
189 bool* has_enough_space);
190
191 // Checks if file corresponding to |resource_id| and |md5| exists in cache.
192 void GetFileOnUIThread(const std::string& resource_id,
193 const std::string& md5,
194 const GetFileFromCacheCallback& callback);
195
196 // Modifies cache state, which involves the following:
197 // - moves or copies (per |file_operation_type|) |source_path|
198 // to |dest_path| in the cache dir
199 // - if necessary, creates symlink
200 // - deletes stale cached versions of |resource_id| in
201 // |dest_path|'s directory.
202 void StoreOnUIThread(const std::string& resource_id,
203 const std::string& md5,
204 const FilePath& source_path,
205 FileOperationType file_operation_type,
206 const CacheOperationCallback& callback);
207
208 // Modifies cache state, which involves the following:
209 // - moves |source_path| to |dest_path| in persistent dir if
210 // file is not dirty
211 // - creates symlink in pinned dir that references downloaded or locally
212 // modified file
213 void PinOnUIThread(const std::string& resource_id,
214 const std::string& md5,
215 const CacheOperationCallback& callback);
216
217 // Modifies cache state, which involves the following:
218 // - moves |source_path| to |dest_path| in tmp dir if file is not dirty
219 // - deletes symlink from pinned dir
220 void UnpinOnUIThread(const std::string& resource_id,
221 const std::string& md5,
222 const CacheOperationCallback& callback);
223
224 // Modifies cache state, which involves the following:
225 // - moves |source_path| to |dest_path|, where
226 // if we're mounting: |source_path| is the unmounted path and has .<md5>
227 // extension, and |dest_path| is the mounted path in persistent dir
228 // and has .<md5>.mounted extension;
229 // if we're unmounting: the opposite is true for the two paths, i.e.
230 // |dest_path| is the mounted path and |source_path| the unmounted path.
231 void SetMountedStateOnUIThread(const FilePath& file_path,
232 bool to_mount,
233 const ChangeCacheStateCallback& callback);
234
235 // Modifies cache state, which involves the following:
236 // - moves |source_path| to |dest_path| in persistent dir, where
237 // |source_path| has .<md5> extension and |dest_path| has .local extension
238 // - if file is pinned, updates symlink in pinned dir to reference dirty file
239 void MarkDirtyOnUIThread(const std::string& resource_id,
240 const std::string& md5,
241 const GetFileFromCacheCallback& callback);
242
243 // Modifies cache state, i.e. creates symlink in outgoing
244 // dir to reference dirty file in persistent dir.
245 void CommitDirtyOnUIThread(const std::string& resource_id,
246 const std::string& md5,
247 const CacheOperationCallback& callback);
248
249 // Modifies cache state, which involves the following:
250 // - moves |source_path| to |dest_path| in persistent dir if
251 // file is pinned or tmp dir otherwise, where |source_path| has .local
252 // extension and |dest_path| has .<md5> extension
253 // - deletes symlink in outgoing dir
254 // - if file is pinned, updates symlink in pinned dir to reference
255 // |dest_path|
256 void ClearDirtyOnUIThread(const std::string& resource_id,
257 const std::string& md5,
258 const CacheOperationCallback& callback);
259
260 // Does the following:
261 // - remove all delete stale cache versions corresponding to |resource_id| in
262 // persistent, tmp and pinned directories
263 // - remove entry corresponding to |resource_id| from cache map.
264 void RemoveOnUIThread(const std::string& resource_id,
265 const CacheOperationCallback& callback);
266
267 // Does the following:
268 // - remove all the files in the cache directory.
269 // - re-create the |metadata_| instance.
270 void ClearAllOnUIThread(const ChangeCacheStateCallback& callback);
271
272 // Utility method to call Initialize on UI thread.
273 void RequestInitializeOnUIThread();
274
275 // Utility method to call InitializeForTesting on UI thread.
276 void RequestInitializeOnUIThreadForTesting();
277
278 // Force a rescan of cache files, for testing.
279 void ForceRescanOnUIThreadForTesting();
280
281 // Gets the cache entry for file corresponding to |resource_id| and |md5|
282 // and returns true if entry exists in cache map. Otherwise, returns false.
283 // |md5| can be empty if only matching |resource_id| is desired, which may
284 // happen when looking for pinned entries where symlinks' filenames have no
285 // extension and hence no md5.
286 bool GetCacheEntry(const std::string& resource_id,
287 const std::string& md5,
288 DriveCacheEntry* entry);
289
290 // Factory methods for GDataCache.
291 // |pool| and |sequence_token| are used to assert that the functions are
292 // called on the right sequenced worker pool with the right sequence token.
293 //
294 // For testing, the thread assertion can be disabled by passing NULL and
295 // the default value of SequenceToken.
296 static GDataCache* CreateGDataCacheOnUIThread(
297 const FilePath& cache_root_path,
298 base::SequencedTaskRunner* blocking_task_runner);
299
300 // Deletes the cache.
301 void DestroyOnUIThread();
302
303 // Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the
304 // profile.
305 // TODO(satorux): Write a unit test for this.
306 static FilePath GetCacheRootPath(Profile* profile);
307
308 // Returns file paths for all the cache sub directories under
309 // |cache_root_path|.
310 static std::vector<FilePath> GetCachePaths(const FilePath& cache_root_path);
311
312 // Creates cache directory and its sub-directories if they don't exist.
313 // TODO(glotov): take care of this when the setup and cleanup part is
314 // landed, noting that these directories need to be created for development
315 // in linux box and unittest. (http://crosbug.com/27577)
316 static bool CreateCacheDirectories(
317 const std::vector<FilePath>& paths_to_create);
318
319 // Returns the type of the sub directory where the cache file is stored.
320 static CacheSubDirectoryType GetSubDirectoryType(
321 const DriveCacheEntry& cache_entry);
322
323 private:
324 GDataCache(const FilePath& cache_root_path,
325 base::SequencedTaskRunner* blocking_task_runner);
326 virtual ~GDataCache();
327
328 // Checks whether the current thread is on the right sequenced worker pool
329 // with the right sequence ID. If not, DCHECK will fail.
330 void AssertOnSequencedWorkerPool();
331
332 // Initializes the cache.
333 void Initialize();
334
335 // Initializes the cache with in-memory cache for testing.
336 // The in-memory cache is used since it's faster than the db.
337 void InitializeForTesting();
338
339 // Deletes the cache.
340 void Destroy();
341
342 // Force a rescan of cache directories.
343 void ForceRescanForTesting();
344
345 // Used to implement GetResourceIdsOfBacklogOnUIThread.
346 void GetResourceIdsOfBacklog(
347 std::vector<std::string>* to_fetch,
348 std::vector<std::string>* to_upload);
349
350 // Used to implement GetResourceIdsOfExistingPinnedFilesOnUIThread.
351 void GetResourceIdsOfExistingPinnedFiles(
352 std::vector<std::string>* resource_ids);
353
354 // Used to implement GetResourceIdsOfAllFilesOnUIThread.
355 void GetResourceIdsOfAllFiles(
356 std::vector<std::string>* resource_ids);
357
358 // Used to implement GetFileOnUIThread.
359 void GetFile(const std::string& resource_id,
360 const std::string& md5,
361 GDataFileError* error,
362 FilePath* cache_file_path);
363
364 // Used to implement StoreOnUIThread.
365 void Store(const std::string& resource_id,
366 const std::string& md5,
367 const FilePath& source_path,
368 FileOperationType file_operation_type,
369 GDataFileError* error);
370
371 // Used to implement PinOnUIThread.
372 void Pin(const std::string& resource_id,
373 const std::string& md5,
374 FileOperationType file_operation_type,
375 GDataFileError* error);
376
377 // Used to implement UnpinOnUIThread.
378 void Unpin(const std::string& resource_id,
379 const std::string& md5,
380 FileOperationType file_operation_type,
381 GDataFileError* error);
382
383 // Used to implement SetMountedStateOnUIThread.
384 void SetMountedState(const FilePath& file_path,
385 bool to_mount,
386 GDataFileError* error,
387 FilePath* cache_file_path);
388
389 // Used to implement MarkDirtyOnUIThread.
390 void MarkDirty(const std::string& resource_id,
391 const std::string& md5,
392 FileOperationType file_operation_type,
393 GDataFileError* error,
394 FilePath* cache_file_path);
395
396 // Used to implement CommitDirtyOnUIThread.
397 void CommitDirty(const std::string& resource_id,
398 const std::string& md5,
399 FileOperationType file_operation_type,
400 GDataFileError* error);
401
402 // Used to implement ClearDirtyOnUIThread.
403 void ClearDirty(const std::string& resource_id,
404 const std::string& md5,
405 FileOperationType file_operation_type,
406 GDataFileError* error);
407
408 // Used to implement RemoveOnUIThread.
409 void Remove(const std::string& resource_id,
410 GDataFileError* error);
411
412 // Used to implement ClearAllUIThread.
413 void ClearAll(GDataFileError* error);
414
415 // Runs callback and notifies the observers when file is pinned.
416 void OnPinned(GDataFileError* error,
417 const std::string& resource_id,
418 const std::string& md5,
419 const CacheOperationCallback& callback);
420
421 // Runs callback and notifies the observers when file is unpinned.
422 void OnUnpinned(GDataFileError* error,
423 const std::string& resource_id,
424 const std::string& md5,
425 const CacheOperationCallback& callback);
426
427 // Runs callback and notifies the observers when file is committed.
428 void OnCommitDirty(GDataFileError* error,
429 const std::string& resource_id,
430 const std::string& md5,
431 const CacheOperationCallback& callback);
432
433 // Helper function to implement GetCacheEntryOnUIThread().
434 void GetCacheEntryHelper(const std::string& resource_id,
435 const std::string& md5,
436 bool* success,
437 DriveCacheEntry* cache_entry);
438
439 // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1).
440 const FilePath cache_root_path_;
441 // Paths for all subdirectories of GCache, one for each
442 // GDataCache::CacheSubDirectoryType enum.
443 const std::vector<FilePath> cache_paths_;
444 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
445
446 // The cache state data. This member must be access only on the blocking pool.
447 scoped_ptr<GDataCacheMetadata> metadata_;
448
449 // List of observers, this member must be accessed on UI thread.
450 ObserverList<Observer> observers_;
451
452 // Note: This should remain the last member so it'll be destroyed and
453 // invalidate its weak pointers before any other members are destroyed.
454 base::WeakPtrFactory<GDataCache> weak_ptr_factory_;
455 DISALLOW_COPY_AND_ASSIGN(GDataCache);
456 };
457
458
459 // The minimum free space to keep. GDataFileSystem::GetFileByPath() returns
460 // GDATA_FILE_ERROR_NO_SPACE if the available space is smaller than
461 // this value.
462 //
463 // Copied from cryptohome/homedirs.h.
464 // TODO(satorux): Share the constant.
465 const int64 kMinFreeSpace = 512 * 1LL << 20;
466
467 // Interface class used for getting the free disk space. Only for testing.
468 class FreeDiskSpaceGetterInterface {
469 public:
470 virtual ~FreeDiskSpaceGetterInterface() {}
471 virtual int64 AmountOfFreeDiskSpace() const = 0;
472 };
473
474 // Sets the free disk space getter for testing.
475 // The existing getter is deleted.
476 void SetFreeDiskSpaceGetterForTesting(
477 FreeDiskSpaceGetterInterface* getter);
478
479 } // namespace gdata
480
481 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc ('k') | chrome/browser/chromeos/gdata/gdata_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698