Commit 3be2f5d4 authored by mtomasz@chromium.org's avatar mtomasz@chromium.org

[fsp] Add support for closing files.

This patch adds support for closing files. It is currently a no-op, and it is
not yet bound to fileapi.

TEST=chromium: *FileSystemProvider*CloseFile*
BUG=248427

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270116 0039d316-1c4b-4281-b951-d872f2087c98
parent 316c18ce
...@@ -169,4 +169,24 @@ bool FileSystemProviderInternalOpenFileRequestedErrorFunction::RunWhenValid() { ...@@ -169,4 +169,24 @@ bool FileSystemProviderInternalOpenFileRequestedErrorFunction::RunWhenValid() {
return true; return true;
} }
bool
FileSystemProviderInternalCloseFileRequestedSuccessFunction::RunWhenValid() {
using api::file_system_provider_internal::CloseFileRequestedSuccess::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
FulfillRequest(scoped_ptr<RequestValue>(new RequestValue()),
false /* has_more */);
return true;
}
bool FileSystemProviderInternalCloseFileRequestedErrorFunction::RunWhenValid() {
using api::file_system_provider_internal::CloseFileRequestedError::Params;
const scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params);
RejectRequest(ProviderErrorToFileError(params->error));
return true;
}
} // namespace extensions } // namespace extensions
...@@ -126,6 +126,30 @@ class FileSystemProviderInternalOpenFileRequestedErrorFunction ...@@ -126,6 +126,30 @@ class FileSystemProviderInternalOpenFileRequestedErrorFunction
virtual bool RunWhenValid() OVERRIDE; virtual bool RunWhenValid() OVERRIDE;
}; };
class FileSystemProviderInternalCloseFileRequestedSuccessFunction
: public FileSystemProviderInternalFunction {
public:
DECLARE_EXTENSION_FUNCTION(
"fileSystemProviderInternal.closeFileRequestedSuccess",
FILESYSTEMPROVIDERINTERNAL_CLOSEFILEREQUESTEDSUCCESS)
protected:
virtual ~FileSystemProviderInternalCloseFileRequestedSuccessFunction() {}
virtual bool RunWhenValid() OVERRIDE;
};
class FileSystemProviderInternalCloseFileRequestedErrorFunction
: public FileSystemProviderInternalFunction {
public:
DECLARE_EXTENSION_FUNCTION(
"fileSystemProviderInternal.closeFileRequestedError",
FILESYSTEMPROVIDERINTERNAL_CLOSEFILEREQUESTEDERROR)
protected:
virtual ~FileSystemProviderInternalCloseFileRequestedErrorFunction() {}
virtual bool RunWhenValid() OVERRIDE;
};
} // namespace extensions } // namespace extensions
#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_FILE_SYSTEM_PROVIDER_API_H_ #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_FILE_SYSTEM_PROVIDER_API_H_
...@@ -32,7 +32,7 @@ void AddDirectoryEntry(fileapi::AsyncFileUtil::EntryList* entry_list, ...@@ -32,7 +32,7 @@ void AddDirectoryEntry(fileapi::AsyncFileUtil::EntryList* entry_list,
FakeProvidedFileSystem::FakeProvidedFileSystem( FakeProvidedFileSystem::FakeProvidedFileSystem(
const ProvidedFileSystemInfo& file_system_info) const ProvidedFileSystemInfo& file_system_info)
: file_system_info_(file_system_info) { : file_system_info_(file_system_info), last_file_handle_(0) {
} }
FakeProvidedFileSystem::~FakeProvidedFileSystem() {} FakeProvidedFileSystem::~FakeProvidedFileSystem() {}
...@@ -118,17 +118,31 @@ void FakeProvidedFileSystem::ReadDirectory( ...@@ -118,17 +118,31 @@ void FakeProvidedFileSystem::ReadDirectory(
} }
} }
void FakeProvidedFileSystem::OpenFile( void FakeProvidedFileSystem::OpenFile(const base::FilePath& file_path,
const base::FilePath& file_path, OpenFileMode mode,
OpenFileMode mode, bool create,
bool create, const OpenFileCallback& callback) {
const fileapi::AsyncFileUtil::StatusCallback& callback) {
if (file_path.AsUTF8Unsafe() != "/hello.txt" || if (file_path.AsUTF8Unsafe() != "/hello.txt" ||
mode == OPEN_FILE_MODE_WRITE || create) { mode == OPEN_FILE_MODE_WRITE || create) {
callback.Run(base::File::FILE_ERROR_SECURITY); callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY);
return;
}
const int file_handle = ++last_file_handle_;
callback.Run(file_handle, base::File::FILE_OK);
}
void FakeProvidedFileSystem::CloseFile(
int file_handle,
const fileapi::AsyncFileUtil::StatusCallback& callback) {
const std::set<int>::iterator opened_file_it =
opened_files_.find(file_handle);
if (opened_file_it == opened_files_.end()) {
callback.Run(base::File::FILE_ERROR_NOT_FOUND);
return; return;
} }
opened_files_.erase(opened_file_it);
callback.Run(base::File::FILE_OK); callback.Run(base::File::FILE_OK);
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_ #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_
#define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_ #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FAKE_PROVIDED_FILE_SYSTEM_H_
#include <set>
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h" #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h" #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
...@@ -34,10 +36,12 @@ class FakeProvidedFileSystem : public ProvidedFileSystemInterface { ...@@ -34,10 +36,12 @@ class FakeProvidedFileSystem : public ProvidedFileSystemInterface {
virtual void ReadDirectory( virtual void ReadDirectory(
const base::FilePath& directory_path, const base::FilePath& directory_path,
const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) OVERRIDE; const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) OVERRIDE;
virtual void OpenFile( virtual void OpenFile(const base::FilePath& file_path,
const base::FilePath& file_path, OpenFileMode mode,
OpenFileMode mode, bool create,
bool create, const OpenFileCallback& callback) OVERRIDE;
virtual void CloseFile(
int file_handle,
const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE; const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const OVERRIDE; virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const OVERRIDE;
virtual RequestManager* GetRequestManager() OVERRIDE; virtual RequestManager* GetRequestManager() OVERRIDE;
...@@ -50,6 +54,8 @@ class FakeProvidedFileSystem : public ProvidedFileSystemInterface { ...@@ -50,6 +54,8 @@ class FakeProvidedFileSystem : public ProvidedFileSystemInterface {
private: private:
ProvidedFileSystemInfo file_system_info_; ProvidedFileSystemInfo file_system_info_;
std::set<int> opened_files_;
int last_file_handle_;
DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem); DISALLOW_COPY_AND_ASSIGN(FakeProvidedFileSystem);
}; };
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/file_system_provider/operations/close_file.h"
#include <string>
#include "chrome/common/extensions/api/file_system_provider.h"
#include "chrome/common/extensions/api/file_system_provider_internal.h"
namespace chromeos {
namespace file_system_provider {
namespace operations {
CloseFile::CloseFile(extensions::EventRouter* event_router,
const ProvidedFileSystemInfo& file_system_info,
int open_request_id,
const fileapi::AsyncFileUtil::StatusCallback& callback)
: Operation(event_router, file_system_info),
open_request_id_(open_request_id),
callback_(callback) {
}
CloseFile::~CloseFile() {
}
bool CloseFile::Execute(int request_id) {
scoped_ptr<base::ListValue> values(new base::ListValue);
values->AppendInteger(open_request_id_);
return SendEvent(
request_id,
extensions::api::file_system_provider::OnCloseFileRequested::kEventName,
values.Pass());
}
void CloseFile::OnSuccess(int /* request_id */,
scoped_ptr<RequestValue> result,
bool has_next) {
callback_.Run(base::File::FILE_OK);
}
void CloseFile::OnError(int /* request_id */, base::File::Error error) {
callback_.Run(error);
}
} // namespace operations
} // namespace file_system_provider
} // namespace chromeos
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_OPERATIONS_CLOSE_FILE_H_
#define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_OPERATIONS_CLOSE_FILE_H_
#include "base/files/file.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/file_system_provider/operations/operation.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
#include "chrome/browser/chromeos/file_system_provider/request_value.h"
#include "webkit/browser/fileapi/async_file_util.h"
namespace base {
class FilePath;
} // namespace base
namespace extensions {
class EventRouter;
} // namespace extensions
namespace chromeos {
namespace file_system_provider {
namespace operations {
// Opens a file for either read or write, with optionally creating the file
// first. Note, that this is part of fileapi::CreateOrOpen file, but it does
// not download the file locally. Created per request.
class CloseFile : public Operation {
public:
CloseFile(extensions::EventRouter* event_router,
const ProvidedFileSystemInfo& file_system_info,
int open_request_id,
const fileapi::AsyncFileUtil::StatusCallback& callback);
virtual ~CloseFile();
// Operation overrides.
virtual bool Execute(int request_id) OVERRIDE;
virtual void OnSuccess(int request_id,
scoped_ptr<RequestValue> result,
bool has_next) OVERRIDE;
virtual void OnError(int request_id, base::File::Error error) OVERRIDE;
private:
int open_request_id_;
const fileapi::AsyncFileUtil::StatusCallback callback_;
DISALLOW_COPY_AND_ASSIGN(CloseFile);
};
} // namespace operations
} // namespace file_system_provider
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_OPERATIONS_CLOSE_FILE_H_
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "chrome/browser/chromeos/file_system_provider/operations/close_file.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
#include "chrome/common/extensions/api/file_system_provider.h"
#include "chrome/common/extensions/api/file_system_provider_internal.h"
#include "extensions/browser/event_router.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/browser/fileapi/async_file_util.h"
namespace chromeos {
namespace file_system_provider {
namespace operations {
namespace {
const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
const int kFileSystemId = 1;
const int kRequestId = 2;
const int kOpenRequestId = 3;
// Fake event dispatcher implementation with extra logging capability. Acts as
// a providing extension end-point.
class LoggingDispatchEventImpl {
public:
explicit LoggingDispatchEventImpl(bool dispatch_reply)
: dispatch_reply_(dispatch_reply) {}
virtual ~LoggingDispatchEventImpl() {}
bool OnDispatchEventImpl(scoped_ptr<extensions::Event> event) {
events_.push_back(event->DeepCopy());
return dispatch_reply_;
}
ScopedVector<extensions::Event>& events() { return events_; }
private:
ScopedVector<extensions::Event> events_;
bool dispatch_reply_;
DISALLOW_COPY_AND_ASSIGN(LoggingDispatchEventImpl);
};
// Callback invocation logger. Acts as a fileapi end-point.
class CallbackLogger {
public:
CallbackLogger() : weak_ptr_factory_(this) {}
virtual ~CallbackLogger() {}
void OnCloseFile(base::File::Error result) { events_.push_back(result); }
std::vector<base::File::Error>& events() { return events_; }
base::WeakPtr<CallbackLogger> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
std::vector<base::File::Error> events_;
base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
};
} // namespace
class FileSystemProviderOperationsCloseFileTest : public testing::Test {
protected:
FileSystemProviderOperationsCloseFileTest() {}
virtual ~FileSystemProviderOperationsCloseFileTest() {}
virtual void SetUp() OVERRIDE {
file_system_info_ =
ProvidedFileSystemInfo(kExtensionId,
kFileSystemId,
"" /* file_system_name */,
base::FilePath() /* mount_path */);
}
ProvidedFileSystemInfo file_system_info_;
};
TEST_F(FileSystemProviderOperationsCloseFileTest, Execute) {
LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
CallbackLogger callback_logger;
CloseFile close_file(
NULL,
file_system_info_,
kOpenRequestId,
base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr()));
close_file.SetDispatchEventImplForTesting(
base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl,
base::Unretained(&dispatcher)));
EXPECT_TRUE(close_file.Execute(kRequestId));
ASSERT_EQ(1u, dispatcher.events().size());
extensions::Event* event = dispatcher.events()[0];
EXPECT_EQ(
extensions::api::file_system_provider::OnCloseFileRequested::kEventName,
event->event_name);
base::ListValue* event_args = event->event_args.get();
ASSERT_EQ(3u, event_args->GetSize());
int event_file_system_id = -1;
EXPECT_TRUE(event_args->GetInteger(0, &event_file_system_id));
EXPECT_EQ(kFileSystemId, event_file_system_id);
int event_request_id = -1;
EXPECT_TRUE(event_args->GetInteger(1, &event_request_id));
EXPECT_EQ(kRequestId, event_request_id);
int event_open_request_id = -1;
EXPECT_TRUE(event_args->GetInteger(2, &event_open_request_id));
EXPECT_EQ(kOpenRequestId, event_open_request_id);
}
TEST_F(FileSystemProviderOperationsCloseFileTest, Execute_NoListener) {
LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
CallbackLogger callback_logger;
CloseFile close_file(
NULL,
file_system_info_,
kOpenRequestId,
base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr()));
close_file.SetDispatchEventImplForTesting(
base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl,
base::Unretained(&dispatcher)));
EXPECT_FALSE(close_file.Execute(kRequestId));
}
TEST_F(FileSystemProviderOperationsCloseFileTest, OnSuccess) {
using extensions::api::file_system_provider::EntryMetadata;
using extensions::api::file_system_provider_internal::
CloseFileRequestedSuccess::Params;
LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
CallbackLogger callback_logger;
CloseFile close_file(
NULL,
file_system_info_,
kOpenRequestId,
base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr()));
close_file.SetDispatchEventImplForTesting(
base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl,
base::Unretained(&dispatcher)));
EXPECT_TRUE(close_file.Execute(kRequestId));
close_file.OnSuccess(kRequestId,
scoped_ptr<RequestValue>(new RequestValue()),
false /* has_next */);
ASSERT_EQ(1u, callback_logger.events().size());
EXPECT_EQ(base::File::FILE_OK, callback_logger.events()[0]);
}
TEST_F(FileSystemProviderOperationsCloseFileTest, OnError) {
using extensions::api::file_system_provider::EntryMetadata;
using extensions::api::file_system_provider_internal::
CloseFileRequestedError::Params;
LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
CallbackLogger callback_logger;
CloseFile close_file(
NULL,
file_system_info_,
kOpenRequestId,
base::Bind(&CallbackLogger::OnCloseFile, callback_logger.GetWeakPtr()));
close_file.SetDispatchEventImplForTesting(
base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl,
base::Unretained(&dispatcher)));
EXPECT_TRUE(close_file.Execute(kRequestId));
close_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED);
ASSERT_EQ(1u, callback_logger.events().size());
EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED,
callback_logger.events()[0]);
}
} // namespace operations
} // namespace file_system_provider
} // namespace chromeos
...@@ -13,12 +13,13 @@ namespace chromeos { ...@@ -13,12 +13,13 @@ namespace chromeos {
namespace file_system_provider { namespace file_system_provider {
namespace operations { namespace operations {
OpenFile::OpenFile(extensions::EventRouter* event_router, OpenFile::OpenFile(
const ProvidedFileSystemInfo& file_system_info, extensions::EventRouter* event_router,
const base::FilePath& file_path, const ProvidedFileSystemInfo& file_system_info,
ProvidedFileSystemInterface::OpenFileMode mode, const base::FilePath& file_path,
bool create, ProvidedFileSystemInterface::OpenFileMode mode,
const fileapi::AsyncFileUtil::StatusCallback& callback) bool create,
const ProvidedFileSystemInterface::OpenFileCallback& callback)
: Operation(event_router, file_system_info), : Operation(event_router, file_system_info),
file_path_(file_path), file_path_(file_path),
mode_(mode), mode_(mode),
...@@ -52,14 +53,15 @@ bool OpenFile::Execute(int request_id) { ...@@ -52,14 +53,15 @@ bool OpenFile::Execute(int request_id) {
values.Pass()); values.Pass());
} }
void OpenFile::OnSuccess(int /* request_id */, void OpenFile::OnSuccess(int request_id,
scoped_ptr<RequestValue> result, scoped_ptr<RequestValue> result,
bool has_next) { bool has_next) {
callback_.Run(base::File::FILE_OK); // File handle is the same as request id of the OpenFile operation.
callback_.Run(request_id, base::File::FILE_OK);
} }
void OpenFile::OnError(int /* request_id */, base::File::Error error) { void OpenFile::OnError(int /* request_id */, base::File::Error error) {
callback_.Run(error); callback_.Run(0 /* file_handle */, error);
} }
} // namespace operations } // namespace operations
......
...@@ -35,7 +35,7 @@ class OpenFile : public Operation { ...@@ -35,7 +35,7 @@ class OpenFile : public Operation {
const base::FilePath& file_path, const base::FilePath& file_path,
ProvidedFileSystemInterface::OpenFileMode mode, ProvidedFileSystemInterface::OpenFileMode mode,
bool create, bool create,
const fileapi::AsyncFileUtil::StatusCallback& callback); const ProvidedFileSystemInterface::OpenFileCallback& callback);
virtual ~OpenFile(); virtual ~OpenFile();
// Operation overrides. // Operation overrides.
...@@ -49,7 +49,7 @@ class OpenFile : public Operation { ...@@ -49,7 +49,7 @@ class OpenFile : public Operation {
base::FilePath file_path_; base::FilePath file_path_;
ProvidedFileSystemInterface::OpenFileMode mode_; ProvidedFileSystemInterface::OpenFileMode mode_;
bool create_; bool create_;
const fileapi::AsyncFileUtil::StatusCallback callback_; const ProvidedFileSystemInterface::OpenFileCallback callback_;
DISALLOW_COPY_AND_ASSIGN(OpenFile); DISALLOW_COPY_AND_ASSIGN(OpenFile);
}; };
......
...@@ -52,19 +52,38 @@ class LoggingDispatchEventImpl { ...@@ -52,19 +52,38 @@ class LoggingDispatchEventImpl {
// Callback invocation logger. Acts as a fileapi end-point. // Callback invocation logger. Acts as a fileapi end-point.
class CallbackLogger { class CallbackLogger {
public: public:
class Event {
public:
Event(int file_handle, base::File::Error result)
: file_handle_(file_handle), result_(result) {}
virtual ~Event() {}
int file_handle() { return file_handle_; }
base::File::Error result() { return result_; }
private:
int file_handle_;
base::File::Error result_;
DISALLOW_COPY_AND_ASSIGN(Event);
};
CallbackLogger() : weak_ptr_factory_(this) {} CallbackLogger() : weak_ptr_factory_(this) {}
virtual ~CallbackLogger() {} virtual ~CallbackLogger() {}
void OnOpenFile(base::File::Error result) { events_.push_back(result); } void OnOpenFile(int file_handle, base::File::Error result) {
events_.push_back(new Event(file_handle, result));
}
std::vector<base::File::Error>& events() { return events_; } ScopedVector<Event>& events() { return events_; }
base::WeakPtr<CallbackLogger> GetWeakPtr() { base::WeakPtr<CallbackLogger> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr(); return weak_ptr_factory_.GetWeakPtr();
} }
private: private:
std::vector<base::File::Error> events_; ScopedVector<Event> events_;
bool dispatch_reply_;
base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_; base::WeakPtrFactory<CallbackLogger> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(CallbackLogger); DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
...@@ -180,7 +199,9 @@ TEST_F(FileSystemProviderOperationsOpenFileTest, OnSuccess) { ...@@ -180,7 +199,9 @@ TEST_F(FileSystemProviderOperationsOpenFileTest, OnSuccess) {
scoped_ptr<RequestValue>(new RequestValue()), scoped_ptr<RequestValue>(new RequestValue()),
false /* has_next */); false /* has_next */);
ASSERT_EQ(1u, callback_logger.events().size()); ASSERT_EQ(1u, callback_logger.events().size());
EXPECT_EQ(base::File::FILE_OK, callback_logger.events()[0]); CallbackLogger::Event* event = callback_logger.events()[0];
EXPECT_EQ(base::File::FILE_OK, event->result());
EXPECT_LT(0, event->file_handle());
} }
TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) { TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) {
...@@ -206,8 +227,9 @@ TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) { ...@@ -206,8 +227,9 @@ TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) {
open_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED); open_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED);
ASSERT_EQ(1u, callback_logger.events().size()); ASSERT_EQ(1u, callback_logger.events().size());
EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, CallbackLogger::Event* event = callback_logger.events()[0];
callback_logger.events()[0]); EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
ASSERT_EQ(0, event->file_handle());
} }
} // namespace operations } // namespace operations
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h"
#include "base/files/file.h" #include "base/files/file.h"
#include "chrome/browser/chromeos/file_system_provider/operations/close_file.h"
#include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h" #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h"
#include "chrome/browser/chromeos/file_system_provider/operations/open_file.h" #include "chrome/browser/chromeos/file_system_provider/operations/open_file.h"
#include "chrome/browser/chromeos/file_system_provider/operations/read_directory.h" #include "chrome/browser/chromeos/file_system_provider/operations/read_directory.h"
...@@ -59,15 +60,14 @@ void ProvidedFileSystem::ReadDirectory( ...@@ -59,15 +60,14 @@ void ProvidedFileSystem::ReadDirectory(
} }
} }
void ProvidedFileSystem::OpenFile( void ProvidedFileSystem::OpenFile(const base::FilePath& file_path,
const base::FilePath& file_path, OpenFileMode mode,
OpenFileMode mode, bool create,
bool create, const OpenFileCallback& callback) {
const fileapi::AsyncFileUtil::StatusCallback& callback) {
// Writing is not supported. Note, that this includes a situation, when a file // Writing is not supported. Note, that this includes a situation, when a file
// exists, but |create| is set to true. // exists, but |create| is set to true.
if (mode == OPEN_FILE_MODE_WRITE || create) { if (mode == OPEN_FILE_MODE_WRITE || create) {
callback.Run(base::File::FILE_ERROR_SECURITY); callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY);
return; return;
} }
...@@ -79,6 +79,17 @@ void ProvidedFileSystem::OpenFile( ...@@ -79,6 +79,17 @@ void ProvidedFileSystem::OpenFile(
mode, mode,
create, create,
callback)))) { callback)))) {
callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY);
}
}
void ProvidedFileSystem::CloseFile(
int file_handle,
const fileapi::AsyncFileUtil::StatusCallback& callback) {
if (!request_manager_.CreateRequest(
scoped_ptr<RequestManager::HandlerInterface>(
new operations::CloseFile(
event_router_, file_system_info_, file_handle, callback)))) {
callback.Run(base::File::FILE_ERROR_SECURITY); callback.Run(base::File::FILE_ERROR_SECURITY);
} }
} }
......
...@@ -38,10 +38,12 @@ class ProvidedFileSystem : public ProvidedFileSystemInterface { ...@@ -38,10 +38,12 @@ class ProvidedFileSystem : public ProvidedFileSystemInterface {
virtual void ReadDirectory( virtual void ReadDirectory(
const base::FilePath& directory_path, const base::FilePath& directory_path,
const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) OVERRIDE; const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) OVERRIDE;
virtual void OpenFile( virtual void OpenFile(const base::FilePath& file_path,
const base::FilePath& file_path, OpenFileMode mode,
OpenFileMode mode, bool create,
bool create, const OpenFileCallback& callback) OVERRIDE;
virtual void CloseFile(
int file_handle,
const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE; const fileapi::AsyncFileUtil::StatusCallback& callback) OVERRIDE;
virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const OVERRIDE; virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const OVERRIDE;
virtual RequestManager* GetRequestManager() OVERRIDE; virtual RequestManager* GetRequestManager() OVERRIDE;
......
...@@ -24,6 +24,9 @@ class RequestManager; ...@@ -24,6 +24,9 @@ class RequestManager;
// TODO(mtomasz): Add more methods once implemented. // TODO(mtomasz): Add more methods once implemented.
class ProvidedFileSystemInterface { class ProvidedFileSystemInterface {
public: public:
typedef base::Callback<void(int file_handle, base::File::Error result)>
OpenFileCallback;
// Mode of opening a file. Used by OpenFile(). // Mode of opening a file. Used by OpenFile().
enum OpenFileMode { OPEN_FILE_MODE_READ, OPEN_FILE_MODE_WRITE }; enum OpenFileMode { OPEN_FILE_MODE_READ, OPEN_FILE_MODE_WRITE };
...@@ -39,6 +42,7 @@ class ProvidedFileSystemInterface { ...@@ -39,6 +42,7 @@ class ProvidedFileSystemInterface {
virtual void GetMetadata( virtual void GetMetadata(
const base::FilePath& entry_path, const base::FilePath& entry_path,
const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) = 0; const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) = 0;
// Requests enumerating entries from the passed |directory_path|. The callback // Requests enumerating entries from the passed |directory_path|. The callback
// can be called multiple times until either an error is returned or the // can be called multiple times until either an error is returned or the
// has_more field is set to false. // has_more field is set to false.
...@@ -47,11 +51,16 @@ class ProvidedFileSystemInterface { ...@@ -47,11 +51,16 @@ class ProvidedFileSystemInterface {
const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) = 0; const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) = 0;
// Requests opening a file at |file_path|. If |create| is set to true, it will // Requests opening a file at |file_path|. If |create| is set to true, it will
// create a file and return succes in case it didn't exist. // create a file and return success in case it didn't exist.
virtual void OpenFile( virtual void OpenFile(const base::FilePath& file_path,
const base::FilePath& file_path, OpenFileMode mode,
OpenFileMode mode, bool create,
bool create, const OpenFileCallback& callback) = 0;
// Requests closing a file, previously opened with OpenFile() as a file with
// |file_handle|. For either succes or error |callback| must be called.
virtual void CloseFile(
int file_handle,
const fileapi::AsyncFileUtil::StatusCallback& callback) = 0; const fileapi::AsyncFileUtil::StatusCallback& callback) = 0;
// Returns a provided file system info for this file system. // Returns a provided file system info for this file system.
......
...@@ -380,6 +380,8 @@ ...@@ -380,6 +380,8 @@
'browser/chromeos/file_system_provider/mount_path_util.cc', 'browser/chromeos/file_system_provider/mount_path_util.cc',
'browser/chromeos/file_system_provider/mount_path_util.h', 'browser/chromeos/file_system_provider/mount_path_util.h',
'browser/chromeos/file_system_provider/observer.h', 'browser/chromeos/file_system_provider/observer.h',
'browser/chromeos/file_system_provider/operations/close_file.cc',
'browser/chromeos/file_system_provider/operations/close_file.h',
'browser/chromeos/file_system_provider/operations/get_metadata.cc', 'browser/chromeos/file_system_provider/operations/get_metadata.cc',
'browser/chromeos/file_system_provider/operations/get_metadata.h', 'browser/chromeos/file_system_provider/operations/get_metadata.h',
'browser/chromeos/file_system_provider/operations/open_file.cc', 'browser/chromeos/file_system_provider/operations/open_file.cc',
......
...@@ -711,6 +711,7 @@ ...@@ -711,6 +711,7 @@
'browser/chromeos/file_system_provider/fake_provided_file_system.h', 'browser/chromeos/file_system_provider/fake_provided_file_system.h',
'browser/chromeos/file_system_provider/fileapi/provider_async_file_util_unittest.cc', 'browser/chromeos/file_system_provider/fileapi/provider_async_file_util_unittest.cc',
'browser/chromeos/file_system_provider/mount_path_util_unittest.cc', 'browser/chromeos/file_system_provider/mount_path_util_unittest.cc',
'browser/chromeos/file_system_provider/operations/close_file_unittest.cc',
'browser/chromeos/file_system_provider/operations/get_metadata_unittest.cc', 'browser/chromeos/file_system_provider/operations/get_metadata_unittest.cc',
'browser/chromeos/file_system_provider/operations/open_file_unittest.cc', 'browser/chromeos/file_system_provider/operations/open_file_unittest.cc',
'browser/chromeos/file_system_provider/operations/read_directory_unittest.cc', 'browser/chromeos/file_system_provider/operations/read_directory_unittest.cc',
......
...@@ -144,6 +144,14 @@ namespace fileSystemProvider { ...@@ -144,6 +144,14 @@ namespace fileSystemProvider {
boolean create, boolean create,
ProviderSuccessCallback successCallback, ProviderSuccessCallback successCallback,
ProviderErrorCallback errorCallback); ProviderErrorCallback errorCallback);
// Raised when opening a file previously opened with <code>openRequestId
// </code> is requested to be closed.
[maxListeners=1] static void onCloseFileRequested(
long fileSystemId,
long openRequestId,
ProviderSuccessCallback successCallback,
ProviderErrorCallback errorCallback);
}; };
}; };
...@@ -61,6 +61,19 @@ namespace fileSystemProviderInternal { ...@@ -61,6 +61,19 @@ namespace fileSystemProviderInternal {
long fileSystemId, long fileSystemId,
long requestId, long requestId,
fileSystemProvider.ProviderError error); fileSystemProvider.ProviderError error);
// Internal. Success callback of the <code>onCloseFileRequested</code>
// event. Must be called, when closing succeeds.
static void closeFileRequestedSuccess(
long fileSystemId,
long requestId);
// Internal. Error callback of the <code>onCloseFileRequested</code> event.
// Must be called when closing fails.
static void closeFileRequestedError(
long fileSystemId,
long requestId,
fileSystemProvider.ProviderError error);
}; };
}; };
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