summaryrefslogtreecommitdiff
path: root/chromium/net/base/upload_file_element_reader.cc
blob: d1f2a12ac8cd13c6d5037032f13cdd98baf2ea03 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) 2012 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 "net/base/upload_file_element_reader.h"

#include "base/bind.h"
#include "base/file_util.h"
#include "base/location.h"
#include "base/task_runner_util.h"
#include "net/base/file_stream.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"

namespace net {

namespace {

// In tests, this value is used to override the return value of
// UploadFileElementReader::GetContentLength() when set to non-zero.
uint64 overriding_content_length = 0;

// This function is used to implement Init().
template<typename FileStreamDeleter>
int InitInternal(const base::FilePath& path,
                 uint64 range_offset,
                 uint64 range_length,
                 const base::Time& expected_modification_time,
                 scoped_ptr<FileStream, FileStreamDeleter>* out_file_stream,
                 uint64* out_content_length) {
  scoped_ptr<FileStream> file_stream(new FileStream(NULL));
  int64 rv = file_stream->OpenSync(
      path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ);
  if (rv != OK) {
    // If the file can't be opened, we'll just upload an empty file.
    DLOG(WARNING) << "Failed to open \"" << path.value()
                  << "\" for reading: " << rv;
    file_stream.reset();
  } else if (range_offset) {
    rv = file_stream->SeekSync(FROM_BEGIN, range_offset);
    if (rv < 0) {
      DLOG(WARNING) << "Failed to seek \"" << path.value()
                    << "\" to offset: " << range_offset << " (" << rv << ")";
      file_stream.reset();
    }
  }

  int64 length = 0;
  if (file_stream.get() &&
      file_util::GetFileSize(path, &length) &&
      range_offset < static_cast<uint64>(length)) {
    // Compensate for the offset.
    length = std::min(length - range_offset, range_length);
  }
  *out_content_length = length;
  out_file_stream->reset(file_stream.release());

  // If the underlying file has been changed and the expected file modification
  // time is set, treat it as error. Note that the expected modification time
  // from WebKit is based on time_t precision. So we have to convert both to
  // time_t to compare. This check is used for sliced files.
  if (!expected_modification_time.is_null()) {
    base::PlatformFileInfo info;
    if (file_util::GetFileInfo(path, &info) &&
        expected_modification_time.ToTimeT() != info.last_modified.ToTimeT()) {
      return ERR_UPLOAD_FILE_CHANGED;
    }
  }

  return OK;
}

// This function is used to implement Read().
int ReadInternal(scoped_refptr<IOBuffer> buf,
                 int buf_length,
                 uint64 bytes_remaining,
                 FileStream* file_stream) {
  DCHECK_LT(0, buf_length);

  const uint64 num_bytes_to_read =
      std::min(bytes_remaining, static_cast<uint64>(buf_length));

  int result = 0;
  if (num_bytes_to_read > 0) {
    DCHECK(file_stream);  // file_stream is non-null if content_length_ > 0.
    result = file_stream->ReadSync(buf->data(), num_bytes_to_read);
    if (result == 0)  // Reached end-of-file earlier than expected.
      result = ERR_UPLOAD_FILE_CHANGED;
  }
  return result;
}

}  // namespace

UploadFileElementReader::FileStreamDeleter::FileStreamDeleter(
    base::TaskRunner* task_runner) : task_runner_(task_runner) {
  DCHECK(task_runner_.get());
}

UploadFileElementReader::FileStreamDeleter::~FileStreamDeleter() {}

void UploadFileElementReader::FileStreamDeleter::operator() (
    FileStream* file_stream) const {
  if (file_stream) {
    task_runner_->PostTask(FROM_HERE,
                           base::Bind(&base::DeletePointer<FileStream>,
                                      file_stream));
  }
}

UploadFileElementReader::UploadFileElementReader(
    base::TaskRunner* task_runner,
    const base::FilePath& path,
    uint64 range_offset,
    uint64 range_length,
    const base::Time& expected_modification_time)
    : task_runner_(task_runner),
      path_(path),
      range_offset_(range_offset),
      range_length_(range_length),
      expected_modification_time_(expected_modification_time),
      file_stream_(NULL, FileStreamDeleter(task_runner_.get())),
      content_length_(0),
      bytes_remaining_(0),
      weak_ptr_factory_(this) {
  DCHECK(task_runner_.get());
}

UploadFileElementReader::~UploadFileElementReader() {
}

const UploadFileElementReader* UploadFileElementReader::AsFileReader() const {
  return this;
}

int UploadFileElementReader::Init(const CompletionCallback& callback) {
  DCHECK(!callback.is_null());
  Reset();

  ScopedFileStreamPtr* file_stream =
      new ScopedFileStreamPtr(NULL, FileStreamDeleter(task_runner_.get()));
  uint64* content_length = new uint64;
  const bool posted = base::PostTaskAndReplyWithResult(
      task_runner_.get(),
      FROM_HERE,
      base::Bind(&InitInternal<FileStreamDeleter>,
                 path_,
                 range_offset_,
                 range_length_,
                 expected_modification_time_,
                 file_stream,
                 content_length),
      base::Bind(&UploadFileElementReader::OnInitCompleted,
                 weak_ptr_factory_.GetWeakPtr(),
                 base::Owned(file_stream),
                 base::Owned(content_length),
                 callback));
  DCHECK(posted);
  return ERR_IO_PENDING;
}

uint64 UploadFileElementReader::GetContentLength() const {
  if (overriding_content_length)
    return overriding_content_length;
  return content_length_;
}

uint64 UploadFileElementReader::BytesRemaining() const {
  return bytes_remaining_;
}

int UploadFileElementReader::Read(IOBuffer* buf,
                                  int buf_length,
                                  const CompletionCallback& callback) {
  DCHECK(!callback.is_null());

  if (BytesRemaining() == 0)
    return 0;

  // Save the value of file_stream_.get() before base::Passed() invalidates it.
  FileStream* file_stream_ptr = file_stream_.get();
  // Pass the ownership of file_stream_ to the worker pool to safely perform
  // operation even when |this| is destructed before the read completes.
  const bool posted = base::PostTaskAndReplyWithResult(
      task_runner_.get(),
      FROM_HERE,
      base::Bind(&ReadInternal,
                 scoped_refptr<IOBuffer>(buf),
                 buf_length,
                 BytesRemaining(),
                 file_stream_ptr),
      base::Bind(&UploadFileElementReader::OnReadCompleted,
                 weak_ptr_factory_.GetWeakPtr(),
                 base::Passed(&file_stream_),
                 callback));
  DCHECK(posted);
  return ERR_IO_PENDING;
}

void UploadFileElementReader::Reset() {
  weak_ptr_factory_.InvalidateWeakPtrs();
  bytes_remaining_ = 0;
  content_length_ = 0;
  file_stream_.reset();
}

void UploadFileElementReader::OnInitCompleted(
    ScopedFileStreamPtr* file_stream,
    uint64* content_length,
    const CompletionCallback& callback,
    int result) {
  file_stream_.swap(*file_stream);
  content_length_ = *content_length;
  bytes_remaining_ = GetContentLength();
  if (!callback.is_null())
    callback.Run(result);
}

void UploadFileElementReader::OnReadCompleted(
    ScopedFileStreamPtr file_stream,
    const CompletionCallback& callback,
    int result) {
  file_stream_.swap(file_stream);
  if (result > 0) {
    DCHECK_GE(bytes_remaining_, static_cast<uint64>(result));
    bytes_remaining_ -= result;
  }
  if (!callback.is_null())
    callback.Run(result);
}

UploadFileElementReader::ScopedOverridingContentLengthForTests::
ScopedOverridingContentLengthForTests(uint64 value) {
  overriding_content_length = value;
}

UploadFileElementReader::ScopedOverridingContentLengthForTests::
~ScopedOverridingContentLengthForTests() {
  overriding_content_length = 0;
}

UploadFileElementReaderSync::UploadFileElementReaderSync(
    const base::FilePath& path,
    uint64 range_offset,
    uint64 range_length,
    const base::Time& expected_modification_time)
    : path_(path),
      range_offset_(range_offset),
      range_length_(range_length),
      expected_modification_time_(expected_modification_time),
      content_length_(0),
      bytes_remaining_(0) {
}

UploadFileElementReaderSync::~UploadFileElementReaderSync() {
}

int UploadFileElementReaderSync::Init(const CompletionCallback& callback) {
  bytes_remaining_ = 0;
  content_length_ = 0;
  file_stream_.reset();

  const int result = InitInternal(path_, range_offset_, range_length_,
                                  expected_modification_time_,
                                  &file_stream_, &content_length_);
  bytes_remaining_ = GetContentLength();
  return result;
}

uint64 UploadFileElementReaderSync::GetContentLength() const {
  return content_length_;
}

uint64 UploadFileElementReaderSync::BytesRemaining() const {
  return bytes_remaining_;
}

int UploadFileElementReaderSync::Read(IOBuffer* buf,
                                      int buf_length,
                                      const CompletionCallback& callback) {
  const int result = ReadInternal(buf, buf_length, BytesRemaining(),
                                  file_stream_.get());
  if (result > 0) {
    DCHECK_GE(bytes_remaining_, static_cast<uint64>(result));
    bytes_remaining_ -= result;
  }
  return result;
}

}  // namespace net