Commit b4db86a6 authored by hidehiko@chromium.org's avatar hidehiko@chromium.org

Introduce State to DriveIntegrationService.

This is small refactoring of DriveIntegrationService by introducing State.
This is preparation of mount/enabled state migration of
DriveIntegrationSerivice. By the migration, DriveIntegrationService can be
obtained even before initialization. This CL supports the change.
Also, by this CL, FileSystem can be deleted even before Initialize() is called.

BUG=284972
TEST=Ran unit_tests
R=kinaba@chromium.org

Review URL: https://codereview.chromium.org/23548025

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@222231 0039d316-1c4b-4281-b951-d872f2087c98
parent 39080c97
......@@ -139,7 +139,7 @@ DriveIntegrationService::DriveIntegrationService(
const base::FilePath& test_cache_root,
FileSystemInterface* test_file_system)
: profile_(profile),
is_initialized_(false),
state_(NOT_INITIALIZED),
cache_root_directory_(!test_cache_root.empty() ?
test_cache_root : util::GetCacheRootPath(profile)),
weak_ptr_factory_(this) {
......@@ -209,6 +209,9 @@ DriveIntegrationService::~DriveIntegrationService() {
void DriveIntegrationService::Initialize() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_EQ(NOT_INITIALIZED, state_);
state_ = INITIALIZING;
drive_service_->Initialize();
file_system_->Initialize();
......@@ -227,6 +230,8 @@ void DriveIntegrationService::Initialize() {
void DriveIntegrationService::Shutdown() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
weak_ptr_factory_.InvalidateWeakPtrs();
DriveNotificationManager* drive_notification_manager =
DriveNotificationManagerFactory::GetForBrowserContext(profile_);
if (drive_notification_manager)
......@@ -272,7 +277,8 @@ bool DriveIntegrationService::IsDriveEnabled() {
if (!util::IsDriveEnabledForProfile(profile_))
return false;
return is_initialized_;
// For backword compatibility, REMOUNTING also means enabled.
return state_ == INITIALIZED || state_ == REMOUNTING;
}
void DriveIntegrationService::ClearCacheAndRemountFileSystem(
......@@ -280,6 +286,13 @@ void DriveIntegrationService::ClearCacheAndRemountFileSystem(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
if (state_ != INITIALIZED) {
callback.Run(false);
return;
}
state_ = REMOUNTING;
RemoveDriveMountPoint();
// Reloading the file system will clear the resource metadata.
file_system_->Reload();
......@@ -324,9 +337,12 @@ void DriveIntegrationService::AddDriveMountPoint() {
drive_mount_point);
if (success) {
state_ = INITIALIZED;
util::Log(logging::LOG_INFO, "Drive mount point is added");
FOR_EACH_OBSERVER(DriveIntegrationServiceObserver, observers_,
OnFileSystemMounted());
} else {
state_ = NOT_INITIALIZED;
}
}
......@@ -362,9 +378,12 @@ void DriveIntegrationService::InitializeAfterMetadataInitialized(
pref_service->SetFilePath(prefs::kDownloadDefaultDirectory,
DownloadPrefs::GetDefaultDownloadDirectory());
}
// Back to NOT_INITIALIZED state. Then, re-running Initialize() should
// work if the error is recovarable manually (such as out of disk space).
state_ = NOT_INITIALIZED;
return;
}
is_initialized_ = true;
content::DownloadManager* download_manager =
g_browser_process->download_status_updater() ?
......@@ -388,6 +407,7 @@ void DriveIntegrationService::InitializeAfterMetadataInitialized(
}
AddDriveMountPoint();
state_ = INITIALIZED;
}
//===================== DriveIntegrationServiceFactory =======================
......
......@@ -110,6 +110,13 @@ class DriveIntegrationService
const base::Callback<void(bool)>& callback);
private:
enum State {
NOT_INITIALIZED,
INITIALIZING,
INITIALIZED,
REMOUNTING,
};
// Returns true if Drive is enabled.
// Must be called on UI thread.
bool IsDriveEnabled();
......@@ -131,7 +138,7 @@ class DriveIntegrationService
friend class DriveIntegrationServiceFactory;
Profile* profile_;
bool is_initialized_;
State state_;
base::FilePath cache_root_directory_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
......
......@@ -401,7 +401,8 @@ FileSystem::~FileSystem() {
// shutdown.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
change_list_loader_->RemoveObserver(this);
if (change_list_loader_)
change_list_loader_->RemoveObserver(this);
}
void FileSystem::AddObserver(FileSystemObserver* observer) {
......
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