Commit 27ffb8aa authored by Austin Tankiang's avatar Austin Tankiang Committed by Commit Bot

Change drive-internals reset cache to delete folder

Bug: 1068479
Change-Id: Iaca92655f6633658e56cd4969646e96fc8030b48
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2139060
Commit-Queue: Austin Tankiang <austinct@chromium.org>
Reviewed-by: default avatarSergei Datsenko <dats@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757761}
parent 3cbf43ae
...@@ -169,10 +169,6 @@ FileError InitializeMetadata( ...@@ -169,10 +169,6 @@ FileError InitializeMetadata(
return FILE_ERROR_OK; return FILE_ERROR_OK;
} }
void ResetCacheDone(base::OnceCallback<void(bool)> callback, FileError error) {
std::move(callback).Run(error == FILE_ERROR_OK);
}
base::FilePath GetFullPath(internal::ResourceMetadataStorage* metadata_storage, base::FilePath GetFullPath(internal::ResourceMetadataStorage* metadata_storage,
const ResourceEntry& entry) { const ResourceEntry& entry) {
std::vector<std::string> path_components; std::vector<std::string> path_components;
...@@ -689,17 +685,55 @@ void DriveIntegrationService::RemoveObserver( ...@@ -689,17 +685,55 @@ void DriveIntegrationService::RemoveObserver(
} }
void DriveIntegrationService::ClearCacheAndRemountFileSystem( void DriveIntegrationService::ClearCacheAndRemountFileSystem(
const base::Callback<void(bool)>& callback) { base::OnceCallback<void(bool)> callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(callback); if (in_clear_cache_) {
std::move(callback).Run(false);
if (state_ != INITIALIZED || !GetDriveFsInterface()) {
callback.Run(false);
return; return;
} }
in_clear_cache_ = true;
if (IsMounted()) {
RemoveDriveMountPoint();
// TODO(crbug/1069328): We wait 2 seconds here so that DriveFS can unmount
// completely. Ideally we'd wait for an unmount complete callback.
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&DriveIntegrationService::
ClearCacheAndRemountFileSystemAfterUnmount,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
base::TimeDelta::FromSeconds(2));
} else {
ClearCacheAndRemountFileSystemAfterUnmount(std::move(callback));
}
}
GetDriveFsInterface()->ResetCache(mojo::WrapCallbackWithDefaultInvokeIfNotRun( void DriveIntegrationService::ClearCacheAndRemountFileSystemAfterUnmount(
base::BindOnce(&ResetCacheDone, callback), FILE_ERROR_ABORT)); base::OnceCallback<void(bool)> callback) {
bool success = true;
base::FilePath cache_path = GetDriveFsHost()->GetDataPath();
base::FilePath logs_path = GetDriveFsLogPath().DirName();
base::FileEnumerator content_enumerator(
cache_path, false,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES |
base::FileEnumerator::SHOW_SYM_LINKS);
for (base::FilePath path = content_enumerator.Next(); !path.empty();
path = content_enumerator.Next()) {
// Keep the logs folder as it's useful for debugging.
if (path == logs_path) {
continue;
}
if (!base::DeleteFileRecursively(path)) {
success = false;
break;
}
}
if (is_enabled()) {
AddDriveMountPoint();
}
in_clear_cache_ = false;
std::move(callback).Run(success);
} }
drivefs::DriveFsHost* DriveIntegrationService::GetDriveFsHost() const { drivefs::DriveFsHost* DriveIntegrationService::GetDriveFsHost() const {
......
...@@ -152,12 +152,10 @@ class DriveIntegrationService : public KeyedService, ...@@ -152,12 +152,10 @@ class DriveIntegrationService : public KeyedService,
EventLogger* event_logger() { return logger_.get(); } EventLogger* event_logger() { return logger_.get(); }
// Clears all the local cache file, the local resource metadata, and // Clears all the local cache folder and remounts the file system. |callback|
// in-memory Drive app registry, and remounts the file system. |callback|
// is called with true when this operation is done successfully. Otherwise, // is called with true when this operation is done successfully. Otherwise,
// |callback| is called with false. |callback| must not be null. // |callback| is called with false. |callback| must not be null.
void ClearCacheAndRemountFileSystem( void ClearCacheAndRemountFileSystem(base::OnceCallback<void(bool)> callback);
const base::Callback<void(bool)>& callback);
// Returns the DriveFsHost if it is enabled. // Returns the DriveFsHost if it is enabled.
drivefs::DriveFsHost* GetDriveFsHost() const; drivefs::DriveFsHost* GetDriveFsHost() const;
...@@ -208,6 +206,11 @@ class DriveIntegrationService : public KeyedService, ...@@ -208,6 +206,11 @@ class DriveIntegrationService : public KeyedService,
void MaybeRemountFileSystem(base::Optional<base::TimeDelta> remount_delay, void MaybeRemountFileSystem(base::Optional<base::TimeDelta> remount_delay,
bool failed_to_mount); bool failed_to_mount);
// Helper function for ClearCacheAndRemountFileSystem() that deletes the cache
// folder and remounts Drive.
void ClearCacheAndRemountFileSystemAfterUnmount(
base::OnceCallback<void(bool)> callback);
// Initializes the object. This function should be called before any // Initializes the object. This function should be called before any
// other functions. // other functions.
void Initialize(); void Initialize();
...@@ -243,6 +246,7 @@ class DriveIntegrationService : public KeyedService, ...@@ -243,6 +246,7 @@ class DriveIntegrationService : public KeyedService,
State state_; State state_;
bool enabled_; bool enabled_;
bool mount_failed_ = false; bool mount_failed_ = false;
bool in_clear_cache_ = false;
// Custom mount point name that can be injected for testing in constructor. // Custom mount point name that can be injected for testing in constructor.
std::string mount_point_name_; std::string mount_point_name_;
......
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
#include "chrome/browser/chromeos/drive/drive_integration_service.h" #include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/test/bind_test_util.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
...@@ -24,6 +28,34 @@ IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest, ...@@ -24,6 +28,34 @@ IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest,
browser()->profile())); browser()->profile()));
} }
IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest,
ClearCacheAndRemountFileSystem) {
base::ScopedAllowBlockingForTesting allow_blocking;
auto* drive_service =
DriveIntegrationServiceFactory::FindForProfile(browser()->profile());
base::FilePath cache_path = drive_service->GetDriveFsHost()->GetDataPath();
base::FilePath log_folder_path = drive_service->GetDriveFsLogPath().DirName();
ASSERT_TRUE(base::CreateDirectory(cache_path));
ASSERT_TRUE(base::CreateDirectory(log_folder_path));
base::FilePath cache_file;
base::FilePath log_file;
ASSERT_TRUE(base::CreateTemporaryFileInDir(cache_path, &cache_file));
ASSERT_TRUE(base::CreateTemporaryFileInDir(log_folder_path, &log_file));
base::RunLoop run_loop;
auto quit_closure = run_loop.QuitClosure();
drive_service->ClearCacheAndRemountFileSystem(
base::BindLambdaForTesting([=](bool success) {
EXPECT_TRUE(success);
EXPECT_FALSE(base::PathExists(cache_file));
EXPECT_TRUE(base::PathExists(log_file));
quit_closure.Run();
}));
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest, IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest,
DisableDrivePolicyTest) { DisableDrivePolicyTest) {
......
...@@ -555,8 +555,8 @@ class DriveInternalsWebUIHandler : public content::WebUIMessageHandler { ...@@ -555,8 +555,8 @@ class DriveInternalsWebUIHandler : public content::WebUIMessageHandler {
GetIntegrationService(); GetIntegrationService();
if (integration_service) { if (integration_service) {
integration_service->ClearCacheAndRemountFileSystem( integration_service->ClearCacheAndRemountFileSystem(
base::Bind(&DriveInternalsWebUIHandler::ResetFinished, base::BindOnce(&DriveInternalsWebUIHandler::ResetFinished,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
} }
......
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