OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include <cstdio> |
| 6 |
| 7 #include "base/files/file_util.h" |
| 8 #include "services/media/framework/parts/file_reader.h" |
| 9 #include "url/gurl.h" |
| 10 |
| 11 namespace mojo { |
| 12 namespace media { |
| 13 |
| 14 FileReader::~FileReader() { |
| 15 if (file_ != nullptr) { |
| 16 fclose(file_); |
| 17 file_ = nullptr; |
| 18 } |
| 19 } |
| 20 |
| 21 Result FileReader::Init(const GURL& gurl) { |
| 22 // TODO(dalesat): Assumes the authority is localhost. |
| 23 std::string path = gurl.path(); |
| 24 file_ = base::OpenFile(base::FilePath(path), "rb"); |
| 25 if (file_ == nullptr) { |
| 26 return Result::kNotFound; |
| 27 } |
| 28 |
| 29 if (fseek(file_, 0, SEEK_END) == 0) { |
| 30 size_ = ftell(file_); |
| 31 if (fseek(file_, 0, SEEK_SET) < 0) { |
| 32 fclose(file_); |
| 33 file_ = nullptr; |
| 34 return Result::kUnsupportedOperation; |
| 35 } |
| 36 } else { |
| 37 size_ = -1; |
| 38 } |
| 39 |
| 40 return Result::kOk; |
| 41 } |
| 42 |
| 43 size_t FileReader::Read(uint8* buffer, int bytes_to_read) { |
| 44 return fread(buffer, 1, bytes_to_read, file_); |
| 45 } |
| 46 |
| 47 int64_t FileReader::GetPosition() const { |
| 48 return ftell(file_); |
| 49 } |
| 50 |
| 51 int64_t FileReader::SetPosition(int64 position) { |
| 52 if (fseek(file_, position, SEEK_SET) < 0) { |
| 53 return -1; |
| 54 } |
| 55 return position; |
| 56 } |
| 57 |
| 58 size_t FileReader::GetSize() const { |
| 59 return size_; |
| 60 } |
| 61 |
| 62 bool FileReader::CanSeek() const { |
| 63 return true; |
| 64 } |
| 65 |
| 66 } // namespace media |
| 67 } // namespace mojo |
OLD | NEW |