2011-04-05 18:17:33 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
2010-10-03 22:35:06 +02:00
|
|
|
// 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"
|
2011-04-05 18:17:33 +02:00
|
|
|
#include "base/memory/scoped_callback_factory.h"
|
2010-12-16 22:54:42 +01:00
|
|
|
#include "base/message_loop.h"
|
2010-10-03 22:35:06 +02:00
|
|
|
#include "base/message_loop_proxy.h"
|
|
|
|
#include "base/time.h"
|
2010-12-16 22:54:42 +01:00
|
|
|
#include "base/utf_string_conversions.h"
|
|
|
|
#include "googleurl/src/gurl.h"
|
2011-07-03 02:03:30 +02:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFileInfo.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemCallbacks.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemEntry.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
|
2011-07-03 02:03:30 +02:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h"
|
2010-10-03 22:35:06 +02:00
|
|
|
#include "webkit/fileapi/file_system_callback_dispatcher.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "webkit/fileapi/file_system_context.h"
|
|
|
|
#include "webkit/fileapi/file_system_operation.h"
|
2010-12-16 22:54:42 +01:00
|
|
|
#include "webkit/fileapi/file_system_path_manager.h"
|
|
|
|
#include "webkit/fileapi/file_system_types.h"
|
2010-10-03 22:35:06 +02:00
|
|
|
#include "webkit/glue/webkit_glue.h"
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
using base::WeakPtr;
|
|
|
|
|
2010-10-03 22:35:06 +02:00
|
|
|
using WebKit::WebFileInfo;
|
2010-12-16 22:54:42 +01:00
|
|
|
using WebKit::WebFileSystem;
|
2010-10-03 22:35:06 +02:00
|
|
|
using WebKit::WebFileSystemCallbacks;
|
|
|
|
using WebKit::WebFileSystemEntry;
|
2010-10-15 20:37:25 +02:00
|
|
|
using WebKit::WebFileWriter;
|
|
|
|
using WebKit::WebFileWriterClient;
|
2010-12-16 22:54:42 +01:00
|
|
|
using WebKit::WebFrame;
|
|
|
|
using WebKit::WebSecurityOrigin;
|
2010-10-03 22:35:06 +02:00
|
|
|
using WebKit::WebString;
|
2011-07-03 02:03:30 +02:00
|
|
|
using WebKit::WebURL;
|
2010-10-03 22:35:06 +02:00
|
|
|
using WebKit::WebVector;
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
using fileapi::FileSystemCallbackDispatcher;
|
2011-02-15 19:07:24 +01:00
|
|
|
using fileapi::FileSystemContext;
|
|
|
|
using fileapi::FileSystemOperation;
|
2010-12-16 22:54:42 +01:00
|
|
|
|
2010-10-03 22:35:06 +02:00
|
|
|
namespace {
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
class BrowserFileSystemCallbackDispatcher
|
2010-12-16 22:54:42 +01:00
|
|
|
: public FileSystemCallbackDispatcher {
|
2010-10-03 22:35:06 +02:00
|
|
|
public:
|
2010-10-15 20:37:25 +02:00
|
|
|
BrowserFileSystemCallbackDispatcher(
|
2010-12-16 22:54:42 +01:00
|
|
|
const WeakPtr<BrowserFileSystem>& file_system,
|
2010-10-03 22:35:06 +02:00
|
|
|
WebFileSystemCallbacks* callbacks)
|
|
|
|
: file_system_(file_system),
|
2010-12-16 22:54:42 +01:00
|
|
|
callbacks_(callbacks) {
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
~BrowserFileSystemCallbackDispatcher() {
|
|
|
|
}
|
2010-10-03 22:35:06 +02:00
|
|
|
|
|
|
|
virtual void DidSucceed() {
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(file_system_);
|
2010-10-03 22:35:06 +02:00
|
|
|
callbacks_->didSucceed();
|
|
|
|
}
|
|
|
|
|
2011-04-05 18:17:33 +02:00
|
|
|
virtual void DidReadMetadata(const base::PlatformFileInfo& info,
|
|
|
|
const FilePath& platform_path) {
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(file_system_);
|
2010-10-03 22:35:06 +02:00
|
|
|
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;
|
2011-04-05 18:17:33 +02:00
|
|
|
web_file_info.platformPath =
|
|
|
|
webkit_glue::FilePathToWebString(platform_path);
|
2010-10-03 22:35:06 +02:00
|
|
|
callbacks_->didReadMetadata(web_file_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(file_system_);
|
2010-10-03 22:35:06 +02:00
|
|
|
std::vector<WebFileSystemEntry> web_entries_vector;
|
2010-11-16 17:01:14 +01:00
|
|
|
for (std::vector<base::FileUtilProxy::Entry>::const_iterator it =
|
2010-12-16 22:54:42 +01:00
|
|
|
entries.begin(); it != entries.end(); ++it) {
|
2010-10-03 22:35:06 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
virtual void DidOpenFileSystem(
|
2011-05-16 18:56:12 +02:00
|
|
|
const std::string& name, const GURL& root) {
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(file_system_);
|
2011-05-16 18:56:12 +02:00
|
|
|
if (!root.is_valid())
|
2010-12-16 22:54:42 +01:00
|
|
|
callbacks_->didFail(WebKit::WebFileErrorSecurity);
|
|
|
|
else
|
2011-07-03 02:03:30 +02:00
|
|
|
callbacks_->didOpenFileSystem(WebString::fromUTF8(name), root);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidFail(base::PlatformFileError error_code) {
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(file_system_);
|
2010-11-16 17:01:14 +01:00
|
|
|
callbacks_->didFail(
|
|
|
|
webkit_glue::PlatformFileErrorToWebFileError(error_code));
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
virtual void DidWrite(int64, bool) {
|
2010-10-03 22:35:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-12-16 22:54:42 +01:00
|
|
|
WeakPtr<BrowserFileSystem> file_system_;
|
2010-10-03 22:35:06 +02:00
|
|
|
WebFileSystemCallbacks* callbacks_;
|
|
|
|
};
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
BrowserFileSystem::BrowserFileSystem() {
|
|
|
|
if (file_system_dir_.CreateUniqueTempDir()) {
|
2011-02-15 19:07:24 +01:00
|
|
|
file_system_context_ = new FileSystemContext(
|
2011-09-10 21:40:03 +02:00
|
|
|
base::MessageLoopProxy::current(),
|
|
|
|
base::MessageLoopProxy::current(),
|
2011-04-05 18:17:33 +02:00
|
|
|
NULL /* special storage policy */,
|
2011-05-16 18:56:12 +02:00
|
|
|
NULL /* quota manager */,
|
2010-12-16 22:54:42 +01:00
|
|
|
file_system_dir_.path(),
|
|
|
|
false /* incognito */,
|
|
|
|
true /* allow_file_access */,
|
2011-05-16 18:56:12 +02:00
|
|
|
true /* unlimited_quota */,
|
|
|
|
NULL);
|
2010-12-16 22:54:42 +01:00
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Failed to create a temp dir for the filesystem."
|
|
|
|
"FileSystem feature will be disabled.";
|
|
|
|
}
|
|
|
|
}
|
2010-10-03 22:35:06 +02:00
|
|
|
|
|
|
|
BrowserFileSystem::~BrowserFileSystem() {
|
2010-12-16 22:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::OpenFileSystem(
|
|
|
|
WebFrame* frame, WebFileSystem::Type web_filesystem_type,
|
|
|
|
long long, bool create,
|
|
|
|
WebFileSystemCallbacks* callbacks) {
|
2011-02-15 19:07:24 +01:00
|
|
|
if (!frame || !file_system_context_.get()) {
|
2010-12-16 22:54:42 +01:00
|
|
|
// The FileSystem temp directory was not initialized successfully.
|
|
|
|
callbacks->didFail(WebKit::WebFileErrorSecurity);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileapi::FileSystemType type;
|
|
|
|
if (web_filesystem_type == WebFileSystem::TypeTemporary)
|
|
|
|
type = fileapi::kFileSystemTypeTemporary;
|
|
|
|
else if (web_filesystem_type == WebFileSystem::TypePersistent)
|
|
|
|
type = fileapi::kFileSystemTypePersistent;
|
2011-05-16 18:56:12 +02:00
|
|
|
else if (web_filesystem_type == WebFileSystem::TypeExternal)
|
|
|
|
type = fileapi::kFileSystemTypeExternal;
|
2010-12-16 22:54:42 +01:00
|
|
|
else {
|
|
|
|
// Unknown type filesystem is requested.
|
|
|
|
callbacks->didFail(WebKit::WebFileErrorSecurity);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-03 02:03:30 +02:00
|
|
|
GURL origin_url(frame->document().securityOrigin().toString());
|
2010-12-16 22:54:42 +01:00
|
|
|
GetNewOperation(callbacks)->OpenFileSystem(origin_url, type, create);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::move(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& src_path,
|
|
|
|
const WebURL& dest_path, WebFileSystemCallbacks* callbacks) {
|
2011-05-16 18:56:12 +02:00
|
|
|
GetNewOperation(callbacks)->Move(GURL(src_path), GURL(dest_path));
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::copy(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& src_path, const WebURL& dest_path,
|
2010-10-03 22:35:06 +02:00
|
|
|
WebFileSystemCallbacks* callbacks) {
|
2011-05-16 18:56:12 +02:00
|
|
|
GetNewOperation(callbacks)->Copy(GURL(src_path), GURL(dest_path));
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::remove(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->Remove(path, false /* recursive */);
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::removeRecursively(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->Remove(path, true /* recursive */);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::readMetadata(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->GetMetadata(path);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::createFile(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->CreateFile(path, exclusive);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::createDirectory(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->CreateDirectory(path, exclusive, false);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::fileExists(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->FileExists(path);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::directoryExists(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->DirectoryExists(path);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileSystem::readDirectory(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, WebFileSystemCallbacks* callbacks) {
|
|
|
|
GetNewOperation(callbacks)->ReadDirectory(path);
|
2010-10-03 22:35:06 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
WebFileWriter* BrowserFileSystem::createFileWriter(
|
2011-07-03 02:03:30 +02:00
|
|
|
const WebURL& path, WebFileWriterClient* client) {
|
|
|
|
return new BrowserFileWriter(path, client, file_system_context_.get());
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
FileSystemOperation* BrowserFileSystem::GetNewOperation(
|
2010-10-03 22:35:06 +02:00
|
|
|
WebFileSystemCallbacks* callbacks) {
|
2010-10-15 20:37:25 +02:00
|
|
|
BrowserFileSystemCallbackDispatcher* dispatcher =
|
2010-12-16 22:54:42 +01:00
|
|
|
new BrowserFileSystemCallbackDispatcher(AsWeakPtr(), callbacks);
|
2011-02-15 19:07:24 +01:00
|
|
|
FileSystemOperation* operation = new FileSystemOperation(
|
2011-09-10 21:40:03 +02:00
|
|
|
dispatcher, base::MessageLoopProxy::current(),
|
2011-04-05 18:17:33 +02:00
|
|
|
file_system_context_.get(), NULL);
|
2010-10-03 22:35:06 +02:00
|
|
|
return operation;
|
2011-09-10 21:40:03 +02:00
|
|
|
}
|