| 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_DRIVE_CACHE_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_GDATA_DRIVE_CACHE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 17 #include "base/platform_file.h" | 17 #include "base/platform_file.h" |
| 18 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" | 18 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" |
| 19 | 19 |
| 20 class Profile; | 20 class Profile; |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 | 23 |
| 24 class SequencedTaskRunner; | 24 class SequencedTaskRunner; |
| 25 | 25 |
| 26 } // namespace base | 26 } // namespace base |
| 27 | 27 |
| 28 namespace gdata { | 28 namespace gdata { |
| 29 | 29 |
| 30 class DriveCacheEntry; | 30 class DriveCacheEntry; |
| 31 class GDataCacheMetadata; | 31 class DriveCacheMetadata; |
| 32 | 32 |
| 33 // Callback for SetMountedStateOnUIThread and ClearAllOnUIThread. | 33 // Callback for SetMountedStateOnUIThread and ClearAllOnUIThread. |
| 34 typedef base::Callback<void(GDataFileError error, | 34 typedef base::Callback<void(GDataFileError error, |
| 35 const FilePath& file_path)> | 35 const FilePath& file_path)> |
| 36 ChangeCacheStateCallback; | 36 ChangeCacheStateCallback; |
| 37 | 37 |
| 38 // Callback for completion of cache operation. | 38 // Callback for completion of cache operation. |
| 39 typedef base::Callback<void(GDataFileError error, | 39 typedef base::Callback<void(GDataFileError error, |
| 40 const std::string& resource_id, | 40 const std::string& resource_id, |
| 41 const std::string& md5)> CacheOperationCallback; | 41 const std::string& md5)> CacheOperationCallback; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 58 typedef base::Callback<void(const std::vector<std::string>& resource_ids)> | 58 typedef base::Callback<void(const std::vector<std::string>& resource_ids)> |
| 59 GetResourceIdsCallback; | 59 GetResourceIdsCallback; |
| 60 | 60 |
| 61 // Callback for GetCacheEntryOnUIThread. | 61 // Callback for GetCacheEntryOnUIThread. |
| 62 // |success| indicates if the operation was successful. | 62 // |success| indicates if the operation was successful. |
| 63 // |cache_entry| is the obtained cache entry. On failure, |cache_state| is | 63 // |cache_entry| is the obtained cache entry. On failure, |cache_state| is |
| 64 // set to TEST_CACHE_STATE_NONE. | 64 // set to TEST_CACHE_STATE_NONE. |
| 65 typedef base::Callback<void(bool success, const DriveCacheEntry& cache_entry)> | 65 typedef base::Callback<void(bool success, const DriveCacheEntry& cache_entry)> |
| 66 GetCacheEntryCallback; | 66 GetCacheEntryCallback; |
| 67 | 67 |
| 68 // GDataCache is used to maintain cache states of GDataFileSystem. | 68 // DriveCache is used to maintain cache states of GDataFileSystem. |
| 69 // | 69 // |
| 70 // All non-static public member functions, unless mentioned otherwise (see | 70 // All non-static public member functions, unless mentioned otherwise (see |
| 71 // GetCacheFilePath() for example), should be called from the sequenced | 71 // GetCacheFilePath() for example), should be called from the sequenced |
| 72 // worker pool with the sequence token set by CreateGDataCacheOnUIThread(). This | 72 // worker pool with the sequence token set by CreateDriveCacheOnUIThread(). This |
| 73 // threading model is enforced by AssertOnSequencedWorkerPool(). | 73 // threading model is enforced by AssertOnSequencedWorkerPool(). |
| 74 // | 74 // |
| 75 // TODO(hashimoto): Change threading model of this class to make public methods | 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 | 76 // being called on UI thread unless mentioned otherwise. crbug.com/132926 |
| 77 class GDataCache { | 77 class DriveCache { |
| 78 public: | 78 public: |
| 79 // Enum defining GCache subdirectory location. | 79 // Enum defining GCache subdirectory location. |
| 80 // This indexes into |GDataCache::cache_paths_| vector. | 80 // This indexes into |DriveCache::cache_paths_| vector. |
| 81 enum CacheSubDirectoryType { | 81 enum CacheSubDirectoryType { |
| 82 CACHE_TYPE_META = 0, // Downloaded feeds. | 82 CACHE_TYPE_META = 0, // Downloaded feeds. |
| 83 CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are | 83 CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are |
| 84 // pinned, or to /dev/null for non-existent | 84 // pinned, or to /dev/null for non-existent |
| 85 // files. | 85 // files. |
| 86 CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to | 86 CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to |
| 87 // be uploaded. | 87 // be uploaded. |
| 88 CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally, | 88 CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally, |
| 89 // not evictable, hopefully. | 89 // not evictable, hopefully. |
| 90 CACHE_TYPE_TMP, // Files that don't meet criteria to be in | 90 CACHE_TYPE_TMP, // Files that don't meet criteria to be in |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 // Can be called on any thread. | 136 // Can be called on any thread. |
| 137 FilePath GetCacheFilePath(const std::string& resource_id, | 137 FilePath GetCacheFilePath(const std::string& resource_id, |
| 138 const std::string& md5, | 138 const std::string& md5, |
| 139 CacheSubDirectoryType sub_dir_type, | 139 CacheSubDirectoryType sub_dir_type, |
| 140 CachedFileOrigin file_orign) const; | 140 CachedFileOrigin file_orign) const; |
| 141 | 141 |
| 142 // Returns true if the given path is under gdata cache directory, i.e. | 142 // Returns true if the given path is under gdata cache directory, i.e. |
| 143 // <user_profile_dir>/GCache/v1 | 143 // <user_profile_dir>/GCache/v1 |
| 144 // | 144 // |
| 145 // Can be called on any thread. | 145 // Can be called on any thread. |
| 146 bool IsUnderGDataCacheDirectory(const FilePath& path) const; | 146 bool IsUnderDriveCacheDirectory(const FilePath& path) const; |
| 147 | 147 |
| 148 // Adds observer. | 148 // Adds observer. |
| 149 // Must be called on UI thread. | 149 // Must be called on UI thread. |
| 150 void AddObserver(Observer* observer); | 150 void AddObserver(Observer* observer); |
| 151 | 151 |
| 152 // Removes observer. | 152 // Removes observer. |
| 153 // Must be called on UI thread. | 153 // Must be called on UI thread. |
| 154 void RemoveObserver(Observer* observer); | 154 void RemoveObserver(Observer* observer); |
| 155 | 155 |
| 156 // Gets the cache entry by the given resource ID and MD5. | 156 // Gets the cache entry by the given resource ID and MD5. |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 280 |
| 281 // Gets the cache entry for file corresponding to |resource_id| and |md5| | 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. | 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 | 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 | 284 // happen when looking for pinned entries where symlinks' filenames have no |
| 285 // extension and hence no md5. | 285 // extension and hence no md5. |
| 286 bool GetCacheEntry(const std::string& resource_id, | 286 bool GetCacheEntry(const std::string& resource_id, |
| 287 const std::string& md5, | 287 const std::string& md5, |
| 288 DriveCacheEntry* entry); | 288 DriveCacheEntry* entry); |
| 289 | 289 |
| 290 // Factory methods for GDataCache. | 290 // Factory methods for DriveCache. |
| 291 // |pool| and |sequence_token| are used to assert that the functions are | 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. | 292 // called on the right sequenced worker pool with the right sequence token. |
| 293 // | 293 // |
| 294 // For testing, the thread assertion can be disabled by passing NULL and | 294 // For testing, the thread assertion can be disabled by passing NULL and |
| 295 // the default value of SequenceToken. | 295 // the default value of SequenceToken. |
| 296 static GDataCache* CreateGDataCacheOnUIThread( | 296 static DriveCache* CreateDriveCacheOnUIThread( |
| 297 const FilePath& cache_root_path, | 297 const FilePath& cache_root_path, |
| 298 base::SequencedTaskRunner* blocking_task_runner); | 298 base::SequencedTaskRunner* blocking_task_runner); |
| 299 | 299 |
| 300 // Deletes the cache. | 300 // Deletes the cache. |
| 301 void DestroyOnUIThread(); | 301 void DestroyOnUIThread(); |
| 302 | 302 |
| 303 // Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the | 303 // Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the |
| 304 // profile. | 304 // profile. |
| 305 // TODO(satorux): Write a unit test for this. | 305 // TODO(satorux): Write a unit test for this. |
| 306 static FilePath GetCacheRootPath(Profile* profile); | 306 static FilePath GetCacheRootPath(Profile* profile); |
| 307 | 307 |
| 308 // Returns file paths for all the cache sub directories under | 308 // Returns file paths for all the cache sub directories under |
| 309 // |cache_root_path|. | 309 // |cache_root_path|. |
| 310 static std::vector<FilePath> GetCachePaths(const FilePath& cache_root_path); | 310 static std::vector<FilePath> GetCachePaths(const FilePath& cache_root_path); |
| 311 | 311 |
| 312 // Creates cache directory and its sub-directories if they don't exist. | 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 | 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 | 314 // landed, noting that these directories need to be created for development |
| 315 // in linux box and unittest. (http://crosbug.com/27577) | 315 // in linux box and unittest. (http://crosbug.com/27577) |
| 316 static bool CreateCacheDirectories( | 316 static bool CreateCacheDirectories( |
| 317 const std::vector<FilePath>& paths_to_create); | 317 const std::vector<FilePath>& paths_to_create); |
| 318 | 318 |
| 319 // Returns the type of the sub directory where the cache file is stored. | 319 // Returns the type of the sub directory where the cache file is stored. |
| 320 static CacheSubDirectoryType GetSubDirectoryType( | 320 static CacheSubDirectoryType GetSubDirectoryType( |
| 321 const DriveCacheEntry& cache_entry); | 321 const DriveCacheEntry& cache_entry); |
| 322 | 322 |
| 323 private: | 323 private: |
| 324 GDataCache(const FilePath& cache_root_path, | 324 DriveCache(const FilePath& cache_root_path, |
| 325 base::SequencedTaskRunner* blocking_task_runner); | 325 base::SequencedTaskRunner* blocking_task_runner); |
| 326 virtual ~GDataCache(); | 326 virtual ~DriveCache(); |
| 327 | 327 |
| 328 // Checks whether the current thread is on the right sequenced worker pool | 328 // Checks whether the current thread is on the right sequenced worker pool |
| 329 // with the right sequence ID. If not, DCHECK will fail. | 329 // with the right sequence ID. If not, DCHECK will fail. |
| 330 void AssertOnSequencedWorkerPool(); | 330 void AssertOnSequencedWorkerPool(); |
| 331 | 331 |
| 332 // Initializes the cache. | 332 // Initializes the cache. |
| 333 void Initialize(); | 333 void Initialize(); |
| 334 | 334 |
| 335 // Initializes the cache with in-memory cache for testing. | 335 // Initializes the cache with in-memory cache for testing. |
| 336 // The in-memory cache is used since it's faster than the db. | 336 // The in-memory cache is used since it's faster than the db. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 | 432 |
| 433 // Helper function to implement GetCacheEntryOnUIThread(). | 433 // Helper function to implement GetCacheEntryOnUIThread(). |
| 434 void GetCacheEntryHelper(const std::string& resource_id, | 434 void GetCacheEntryHelper(const std::string& resource_id, |
| 435 const std::string& md5, | 435 const std::string& md5, |
| 436 bool* success, | 436 bool* success, |
| 437 DriveCacheEntry* cache_entry); | 437 DriveCacheEntry* cache_entry); |
| 438 | 438 |
| 439 // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1). | 439 // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1). |
| 440 const FilePath cache_root_path_; | 440 const FilePath cache_root_path_; |
| 441 // Paths for all subdirectories of GCache, one for each | 441 // Paths for all subdirectories of GCache, one for each |
| 442 // GDataCache::CacheSubDirectoryType enum. | 442 // DriveCache::CacheSubDirectoryType enum. |
| 443 const std::vector<FilePath> cache_paths_; | 443 const std::vector<FilePath> cache_paths_; |
| 444 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 444 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 445 | 445 |
| 446 // The cache state data. This member must be access only on the blocking pool. | 446 // The cache state data. This member must be access only on the blocking pool. |
| 447 scoped_ptr<GDataCacheMetadata> metadata_; | 447 scoped_ptr<DriveCacheMetadata> metadata_; |
| 448 | 448 |
| 449 // List of observers, this member must be accessed on UI thread. | 449 // List of observers, this member must be accessed on UI thread. |
| 450 ObserverList<Observer> observers_; | 450 ObserverList<Observer> observers_; |
| 451 | 451 |
| 452 // Note: This should remain the last member so it'll be destroyed and | 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. | 453 // invalidate its weak pointers before any other members are destroyed. |
| 454 base::WeakPtrFactory<GDataCache> weak_ptr_factory_; | 454 base::WeakPtrFactory<DriveCache> weak_ptr_factory_; |
| 455 DISALLOW_COPY_AND_ASSIGN(GDataCache); | 455 DISALLOW_COPY_AND_ASSIGN(DriveCache); |
| 456 }; | 456 }; |
| 457 | 457 |
| 458 | 458 |
| 459 // The minimum free space to keep. GDataFileSystem::GetFileByPath() returns | 459 // The minimum free space to keep. GDataFileSystem::GetFileByPath() returns |
| 460 // GDATA_FILE_ERROR_NO_SPACE if the available space is smaller than | 460 // GDATA_FILE_ERROR_NO_SPACE if the available space is smaller than |
| 461 // this value. | 461 // this value. |
| 462 // | 462 // |
| 463 // Copied from cryptohome/homedirs.h. | 463 // Copied from cryptohome/homedirs.h. |
| 464 // TODO(satorux): Share the constant. | 464 // TODO(satorux): Share the constant. |
| 465 const int64 kMinFreeSpace = 512 * 1LL << 20; | 465 const int64 kMinFreeSpace = 512 * 1LL << 20; |
| 466 | 466 |
| 467 // Interface class used for getting the free disk space. Only for testing. | 467 // Interface class used for getting the free disk space. Only for testing. |
| 468 class FreeDiskSpaceGetterInterface { | 468 class FreeDiskSpaceGetterInterface { |
| 469 public: | 469 public: |
| 470 virtual ~FreeDiskSpaceGetterInterface() {} | 470 virtual ~FreeDiskSpaceGetterInterface() {} |
| 471 virtual int64 AmountOfFreeDiskSpace() const = 0; | 471 virtual int64 AmountOfFreeDiskSpace() const = 0; |
| 472 }; | 472 }; |
| 473 | 473 |
| 474 // Sets the free disk space getter for testing. | 474 // Sets the free disk space getter for testing. |
| 475 // The existing getter is deleted. | 475 // The existing getter is deleted. |
| 476 void SetFreeDiskSpaceGetterForTesting( | 476 void SetFreeDiskSpaceGetterForTesting( |
| 477 FreeDiskSpaceGetterInterface* getter); | 477 FreeDiskSpaceGetterInterface* getter); |
| 478 | 478 |
| 479 } // namespace gdata | 479 } // namespace gdata |
| 480 | 480 |
| 481 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ | 481 #endif // CHROME_BROWSER_CHROMEOS_GDATA_DRIVE_CACHE_H_ |
| OLD | NEW |