Fix some potential after free errors on TestCompletionCallback
[chromium-blink-merge.git] / media / filters / file_data_source.cc
blob2f34718a47f684975e4ff192884d822ae1b9f2ac
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/file_data_source.h"
7 #include <algorithm>
9 #include "base/logging.h"
11 namespace media {
13 FileDataSource::FileDataSource()
14 : force_read_errors_(false),
15 force_streaming_(false) {
18 bool FileDataSource::Initialize(const base::FilePath& file_path) {
19 DCHECK(!file_.IsValid());
21 if (!file_.Initialize(file_path))
22 return false;
24 UpdateHostBytes();
25 return true;
28 void FileDataSource::set_host(DataSourceHost* host) {
29 DataSource::set_host(host);
30 UpdateHostBytes();
33 void FileDataSource::Stop(const base::Closure& callback) {
34 callback.Run();
37 void FileDataSource::Read(int64 position, int size, uint8* data,
38 const DataSource::ReadCB& read_cb) {
39 if (force_read_errors_ || !file_.IsValid()) {
40 read_cb.Run(kReadError);
41 return;
44 int64 file_size = file_.length();
46 CHECK_GE(file_size, 0);
47 CHECK_GE(position, 0);
48 CHECK_GE(size, 0);
50 // Cap position and size within bounds.
51 position = std::min(position, file_size);
52 int64 clamped_size = std::min(static_cast<int64>(size), file_size - position);
54 memcpy(data, file_.data() + position, clamped_size);
55 read_cb.Run(clamped_size);
58 bool FileDataSource::GetSize(int64* size_out) {
59 *size_out = file_.length();
60 return true;
63 bool FileDataSource::IsStreaming() {
64 return force_streaming_;
67 void FileDataSource::SetBitrate(int bitrate) {}
69 FileDataSource::~FileDataSource() {}
71 void FileDataSource::UpdateHostBytes() {
72 if (host() && file_.IsValid()) {
73 host()->SetTotalBytes(file_.length());
74 host()->AddBufferedByteRange(0, file_.length());
78 } // namespace media