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 "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_file_stream
_reader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "chrome/browser/chromeos/arc/fileapi/arc_content_file_system_file_strea
m_reader.h" |
| 10 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root.h" |
| 11 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root_map.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 using content::BrowserThread; |
| 18 |
| 19 namespace arc { |
| 20 |
| 21 ArcDocumentsProviderFileStreamReader::ArcDocumentsProviderFileStreamReader( |
| 22 const storage::FileSystemURL& url, |
| 23 int64_t offset, |
| 24 ArcDocumentsProviderRootMap* roots) |
| 25 : offset_(offset), content_url_resolved_(false), weak_ptr_factory_(this) { |
| 26 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 27 |
| 28 base::FilePath path; |
| 29 ArcDocumentsProviderRoot* root = roots->ParseAndLookup(url, &path); |
| 30 if (!root) { |
| 31 content_url_resolved_ = true; |
| 32 return; |
| 33 } |
| 34 root->ResolveToContentUrl( |
| 35 path, |
| 36 base::Bind(&ArcDocumentsProviderFileStreamReader::OnResolveToContentUrl, |
| 37 weak_ptr_factory_.GetWeakPtr())); |
| 38 } |
| 39 |
| 40 ArcDocumentsProviderFileStreamReader::~ArcDocumentsProviderFileStreamReader() { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 42 } |
| 43 |
| 44 int ArcDocumentsProviderFileStreamReader::Read( |
| 45 net::IOBuffer* buffer, |
| 46 int buffer_length, |
| 47 const net::CompletionCallback& callback) { |
| 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 49 if (!content_url_resolved_) { |
| 50 pending_operations_.emplace_back(base::Bind( |
| 51 &ArcDocumentsProviderFileStreamReader::RunPendingRead, |
| 52 base::Unretained(this), base::Passed(make_scoped_refptr(buffer)), |
| 53 buffer_length, callback)); |
| 54 return net::ERR_IO_PENDING; |
| 55 } |
| 56 if (!underlying_reader_) |
| 57 return net::ERR_FILE_NOT_FOUND; |
| 58 return underlying_reader_->Read(buffer, buffer_length, callback); |
| 59 } |
| 60 |
| 61 int64_t ArcDocumentsProviderFileStreamReader::GetLength( |
| 62 const net::Int64CompletionCallback& callback) { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 64 if (!content_url_resolved_) { |
| 65 pending_operations_.emplace_back( |
| 66 base::Bind(&ArcDocumentsProviderFileStreamReader::RunPendingGetLength, |
| 67 base::Unretained(this), callback)); |
| 68 return net::ERR_IO_PENDING; |
| 69 } |
| 70 if (!underlying_reader_) |
| 71 return net::ERR_FILE_NOT_FOUND; |
| 72 return underlying_reader_->GetLength(callback); |
| 73 } |
| 74 |
| 75 void ArcDocumentsProviderFileStreamReader::OnResolveToContentUrl( |
| 76 const GURL& content_url) { |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 78 DCHECK(!content_url_resolved_); |
| 79 |
| 80 if (content_url.is_valid()) { |
| 81 underlying_reader_ = base::MakeUnique<ArcContentFileSystemFileStreamReader>( |
| 82 content_url, offset_); |
| 83 } |
| 84 content_url_resolved_ = true; |
| 85 |
| 86 std::vector<base::Closure> pending_operations; |
| 87 pending_operations.swap(pending_operations_); |
| 88 for (const base::Closure& callback : pending_operations) { |
| 89 callback.Run(); |
| 90 } |
| 91 } |
| 92 |
| 93 void ArcDocumentsProviderFileStreamReader::RunPendingRead( |
| 94 scoped_refptr<net::IOBuffer> buffer, |
| 95 int buffer_length, |
| 96 const net::CompletionCallback& callback) { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 98 DCHECK(content_url_resolved_); |
| 99 int result = |
| 100 underlying_reader_ |
| 101 ? underlying_reader_->Read(buffer.get(), buffer_length, callback) |
| 102 : net::ERR_FILE_NOT_FOUND; |
| 103 if (result != net::ERR_IO_PENDING) |
| 104 callback.Run(result); |
| 105 } |
| 106 |
| 107 void ArcDocumentsProviderFileStreamReader::RunPendingGetLength( |
| 108 const net::Int64CompletionCallback& callback) { |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 110 DCHECK(content_url_resolved_); |
| 111 int64_t result = underlying_reader_ ? underlying_reader_->GetLength(callback) |
| 112 : net::ERR_FILE_NOT_FOUND; |
| 113 if (result != net::ERR_IO_PENDING) |
| 114 callback.Run(result); |
| 115 } |
| 116 |
| 117 } // namespace arc |
OLD | NEW |