2010-10-03 22:35:06 +02:00
|
|
|
// Copyright (c) 2010 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 "browser_file_system.h"
|
2010-10-15 20:37:25 +02:00
|
|
|
#include "browser_file_writer.h"
|
2010-10-03 22:35:06 +02:00
|
|
|
|
|
|
|
#include "base/file_path.h"
|
|
|
|
#include "base/message_loop_proxy.h"
|
|
|
|
#include "base/time.h"
|
|
|
|
#include "third_party/WebKit/WebKit/chromium/public/WebFileInfo.h"
|
|
|
|
#include "third_party/WebKit/WebKit/chromium/public/WebFileSystemEntry.h"
|
|
|
|
#include "third_party/WebKit/WebKit/chromium/public/WebVector.h"
|
|
|
|
#include "webkit/fileapi/file_system_callback_dispatcher.h"
|
|
|
|
#include "webkit/glue/webkit_glue.h"
|
|
|
|
|
|
|
|
using WebKit::WebFileInfo;
|
|
|
|
using WebKit::WebFileSystemCallbacks;
|
|
|
|
using WebKit::WebFileSystemEntry;
|
2010-10-15 20:37:25 +02:00
|
|
|
using WebKit::WebFileWriter;
|
|
|
|
using WebKit::WebFileWriterClient;
|
2010-10-03 22:35:06 +02:00
|
|
|
using WebKit::WebString;
|
|
|
|
using WebKit::WebVector;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
class BrowserFileSystemCallbackDispatcher
|
2010-10-03 22:35:06 +02:00
|
|
|
: public fileapi::FileSystemCallbackDispatcher {
|
|
|
|
public:
|
2010-10-15 20:37:25 +02:00
|
|
|
BrowserFileSystemCallbackDispatcher(
|
2010-10-03 22:35:06 +02:00
|
|
|
BrowserFileSystem* file_system,
|
|
|
|
WebFileSystemCallbacks* callbacks)
|
|
|
|
: file_system_(file_system),
|
|
|
|
callbacks_(callbacks),
|
|
|
|
request_id_(-1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_request_id(int request_id) { request_id_ = request_id; }
|
|
|
|
|
|
|
|
virtual void DidSucceed() {
|
|
|
|
callbacks_->didSucceed();
|
|
|
|
file_system_->RemoveCompletedOperation(request_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidReadMetadata(const base::PlatformFileInfo& info) {
|
|
|
|
WebFileInfo web_file_info;
|
2010-10-15 20:37:25 +02:00
|
|
|
web_file_info.length = info.size;
|
2010-10-03 22:35:06 +02:00
|
|
|
web_file_info.modificationTime = info.last_modified.ToDoubleT();
|
2010-10-15 20:37:25 +02:00
|
|
|
web_file_info.type = info.is_directory ?
|
|
|
|
WebFileInfo::TypeDirectory : WebFileInfo::TypeFile;
|
2010-10-03 22:35:06 +02:00
|
|
|
callbacks_->didReadMetadata(web_file_info);
|
|
|
|
file_system_->RemoveCompletedOperation(request_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidReadDirectory(
|
2010-11-16 17:01:14 +01:00
|
|
|
const std::vector<base::FileUtilProxy::Entry>& entries,
|
2010-10-03 22:35:06 +02:00
|
|
|
bool has_more) {
|
|
|
|
std::vector<WebFileSystemEntry> web_entries_vector;
|
2010-11-16 17:01:14 +01:00
|
|
|
for (std::vector<base::FileUtilProxy::Entry>::const_iterator it =
|
2010-10-03 22:35:06 +02:00
|
|
|
entries.begin(); it != entries.end(); ++it) {
|
|
|
|
WebFileSystemEntry entry;
|
|
|
|
entry.name = webkit_glue::FilePathStringToWebString(it->name);
|
|
|
|
entry.isDirectory = it->is_directory;
|
|
|
|
web_entries_vector.push_back(entry);
|
|
|
|
}
|
|
|
|
WebVector<WebKit::WebFileSystemEntry> web_entries =
|
|
|
|
web_entries_vector;
|
|
|
|
callbacks_->didReadDirectory(web_entries, has_more);
|
|
|
|
file_system_->RemoveCompletedOperation(request_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidOpenFileSystem(const std::string&, const FilePath&) {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidFail(base::PlatformFileError error_code) {
|
2010-11-16 17:01:14 +01:00
|
|
|
callbacks_->didFail(
|
|
|
|
webkit_glue::PlatformFileErrorToWebFileError(error_code));
|
2010-10-03 22:35:06 +02:00
|
|
|
file_system_->RemoveCompletedOperation(request_id_);
|
|
|
|
}
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
virtual void DidWrite(int64, bool) {
|
2010-10-03 22:35:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BrowserFileSystem* file_system_;
|
|
|
|
WebFileSystemCallbacks* callbacks_;
|
|
|
|
int request_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
BrowserFileSystem::~BrowserFileSystem() {
|
|
|
|
// Drop all the operations.
|
|
|
|
for (OperationsMap::const_iterator iter(&operations_);
|
|
|
|
!iter.IsAtEnd(); iter.Advance())
|
|
|
|
operations_.Remove(iter.GetCurrentKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::move(
|
|
|
|
const WebString& src_path,
|
|
|
|
const WebString& dest_path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath dest_filepath(webkit_glue::WebStringToFilePath(dest_path));
|
|
|
|
FilePath src_filepath(webkit_glue::WebStringToFilePath(src_path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->Move(src_filepath, dest_filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::copy(
|
|
|
|
const WebString& src_path, const WebString& dest_path,
|
|
|
|
WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath dest_filepath(webkit_glue::WebStringToFilePath(dest_path));
|
|
|
|
FilePath src_filepath(webkit_glue::WebStringToFilePath(src_path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->Copy(src_filepath, dest_filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::remove(
|
|
|
|
const WebString& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
GetNewOperation(callbacks)->Remove(filepath, false /* recursive */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::removeRecursively(
|
|
|
|
const WebString& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->Remove(filepath, true /* recursive */);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::readMetadata(
|
|
|
|
const WebString& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->GetMetadata(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::createFile(
|
|
|
|
const WebString& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->CreateFile(filepath, exclusive);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::createDirectory(
|
|
|
|
const WebString& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->CreateDirectory(filepath, exclusive, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::fileExists(
|
2010-10-15 20:37:25 +02:00
|
|
|
const WebString& path, WebFileSystemCallbacks* callbacks) {
|
2010-10-03 22:35:06 +02:00
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->FileExists(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::directoryExists(
|
|
|
|
const WebString& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->DirectoryExists(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::readDirectory(
|
|
|
|
const WebString& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
FilePath filepath(webkit_glue::WebStringToFilePath(path));
|
|
|
|
|
|
|
|
GetNewOperation(callbacks)->ReadDirectory(filepath);
|
|
|
|
}
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
WebFileWriter* BrowserFileSystem::createFileWriter(
|
|
|
|
const WebString& path, WebFileWriterClient* client) {
|
|
|
|
return new BrowserFileWriter(path, client);
|
|
|
|
}
|
|
|
|
|
2010-10-03 22:35:06 +02:00
|
|
|
fileapi::FileSystemOperation* BrowserFileSystem::GetNewOperation(
|
|
|
|
WebFileSystemCallbacks* callbacks) {
|
|
|
|
// This pointer will be owned by |operation|.
|
2010-10-15 20:37:25 +02:00
|
|
|
BrowserFileSystemCallbackDispatcher* dispatcher =
|
|
|
|
new BrowserFileSystemCallbackDispatcher(this, callbacks);
|
2010-10-03 22:35:06 +02:00
|
|
|
fileapi::FileSystemOperation* operation = new fileapi::FileSystemOperation(
|
|
|
|
dispatcher, base::MessageLoopProxy::CreateForCurrentThread());
|
|
|
|
int32 request_id = operations_.Add(operation);
|
|
|
|
dispatcher->set_request_id(request_id);
|
|
|
|
return operation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::RemoveCompletedOperation(int request_id) {
|
|
|
|
operations_.Remove(request_id);
|
|
|
|
}
|