OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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_deferred_file_system_operation _runner.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/optional.h" | |
9 #include "components/arc/arc_bridge_service.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "url/gurl.h" | |
12 | |
13 using content::BrowserThread; | |
14 | |
15 namespace arc { | |
16 | |
17 ArcDeferredFileSystemOperationRunner::ArcDeferredFileSystemOperationRunner( | |
18 ArcBridgeService* bridge_service) | |
19 : ArcDeferredFileSystemOperationRunner(bridge_service, true) {} | |
20 | |
21 ArcDeferredFileSystemOperationRunner::ArcDeferredFileSystemOperationRunner( | |
22 ArcBridgeService* bridge_service, | |
23 bool observe_events) | |
24 : ArcFileSystemOperationRunner(bridge_service), | |
25 observe_events_(observe_events), | |
26 weak_ptr_factory_(this) { | |
27 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
28 | |
29 if (observe_events_) { | |
30 ArcSessionManager::Get()->AddObserver(this); | |
31 arc_bridge_service()->file_system()->AddObserver(this); | |
32 OnStateChanged(); | |
33 } | |
34 } | |
35 | |
36 ArcDeferredFileSystemOperationRunner::~ArcDeferredFileSystemOperationRunner() { | |
37 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
38 | |
39 if (observe_events_) { | |
40 ArcSessionManager::Get()->RemoveObserver(this); | |
41 arc_bridge_service()->file_system()->RemoveObserver(this); | |
42 } | |
43 // On destruction, deferred operations are discarded. | |
44 } | |
45 | |
46 void ArcDeferredFileSystemOperationRunner::GetFileSize( | |
47 const GURL& url, | |
48 const GetFileSizeCallback& callback) { | |
49 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
50 if (should_defer_) { | |
51 deferred_operations_.emplace_back( | |
52 base::Bind(&ArcDeferredFileSystemOperationRunner::GetFileSize, | |
53 weak_ptr_factory_.GetWeakPtr(), url, callback)); | |
54 return; | |
55 } | |
56 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
57 arc_bridge_service()->file_system(), GetFileSize); | |
58 if (!file_system_instance) { | |
59 callback.Run(-1); | |
hashimoto
2017/01/23 08:28:42
Could you run the callback asynchronously here, us
Shuhei Takahashi
2017/01/23 09:17:51
Done.
| |
60 return; | |
61 } | |
62 file_system_instance->GetFileSize(url.spec(), callback); | |
63 } | |
64 | |
65 void ArcDeferredFileSystemOperationRunner::OpenFileToRead( | |
66 const GURL& url, | |
67 const OpenFileToReadCallback& callback) { | |
68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
69 if (should_defer_) { | |
70 deferred_operations_.emplace_back( | |
71 base::Bind(&ArcDeferredFileSystemOperationRunner::OpenFileToRead, | |
72 weak_ptr_factory_.GetWeakPtr(), url, callback)); | |
73 return; | |
74 } | |
75 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
76 arc_bridge_service()->file_system(), OpenFileToRead); | |
77 if (!file_system_instance) { | |
78 callback.Run(mojo::ScopedHandle()); | |
79 return; | |
80 } | |
81 file_system_instance->OpenFileToRead(url.spec(), callback); | |
82 } | |
83 | |
84 void ArcDeferredFileSystemOperationRunner::GetDocument( | |
85 const std::string& authority, | |
86 const std::string& document_id, | |
87 const GetDocumentCallback& callback) { | |
88 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
89 if (should_defer_) { | |
90 deferred_operations_.emplace_back(base::Bind( | |
91 &ArcDeferredFileSystemOperationRunner::GetDocument, | |
92 weak_ptr_factory_.GetWeakPtr(), authority, document_id, callback)); | |
93 return; | |
94 } | |
95 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
96 arc_bridge_service()->file_system(), GetDocument); | |
97 if (!file_system_instance) { | |
98 callback.Run(mojom::DocumentPtr()); | |
99 return; | |
100 } | |
101 file_system_instance->GetDocument(authority, document_id, callback); | |
102 } | |
103 | |
104 void ArcDeferredFileSystemOperationRunner::GetChildDocuments( | |
105 const std::string& authority, | |
106 const std::string& parent_document_id, | |
107 const GetChildDocumentsCallback& callback) { | |
108 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
109 if (should_defer_) { | |
110 deferred_operations_.emplace_back( | |
111 base::Bind(&ArcDeferredFileSystemOperationRunner::GetChildDocuments, | |
112 weak_ptr_factory_.GetWeakPtr(), authority, | |
113 parent_document_id, callback)); | |
114 return; | |
115 } | |
116 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
117 arc_bridge_service()->file_system(), GetChildDocuments); | |
118 if (!file_system_instance) { | |
119 callback.Run(base::nullopt); | |
120 return; | |
121 } | |
122 file_system_instance->GetChildDocuments(authority, parent_document_id, | |
123 callback); | |
124 } | |
125 | |
126 void ArcDeferredFileSystemOperationRunner::OnArcOptInChanged(bool enabled) { | |
127 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
128 OnStateChanged(); | |
129 } | |
130 | |
131 void ArcDeferredFileSystemOperationRunner::OnInstanceReady() { | |
132 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
133 OnStateChanged(); | |
134 } | |
135 | |
136 void ArcDeferredFileSystemOperationRunner::OnInstanceClosed() { | |
137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
138 OnStateChanged(); | |
139 } | |
140 | |
141 void ArcDeferredFileSystemOperationRunner::OnStateChanged() { | |
142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
143 SetShouldDefer(ArcSessionManager::Get()->IsArcEnabled() && | |
144 !arc_bridge_service()->file_system()->has_instance()); | |
145 } | |
146 | |
147 void ArcDeferredFileSystemOperationRunner::SetShouldDefer(bool should_defer) { | |
148 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
149 | |
150 should_defer_ = should_defer; | |
151 | |
152 if (should_defer_) | |
153 return; | |
154 | |
155 // Run deferred operations. | |
156 std::vector<base::Closure> deferred_operations; | |
157 deferred_operations.swap(deferred_operations_); | |
158 for (const base::Closure& operation : deferred_operations) { | |
159 operation.Run(); | |
160 } | |
161 | |
162 // No deferred operations should be left at this point. | |
163 DCHECK(deferred_operations_.empty()); | |
164 } | |
165 | |
166 } // namespace arc | |
OLD | NEW |