This source file includes following definitions.
- cancel_state_
- truncate
- write
- cancel
- DidFinish
- DidWrite
- DidSucceed
- DidFail
- FinishCancel
#include "content/child/fileapi/webfilewriter_base.h"
#include "base/logging.h"
#include "third_party/WebKit/public/platform/WebFileError.h"
#include "third_party/WebKit/public/platform/WebFileWriterClient.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "webkit/common/fileapi/file_system_util.h"
using fileapi::FileErrorToWebFileError;
namespace content {
WebFileWriterBase::WebFileWriterBase(const GURL& path,
blink::WebFileWriterClient* client)
: path_(path),
client_(client),
operation_(kOperationNone),
cancel_state_(kCancelNotInProgress) {}
WebFileWriterBase::~WebFileWriterBase() {}
void WebFileWriterBase::truncate(long long length) {
DCHECK(kOperationNone == operation_);
DCHECK(kCancelNotInProgress == cancel_state_);
operation_ = kOperationTruncate;
DoTruncate(path_, length);
}
void WebFileWriterBase::write(
long long position,
const blink::WebString& id) {
DCHECK_EQ(kOperationNone, operation_);
DCHECK_EQ(kCancelNotInProgress, cancel_state_);
operation_ = kOperationWrite;
DoWrite(path_, id.utf8(), position);
}
void WebFileWriterBase::cancel() {
if (kOperationWrite != operation_ && kOperationTruncate != operation_)
return;
if (kCancelNotInProgress != cancel_state_)
return;
cancel_state_ = kCancelSent;
DoCancel();
}
void WebFileWriterBase::DidFinish(base::File::Error error_code) {
if (error_code == base::File::FILE_OK)
DidSucceed();
else
DidFail(error_code);
}
void WebFileWriterBase::DidWrite(int64 bytes, bool complete) {
DCHECK(kOperationWrite == operation_);
switch (cancel_state_) {
case kCancelNotInProgress:
if (complete)
operation_ = kOperationNone;
client_->didWrite(bytes, complete);
break;
case kCancelSent:
if (complete)
cancel_state_ = kCancelReceivedWriteResponse;
break;
case kCancelReceivedWriteResponse:
default:
NOTREACHED();
}
}
void WebFileWriterBase::DidSucceed() {
switch (cancel_state_) {
case kCancelNotInProgress:
DCHECK(kOperationTruncate == operation_);
operation_ = kOperationNone;
client_->didTruncate();
break;
case kCancelSent:
DCHECK(kOperationTruncate == operation_);
cancel_state_ = kCancelReceivedWriteResponse;
break;
case kCancelReceivedWriteResponse:
FinishCancel();
break;
default:
NOTREACHED();
}
}
void WebFileWriterBase::DidFail(base::File::Error error_code) {
DCHECK(kOperationNone != operation_);
switch (cancel_state_) {
case kCancelNotInProgress:
operation_ = kOperationNone;
client_->didFail(FileErrorToWebFileError(error_code));
break;
case kCancelSent:
cancel_state_ = kCancelReceivedWriteResponse;
break;
case kCancelReceivedWriteResponse:
FinishCancel();
break;
default:
NOTREACHED();
}
}
void WebFileWriterBase::FinishCancel() {
DCHECK(kCancelReceivedWriteResponse == cancel_state_);
DCHECK(kOperationNone != operation_);
cancel_state_ = kCancelNotInProgress;
operation_ = kOperationNone;
client_->didFail(blink::WebFileErrorAbort);
}
}