OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "webkit/browser/blob/content_url_stream_reader_android.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/files/file_util_proxy.h" |
| 9 #include "base/location.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/platform_file.h" |
| 12 #include "base/task_runner.h" |
| 13 #include "net/android/content_uri_utils.h" |
| 14 #include "net/base/file_stream.h" |
| 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" |
| 17 |
| 18 namespace webkit_blob { |
| 19 |
| 20 const int kOpenFlagsForRead = base::PLATFORM_FILE_OPEN | |
| 21 base::PLATFORM_FILE_READ | |
| 22 base::PLATFORM_FILE_ASYNC; |
| 23 |
| 24 FileStreamReader* FileStreamReader::CreateForContentUrl( |
| 25 base::TaskRunner* task_runner, |
| 26 const GURL& content_url, |
| 27 int64 initial_offset) { |
| 28 return new ContentUrlStreamReader(task_runner, content_url, initial_offset); |
| 29 } |
| 30 |
| 31 ContentUrlStreamReader::ContentUrlStreamReader( |
| 32 base::TaskRunner* task_runner, |
| 33 const GURL& content_url, |
| 34 int64 initial_offset) |
| 35 : task_runner_(task_runner), |
| 36 content_url_(content_url), |
| 37 initial_offset_(initial_offset), |
| 38 weak_factory_(this) {} |
| 39 |
| 40 ContentUrlStreamReader::~ContentUrlStreamReader() {} |
| 41 |
| 42 int64 ContentUrlStreamReader::GetLength( |
| 43 const net::Int64CompletionCallback& callback) { |
| 44 net::GetContentUrlLength(task_runner_.get(), content_url_, callback); |
| 45 return net::ERR_IO_PENDING; |
| 46 } |
| 47 |
| 48 int ContentUrlStreamReader::Read(net::IOBuffer* buf, int buf_len, |
| 49 const net::CompletionCallback& callback) { |
| 50 if (stream_impl_) |
| 51 return stream_impl_->Read(buf, buf_len, callback); |
| 52 return Open(base::Bind(&ContentUrlStreamReader::DidOpenForRead, |
| 53 weak_factory_.GetWeakPtr(), |
| 54 make_scoped_refptr(buf), buf_len, callback)); |
| 55 } |
| 56 |
| 57 int ContentUrlStreamReader::Open(const net::CompletionCallback& callback) { |
| 58 DCHECK(!stream_impl_); |
| 59 stream_impl_.reset(new net::FileStream(NULL, task_runner_)); |
| 60 const int result = stream_impl_->OpenContentUrl( |
| 61 content_url_, kOpenFlagsForRead, |
| 62 base::Bind(&ContentUrlStreamReader::DidOpenContentUrl, |
| 63 weak_factory_.GetWeakPtr(), |
| 64 callback)); |
| 65 return result; |
| 66 } |
| 67 |
| 68 void ContentUrlStreamReader::DidOpenContentUrl( |
| 69 const net::CompletionCallback& callback, |
| 70 int result) { |
| 71 if (result != net::OK) { |
| 72 callback.Run(result); |
| 73 return; |
| 74 } |
| 75 |
| 76 result = stream_impl_->Seek( |
| 77 net::FROM_BEGIN, initial_offset_, |
| 78 base::Bind(&ContentUrlStreamReader::DidSeekContentUrl, |
| 79 weak_factory_.GetWeakPtr(), |
| 80 callback)); |
| 81 if (result != net::ERR_IO_PENDING) |
| 82 callback.Run(result); |
| 83 } |
| 84 |
| 85 void ContentUrlStreamReader::DidOpenForRead( |
| 86 net::IOBuffer* buf, |
| 87 int buf_len, |
| 88 const net::CompletionCallback& callback, |
| 89 int open_result) { |
| 90 if (open_result != net::OK) { |
| 91 stream_impl_.reset(); |
| 92 callback.Run(open_result); |
| 93 return; |
| 94 } |
| 95 const int read_result = stream_impl_->Read(buf, buf_len, callback); |
| 96 if (read_result != net::ERR_IO_PENDING) |
| 97 callback.Run(read_result); |
| 98 } |
| 99 |
| 100 void ContentUrlStreamReader::DidSeekContentUrl( |
| 101 const net::CompletionCallback& callback, |
| 102 int64 seek_result) { |
| 103 if (seek_result < 0) { |
| 104 callback.Run(static_cast<int>(seek_result)); |
| 105 return; |
| 106 } |
| 107 if (seek_result != initial_offset_) { |
| 108 callback.Run(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); |
| 109 return; |
| 110 } |
| 111 callback.Run(net::OK); |
| 112 } |
| 113 |
| 114 } // namespace webkit_blob |
OLD | NEW |