Commit 42b6c357 authored by kinaba@chromium.org's avatar kinaba@chromium.org

Make chrome.fileSystem.chooseEntry work on Google Drive directories.

BUG=323097
TEST=added auto tests, and manually tested by samples apps for the API

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245453 0039d316-1c4b-4281-b951-d872f2087c98
parent 334d0b9f
...@@ -109,6 +109,15 @@ DriveIntegrationService* GetIntegrationServiceByProfile(Profile* profile) { ...@@ -109,6 +109,15 @@ DriveIntegrationService* GetIntegrationServiceByProfile(Profile* profile) {
return service; return service;
} }
void CheckDirectoryExistsAfterGetResourceEntry(
const FileOperationCallback& callback,
FileError error,
scoped_ptr<ResourceEntry> entry) {
if (error == FILE_ERROR_OK && !entry->file_info().is_directory())
error = FILE_ERROR_NOT_A_DIRECTORY;
callback.Run(error);
}
} // namespace } // namespace
const base::FilePath& GetDriveGrandRootPath() { const base::FilePath& GetDriveGrandRootPath() {
...@@ -343,6 +352,20 @@ void EnsureDirectoryExists(Profile* profile, ...@@ -343,6 +352,20 @@ void EnsureDirectoryExists(Profile* profile,
} }
} }
void CheckDirectoryExists(Profile* profile,
const base::FilePath& directory,
const FileOperationCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
FileSystemInterface* file_system = GetFileSystemByProfile(profile);
DCHECK(file_system);
file_system->GetResourceEntry(
ExtractDrivePath(directory),
base::Bind(&CheckDirectoryExistsAfterGetResourceEntry, callback));
}
void EmptyFileOperationCallback(FileError error) { void EmptyFileOperationCallback(FileError error) {
} }
......
...@@ -153,6 +153,12 @@ void PrepareWritableFileAndRun(Profile* profile, ...@@ -153,6 +153,12 @@ void PrepareWritableFileAndRun(Profile* profile,
const base::FilePath& path, const base::FilePath& path,
const PrepareWritableFileCallback& callback); const PrepareWritableFileCallback& callback);
// Checks whether a directory exists at the given Drive path |directory|.
// Must be called from UI thread. The result will be called back to |callback|.
void CheckDirectoryExists(Profile* profile,
const base::FilePath& directory,
const FileOperationCallback& callback);
// Ensures the existence of |directory| of '/special/drive/foo'. This will // Ensures the existence of |directory| of '/special/drive/foo'. This will
// create |directory| and its ancestors if they don't exist. |callback| is // create |directory| and its ancestors if they don't exist. |callback| is
// invoked after making sure that |directory| exists. |callback| should // invoked after making sure that |directory| exists. |callback| should
......
...@@ -781,8 +781,7 @@ CancelCallback FakeDriveService::CopyResource( ...@@ -781,8 +781,7 @@ CancelCallback FakeDriveService::CopyResource(
resource_id == current_resource_id) { resource_id == current_resource_id) {
// Make a copy and set the new resource ID and the new title. // Make a copy and set the new resource ID and the new title.
scoped_ptr<base::DictionaryValue> copied_entry(entry->DeepCopy()); scoped_ptr<base::DictionaryValue> copied_entry(entry->DeepCopy());
copied_entry->SetString("gd$resourceId.$t", copied_entry->SetString("gd$resourceId.$t", GetNewResourceId());
resource_id + "_copied");
copied_entry->SetString("title.$t", new_title); copied_entry->SetString("title.$t", new_title);
// Reset parent directory. // Reset parent directory.
......
...@@ -1118,7 +1118,7 @@ TEST_F(FakeDriveServiceTest, CopyResource) { ...@@ -1118,7 +1118,7 @@ TEST_F(FakeDriveServiceTest, CopyResource) {
EXPECT_EQ(HTTP_SUCCESS, error); EXPECT_EQ(HTTP_SUCCESS, error);
ASSERT_TRUE(resource_entry); ASSERT_TRUE(resource_entry);
// The copied entry should have the new resource ID and the title. // The copied entry should have the new resource ID and the title.
EXPECT_EQ(kResourceId + "_copied", resource_entry->resource_id()); EXPECT_NE(kResourceId, resource_entry->resource_id());
EXPECT_EQ("new title", resource_entry->title()); EXPECT_EQ("new title", resource_entry->title());
EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate), EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate),
resource_entry->updated_time()); resource_entry->updated_time());
...@@ -1168,7 +1168,7 @@ TEST_F(FakeDriveServiceTest, CopyResource_EmptyParentResourceId) { ...@@ -1168,7 +1168,7 @@ TEST_F(FakeDriveServiceTest, CopyResource_EmptyParentResourceId) {
EXPECT_EQ(HTTP_SUCCESS, error); EXPECT_EQ(HTTP_SUCCESS, error);
ASSERT_TRUE(resource_entry); ASSERT_TRUE(resource_entry);
// The copied entry should have the new resource ID and the title. // The copied entry should have the new resource ID and the title.
EXPECT_EQ(kResourceId + "_copied", resource_entry->resource_id()); EXPECT_NE(kResourceId, resource_entry->resource_id());
EXPECT_EQ("new title", resource_entry->title()); EXPECT_EQ("new title", resource_entry->title());
EXPECT_TRUE(HasParent(kResourceId, fake_service_.GetRootResourceId())); EXPECT_TRUE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
// Should be incremented as a new hosted document was created. // Should be incremented as a new hosted document was created.
...@@ -1222,7 +1222,7 @@ TEST_F(FakeDriveServiceTest, UpdateResource) { ...@@ -1222,7 +1222,7 @@ TEST_F(FakeDriveServiceTest, UpdateResource) {
EXPECT_EQ(HTTP_SUCCESS, error); EXPECT_EQ(HTTP_SUCCESS, error);
ASSERT_TRUE(resource_entry); ASSERT_TRUE(resource_entry);
// The copied entry should have the new resource ID and the title. // The updated entry should have the new title.
EXPECT_EQ(kResourceId, resource_entry->resource_id()); EXPECT_EQ(kResourceId, resource_entry->resource_id());
EXPECT_EQ("new title", resource_entry->title()); EXPECT_EQ("new title", resource_entry->title());
EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate), EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate),
...@@ -1280,7 +1280,7 @@ TEST_F(FakeDriveServiceTest, UpdateResource_EmptyParentResourceId) { ...@@ -1280,7 +1280,7 @@ TEST_F(FakeDriveServiceTest, UpdateResource_EmptyParentResourceId) {
EXPECT_EQ(HTTP_SUCCESS, error); EXPECT_EQ(HTTP_SUCCESS, error);
ASSERT_TRUE(resource_entry); ASSERT_TRUE(resource_entry);
// The copied entry should have the new resource ID and the title. // The updated entry should have the new title.
EXPECT_EQ(kResourceId, resource_entry->resource_id()); EXPECT_EQ(kResourceId, resource_entry->resource_id());
EXPECT_EQ("new title", resource_entry->title()); EXPECT_EQ("new title", resource_entry->title());
EXPECT_TRUE(HasParent(kResourceId, "fake_root")); EXPECT_TRUE(HasParent(kResourceId, "fake_root"));
......
...@@ -124,6 +124,8 @@ class WritableFileChecker ...@@ -124,6 +124,8 @@ class WritableFileChecker
void CheckRemoteWritableFile(const base::FilePath& remote_path, void CheckRemoteWritableFile(const base::FilePath& remote_path,
drive::FileError error, drive::FileError error,
const base::FilePath& local_path); const base::FilePath& local_path);
void RemoteCheckDone(const base::FilePath& remote_path,
drive::FileError error);
#endif #endif
const std::vector<base::FilePath> paths_; const std::vector<base::FilePath> paths_;
...@@ -156,10 +158,18 @@ void WritableFileChecker::Check() { ...@@ -156,10 +158,18 @@ void WritableFileChecker::Check() {
it != paths_.end(); it != paths_.end();
++it) { ++it) {
DCHECK(drive::util::IsUnderDriveMountPoint(*it)); DCHECK(drive::util::IsUnderDriveMountPoint(*it));
drive::util::PrepareWritableFileAndRun( if (is_directory_) {
profile_, drive::util::CheckDirectoryExists(
*it, profile_,
base::Bind(&WritableFileChecker::CheckRemoteWritableFile, this, *it)); *it,
base::Bind(&WritableFileChecker::RemoteCheckDone, this, *it));
} else {
drive::util::PrepareWritableFileAndRun(
profile_,
*it,
base::Bind(&WritableFileChecker::CheckRemoteWritableFile, this,
*it));
}
} }
return; return;
} }
...@@ -215,6 +225,12 @@ void WritableFileChecker::CheckRemoteWritableFile( ...@@ -215,6 +225,12 @@ void WritableFileChecker::CheckRemoteWritableFile(
const base::FilePath& remote_path, const base::FilePath& remote_path,
drive::FileError error, drive::FileError error,
const base::FilePath& /* local_path */) { const base::FilePath& /* local_path */) {
RemoteCheckDone(remote_path, error);
}
void WritableFileChecker::RemoteCheckDone(
const base::FilePath& remote_path,
drive::FileError error) {
if (error == drive::FILE_ERROR_OK) { if (error == drive::FILE_ERROR_OK) {
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, content::BrowserThread::UI,
......
...@@ -49,6 +49,10 @@ ...@@ -49,6 +49,10 @@
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#endif #endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/drive/file_system_util.h"
#endif
using apps::SavedFileEntry; using apps::SavedFileEntry;
using apps::SavedFilesService; using apps::SavedFilesService;
using apps::ShellWindow; using apps::ShellWindow;
...@@ -698,7 +702,13 @@ void FileSystemChooseEntryFunction::ConfirmDirectoryAccessOnFileThread( ...@@ -698,7 +702,13 @@ void FileSystemChooseEntryFunction::ConfirmDirectoryAccessOnFileThread(
const std::vector<base::FilePath>& paths, const std::vector<base::FilePath>& paths,
content::WebContents* web_contents) { content::WebContents* web_contents) {
DCHECK_EQ(paths.size(), 1u); DCHECK_EQ(paths.size(), 1u);
#if defined(OS_CHROMEOS)
const base::FilePath path =
drive::util::IsUnderDriveMountPoint(paths[0]) ? paths[0] :
base::MakeAbsoluteFilePath(paths[0]);
#else
const base::FilePath path = base::MakeAbsoluteFilePath(paths[0]); const base::FilePath path = base::MakeAbsoluteFilePath(paths[0]);
#endif
if (path.empty()) { if (path.empty()) {
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, content::BrowserThread::UI,
......
...@@ -77,6 +77,9 @@ class FileSystemApiTestForDrive : public PlatformAppBrowserTest { ...@@ -77,6 +77,9 @@ class FileSystemApiTestForDrive : public PlatformAppBrowserTest {
void SetUpTestFileHierarchy() { void SetUpTestFileHierarchy() {
const std::string root = fake_drive_service_->GetRootResourceId(); const std::string root = fake_drive_service_->GetRootResourceId();
ASSERT_TRUE(AddTestFile("open_existing.txt", "Can you see me?", root)); ASSERT_TRUE(AddTestFile("open_existing.txt", "Can you see me?", root));
const std::string subdir = AddTestDirectory("subdir", root);
ASSERT_FALSE(subdir.empty());
ASSERT_TRUE(AddTestFile("open_existing.txt", "Can you see me?", subdir));
} }
bool AddTestFile(const std::string& title, bool AddTestFile(const std::string& title,
...@@ -92,6 +95,19 @@ class FileSystemApiTestForDrive : public PlatformAppBrowserTest { ...@@ -92,6 +95,19 @@ class FileSystemApiTestForDrive : public PlatformAppBrowserTest {
return error == google_apis::HTTP_CREATED && resource_entry; return error == google_apis::HTTP_CREATED && resource_entry;
} }
std::string AddTestDirectory(const std::string& title,
const std::string& parent_id) {
scoped_ptr<google_apis::ResourceEntry> resource_entry;
google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
fake_drive_service_->AddNewDirectory(
parent_id, title,
google_apis::test_util::CreateCopyResultCallback(&error,
&resource_entry));
content::RunAllPendingInMessageLoop();
return error == google_apis::HTTP_CREATED && resource_entry ?
resource_entry->resource_id() : "";
}
base::ScopedTempDir test_cache_root_; base::ScopedTempDir test_cache_root_;
drive::FakeDriveService* fake_drive_service_; drive::FakeDriveService* fake_drive_service_;
drive::DriveIntegrationService* integration_service_; drive::DriveIntegrationService* integration_service_;
...@@ -121,4 +137,47 @@ IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive, ...@@ -121,4 +137,47 @@ IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
"api_test/file_system/open_existing_with_write")) << message_; "api_test/file_system/open_existing_with_write")) << message_;
} }
IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
FileSystemApiOpenDirectoryTest) {
base::FilePath test_directory =
drive::util::GetDriveMountPointPath().AppendASCII("root/subdir");
FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
&test_directory);
ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory"))
<< message_;
}
IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
FileSystemApiOpenDirectoryWithWriteTest) {
base::FilePath test_directory =
drive::util::GetDriveMountPointPath().AppendASCII("root/subdir");
FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
&test_directory);
ASSERT_TRUE(
RunPlatformAppTest("api_test/file_system/open_directory_with_write"))
<< message_;
}
IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
FileSystemApiOpenDirectoryWithoutPermissionTest) {
base::FilePath test_directory =
drive::util::GetDriveMountPointPath().AppendASCII("root/subdir");
FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
&test_directory);
ASSERT_TRUE(RunPlatformAppTest(
"api_test/file_system/open_directory_without_permission"))
<< message_;
}
IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
FileSystemApiOpenDirectoryWithOnlyWritePermissionTest) {
base::FilePath test_directory =
drive::util::GetDriveMountPointPath().AppendASCII("root/subdir");
FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
&test_directory);
ASSERT_TRUE(RunPlatformAppTest(
"api_test/file_system/open_directory_with_only_write"))
<< message_;
}
} // namespace extensions } // namespace extensions
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