Chromium Code Reviews| Index: content/browser/blob_storage/blob_dispatcher_host.cc |
| diff --git a/content/browser/blob_storage/blob_dispatcher_host.cc b/content/browser/blob_storage/blob_dispatcher_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..90c3e807f7ebdaf289bc85f7300721d5b8d92d0c |
| --- /dev/null |
| +++ b/content/browser/blob_storage/blob_dispatcher_host.cc |
| @@ -0,0 +1,242 @@ |
| +// Copyright 2015 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 "content/browser/blob_storage/blob_dispatcher_host.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/bind.h" |
| +#include "content/browser/bad_message.h" |
| +#include "content/browser/fileapi/chrome_blob_storage_context.h" |
| +#include "content/common/fileapi/webblob_messages.h" |
| +#include "ipc/ipc_platform_file.h" |
| +#include "storage/browser/blob/blob_storage_context.h" |
| +#include "storage/browser/blob/blob_transport_result.h" |
| +#include "storage/common/blob_storage/blob_item_bytes_request.h" |
| +#include "storage/common/blob_storage/blob_item_bytes_response.h" |
| +#include "storage/common/data_element.h" |
| +#include "url/gurl.h" |
| + |
| +using storage::BlobStorageContext; |
| +using storage::BlobTransportResult; |
| +using storage::IPCBlobCreationCancelCode; |
| + |
| +namespace content { |
| + |
| +BlobDispatcherHost::BlobDispatcherHost( |
| + ChromeBlobStorageContext* blob_storage_context) |
| + : BrowserMessageFilter(BlobMsgStart), |
| + blob_storage_context_(blob_storage_context) {} |
| + |
| +BlobDispatcherHost::~BlobDispatcherHost() { |
| + ClearHostFromBlobStorageContext(); |
| +} |
| + |
| +void BlobDispatcherHost::OnChannelClosing() { |
| + ClearHostFromBlobStorageContext(); |
| + public_blob_urls_.clear(); |
| + blobs_inuse_map_.clear(); |
| +} |
| + |
| +bool BlobDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(BlobDispatcherHost, message) |
| + IPC_MESSAGE_HANDLER(BlobStorageMsg_RegisterBlobUUID, OnRegisterBlobUUID) |
| + IPC_MESSAGE_HANDLER(BlobStorageMsg_StartBuildingBlob, OnStartBuildingBlob) |
| + IPC_MESSAGE_HANDLER(BlobStorageMsg_MemoryItemResponse, OnMemoryItemResponse) |
| + IPC_MESSAGE_HANDLER(BlobStorageMsg_CancelBuildingBlob, OnCancelBuildingBlob) |
| + IPC_MESSAGE_HANDLER(BlobHostMsg_IncrementRefCount, OnIncrementBlobRefCount) |
| + IPC_MESSAGE_HANDLER(BlobHostMsg_DecrementRefCount, OnDecrementBlobRefCount) |
| + IPC_MESSAGE_HANDLER(BlobHostMsg_RegisterPublicURL, OnRegisterPublicBlobURL) |
| + IPC_MESSAGE_HANDLER(BlobHostMsg_RevokePublicURL, OnRevokePublicBlobURL) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void BlobDispatcherHost::OnRegisterBlobUUID( |
| + const std::string& uuid, |
| + const std::string& content_type, |
| + const std::string& content_disposition) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BlobStorageContext* context = this->context(); |
| + if (uuid.empty() || context->registry().HasEntry(uuid)) { |
| + bad_message::ReceivedBadMessage(this, bad_message::BDH_UUID_REGISTERED); |
| + return; |
| + } |
| + blobs_inuse_map_[uuid] = 1; |
| + async_builder_.RegisterBlobUUID(uuid, content_type, content_disposition, |
| + context); |
| +} |
| + |
| +void BlobDispatcherHost::OnStartBuildingBlob( |
| + const std::string& uuid, |
| + const std::vector<storage::DataElement>& descriptions) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BlobTransportResult result = StartBuildingBlob(uuid, descriptions); |
| + SendIPCResponse(uuid, result); |
| +} |
| + |
| +void BlobDispatcherHost::OnMemoryItemResponse( |
| + const std::string& uuid, |
| + const std::vector<storage::BlobItemBytesResponse>& responses) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BlobTransportResult result = MemoryItemResponse(uuid, responses); |
| + SendIPCResponse(uuid, result); |
| +} |
| + |
| +void BlobDispatcherHost::OnCancelBuildingBlob( |
| + const std::string& uuid, |
| + const storage::IPCBlobCreationCancelCode code) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (uuid.empty() || !IsBeingBuiltInHost(uuid)) { |
| + bad_message::ReceivedBadMessage(this, bad_message::BDH_INVALID_OPERATION); |
| + return; |
| + } |
| + VLOG(1) << "Blob construction of " << uuid << " cancelled by renderer. " |
| + << " Reason: " << static_cast<int>(code) << "."; |
| + async_builder_.MaybeCancelBuildingBlob(uuid, code, context()); |
| +} |
| + |
| +void BlobDispatcherHost::OnIncrementBlobRefCount(const std::string& uuid) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BlobStorageContext* context = this->context(); |
| + if (uuid.empty() || !context->registry().HasEntry(uuid)) { |
| + bad_message::ReceivedBadMessage(this, bad_message::BDH_INVALID_OPERATION); |
| + return; |
| + } |
| + context->IncrementBlobRefCount(uuid); |
| + blobs_inuse_map_[uuid] += 1; |
| +} |
| + |
| +void BlobDispatcherHost::OnDecrementBlobRefCount(const std::string& uuid) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (uuid.empty() || !IsInUseInHost(uuid)) { |
| + bad_message::ReceivedBadMessage(this, bad_message::BDH_INVALID_OPERATION); |
| + return; |
| + } |
| + context()->DecrementBlobRefCount(uuid); |
| + blobs_inuse_map_[uuid] -= 1; |
| + if (blobs_inuse_map_[uuid] == 0) |
| + blobs_inuse_map_.erase(uuid); |
| +} |
| + |
| +void BlobDispatcherHost::OnRegisterPublicBlobURL(const GURL& public_url, |
| + const std::string& uuid) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BlobStorageContext* context = this->context(); |
| + if (uuid.empty() || !IsInUseInHost(uuid) || |
| + context->registry().IsURLMapped(public_url)) { |
| + bad_message::ReceivedBadMessage(this, bad_message::BDH_INVALID_OPERATION); |
| + return; |
| + } |
| + context->RegisterPublicBlobURL(public_url, uuid); |
| + public_blob_urls_.insert(public_url); |
| +} |
| + |
| +void BlobDispatcherHost::OnRevokePublicBlobURL(const GURL& public_url) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (!IsUrlRegisteredInHost(public_url)) { |
| + bad_message::ReceivedBadMessage(this, bad_message::BDH_INVALID_OPERATION); |
| + return; |
| + } |
| + BlobStorageContext* context = this->context(); |
| + context->RevokePublicBlobURL(public_url); |
| + public_blob_urls_.erase(public_url); |
| +} |
| + |
| +storage::BlobStorageContext* BlobDispatcherHost::context() { |
| + return blob_storage_context_->context(); |
| +} |
| + |
| +storage::BlobTransportResult BlobDispatcherHost::StartBuildingBlob( |
| + const std::string& uuid, |
| + const std::vector<storage::DataElement>& descriptions) { |
| + BlobStorageContext* context = this->context(); |
| + |
| + if (uuid.empty() || !context->IsBeingBuilt(uuid)) { |
| + return BlobTransportResult::BAD_IPC; |
| + } |
| + |
| + // Now we know that we're expecting to be built. |
| + return async_builder_.StartBuildingBlob( |
| + uuid, descriptions, context->memory_available(), context, |
| + base::Bind(&BlobDispatcherHost::SendMemoryRequest, this, uuid)); |
| +} |
| + |
| +storage::BlobTransportResult BlobDispatcherHost::MemoryItemResponse( |
| + const std::string& uuid, |
| + const std::vector<storage::BlobItemBytesResponse>& responses) { |
| + if (uuid.empty() || !IsBeingBuiltInHost(uuid)) { |
| + return BlobTransportResult::BAD_IPC; |
| + } |
| + return async_builder_.OnMemoryResponses(uuid, responses, context()); |
| +} |
| + |
| +void BlobDispatcherHost::SendMemoryRequest( |
| + const std::string& uuid, |
| + const std::vector<storage::BlobItemBytesRequest>& requests, |
| + const std::vector<base::SharedMemoryHandle>& memory_handles, |
| + std::vector<base::File>* files) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + std::vector<IPC::PlatformFileForTransit> file_handles; |
| + // TODO(dmurph): Support file-backed blob transportation. |
| + DCHECK(files->empty()); |
| + Send(new BlobStorageMsg_RequestMemoryItem(uuid, requests, memory_handles, |
| + file_handles)); |
| +} |
| + |
| +void BlobDispatcherHost::SendIPCResponse(const std::string& uuid, |
| + storage::BlobTransportResult result) { |
| + switch (result) { |
| + case BlobTransportResult::BAD_IPC: |
| + bad_message::ReceivedBadMessage(this, |
| + bad_message::BDH_CONSTRUCTION_FAILED); |
| + return; |
| + case BlobTransportResult::CANCEL_MEMORY_FULL: |
| + Send(new BlobStorageMsg_CancelBuildingBlob( |
| + uuid, IPCBlobCreationCancelCode::OUT_OF_MEMORY)); |
| + return; |
| + case BlobTransportResult::CANCEL_FILE_ERROR: |
| + Send(new BlobStorageMsg_CancelBuildingBlob( |
| + uuid, IPCBlobCreationCancelCode::FILE_WRITE_FAILED)); |
| + return; |
| + case BlobTransportResult::CANCEL_UNKNOWN: |
| + Send(new BlobStorageMsg_CancelBuildingBlob( |
| + uuid, IPCBlobCreationCancelCode::UNKNOWN)); |
| + return; |
| + case BlobTransportResult::PENDING_RESPONSES: |
| + return; |
| + case BlobTransportResult::DONE: |
| + Send(new BlobStorageMsg_DoneBuildingBlob(uuid)); |
| + return; |
| + } |
| + NOTREACHED(); |
| +} |
| + |
| +bool BlobDispatcherHost::IsInUseInHost(const std::string& uuid) { |
| + return blobs_inuse_map_.find(uuid) != blobs_inuse_map_.end(); |
| +} |
| + |
| +bool BlobDispatcherHost::IsBeingBuiltInHost(const std::string& uuid) { |
| + return IsInUseInHost(uuid) && context()->IsBeingBuilt(uuid); |
|
michaeln
2016/02/06 00:08:39
I don't think this is valid test for whether it's
dmurph
2016/02/09 00:52:25
True. Done.
|
| +} |
| + |
| +bool BlobDispatcherHost::IsUrlRegisteredInHost(const GURL& blob_url) { |
| + return public_blob_urls_.find(blob_url) != public_blob_urls_.end(); |
| +} |
| + |
| +void BlobDispatcherHost::ClearHostFromBlobStorageContext() { |
| + BlobStorageContext* context = this->context(); |
| + for (const auto& url : public_blob_urls_) { |
| + context->RevokePublicBlobURL(url); |
| + } |
| + for (const auto& uuid_refnum_pair : blobs_inuse_map_) { |
| + for (int i = 0; i < uuid_refnum_pair.second; ++i) |
| + context->DecrementBlobRefCount(uuid_refnum_pair.first); |
| + } |
| + async_builder_.ClearAndBreakPendingBlobConstruction(context); |
| +} |
| + |
| +} // namespace content |