gdata: Move ownership of GDataCache to GDataSystemService

BUG=131756
TEST=unit_tests --gtest_filter="GData*"


Review URL: https://chromiumcodereview.appspot.com/10546093

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141643 0039d316-1c4b-4281-b951-d872f2087c98
parent 580dd72d
...@@ -318,9 +318,13 @@ void FileBrowserEventRouter::MountCompleted( ...@@ -318,9 +318,13 @@ void FileBrowserEventRouter::MountCompleted(
(error_code == chromeos::MOUNT_ERROR_NONE)) { (error_code == chromeos::MOUNT_ERROR_NONE)) {
FilePath source_path(mount_info.source_path); FilePath source_path(mount_info.source_path);
gdata::GDataFileSystem* file_system = GetRemoteFileSystem(); gdata::GDataFileSystem* file_system = GetRemoteFileSystem();
if (file_system && file_system->IsUnderGDataCacheDirectory(source_path)) gdata::GDataSystemService* system_service =
gdata::GDataSystemServiceFactory::GetForProfile(profile_);
if (file_system && system_service &&
system_service->cache()->IsUnderGDataCacheDirectory(source_path)) {
file_system->SetMountedState(source_path, false, file_system->SetMountedState(source_path, false,
gdata::SetMountedStateCallback()); gdata::SetMountedStateCallback());
}
} }
} }
} }
......
...@@ -198,22 +198,23 @@ void AddGDataMountPoint( ...@@ -198,22 +198,23 @@ void AddGDataMountPoint(
// |system_service| is NULL if incognito window / guest login. // |system_service| is NULL if incognito window / guest login.
if (!system_service || !system_service->file_system()) if (!system_service || !system_service->file_system())
return; return;
gdata::GDataFileSystem* gdata_file_system = system_service->file_system(); gdata::GDataCache* cache = system_service->cache();
// We check permissions for raw cache file paths only for read-only // We check permissions for raw cache file paths only for read-only
// operations (when fileEntry.file() is called), so read only permissions // operations (when fileEntry.file() is called), so read only permissions
// should be sufficient for all cache paths. For the rest of supported // should be sufficient for all cache paths. For the rest of supported
// operations the file access check is done for drive/ paths. // operations the file access check is done for drive/ paths.
GrantFilePermissionsToHost(render_view_host, GrantFilePermissionsToHost(render_view_host,
gdata_file_system->GetCacheDirectoryPath( cache->GetCacheDirectoryPath(
gdata::GDataCache::CACHE_TYPE_TMP), gdata::GDataCache::CACHE_TYPE_TMP),
file_handler_util::GetReadOnlyPermissions()); file_handler_util::GetReadOnlyPermissions());
GrantFilePermissionsToHost( GrantFilePermissionsToHost(
render_view_host, render_view_host,
gdata_file_system->GetCacheDirectoryPath( cache->GetCacheDirectoryPath(
gdata::GDataCache::CACHE_TYPE_PERSISTENT), gdata::GDataCache::CACHE_TYPE_PERSISTENT),
file_handler_util::GetReadOnlyPermissions()); file_handler_util::GetReadOnlyPermissions());
gdata::GDataFileSystem* gdata_file_system = system_service->file_system();
provider->AddRemoteMountPoint( provider->AddRemoteMountPoint(
mount_point, mount_point,
new gdata::GDataFileSystemProxy(gdata_file_system)); new gdata::GDataFileSystemProxy(gdata_file_system));
...@@ -1003,7 +1004,9 @@ void AddMountFunction::GetLocalPathsResponseOnUIThread( ...@@ -1003,7 +1004,9 @@ void AddMountFunction::GetLocalPathsResponseOnUIThread(
gdata::GDataSystemServiceFactory::GetForProfile(profile_); gdata::GDataSystemServiceFactory::GetForProfile(profile_);
gdata::GDataFileSystem* file_system = gdata::GDataFileSystem* file_system =
system_service ? system_service->file_system() : NULL; system_service ? system_service->file_system() : NULL;
if (file_system && file_system->IsUnderGDataCacheDirectory(source_path)) { gdata::GDataCache* cache =
system_service ? system_service->cache() : NULL;
if (file_system && cache && cache->IsUnderGDataCacheDirectory(source_path)) {
file_system->SetMountedState( file_system->SetMountedState(
source_path, source_path,
true, true,
......
...@@ -122,7 +122,7 @@ void GDataDownloadObserver::SubstituteGDataDownloadPath(Profile* profile, ...@@ -122,7 +122,7 @@ void GDataDownloadObserver::SubstituteGDataDownloadPath(Profile* profile,
SetDownloadParams(gdata_path, download); SetDownloadParams(gdata_path, download);
const FilePath gdata_tmp_download_dir = const FilePath gdata_tmp_download_dir =
system_service->file_system()->GetCacheDirectoryPath( system_service->cache()->GetCacheDirectoryPath(
gdata::GDataCache::CACHE_TYPE_TMP_DOWNLOADS); gdata::GDataCache::CACHE_TYPE_TMP_DOWNLOADS);
// Swap the gdata path with a local path. Local path must be created // Swap the gdata path with a local path. Local path must be created
......
...@@ -917,9 +917,13 @@ GDataFileSystem::GetFileFromCacheParams::~GetFileFromCacheParams() { ...@@ -917,9 +917,13 @@ GDataFileSystem::GetFileFromCacheParams::~GetFileFromCacheParams() {
// GDataFileSystem class implementation. // GDataFileSystem class implementation.
GDataFileSystem::GDataFileSystem(Profile* profile, GDataFileSystem::GDataFileSystem(
DocumentsServiceInterface* documents_service) Profile* profile,
GDataCache* cache,
DocumentsServiceInterface* documents_service,
const base::SequencedWorkerPool::SequenceToken& sequence_token)
: profile_(profile), : profile_(profile),
cache_(cache),
documents_service_(documents_service), documents_service_(documents_service),
on_io_completed_(new base::WaitableEvent( on_io_completed_(new base::WaitableEvent(
true /* manual reset */, true /* initially signaled */)), true /* manual reset */, true /* initially signaled */)),
...@@ -929,7 +933,7 @@ GDataFileSystem::GDataFileSystem(Profile* profile, ...@@ -929,7 +933,7 @@ GDataFileSystem::GDataFileSystem(Profile* profile,
ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST( ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(
new base::WeakPtrFactory<GDataFileSystem>(this))), new base::WeakPtrFactory<GDataFileSystem>(this))),
ui_weak_ptr_(ui_weak_ptr_factory_->GetWeakPtr()), ui_weak_ptr_(ui_weak_ptr_factory_->GetWeakPtr()),
sequence_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()) { sequence_token_(sequence_token) {
// Should be created from the file browser extension API on UI thread. // Should be created from the file browser extension API on UI thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
} }
...@@ -940,10 +944,6 @@ void GDataFileSystem::Initialize() { ...@@ -940,10 +944,6 @@ void GDataFileSystem::Initialize() {
documents_service_->Initialize(profile_); documents_service_->Initialize(profile_);
root_.reset(new GDataRootDirectory); root_.reset(new GDataRootDirectory);
const FilePath cache_root_path = GDataCache::GetCacheRootPath(profile_);
cache_ = GDataCache::CreateGDataCache(cache_root_path,
BrowserThread::GetBlockingPool(),
sequence_token_).Pass();
PrefService* pref_service = profile_->GetPrefs(); PrefService* pref_service = profile_->GetPrefs();
hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles); hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles);
...@@ -1013,8 +1013,6 @@ GDataFileSystem::~GDataFileSystem() { ...@@ -1013,8 +1013,6 @@ GDataFileSystem::~GDataFileSystem() {
// Lock to let root destroy cache map and resource map. // Lock to let root destroy cache map and resource map.
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
root_.reset(); root_.reset();
// TODO(satorux): Should not delete this on UI thread. crbug.com/131826.
cache_.reset();
// Let's make sure that num_pending_tasks_lock_ has been released on all // Let's make sure that num_pending_tasks_lock_ has been released on all
// other threads. // other threads.
...@@ -2036,7 +2034,7 @@ void GDataFileSystem::GetResolvedFileByPath( ...@@ -2036,7 +2034,7 @@ void GDataFileSystem::GetResolvedFileByPath(
PostBlockingPoolSequencedTaskAndReply( PostBlockingPoolSequencedTaskAndReply(
FROM_HERE, FROM_HERE,
base::Bind(&CreateDocumentJsonFileOnBlockingPool, base::Bind(&CreateDocumentJsonFileOnBlockingPool,
GetCacheDirectoryPath( cache_->GetCacheDirectoryPath(
GDataCache::CACHE_TYPE_TMP_DOCUMENTS), GDataCache::CACHE_TYPE_TMP_DOCUMENTS),
GURL(file_proto->alternate_url()), GURL(file_proto->alternate_url()),
file_proto->gdata_entry().resource_id(), file_proto->gdata_entry().resource_id(),
...@@ -2236,7 +2234,7 @@ void GDataFileSystem::FreeDiskSpaceIfNeededFor(int64 num_bytes, ...@@ -2236,7 +2234,7 @@ void GDataFileSystem::FreeDiskSpaceIfNeededFor(int64 num_bytes,
// First remove temporary files from the cache map. // First remove temporary files from the cache map.
cache_->RemoveTemporaryFiles(); cache_->RemoveTemporaryFiles();
// Then remove all files under "tmp" directory. // Then remove all files under "tmp" directory.
RemoveAllFiles(GetCacheDirectoryPath(GDataCache::CACHE_TYPE_TMP)); RemoveAllFiles(cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_TMP));
// Check the disk space again. // Check the disk space again.
*has_enough_space = HasEnoughSpaceFor(num_bytes); *has_enough_space = HasEnoughSpaceFor(num_bytes);
...@@ -2957,7 +2955,7 @@ void GDataFileSystem::OnGetDocuments(ContentOrigin initial_origin, ...@@ -2957,7 +2955,7 @@ void GDataFileSystem::OnGetDocuments(ContentOrigin initial_origin,
PostBlockingPoolSequencedTask( PostBlockingPoolSequencedTask(
FROM_HERE, FROM_HERE,
base::Bind(&SaveFeedOnBlockingPoolForDebugging, base::Bind(&SaveFeedOnBlockingPoolForDebugging,
GetCacheDirectoryPath( cache_->GetCacheDirectoryPath(
GDataCache::CACHE_TYPE_META).Append(file_name), GDataCache::CACHE_TYPE_META).Append(file_name),
base::Passed(&data))); base::Passed(&data)));
#endif #endif
...@@ -3008,7 +3006,7 @@ void GDataFileSystem::LoadRootFeedFromCache( ...@@ -3008,7 +3006,7 @@ void GDataFileSystem::LoadRootFeedFromCache(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const FilePath path = const FilePath path =
GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append(
kFilesystemProtoFile); kFilesystemProtoFile);
LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path, LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path,
should_load_from_server, should_load_from_server,
...@@ -3094,7 +3092,7 @@ void GDataFileSystem::SaveFileSystemAsProto() { ...@@ -3094,7 +3092,7 @@ void GDataFileSystem::SaveFileSystemAsProto() {
} }
const FilePath path = const FilePath path =
GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append(
kFilesystemProtoFile); kFilesystemProtoFile);
scoped_ptr<std::string> serialized_proto(new std::string()); scoped_ptr<std::string> serialized_proto(new std::string());
root_->SerializeToString(serialized_proto.get()); root_->SerializeToString(serialized_proto.get());
...@@ -3955,23 +3953,6 @@ void GDataFileSystem::SetHideHostedDocuments(bool hide) { ...@@ -3955,23 +3953,6 @@ void GDataFileSystem::SetHideHostedDocuments(bool hide) {
//===================== GDataFileSystem: Cache entry points ==================== //===================== GDataFileSystem: Cache entry points ====================
bool GDataFileSystem::IsUnderGDataCacheDirectory(const FilePath& path) const {
return cache_->IsUnderGDataCacheDirectory(path);
}
FilePath GDataFileSystem::GetCacheDirectoryPath(
GDataCache::CacheSubDirectoryType sub_dir_type) const {
return cache_->GetCacheDirectoryPath(sub_dir_type);
}
FilePath GDataFileSystem::GetCacheFilePath(
const std::string& resource_id,
const std::string& md5,
GDataCache::CacheSubDirectoryType sub_dir_type,
GDataCache::CachedFileOrigin file_origin) const {
return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, file_origin);
}
void GDataFileSystem::StoreToCache(const std::string& resource_id, void GDataFileSystem::StoreToCache(const std::string& resource_id,
const std::string& md5, const std::string& md5,
const FilePath& source_path, const FilePath& source_path,
...@@ -4152,7 +4133,7 @@ void GDataFileSystem::InitializeCacheOnBlockingPool() { ...@@ -4152,7 +4133,7 @@ void GDataFileSystem::InitializeCacheOnBlockingPool() {
// Change permissions of cache persistent directory to u+rwx,og+x in order to // Change permissions of cache persistent directory to u+rwx,og+x in order to
// allow archive files in that directory to be mounted by cros-disks. // allow archive files in that directory to be mounted by cros-disks.
error = ChangeFilePermissions( error = ChangeFilePermissions(
GetCacheDirectoryPath(GDataCache::CACHE_TYPE_PERSISTENT), cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_PERSISTENT),
S_IRWXU | S_IXGRP | S_IXOTH); S_IRWXU | S_IXGRP | S_IXOTH);
if (error != base::PLATFORM_FILE_OK) if (error != base::PLATFORM_FILE_OK)
return; return;
...@@ -4896,7 +4877,7 @@ void GDataFileSystem::ScanCacheDirectory( ...@@ -4896,7 +4877,7 @@ void GDataFileSystem::ScanCacheDirectory(
GDataCache::CacheSubDirectoryType sub_dir_type, GDataCache::CacheSubDirectoryType sub_dir_type,
GDataCache::CacheMap* cache_map) { GDataCache::CacheMap* cache_map) {
file_util::FileEnumerator enumerator( file_util::FileEnumerator enumerator(
GetCacheDirectoryPath(sub_dir_type), cache_->GetCacheDirectoryPath(sub_dir_type),
false, // not recursive false, // not recursive
static_cast<file_util::FileEnumerator::FileType>( static_cast<file_util::FileEnumerator::FileType>(
file_util::FileEnumerator::FILES | file_util::FileEnumerator::FILES |
......
...@@ -377,22 +377,6 @@ class GDataFileSystemInterface { ...@@ -377,22 +377,6 @@ class GDataFileSystemInterface {
virtual void SearchAsync(const std::string& search_query, virtual void SearchAsync(const std::string& search_query,
const ReadDirectoryCallback& callback) = 0; const ReadDirectoryCallback& callback) = 0;
// Returns true if the given path is under gdata cache directory, i.e.
// <user_profile_dir>/GCache/v1
virtual bool IsUnderGDataCacheDirectory(const FilePath& path) const = 0;
// Returns the sub-directory under gdata cache directory for the given sub
// directory type. Example: <user_profile_dir>/GCache/v1/tmp
virtual FilePath GetCacheDirectoryPath(
GDataCache::CacheSubDirectoryType sub_dir_type) const = 0;
// Returns absolute path of the file if it were cached or to be cached.
virtual FilePath GetCacheFilePath(
const std::string& resource_id,
const std::string& md5,
GDataCache::CacheSubDirectoryType sub_dir_type,
GDataCache::CachedFileOrigin file_orign) const = 0;
// Fetches the user's Account Metadata to find out current quota information // Fetches the user's Account Metadata to find out current quota information
// and returns it to the callback. // and returns it to the callback.
virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0; virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0;
...@@ -422,8 +406,11 @@ class GDataFileSystemInterface { ...@@ -422,8 +406,11 @@ class GDataFileSystemInterface {
class GDataFileSystem : public GDataFileSystemInterface, class GDataFileSystem : public GDataFileSystemInterface,
public content::NotificationObserver { public content::NotificationObserver {
public: public:
GDataFileSystem(Profile* profile, GDataFileSystem(
DocumentsServiceInterface* documents_service); Profile* profile,
GDataCache* cache,
DocumentsServiceInterface* documents_service,
const base::SequencedWorkerPool::SequenceToken& sequence_token);
virtual ~GDataFileSystem(); virtual ~GDataFileSystem();
// GDataFileSystem overrides. // GDataFileSystem overrides.
...@@ -486,14 +473,6 @@ class GDataFileSystem : public GDataFileSystemInterface, ...@@ -486,14 +473,6 @@ class GDataFileSystem : public GDataFileSystemInterface,
const ReadDirectoryCallback& callback) OVERRIDE; const ReadDirectoryCallback& callback) OVERRIDE;
virtual void RequestDirectoryRefresh( virtual void RequestDirectoryRefresh(
const FilePath& file_path) OVERRIDE; const FilePath& file_path) OVERRIDE;
virtual bool IsUnderGDataCacheDirectory(const FilePath& path) const OVERRIDE;
virtual FilePath GetCacheDirectoryPath(
GDataCache::CacheSubDirectoryType sub_dir_type) const OVERRIDE;
virtual FilePath GetCacheFilePath(
const std::string& resource_id,
const std::string& md5,
GDataCache::CacheSubDirectoryType sub_dir_type,
GDataCache::CachedFileOrigin file_orign) const OVERRIDE;
virtual void GetAvailableSpace( virtual void GetAvailableSpace(
const GetAvailableSpaceCallback& callback) OVERRIDE; const GetAvailableSpaceCallback& callback) OVERRIDE;
// Calls private Pin or Unpin methods with |callback|. // Calls private Pin or Unpin methods with |callback|.
...@@ -1472,7 +1451,6 @@ class GDataFileSystem : public GDataFileSystemInterface, ...@@ -1472,7 +1451,6 @@ class GDataFileSystem : public GDataFileSystemInterface,
const SetMountedStateCallback& callback); const SetMountedStateCallback& callback);
scoped_ptr<GDataRootDirectory> root_; scoped_ptr<GDataRootDirectory> root_;
scoped_ptr<GDataCache> cache_;
// This guards regular states. // This guards regular states.
base::Lock lock_; base::Lock lock_;
...@@ -1480,7 +1458,10 @@ class GDataFileSystem : public GDataFileSystemInterface, ...@@ -1480,7 +1458,10 @@ class GDataFileSystem : public GDataFileSystemInterface,
// The profile hosts the GDataFileSystem via GDataSystemService. // The profile hosts the GDataFileSystem via GDataSystemService.
Profile* profile_; Profile* profile_;
// The document service for the GDataFileSystem. // The cache owned by GDataSystemService.
GDataCache* cache_;
// The document service owned by GDataSystemService.
DocumentsServiceInterface* documents_service_; DocumentsServiceInterface* documents_service_;
// Waitable events used to block destructor until all the tasks on blocking // Waitable events used to block destructor until all the tasks on blocking
......
...@@ -73,9 +73,11 @@ void ScanPinnedDirectory(const FilePath& directory, ...@@ -73,9 +73,11 @@ void ScanPinnedDirectory(const FilePath& directory,
} // namespace } // namespace
GDataSyncClient::GDataSyncClient(Profile* profile, GDataSyncClient::GDataSyncClient(Profile* profile,
GDataFileSystemInterface* file_system) GDataFileSystemInterface* file_system,
GDataCache* cache)
: profile_(profile), : profile_(profile),
file_system_(file_system), file_system_(file_system),
cache_(cache),
registrar_(new PrefChangeRegistrar), registrar_(new PrefChangeRegistrar),
fetch_loop_is_running_(false), fetch_loop_is_running_(false),
weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
...@@ -120,7 +122,7 @@ void GDataSyncClient::StartInitialScan(const base::Closure& closure) { ...@@ -120,7 +122,7 @@ void GDataSyncClient::StartInitialScan(const base::Closure& closure) {
BrowserThread::GetBlockingPool()->PostTaskAndReply( BrowserThread::GetBlockingPool()->PostTaskAndReply(
FROM_HERE, FROM_HERE,
base::Bind(&ScanPinnedDirectory, base::Bind(&ScanPinnedDirectory,
file_system_->GetCacheDirectoryPath( cache_->GetCacheDirectoryPath(
GDataCache::CACHE_TYPE_PINNED), GDataCache::CACHE_TYPE_PINNED),
resource_ids), resource_ids),
base::Bind(&GDataSyncClient::OnInitialScanComplete, base::Bind(&GDataSyncClient::OnInitialScanComplete,
......
...@@ -62,7 +62,8 @@ class GDataSyncClient ...@@ -62,7 +62,8 @@ class GDataSyncClient
// |file_system| is used access the // |file_system| is used access the
// cache (ex. store a file to the cache when the file is downloaded). // cache (ex. store a file to the cache when the file is downloaded).
GDataSyncClient(Profile* profile, GDataSyncClient(Profile* profile,
GDataFileSystemInterface* file_system); GDataFileSystemInterface* file_system,
GDataCache* cache);
virtual ~GDataSyncClient(); virtual ~GDataSyncClient();
// GDataSyncClientInterface overrides. // GDataSyncClientInterface overrides.
...@@ -127,7 +128,8 @@ class GDataSyncClient ...@@ -127,7 +128,8 @@ class GDataSyncClient
const content::NotificationSource& source, const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE; const content::NotificationDetails& details) OVERRIDE;
Profile* profile_; Profile* profile_;
GDataFileSystemInterface* file_system_; GDataFileSystemInterface* file_system_; // Owned by GDataSystemService.
GDataCache* cache_; // Owned by GDataSystemService.
scoped_ptr<PrefChangeRegistrar> registrar_; scoped_ptr<PrefChangeRegistrar> registrar_;
// The queue of resource IDs used to fetch pinned-but-not-fetched files in // The queue of resource IDs used to fetch pinned-but-not-fetched files in
......
...@@ -37,10 +37,11 @@ class GDataSyncClientTest : public testing::Test { ...@@ -37,10 +37,11 @@ class GDataSyncClientTest : public testing::Test {
public: public:
GDataSyncClientTest() GDataSyncClientTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_), : ui_thread_(content::BrowserThread::UI, &message_loop_),
io_thread_(content::BrowserThread::IO),
sequence_token_(
content::BrowserThread::GetBlockingPool()->GetSequenceToken()),
profile_(new TestingProfile), profile_(new TestingProfile),
mock_file_system_(new MockGDataFileSystem), mock_file_system_(new MockGDataFileSystem),
sync_client_(new GDataSyncClient(profile_.get(),
mock_file_system_.get())),
mock_network_library_(NULL) { mock_network_library_(NULL) {
} }
...@@ -51,14 +52,23 @@ class GDataSyncClientTest : public testing::Test { ...@@ -51,14 +52,23 @@ class GDataSyncClientTest : public testing::Test {
mock_network_library_ = new chromeos::MockNetworkLibrary; mock_network_library_ = new chromeos::MockNetworkLibrary;
chromeos::CrosLibrary::Get()->GetTestApi()->SetNetworkLibrary( chromeos::CrosLibrary::Get()->GetTestApi()->SetNetworkLibrary(
mock_network_library_, true); mock_network_library_, true);
EXPECT_CALL(*mock_network_library_, AddNetworkManagerObserver(
sync_client_.get())).Times(1);
EXPECT_CALL(*mock_network_library_, RemoveNetworkManagerObserver(
sync_client_.get())).Times(1);
// Create a temporary directory. // Create a temporary directory.
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
// Initialize the sync client.
cache_.reset(GDataCache::CreateGDataCache(
temp_dir_.path(),
content::BrowserThread::GetBlockingPool(),
sequence_token_).release());
sync_client_.reset(new GDataSyncClient(profile_.get(),
mock_file_system_.get(),
cache_.get()));
EXPECT_CALL(*mock_network_library_, AddNetworkManagerObserver(
sync_client_.get())).Times(1);
EXPECT_CALL(*mock_network_library_, RemoveNetworkManagerObserver(
sync_client_.get())).Times(1);
EXPECT_CALL(*mock_file_system_, AddObserver(sync_client_.get())) EXPECT_CALL(*mock_file_system_, AddObserver(sync_client_.get()))
.Times(1); .Times(1);
EXPECT_CALL(*mock_file_system_, RemoveObserver(sync_client_.get())) EXPECT_CALL(*mock_file_system_, RemoveObserver(sync_client_.get()))
...@@ -72,6 +82,23 @@ class GDataSyncClientTest : public testing::Test { ...@@ -72,6 +82,23 @@ class GDataSyncClientTest : public testing::Test {
// client registers itself as observer of NetworkLibrary. // client registers itself as observer of NetworkLibrary.
sync_client_.reset(); sync_client_.reset();
chromeos::CrosLibrary::Shutdown(); chromeos::CrosLibrary::Shutdown();
content::BrowserThread::GetBlockingPool()
->GetSequencedTaskRunner(sequence_token_)->PostTask(
FROM_HERE,
base::Bind(&base::DeletePointer<GDataCache>, cache_.release()));
RunAllPendingForIO();
}
// Used to wait for the result from an operation that involves file IO,
// that runs on the blocking pool thread.
void RunAllPendingForIO() {
// We should first flush tasks on UI thread, as it can require some
// tasks to be run before IO tasks start.
message_loop_.RunAllPending();
content::BrowserThread::GetBlockingPool()->FlushForTesting();
// Once IO tasks are done, flush UI thread again so the results from IO
// tasks are processed.
message_loop_.RunAllPending();
} }
// Sets up MockNetworkLibrary as if it's connected to wifi network. // Sets up MockNetworkLibrary as if it's connected to wifi network.
...@@ -124,21 +151,26 @@ class GDataSyncClientTest : public testing::Test { ...@@ -124,21 +151,26 @@ class GDataSyncClientTest : public testing::Test {
// Sets up test files in the temporary directory. // Sets up test files in the temporary directory.
void SetUpTestFiles() { void SetUpTestFiles() {
// Create a directory in the temporary directory for pinned files.
const FilePath pinned_dir =
cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_PINNED);
ASSERT_TRUE(file_util::CreateDirectory(pinned_dir));
// Create a symlink in the temporary directory to /dev/null. // Create a symlink in the temporary directory to /dev/null.
// We'll collect this resource ID as a file to be fetched. // We'll collect this resource ID as a file to be fetched.
ASSERT_TRUE( ASSERT_TRUE(
file_util::CreateSymbolicLink( file_util::CreateSymbolicLink(
FilePath::FromUTF8Unsafe("/dev/null"), FilePath::FromUTF8Unsafe("/dev/null"),
temp_dir_.path().Append("resource_id_not_fetched_foo"))); pinned_dir.Append("resource_id_not_fetched_foo")));
// Create some more. // Create some more.
ASSERT_TRUE( ASSERT_TRUE(
file_util::CreateSymbolicLink( file_util::CreateSymbolicLink(
FilePath::FromUTF8Unsafe("/dev/null"), FilePath::FromUTF8Unsafe("/dev/null"),
temp_dir_.path().Append("resource_id_not_fetched_bar"))); pinned_dir.Append("resource_id_not_fetched_bar")));
ASSERT_TRUE( ASSERT_TRUE(
file_util::CreateSymbolicLink( file_util::CreateSymbolicLink(
FilePath::FromUTF8Unsafe("/dev/null"), FilePath::FromUTF8Unsafe("/dev/null"),
temp_dir_.path().Append("resource_id_not_fetched_baz"))); pinned_dir.Append("resource_id_not_fetched_baz")));
// Create a symlink in the temporary directory to a test file // Create a symlink in the temporary directory to a test file
// We won't collect this resource ID, as it already exists. // We won't collect this resource ID, as it already exists.
...@@ -149,7 +181,7 @@ class GDataSyncClientTest : public testing::Test { ...@@ -149,7 +181,7 @@ class GDataSyncClientTest : public testing::Test {
ASSERT_TRUE( ASSERT_TRUE(
file_util::CreateSymbolicLink( file_util::CreateSymbolicLink(
test_file_path, test_file_path,
temp_dir_.path().Append("resource_id_fetched"))); pinned_dir.Append("resource_id_fetched")));
} }
// Called when StartInitialScan() is complete. // Called when StartInitialScan() is complete.
...@@ -172,9 +204,12 @@ class GDataSyncClientTest : public testing::Test { ...@@ -172,9 +204,12 @@ class GDataSyncClientTest : public testing::Test {
protected: protected:
MessageLoopForUI message_loop_; MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_; content::TestBrowserThread ui_thread_;
content::TestBrowserThread io_thread_;
const base::SequencedWorkerPool::SequenceToken sequence_token_;
ScopedTempDir temp_dir_; ScopedTempDir temp_dir_;
scoped_ptr<TestingProfile> profile_; scoped_ptr<TestingProfile> profile_;
scoped_ptr<MockGDataFileSystem> mock_file_system_; scoped_ptr<MockGDataFileSystem> mock_file_system_;
scoped_ptr<GDataCache> cache_;
scoped_ptr<GDataSyncClient> sync_client_; scoped_ptr<GDataSyncClient> sync_client_;
chromeos::MockNetworkLibrary* mock_network_library_; chromeos::MockNetworkLibrary* mock_network_library_;
scoped_ptr<chromeos::Network> active_network_; scoped_ptr<chromeos::Network> active_network_;
...@@ -183,10 +218,6 @@ class GDataSyncClientTest : public testing::Test { ...@@ -183,10 +218,6 @@ class GDataSyncClientTest : public testing::Test {
TEST_F(GDataSyncClientTest, StartInitialScan) { TEST_F(GDataSyncClientTest, StartInitialScan) {
SetUpTestFiles(); SetUpTestFiles();
EXPECT_CALL(*mock_file_system_, GetCacheDirectoryPath(
GDataCache::CACHE_TYPE_PINNED))
.WillOnce(Return(temp_dir_.path()));
sync_client_->StartInitialScan( sync_client_->StartInitialScan(
base::Bind(&GDataSyncClientTest::OnInitialScanComplete, base::Bind(&GDataSyncClientTest::OnInitialScanComplete,
base::Unretained(this))); base::Unretained(this)));
......
...@@ -28,17 +28,30 @@ namespace gdata { ...@@ -28,17 +28,30 @@ namespace gdata {
//===================== GDataSystemService ==================================== //===================== GDataSystemService ====================================
GDataSystemService::GDataSystemService(Profile* profile) GDataSystemService::GDataSystemService(Profile* profile)
: profile_(profile), : profile_(profile),
sequence_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()),
// TODO(hashimoto): Create cache_ on the blocking pool. crbug.com/131756
cache_(
GDataCache::CreateGDataCache(GDataCache::GetCacheRootPath(profile_),
BrowserThread::GetBlockingPool(),
sequence_token_)),
documents_service_(new DocumentsService), documents_service_(new DocumentsService),
file_system_(new GDataFileSystem(profile, docs_service())), file_system_(new GDataFileSystem(profile,
cache(),
docs_service(),
sequence_token_)),
uploader_(new GDataUploader(file_system(), docs_service())), uploader_(new GDataUploader(file_system(), docs_service())),
download_observer_(new GDataDownloadObserver), download_observer_(new GDataDownloadObserver),
sync_client_(new GDataSyncClient(profile, file_system())), sync_client_(new GDataSyncClient(profile, file_system(), cache())),
webapps_registry_(new DriveWebAppsRegistry) { webapps_registry_(new DriveWebAppsRegistry) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
} }
GDataSystemService::~GDataSystemService() { GDataSystemService::~GDataSystemService() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(sequence_token_)
->PostTask(
FROM_HERE,
base::Bind(&base::DeletePointer<GDataCache>, cache_.release()));
} }
void GDataSystemService::Initialize() { void GDataSystemService::Initialize() {
...@@ -55,7 +68,7 @@ void GDataSystemService::Initialize() { ...@@ -55,7 +68,7 @@ void GDataSystemService::Initialize() {
download_observer_->Initialize( download_observer_->Initialize(
uploader_.get(), uploader_.get(),
download_manager, download_manager,
file_system_->GetCacheDirectoryPath( cache_->GetCacheDirectoryPath(
GDataCache::CACHE_TYPE_TMP_DOWNLOADS)); GDataCache::CACHE_TYPE_TMP_DOWNLOADS));
} }
......
...@@ -31,6 +31,9 @@ class GDataSystemService : public ProfileKeyedService { ...@@ -31,6 +31,9 @@ class GDataSystemService : public ProfileKeyedService {
// Returns the documents service instance. // Returns the documents service instance.
DocumentsServiceInterface* docs_service() { return documents_service_.get(); } DocumentsServiceInterface* docs_service() { return documents_service_.get(); }
// Returns the cache instance.
GDataCache* cache() { return cache_.get(); }
// Returns the file system instance. // Returns the file system instance.
GDataFileSystem* file_system() { return file_system_.get(); } GDataFileSystem* file_system() { return file_system_.get(); }
...@@ -54,6 +57,8 @@ class GDataSystemService : public ProfileKeyedService { ...@@ -54,6 +57,8 @@ class GDataSystemService : public ProfileKeyedService {
friend class GDataSystemServiceFactory; friend class GDataSystemServiceFactory;
Profile* profile_; Profile* profile_;
const base::SequencedWorkerPool::SequenceToken sequence_token_;
scoped_ptr<GDataCache> cache_;
scoped_ptr<DocumentsServiceInterface> documents_service_; scoped_ptr<DocumentsServiceInterface> documents_service_;
scoped_ptr<GDataFileSystem> file_system_; scoped_ptr<GDataFileSystem> file_system_;
scoped_ptr<GDataUploader> uploader_; scoped_ptr<GDataUploader> uploader_;
......
...@@ -63,6 +63,12 @@ GDataFileSystem* GetGDataFileSystem(Profile* profile) { ...@@ -63,6 +63,12 @@ GDataFileSystem* GetGDataFileSystem(Profile* profile) {
return system_service ? system_service->file_system() : NULL; return system_service ? system_service->file_system() : NULL;
} }
GDataCache* GetGDataCache(Profile* profile) {
GDataSystemService* system_service =
GDataSystemServiceFactory::GetForProfile(profile);
return system_service ? system_service->cache() : NULL;
}
void GetHostedDocumentURLBlockingThread(const FilePath& gdata_cache_path, void GetHostedDocumentURLBlockingThread(const FilePath& gdata_cache_path,
GURL* url) { GURL* url) {
std::string json; std::string json;
...@@ -106,8 +112,8 @@ void OnGetFileInfoForInsertGDataCachePathsPermissions( ...@@ -106,8 +112,8 @@ void OnGetFileInfoForInsertGDataCachePathsPermissions(
DCHECK(cache_paths); DCHECK(cache_paths);
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
GDataFileSystem* file_system = GetGDataFileSystem(profile); GDataCache* cache = GetGDataCache(profile);
if (!file_system || error != base::PLATFORM_FILE_OK) { if (!cache || error != base::PLATFORM_FILE_OK) {
callback.Run(); callback.Run();
return; return;
} }
...@@ -121,24 +127,24 @@ void OnGetFileInfoForInsertGDataCachePathsPermissions( ...@@ -121,24 +127,24 @@ void OnGetFileInfoForInsertGDataCachePathsPermissions(
// should be sufficient for all cache paths. For the rest of supported // should be sufficient for all cache paths. For the rest of supported
// operations the file access check is done for drive/ paths. // operations the file access check is done for drive/ paths.
cache_paths->push_back(std::make_pair( cache_paths->push_back(std::make_pair(
file_system->GetCacheFilePath(resource_id, file_md5, cache->GetCacheFilePath(resource_id, file_md5,
GDataCache::CACHE_TYPE_PERSISTENT, GDataCache::CACHE_TYPE_PERSISTENT,
GDataCache::CACHED_FILE_FROM_SERVER), GDataCache::CACHED_FILE_FROM_SERVER),
kReadOnlyFilePermissions)); kReadOnlyFilePermissions));
// TODO(tbarzic): When we start supporting openFile operation, we may have to // TODO(tbarzic): When we start supporting openFile operation, we may have to
// change permission for localy modified files to match handler's permissions. // change permission for localy modified files to match handler's permissions.
cache_paths->push_back(std::make_pair( cache_paths->push_back(std::make_pair(
file_system->GetCacheFilePath(resource_id, file_md5, cache->GetCacheFilePath(resource_id, file_md5,
GDataCache::CACHE_TYPE_PERSISTENT, GDataCache::CACHE_TYPE_PERSISTENT,
GDataCache::CACHED_FILE_LOCALLY_MODIFIED), GDataCache::CACHED_FILE_LOCALLY_MODIFIED),
kReadOnlyFilePermissions)); kReadOnlyFilePermissions));
cache_paths->push_back(std::make_pair( cache_paths->push_back(std::make_pair(
file_system->GetCacheFilePath(resource_id, file_md5, cache->GetCacheFilePath(resource_id, file_md5,
GDataCache::CACHE_TYPE_PERSISTENT, GDataCache::CACHE_TYPE_PERSISTENT,
GDataCache::CACHED_FILE_MOUNTED), GDataCache::CACHED_FILE_MOUNTED),
kReadOnlyFilePermissions)); kReadOnlyFilePermissions));
cache_paths->push_back(std::make_pair( cache_paths->push_back(std::make_pair(
file_system->GetCacheFilePath(resource_id, file_md5, cache->GetCacheFilePath(resource_id, file_md5,
GDataCache::CACHE_TYPE_TMP, GDataCache::CACHE_TYPE_TMP,
GDataCache::CACHED_FILE_FROM_SERVER), GDataCache::CACHED_FILE_FROM_SERVER),
kReadOnlyFilePermissions)); kReadOnlyFilePermissions));
...@@ -181,10 +187,13 @@ void ModifyGDataFileResourceUrl(Profile* profile, ...@@ -181,10 +187,13 @@ void ModifyGDataFileResourceUrl(Profile* profile,
GDataFileSystem* file_system = GetGDataFileSystem(profile); GDataFileSystem* file_system = GetGDataFileSystem(profile);
if (!file_system) if (!file_system)
return; return;
GDataCache* cache = GetGDataCache(profile);
if (!cache)
return;
// Handle hosted documents. The edit url is in the temporary file, so we // Handle hosted documents. The edit url is in the temporary file, so we
// read it on a blocking thread. // read it on a blocking thread.
if (file_system->GetCacheDirectoryPath( if (cache->GetCacheDirectoryPath(
GDataCache::CACHE_TYPE_TMP_DOCUMENTS).IsParent( GDataCache::CACHE_TYPE_TMP_DOCUMENTS).IsParent(
gdata_cache_path)) { gdata_cache_path)) {
GURL* edit_url = new GURL(); GURL* edit_url = new GURL();
...@@ -197,7 +206,7 @@ void ModifyGDataFileResourceUrl(Profile* profile, ...@@ -197,7 +206,7 @@ void ModifyGDataFileResourceUrl(Profile* profile,
} }
// Handle all other gdata files. // Handle all other gdata files.
if (file_system->GetCacheDirectoryPath( if (cache->GetCacheDirectoryPath(
GDataCache::CACHE_TYPE_TMP).IsParent(gdata_cache_path)) { GDataCache::CACHE_TYPE_TMP).IsParent(gdata_cache_path)) {
const std::string resource_id = const std::string resource_id =
gdata_cache_path.BaseName().RemoveExtension().AsUTF8Unsafe(); gdata_cache_path.BaseName().RemoveExtension().AsUTF8Unsafe();
......
...@@ -80,14 +80,6 @@ class MockGDataFileSystem : public GDataFileSystemInterface { ...@@ -80,14 +80,6 @@ class MockGDataFileSystem : public GDataFileSystemInterface {
const ReadDirectoryCallback& callback)); const ReadDirectoryCallback& callback));
MOCK_METHOD1(RequestDirectoryRefresh, MOCK_METHOD1(RequestDirectoryRefresh,
void(const FilePath& file_path)); void(const FilePath& file_path));
MOCK_CONST_METHOD1(IsUnderGDataCacheDirectory, bool(const FilePath& path));
MOCK_CONST_METHOD1(GetCacheDirectoryPath, FilePath(
GDataCache::CacheSubDirectoryType));
MOCK_CONST_METHOD4(GetCacheFilePath, FilePath(
const std::string&,
const std::string&,
GDataCache::CacheSubDirectoryType,
GDataCache::CachedFileOrigin));
MOCK_METHOD1(GetAvailableSpace, MOCK_METHOD1(GetAvailableSpace,
void(const GetAvailableSpaceCallback& callback)); void(const GetAvailableSpaceCallback& callback));
MOCK_METHOD3(SetPinState, void(const FilePath&, MOCK_METHOD3(SetPinState, void(const FilePath&,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment