| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 15 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "chrome/browser/chromeos/gdata/gdata_cache_entry.h" |
| 17 | 18 |
| 18 class Profile; | 19 class Profile; |
| 19 | 20 |
| 20 namespace gdata { | 21 namespace gdata { |
| 21 | 22 |
| 22 class GDataCacheMetadata; | 23 class GDataCacheMetadata; |
| 23 | 24 |
| 24 // Callback for SetMountedStateOnUIThread. | 25 // Callback for SetMountedStateOnUIThread. |
| 25 typedef base::Callback<void(base::PlatformFileError error, | 26 typedef base::Callback<void(base::PlatformFileError error, |
| 26 const FilePath& file_path)> SetMountedStateCallback; | 27 const FilePath& file_path)> SetMountedStateCallback; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 41 // |to_fetch| is for resource IDs of pinned-but-not-fetched files. | 42 // |to_fetch| is for resource IDs of pinned-but-not-fetched files. |
| 42 // |to_upload| is for resource IDs of dirty-but-not-uploaded files. | 43 // |to_upload| is for resource IDs of dirty-but-not-uploaded files. |
| 43 typedef base::Callback<void(const std::vector<std::string>& to_fetch, | 44 typedef base::Callback<void(const std::vector<std::string>& to_fetch, |
| 44 const std::vector<std::string>& to_upload)> | 45 const std::vector<std::string>& to_upload)> |
| 45 GetResourceIdsOfBacklogCallback; | 46 GetResourceIdsOfBacklogCallback; |
| 46 | 47 |
| 47 // Callback for GetResourceIdsOfExistingPinnedFilesOnUIThread. | 48 // Callback for GetResourceIdsOfExistingPinnedFilesOnUIThread. |
| 48 typedef base::Callback<void(const std::vector<std::string>& resource_ids)> | 49 typedef base::Callback<void(const std::vector<std::string>& resource_ids)> |
| 49 GetResourceIdsCallback; | 50 GetResourceIdsCallback; |
| 50 | 51 |
| 52 // Callback for GetCacheEntryOnUIThread. |
| 53 // |success| indicates if the operation was successful. |
| 54 // |cache_entry| is the obtained cache entry. On failure, |cache_state| is |
| 55 // set to CACHE_STATE_NONE. |
| 56 typedef base::Callback<void(bool success, const GDataCacheEntry& cache_entry)> |
| 57 GetCacheEntryCallback; |
| 58 |
| 51 // GDataCache is used to maintain cache states of GDataFileSystem. | 59 // GDataCache is used to maintain cache states of GDataFileSystem. |
| 52 // | 60 // |
| 53 // All non-static public member functions, unless mentioned otherwise (see | 61 // All non-static public member functions, unless mentioned otherwise (see |
| 54 // GetCacheFilePath() for example), should be called from the sequenced | 62 // GetCacheFilePath() for example), should be called from the sequenced |
| 55 // worker pool with the sequence token set by CreateGDataCacheOnUIThread(). This | 63 // worker pool with the sequence token set by CreateGDataCacheOnUIThread(). This |
| 56 // threading model is enforced by AssertOnSequencedWorkerPool(). | 64 // threading model is enforced by AssertOnSequencedWorkerPool(). |
| 57 // | 65 // |
| 58 // TODO(hashimoto): Change threading model of this class to make public methods | 66 // TODO(hashimoto): Change threading model of this class to make public methods |
| 59 // being called on UI thread unless mentioned otherwise. crbug.com/132926 | 67 // being called on UI thread unless mentioned otherwise. crbug.com/132926 |
| 60 class GDataCache { | 68 class GDataCache { |
| 61 public: | 69 public: |
| 62 // Enum defining GCache subdirectory location. | 70 // Enum defining GCache subdirectory location. |
| 63 // This indexes into |GDataCache::cache_paths_| vector. | 71 // This indexes into |GDataCache::cache_paths_| vector. |
| 64 enum CacheSubDirectoryType { | 72 enum CacheSubDirectoryType { |
| 65 CACHE_TYPE_META = 0, // Downloaded feeds. | 73 CACHE_TYPE_META = 0, // Downloaded feeds. |
| 66 CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are | 74 CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are |
| 67 // pinned, or to /dev/null for non-existent | 75 // pinned, or to /dev/null for non-existent |
| 68 // files. | 76 // files. |
| 69 CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to | 77 CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to |
| 70 // be uploaded. | 78 // be uploaded. |
| 71 CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally, | 79 CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally, |
| 72 // not evictable, hopefully. | 80 // not evictable, hopefully. |
| 73 CACHE_TYPE_TMP, // Files that don't meet criteria to be in | 81 CACHE_TYPE_TMP, // Files that don't meet criteria to be in |
| 74 // persistent dir, and hence evictable. | 82 // persistent dir, and hence evictable. |
| 75 CACHE_TYPE_TMP_DOWNLOADS, // Downloaded files. | 83 CACHE_TYPE_TMP_DOWNLOADS, // Downloaded files. |
| 76 CACHE_TYPE_TMP_DOCUMENTS, // Temporary JSON files for hosted documents. | 84 CACHE_TYPE_TMP_DOCUMENTS, // Temporary JSON files for hosted documents. |
| 77 NUM_CACHE_TYPES, // This must be at the end. | 85 NUM_CACHE_TYPES, // This must be at the end. |
| 78 }; | 86 }; |
| 79 | 87 |
| 80 // This is used as a bitmask for the cache state. | |
| 81 enum CacheState { | |
| 82 CACHE_STATE_NONE = 0x0, | |
| 83 CACHE_STATE_PINNED = 0x1 << 0, | |
| 84 CACHE_STATE_PRESENT = 0x1 << 1, | |
| 85 CACHE_STATE_DIRTY = 0x1 << 2, | |
| 86 CACHE_STATE_MOUNTED = 0x1 << 3, | |
| 87 CACHE_STATE_PERSISTENT = 0x1 << 4, | |
| 88 }; | |
| 89 | |
| 90 // Enum defining origin of a cached file. | 88 // Enum defining origin of a cached file. |
| 91 enum CachedFileOrigin { | 89 enum CachedFileOrigin { |
| 92 CACHED_FILE_FROM_SERVER = 0, | 90 CACHED_FILE_FROM_SERVER = 0, |
| 93 CACHED_FILE_LOCALLY_MODIFIED, | 91 CACHED_FILE_LOCALLY_MODIFIED, |
| 94 CACHED_FILE_MOUNTED, | 92 CACHED_FILE_MOUNTED, |
| 95 }; | 93 }; |
| 96 | 94 |
| 97 // Enum defining type of file operation e.g. copy or move, etc. | 95 // Enum defining type of file operation e.g. copy or move, etc. |
| 98 enum FileOperationType { | 96 enum FileOperationType { |
| 99 FILE_OPERATION_MOVE = 0, | 97 FILE_OPERATION_MOVE = 0, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 111 virtual void OnCacheUnpinned(const std::string& resource_id, | 109 virtual void OnCacheUnpinned(const std::string& resource_id, |
| 112 const std::string& md5) {} | 110 const std::string& md5) {} |
| 113 | 111 |
| 114 // Triggered when a dirty file has been committed (saved) successfully. | 112 // Triggered when a dirty file has been committed (saved) successfully. |
| 115 virtual void OnCacheCommitted(const std::string& resource_id) {} | 113 virtual void OnCacheCommitted(const std::string& resource_id) {} |
| 116 | 114 |
| 117 protected: | 115 protected: |
| 118 virtual ~Observer() {} | 116 virtual ~Observer() {} |
| 119 }; | 117 }; |
| 120 | 118 |
| 121 // Structure to store information of an existing cache file. | |
| 122 class CacheEntry { | |
| 123 public: | |
| 124 CacheEntry() : cache_state_(CACHE_STATE_NONE) {} | |
| 125 | |
| 126 CacheEntry(const std::string& md5, | |
| 127 int cache_state) | |
| 128 : md5_(md5), | |
| 129 cache_state_(cache_state) { | |
| 130 } | |
| 131 | |
| 132 // The MD5 of the cache file. This can be "local" if the file is | |
| 133 // locally modified. | |
| 134 const std::string& md5() const { return md5_; } | |
| 135 | |
| 136 // The cache state represented as a bitmask of GDataCacheState. | |
| 137 int cache_state() const { return cache_state_; } | |
| 138 | |
| 139 void set_md5(const std::string& md5) { md5_ = md5; } | |
| 140 void set_cache_state(int cache_state) { cache_state_ = cache_state; } | |
| 141 | |
| 142 // Returns true if the file is present locally. | |
| 143 bool IsPresent() const { return cache_state_ & CACHE_STATE_PRESENT; } | |
| 144 | |
| 145 // Returns true if the file is pinned (i.e. available offline). | |
| 146 bool IsPinned() const { return cache_state_ & CACHE_STATE_PINNED; } | |
| 147 | |
| 148 // Returns true if the file is dirty (i.e. modified locally). | |
| 149 bool IsDirty() const { return cache_state_ & CACHE_STATE_DIRTY; } | |
| 150 | |
| 151 // Returns true if the file is a mounted archive file. | |
| 152 bool IsMounted() const { return cache_state_ & CACHE_STATE_MOUNTED; } | |
| 153 | |
| 154 // Returns true if the file is in the persistent directory. | |
| 155 bool IsPersistent() const { return cache_state_ & CACHE_STATE_PERSISTENT; } | |
| 156 | |
| 157 // Setters for the states describe above. | |
| 158 void SetPresent(bool value) { | |
| 159 if (value) | |
| 160 cache_state_ |= CACHE_STATE_PRESENT; | |
| 161 else | |
| 162 cache_state_ &= ~CACHE_STATE_PRESENT; | |
| 163 } | |
| 164 void SetPinned(bool value) { | |
| 165 if (value) | |
| 166 cache_state_ |= CACHE_STATE_PINNED; | |
| 167 else | |
| 168 cache_state_ &= ~CACHE_STATE_PINNED; | |
| 169 } | |
| 170 void SetDirty(bool value) { | |
| 171 if (value) | |
| 172 cache_state_ |= CACHE_STATE_DIRTY; | |
| 173 else | |
| 174 cache_state_ &= ~CACHE_STATE_DIRTY; | |
| 175 } | |
| 176 void SetMounted(bool value) { | |
| 177 if (value) | |
| 178 cache_state_ |= CACHE_STATE_MOUNTED; | |
| 179 else | |
| 180 cache_state_ &= ~CACHE_STATE_MOUNTED; | |
| 181 } | |
| 182 void SetPersistent(bool value) { | |
| 183 if (value) | |
| 184 cache_state_ |= CACHE_STATE_PERSISTENT; | |
| 185 else | |
| 186 cache_state_ &= ~CACHE_STATE_PERSISTENT; | |
| 187 } | |
| 188 | |
| 189 // Returns the type of the sub directory where the cache file is stored. | |
| 190 CacheSubDirectoryType GetSubDirectoryType() const { | |
| 191 return IsPersistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; | |
| 192 } | |
| 193 | |
| 194 // For debugging purposes. | |
| 195 std::string ToString() const; | |
| 196 | |
| 197 private: | |
| 198 std::string md5_; | |
| 199 int cache_state_; | |
| 200 }; | |
| 201 | |
| 202 // Callback for GetCacheEntryOnUIThread. | |
| 203 // |success| indicates if the operation was successful. | |
| 204 // |cache_entry| is the obtained cache entry. On failure, |cache_state| is | |
| 205 // set to CACHE_STATE_NONE. | |
| 206 // | |
| 207 // TODO(satorux): Unlike other callback types, this has to be defined | |
| 208 // inside GDataCache as CacheEntry is inside GDataCache. We should get them | |
| 209 // outside of GDataCache. | |
| 210 typedef base::Callback<void(bool success, const CacheEntry& cache_entry)> | |
| 211 GetCacheEntryCallback; | |
| 212 | |
| 213 // Returns the sub-directory under gdata cache directory for the given sub | 119 // Returns the sub-directory under gdata cache directory for the given sub |
| 214 // directory type. Example: <user_profile_dir>/GCache/v1/tmp | 120 // directory type. Example: <user_profile_dir>/GCache/v1/tmp |
| 215 // | 121 // |
| 216 // Can be called on any thread. | 122 // Can be called on any thread. |
| 217 FilePath GetCacheDirectoryPath(CacheSubDirectoryType sub_dir_type) const; | 123 FilePath GetCacheDirectoryPath(CacheSubDirectoryType sub_dir_type) const; |
| 218 | 124 |
| 219 // Returns absolute path of the file if it were cached or to be cached. | 125 // Returns absolute path of the file if it were cached or to be cached. |
| 220 // | 126 // |
| 221 // Can be called on any thread. | 127 // Can be called on any thread. |
| 222 FilePath GetCacheFilePath(const std::string& resource_id, | 128 FilePath GetCacheFilePath(const std::string& resource_id, |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 const CacheOperationCallback& callback); | 249 const CacheOperationCallback& callback); |
| 344 | 250 |
| 345 // Utility method to call Initialize on UI thread. | 251 // Utility method to call Initialize on UI thread. |
| 346 void RequestInitializeOnUIThread(); | 252 void RequestInitializeOnUIThread(); |
| 347 | 253 |
| 348 // Returns the cache entry for file corresponding to |resource_id| and |md5| | 254 // Returns the cache entry for file corresponding to |resource_id| and |md5| |
| 349 // if entry exists in cache map. Otherwise, returns NULL. | 255 // if entry exists in cache map. Otherwise, returns NULL. |
| 350 // |md5| can be empty if only matching |resource_id| is desired, which may | 256 // |md5| can be empty if only matching |resource_id| is desired, which may |
| 351 // happen when looking for pinned entries where symlinks' filenames have no | 257 // happen when looking for pinned entries where symlinks' filenames have no |
| 352 // extension and hence no md5. | 258 // extension and hence no md5. |
| 353 scoped_ptr<CacheEntry> GetCacheEntry(const std::string& resource_id, | 259 scoped_ptr<GDataCacheEntry> GetCacheEntry(const std::string& resource_id, |
| 354 const std::string& md5); | 260 const std::string& md5); |
| 355 | 261 |
| 356 // Factory methods for GDataCache. | 262 // Factory methods for GDataCache. |
| 357 // |pool| and |sequence_token| are used to assert that the functions are | 263 // |pool| and |sequence_token| are used to assert that the functions are |
| 358 // called on the right sequenced worker pool with the right sequence token. | 264 // called on the right sequenced worker pool with the right sequence token. |
| 359 // | 265 // |
| 360 // For testing, the thread assertion can be disabled by passing NULL and | 266 // For testing, the thread assertion can be disabled by passing NULL and |
| 361 // the default value of SequenceToken. | 267 // the default value of SequenceToken. |
| 362 static GDataCache* CreateGDataCacheOnUIThread( | 268 static GDataCache* CreateGDataCacheOnUIThread( |
| 363 const FilePath& cache_root_path, | 269 const FilePath& cache_root_path, |
| 364 base::SequencedWorkerPool* pool, | 270 base::SequencedWorkerPool* pool, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 376 // |cache_root_path|. | 282 // |cache_root_path|. |
| 377 static std::vector<FilePath> GetCachePaths(const FilePath& cache_root_path); | 283 static std::vector<FilePath> GetCachePaths(const FilePath& cache_root_path); |
| 378 | 284 |
| 379 // Creates cache directory and its sub-directories if they don't exist. | 285 // Creates cache directory and its sub-directories if they don't exist. |
| 380 // TODO(glotov): take care of this when the setup and cleanup part is | 286 // TODO(glotov): take care of this when the setup and cleanup part is |
| 381 // landed, noting that these directories need to be created for development | 287 // landed, noting that these directories need to be created for development |
| 382 // in linux box and unittest. (http://crosbug.com/27577) | 288 // in linux box and unittest. (http://crosbug.com/27577) |
| 383 static bool CreateCacheDirectories( | 289 static bool CreateCacheDirectories( |
| 384 const std::vector<FilePath>& paths_to_create); | 290 const std::vector<FilePath>& paths_to_create); |
| 385 | 291 |
| 292 // Returns the type of the sub directory where the cache file is stored. |
| 293 static CacheSubDirectoryType GetSubDirectoryType( |
| 294 const GDataCacheEntry& cache_entry); |
| 295 |
| 386 private: | 296 private: |
| 387 GDataCache( | 297 GDataCache( |
| 388 const FilePath& cache_root_path, | 298 const FilePath& cache_root_path, |
| 389 base::SequencedWorkerPool* pool_, | 299 base::SequencedWorkerPool* pool_, |
| 390 const base::SequencedWorkerPool::SequenceToken& sequence_token); | 300 const base::SequencedWorkerPool::SequenceToken& sequence_token); |
| 391 virtual ~GDataCache(); | 301 virtual ~GDataCache(); |
| 392 | 302 |
| 393 // Checks whether the current thread is on the right sequenced worker pool | 303 // Checks whether the current thread is on the right sequenced worker pool |
| 394 // with the right sequence ID. If not, DCHECK will fail. | 304 // with the right sequence ID. If not, DCHECK will fail. |
| 395 void AssertOnSequencedWorkerPool(); | 305 void AssertOnSequencedWorkerPool(); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 // Runs callback and notifies the observers when file is committed. | 388 // Runs callback and notifies the observers when file is committed. |
| 479 void OnCommitDirty(base::PlatformFileError* error, | 389 void OnCommitDirty(base::PlatformFileError* error, |
| 480 const std::string& resource_id, | 390 const std::string& resource_id, |
| 481 const std::string& md5, | 391 const std::string& md5, |
| 482 const CacheOperationCallback& callback); | 392 const CacheOperationCallback& callback); |
| 483 | 393 |
| 484 // Helper function to implement GetCacheEntryOnUIThread(). | 394 // Helper function to implement GetCacheEntryOnUIThread(). |
| 485 void GetCacheEntryHelper(const std::string& resource_id, | 395 void GetCacheEntryHelper(const std::string& resource_id, |
| 486 const std::string& md5, | 396 const std::string& md5, |
| 487 bool* success, | 397 bool* success, |
| 488 GDataCache::CacheEntry* cache_entry); | 398 GDataCacheEntry* cache_entry); |
| 489 | 399 |
| 490 // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1). | 400 // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1). |
| 491 const FilePath cache_root_path_; | 401 const FilePath cache_root_path_; |
| 492 // Paths for all subdirectories of GCache, one for each | 402 // Paths for all subdirectories of GCache, one for each |
| 493 // GDataCache::CacheSubDirectoryType enum. | 403 // GDataCache::CacheSubDirectoryType enum. |
| 494 const std::vector<FilePath> cache_paths_; | 404 const std::vector<FilePath> cache_paths_; |
| 495 base::SequencedWorkerPool* pool_; | 405 base::SequencedWorkerPool* pool_; |
| 496 const base::SequencedWorkerPool::SequenceToken sequence_token_; | 406 const base::SequencedWorkerPool::SequenceToken sequence_token_; |
| 497 | 407 |
| 498 // The cache state data. This member must be access only on the blocking pool. | 408 // The cache state data. This member must be access only on the blocking pool. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 525 }; | 435 }; |
| 526 | 436 |
| 527 // Sets the free disk space getter for testing. | 437 // Sets the free disk space getter for testing. |
| 528 // The existing getter is deleted. | 438 // The existing getter is deleted. |
| 529 void SetFreeDiskSpaceGetterForTesting( | 439 void SetFreeDiskSpaceGetterForTesting( |
| 530 FreeDiskSpaceGetterInterface* getter); | 440 FreeDiskSpaceGetterInterface* getter); |
| 531 | 441 |
| 532 } // namespace gdata | 442 } // namespace gdata |
| 533 | 443 |
| 534 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ | 444 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ |
| OLD | NEW |