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 "content/public/browser/browser_thread.h" | |
11 #include "net/base/net_errors.h" | |
12 #include "url/gurl.h" | |
13 | |
14 using content::BrowserThread; | |
15 | |
16 namespace arc { | |
17 | |
18 ArcDocumentsProviderFileStreamReader::ArcDocumentsProviderFileStreamReader( | |
19 int64_t offset) | |
20 : offset_(offset), resolved_(false), weak_ptr_factory_(this) { | |
21 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
22 } | |
23 | |
24 ArcDocumentsProviderFileStreamReader::~ArcDocumentsProviderFileStreamReader() { | |
25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
26 } | |
27 | |
28 void ArcDocumentsProviderFileStreamReader::SetContentUrl( | |
29 const GURL& content_url) { | |
30 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
31 DCHECK(!resolved_); | |
32 | |
33 if (content_url.is_valid()) { | |
34 underlying_reader_ = base::MakeUnique<ArcContentFileSystemFileStreamReader>( | |
35 content_url, offset_); | |
36 } | |
37 resolved_ = true; | |
38 | |
39 for (const base::Closure& callback : pending_operations_) { | |
40 callback.Run(); | |
41 } | |
42 pending_operations_.clear(); | |
hashimoto
2017/01/05 09:21:22
How about std::move'ing pending_operations_ to a t
Shuhei Takahashi
2017/01/05 11:08:28
Well, since |resolved_| = true at this moment, fur
| |
43 } | |
44 | |
45 base::WeakPtr<ArcDocumentsProviderFileStreamReader> | |
46 ArcDocumentsProviderFileStreamReader::GetWeakPtr() { | |
47 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
48 return weak_ptr_factory_.GetWeakPtr(); | |
49 } | |
50 | |
51 int ArcDocumentsProviderFileStreamReader::Read( | |
52 net::IOBuffer* buffer, | |
53 int buffer_length, | |
54 const net::CompletionCallback& callback) { | |
55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
56 if (!resolved_) { | |
57 pending_operations_.emplace_back(base::Bind( | |
58 base::IgnoreResult(&ArcDocumentsProviderFileStreamReader::Read), | |
hashimoto
2017/01/05 09:21:22
IgnoreResult here feels a bit dangerous to me.
ATM
Shuhei Takahashi
2017/01/05 11:08:28
True. I added proper handling.
| |
59 weak_ptr_factory_.GetWeakPtr(), buffer, buffer_length, callback)); | |
hashimoto
2017/01/05 09:21:22
Without holding a refptr, this buffer may die befo
Shuhei Takahashi
2017/01/05 11:08:28
Good catch!
| |
60 return net::ERR_IO_PENDING; | |
61 } | |
62 if (!underlying_reader_) | |
63 return net::ERR_FILE_NOT_FOUND; | |
64 return underlying_reader_->Read(buffer, buffer_length, callback); | |
65 } | |
66 | |
67 int64_t ArcDocumentsProviderFileStreamReader::GetLength( | |
68 const net::Int64CompletionCallback& callback) { | |
69 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
70 if (!resolved_) { | |
71 pending_operations_.emplace_back(base::Bind( | |
72 base::IgnoreResult(&ArcDocumentsProviderFileStreamReader::GetLength), | |
73 weak_ptr_factory_.GetWeakPtr(), callback)); | |
74 return net::ERR_IO_PENDING; | |
75 } | |
76 if (!underlying_reader_) | |
77 return net::ERR_FILE_NOT_FOUND; | |
78 return underlying_reader_->GetLength(callback); | |
79 } | |
80 | |
81 } // namespace arc | |
OLD | NEW |