Skip to content

Commit

Permalink
Adding FileSystemDispatcherHost implementation for createSnapshot
Browse files Browse the repository at this point in the history
BUG=115603
TEST=will test when we finish adding the plumbing

Review URL: https://chromiumcodereview.appspot.com/9475015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124085 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kinuko@chromium.org committed Feb 29, 2012
1 parent 997ac73 commit 1d4d298
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
75 changes: 71 additions & 4 deletions content/browser/file_system/file_system_dispatcher_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
#include "base/platform_file.h"
#include "base/threading/thread.h"
#include "base/time.h"
#include "content/browser/chrome_blob_storage_context.h"
#include "content/common/file_system_messages.h"
#include "content/public/browser/user_metrics.h"
#include "googleurl/src/gurl.h"
#include "ipc/ipc_platform_file.h"
#include "net/base/mime_util.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "webkit/blob/blob_data.h"
#include "webkit/blob/blob_storage_controller.h"
#include "webkit/blob/deletable_file_reference.h"
#include "webkit/fileapi/file_system_context.h"
#include "webkit/fileapi/file_system_operation.h"
#include "webkit/fileapi/file_system_quota_util.h"
Expand All @@ -31,29 +36,46 @@ using content::UserMetricsAction;
using fileapi::FileSystemFileUtil;
using fileapi::FileSystemOperation;
using fileapi::FileSystemOperationInterface;
using webkit_blob::BlobData;
using webkit_blob::BlobStorageController;

FileSystemDispatcherHost::FileSystemDispatcherHost(
net::URLRequestContextGetter* request_context_getter,
fileapi::FileSystemContext* file_system_context)
fileapi::FileSystemContext* file_system_context,
ChromeBlobStorageContext* blob_storage_context)
: context_(file_system_context),
request_context_getter_(request_context_getter),
request_context_(NULL) {
request_context_(NULL),
blob_storage_context_(blob_storage_context) {
DCHECK(context_);
DCHECK(request_context_getter_);
}

FileSystemDispatcherHost::FileSystemDispatcherHost(
net::URLRequestContext* request_context,
fileapi::FileSystemContext* file_system_context)
fileapi::FileSystemContext* file_system_context,
ChromeBlobStorageContext* blob_storage_context)
: context_(file_system_context),
request_context_(request_context) {
request_context_(request_context),
blob_storage_context_(blob_storage_context) {
DCHECK(context_);
DCHECK(request_context_);
}

FileSystemDispatcherHost::~FileSystemDispatcherHost() {
}

void FileSystemDispatcherHost::OnChannelClosing() {
BrowserMessageFilter::OnChannelClosing();

// Unregister all the blob URLs that are previously registered in this
// process.
for (base::hash_set<std::string>::const_iterator iter = blob_urls_.begin();
iter != blob_urls_.end(); ++iter) {
blob_storage_context_->controller()->RemoveBlob(GURL(*iter));
}
}

void FileSystemDispatcherHost::OnChannelConnected(int32 peer_pid) {
BrowserMessageFilter::OnChannelConnected(peer_pid);

Expand Down Expand Up @@ -283,6 +305,16 @@ void FileSystemDispatcherHost::OnSyncGetPlatformPath(
operation->SyncGetPlatformPath(path, platform_path);
}

// TODO(kinuko,tzik): Add IPC plumbing to wire this.
void FileSystemDispatcherHost::OnCreateSnapshotFile(
int request_id, const GURL& blob_url, const GURL& path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
GetNewOperation(path, request_id)->CreateSnapshotFile(
path,
base::Bind(&FileSystemDispatcherHost::DidCreateSnapshot,
this, request_id, blob_url));
}

void FileSystemDispatcherHost::DidFinish(int request_id,
base::PlatformFileError result) {
if (result == base::PLATFORM_FILE_OK)
Expand Down Expand Up @@ -368,6 +400,41 @@ void FileSystemDispatcherHost::DidOpenFileSystem(int request_id,
// For OpenFileSystem we do not create a new operation, so no unregister here.
}

void FileSystemDispatcherHost::DidCreateSnapshot(
int request_id,
const GURL& blob_url,
base::PlatformFileError result,
const base::PlatformFileInfo& info,
const FilePath& platform_path,
const scoped_refptr<webkit_blob::DeletableFileReference>& unused) {
if (result != base::PLATFORM_FILE_OK) {
Send(new FileSystemMsg_DidFail(request_id, result));
return;
}

FilePath::StringType extension = platform_path.Extension();
if (!extension.empty())
extension = extension.substr(1); // Strip leading ".".

// This may fail, but then we'll be just setting the empty mime type.
std::string mime_type;
net::GetWellKnownMimeTypeFromExtension(extension, &mime_type);

// Register the created file to the blob registry.
// Blob storage automatically finds and refs deletable files, so we don't
// need to do anything for the returned file reference (|unused|) here.
BlobData::Item item;
item.SetToFile(platform_path, 0, -1, base::Time());
BlobStorageController* controller = blob_storage_context_->controller();
controller->StartBuildingBlob(blob_url);
controller->AppendBlobDataItem(blob_url, item);
controller->FinishBuildingBlob(blob_url, mime_type);
blob_urls_.insert(blob_url.spec());

// Return the file info and platform_path.
Send(new FileSystemMsg_DidReadMetadata(request_id, info, platform_path));
}

FileSystemOperationInterface* FileSystemDispatcherHost::GetNewOperation(
const GURL& target_path,
int request_id) {
Expand Down
32 changes: 30 additions & 2 deletions content/browser/file_system/file_system_dispatcher_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "content/public/browser/browser_message_filter.h"
#include "webkit/fileapi/file_system_types.h"

class ChromeBlobStorageContext;
class FilePath;
class GURL;

Expand All @@ -31,19 +32,26 @@ class URLRequestContext;
class URLRequestContextGetter;
} // namespace net

namespace webkit_blob {
class DeletableFileReference;
}

class FileSystemDispatcherHost : public content::BrowserMessageFilter {
public:
// Used by the renderer process host on the UI thread.
FileSystemDispatcherHost(
net::URLRequestContextGetter* request_context_getter,
fileapi::FileSystemContext* file_system_context);
fileapi::FileSystemContext* file_system_context,
ChromeBlobStorageContext* blob_storage_context);
// Used by the worker process host on the IO thread.
FileSystemDispatcherHost(
net::URLRequestContext* request_context,
fileapi::FileSystemContext* file_system_context);
fileapi::FileSystemContext* file_system_context,
ChromeBlobStorageContext* blob_storage_context);
virtual ~FileSystemDispatcherHost();

// content::BrowserMessageFilter implementation.
virtual void OnChannelClosing() OVERRIDE;
virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
virtual void OverrideThreadForMessage(
const IPC::Message& message,
Expand Down Expand Up @@ -89,6 +97,9 @@ class FileSystemDispatcherHost : public content::BrowserMessageFilter {
void OnDidUpdate(const GURL& path, int64 delta);
void OnSyncGetPlatformPath(const GURL& path,
FilePath* platform_path);
void OnCreateSnapshotFile(int request_id,
const GURL& blob_url,
const GURL& path);

// Callback functions to be used when each file operation is finished.
void DidFinish(int request_id, base::PlatformFileError result);
Expand All @@ -113,6 +124,14 @@ class FileSystemDispatcherHost : public content::BrowserMessageFilter {
base::PlatformFileError result,
const std::string& name,
const GURL& root);
void DidCreateSnapshot(
int request_id,
const GURL& blob_url,
base::PlatformFileError result,
const base::PlatformFileInfo& info,
const FilePath& platform_path,
const scoped_refptr<webkit_blob::DeletableFileReference>&
deletable_ref);

// Creates a new FileSystemOperationInterface based on |target_path|.
fileapi::FileSystemOperationInterface* GetNewOperation(
Expand All @@ -130,6 +149,15 @@ class FileSystemDispatcherHost : public content::BrowserMessageFilter {
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
net::URLRequestContext* request_context_;

// We access BlobStorageContext to construct an internal blob for
// snapshot files.
scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;

// Keeps track of internal blob URLs for temporary snapshot files.
// (As we do for regular blobs in BlobMessageFilter.)
// TODO(kinuko,tzik): merge this with the one in BlobMessageFilter.
base::hash_set<std::string> blob_urls_;

DISALLOW_COPY_AND_ASSIGN(FileSystemDispatcherHost);
};

Expand Down
3 changes: 2 additions & 1 deletion content/browser/renderer_host/render_process_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ void RenderProcessHostImpl::CreateMessageFilters() {
#endif
channel_->AddFilter(new FileSystemDispatcherHost(
browser_context->GetRequestContext(),
BrowserContext::GetFileSystemContext(browser_context)));
BrowserContext::GetFileSystemContext(browser_context),
ChromeBlobStorageContext::GetFor(browser_context)));
channel_->AddFilter(new device_orientation::MessageFilter());
channel_->AddFilter(new BlobMessageFilter(GetID(),
ChromeBlobStorageContext::GetFor(browser_context)));
Expand Down
4 changes: 3 additions & 1 deletion content/browser/worker_host/worker_process_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ void WorkerProcessHost::CreateMessageFilters(int render_process_id) {
process_->GetData().id));
process_->GetHost()->AddFilter(new FileSystemDispatcherHost(
request_context,
ResourceContext::GetFileSystemContext(resource_context_)));
ResourceContext::GetFileSystemContext(resource_context_),
content::GetChromeBlobStorageContextForResourceContext(
resource_context_)));
process_->GetHost()->AddFilter(new FileUtilitiesMessageFilter(
process_->GetData().id));
process_->GetHost()->AddFilter(new BlobMessageFilter(
Expand Down

0 comments on commit 1d4d298

Please sign in to comment.