1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "storage/browser/blob/blob_data_handle.h"
#include <stdint.h>
#include <memory>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_runner.h"
#include "base/time/time.h"
#include "storage/browser/blob/blob_data_snapshot.h"
#include "storage/browser/blob/blob_reader.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/file_system/file_stream_reader.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "url/gurl.h"
namespace storage {
BlobDataHandle::BlobDataHandleShared::BlobDataHandleShared(
const std::string& uuid,
const std::string& content_type,
const std::string& content_disposition,
uint64_t size,
BlobStorageContext* context)
: uuid_(uuid),
content_type_(content_type),
content_disposition_(content_disposition),
size_(size),
context_(context->AsWeakPtr()) {
context_->IncrementBlobRefCount(uuid);
}
std::unique_ptr<BlobReader> BlobDataHandle::CreateReader() const {
return base::WrapUnique(new BlobReader(this));
}
BlobDataHandle::BlobDataHandleShared::~BlobDataHandleShared() {
if (context_.get())
context_->DecrementBlobRefCount(uuid_);
}
BlobDataHandle::BlobDataHandle(const std::string& uuid,
const std::string& content_type,
const std::string& content_disposition,
uint64_t size,
BlobStorageContext* context,
base::SequencedTaskRunner* io_task_runner)
: io_task_runner_(io_task_runner),
shared_(new BlobDataHandleShared(uuid,
content_type,
content_disposition,
size,
context)) {
DCHECK(io_task_runner_.get());
DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
}
BlobDataHandle::BlobDataHandle(const BlobDataHandle& other) = default;
BlobDataHandle::~BlobDataHandle() {
if (!io_task_runner_->RunsTasksInCurrentSequence()) {
io_task_runner_->ReleaseSoon(FROM_HERE, std::move(shared_));
}
}
BlobDataHandle& BlobDataHandle::operator=(
const BlobDataHandle& other) = default;
bool BlobDataHandle::IsBeingBuilt() const {
DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
if (!shared_->context_)
return false;
return BlobStatusIsPending(GetBlobStatus());
}
bool BlobDataHandle::IsBroken() const {
DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
if (!shared_->context_)
return true;
return BlobStatusIsError(GetBlobStatus());
}
BlobStatus BlobDataHandle::GetBlobStatus() const {
DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
if (!shared_->context_)
return BlobStatus::ERR_REFERENCED_BLOB_BROKEN;
return shared_->context_->GetBlobStatus(shared_->uuid_);
}
void BlobDataHandle::RunOnConstructionComplete(BlobStatusCallback done) {
DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
if (!shared_->context_.get()) {
std::move(done).Run(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS);
return;
}
shared_->context_->RunOnConstructionComplete(shared_->uuid_, std::move(done));
}
void BlobDataHandle::RunOnConstructionBegin(BlobStatusCallback done) {
DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
if (!shared_->context_.get()) {
std::move(done).Run(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS);
return;
}
shared_->context_->RunOnConstructionBegin(shared_->uuid_, std::move(done));
}
std::unique_ptr<BlobDataSnapshot> BlobDataHandle::CreateSnapshot() const {
DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
if (!shared_->context_.get())
return nullptr;
return shared_->context_->CreateSnapshot(shared_->uuid_);
}
const std::string& BlobDataHandle::uuid() const {
return shared_->uuid_;
}
const std::string& BlobDataHandle::content_type() const {
return shared_->content_type_;
}
const std::string& BlobDataHandle::content_disposition() const {
return shared_->content_disposition_;
}
uint64_t BlobDataHandle::size() const {
return shared_->size_;
}
} // namespace storage
|