OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/arc/fileapi/arc_deferred_file_system_operation _runner.h" | 5 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner.h " |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/optional.h" | 9 #include "base/optional.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
11 #include "components/arc/arc_bridge_service.h" | 11 #include "components/arc/arc_bridge_service.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
14 | 14 |
15 using content::BrowserThread; | 15 using content::BrowserThread; |
16 | 16 |
17 namespace arc { | 17 namespace arc { |
18 | 18 |
19 ArcDeferredFileSystemOperationRunner::ArcDeferredFileSystemOperationRunner( | 19 // static |
20 const char ArcFileSystemOperationRunner::kArcServiceName[] = | |
21 "arc::ArcFileSystemOperationRunner"; | |
22 | |
23 // static | |
24 ArcFileSystemOperationRunner* ArcFileSystemOperationRunner::CreateForTesting( | |
25 ArcBridgeService* bridge_service) { | |
26 return new ArcFileSystemOperationRunner(bridge_service, false); | |
hidehiko
2017/01/26 07:24:20
So, corresponding to the header's comment, this ca
Shuhei Takahashi
2017/01/27 09:55:10
Done.
| |
27 } | |
28 | |
29 ArcFileSystemOperationRunner::ArcFileSystemOperationRunner( | |
20 ArcBridgeService* bridge_service) | 30 ArcBridgeService* bridge_service) |
21 : ArcDeferredFileSystemOperationRunner(bridge_service, true) {} | 31 : ArcFileSystemOperationRunner(bridge_service, true) {} |
22 | 32 |
23 ArcDeferredFileSystemOperationRunner::ArcDeferredFileSystemOperationRunner( | 33 ArcFileSystemOperationRunner::ArcFileSystemOperationRunner( |
24 ArcBridgeService* bridge_service, | 34 ArcBridgeService* bridge_service, |
25 bool observe_events) | 35 bool observe_events) |
26 : ArcFileSystemOperationRunner(bridge_service), | 36 : ArcService(bridge_service), |
27 observe_events_(observe_events), | 37 observe_events_(observe_events), |
28 weak_ptr_factory_(this) { | 38 weak_ptr_factory_(this) { |
29 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 39 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
30 | 40 |
31 if (observe_events_) { | 41 if (observe_events_) { |
32 ArcSessionManager::Get()->AddObserver(this); | 42 ArcSessionManager::Get()->AddObserver(this); |
33 arc_bridge_service()->file_system()->AddObserver(this); | 43 arc_bridge_service()->file_system()->AddObserver(this); |
34 OnStateChanged(); | 44 OnStateChanged(); |
35 } | 45 } |
36 } | 46 } |
37 | 47 |
38 ArcDeferredFileSystemOperationRunner::~ArcDeferredFileSystemOperationRunner() { | 48 ArcFileSystemOperationRunner::~ArcFileSystemOperationRunner() { |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
40 | 50 |
41 if (observe_events_) { | 51 if (observe_events_) { |
42 ArcSessionManager::Get()->RemoveObserver(this); | 52 ArcSessionManager::Get()->RemoveObserver(this); |
43 arc_bridge_service()->file_system()->RemoveObserver(this); | 53 arc_bridge_service()->file_system()->RemoveObserver(this); |
44 } | 54 } |
45 // On destruction, deferred operations are discarded. | 55 // On destruction, deferred operations are discarded. |
46 } | 56 } |
47 | 57 |
48 void ArcDeferredFileSystemOperationRunner::GetFileSize( | 58 void ArcFileSystemOperationRunner::GetFileSize( |
49 const GURL& url, | 59 const GURL& url, |
50 const GetFileSizeCallback& callback) { | 60 const GetFileSizeCallback& callback) { |
51 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
52 if (should_defer_) { | 62 if (should_defer_) { |
53 deferred_operations_.emplace_back( | 63 deferred_operations_.emplace_back( |
54 base::Bind(&ArcDeferredFileSystemOperationRunner::GetFileSize, | 64 base::Bind(&ArcFileSystemOperationRunner::GetFileSize, |
55 weak_ptr_factory_.GetWeakPtr(), url, callback)); | 65 weak_ptr_factory_.GetWeakPtr(), url, callback)); |
56 return; | 66 return; |
57 } | 67 } |
58 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | 68 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
59 arc_bridge_service()->file_system(), GetFileSize); | 69 arc_bridge_service()->file_system(), GetFileSize); |
60 if (!file_system_instance) { | 70 if (!file_system_instance) { |
61 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 71 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
62 base::Bind(callback, -1)); | 72 base::Bind(callback, -1)); |
63 return; | 73 return; |
64 } | 74 } |
65 file_system_instance->GetFileSize(url.spec(), callback); | 75 file_system_instance->GetFileSize(url.spec(), callback); |
66 } | 76 } |
67 | 77 |
68 void ArcDeferredFileSystemOperationRunner::OpenFileToRead( | 78 void ArcFileSystemOperationRunner::OpenFileToRead( |
69 const GURL& url, | 79 const GURL& url, |
70 const OpenFileToReadCallback& callback) { | 80 const OpenFileToReadCallback& callback) { |
71 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 81 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
72 if (should_defer_) { | 82 if (should_defer_) { |
73 deferred_operations_.emplace_back( | 83 deferred_operations_.emplace_back( |
74 base::Bind(&ArcDeferredFileSystemOperationRunner::OpenFileToRead, | 84 base::Bind(&ArcFileSystemOperationRunner::OpenFileToRead, |
75 weak_ptr_factory_.GetWeakPtr(), url, callback)); | 85 weak_ptr_factory_.GetWeakPtr(), url, callback)); |
76 return; | 86 return; |
77 } | 87 } |
78 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | 88 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
79 arc_bridge_service()->file_system(), OpenFileToRead); | 89 arc_bridge_service()->file_system(), OpenFileToRead); |
80 if (!file_system_instance) { | 90 if (!file_system_instance) { |
81 base::ThreadTaskRunnerHandle::Get()->PostTask( | 91 base::ThreadTaskRunnerHandle::Get()->PostTask( |
82 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); | 92 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); |
83 return; | 93 return; |
84 } | 94 } |
85 file_system_instance->OpenFileToRead(url.spec(), callback); | 95 file_system_instance->OpenFileToRead(url.spec(), callback); |
86 } | 96 } |
87 | 97 |
88 void ArcDeferredFileSystemOperationRunner::GetDocument( | 98 void ArcFileSystemOperationRunner::GetDocument( |
89 const std::string& authority, | 99 const std::string& authority, |
90 const std::string& document_id, | 100 const std::string& document_id, |
91 const GetDocumentCallback& callback) { | 101 const GetDocumentCallback& callback) { |
92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 102 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
93 if (should_defer_) { | 103 if (should_defer_) { |
94 deferred_operations_.emplace_back(base::Bind( | 104 deferred_operations_.emplace_back(base::Bind( |
95 &ArcDeferredFileSystemOperationRunner::GetDocument, | 105 &ArcFileSystemOperationRunner::GetDocument, |
96 weak_ptr_factory_.GetWeakPtr(), authority, document_id, callback)); | 106 weak_ptr_factory_.GetWeakPtr(), authority, document_id, callback)); |
97 return; | 107 return; |
98 } | 108 } |
99 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | 109 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
100 arc_bridge_service()->file_system(), GetDocument); | 110 arc_bridge_service()->file_system(), GetDocument); |
101 if (!file_system_instance) { | 111 if (!file_system_instance) { |
102 base::ThreadTaskRunnerHandle::Get()->PostTask( | 112 base::ThreadTaskRunnerHandle::Get()->PostTask( |
103 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); | 113 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); |
104 return; | 114 return; |
105 } | 115 } |
106 file_system_instance->GetDocument(authority, document_id, callback); | 116 file_system_instance->GetDocument(authority, document_id, callback); |
107 } | 117 } |
108 | 118 |
109 void ArcDeferredFileSystemOperationRunner::GetChildDocuments( | 119 void ArcFileSystemOperationRunner::GetChildDocuments( |
110 const std::string& authority, | 120 const std::string& authority, |
111 const std::string& parent_document_id, | 121 const std::string& parent_document_id, |
112 const GetChildDocumentsCallback& callback) { | 122 const GetChildDocumentsCallback& callback) { |
113 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 123 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
114 if (should_defer_) { | 124 if (should_defer_) { |
115 deferred_operations_.emplace_back( | 125 deferred_operations_.emplace_back( |
116 base::Bind(&ArcDeferredFileSystemOperationRunner::GetChildDocuments, | 126 base::Bind(&ArcFileSystemOperationRunner::GetChildDocuments, |
117 weak_ptr_factory_.GetWeakPtr(), authority, | 127 weak_ptr_factory_.GetWeakPtr(), authority, |
118 parent_document_id, callback)); | 128 parent_document_id, callback)); |
119 return; | 129 return; |
120 } | 130 } |
121 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | 131 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
122 arc_bridge_service()->file_system(), GetChildDocuments); | 132 arc_bridge_service()->file_system(), GetChildDocuments); |
123 if (!file_system_instance) { | 133 if (!file_system_instance) { |
124 base::ThreadTaskRunnerHandle::Get()->PostTask( | 134 base::ThreadTaskRunnerHandle::Get()->PostTask( |
125 FROM_HERE, base::Bind(callback, base::nullopt)); | 135 FROM_HERE, base::Bind(callback, base::nullopt)); |
126 return; | 136 return; |
127 } | 137 } |
128 file_system_instance->GetChildDocuments(authority, parent_document_id, | 138 file_system_instance->GetChildDocuments(authority, parent_document_id, |
129 callback); | 139 callback); |
130 } | 140 } |
131 | 141 |
132 void ArcDeferredFileSystemOperationRunner::OnArcOptInChanged(bool enabled) { | 142 void ArcFileSystemOperationRunner::OnArcOptInChanged(bool enabled) { |
133 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 143 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
134 OnStateChanged(); | 144 OnStateChanged(); |
135 } | 145 } |
136 | 146 |
137 void ArcDeferredFileSystemOperationRunner::OnInstanceReady() { | 147 void ArcFileSystemOperationRunner::OnInstanceReady() { |
138 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 148 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
139 OnStateChanged(); | 149 OnStateChanged(); |
140 } | 150 } |
141 | 151 |
142 void ArcDeferredFileSystemOperationRunner::OnInstanceClosed() { | 152 void ArcFileSystemOperationRunner::OnInstanceClosed() { |
143 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
144 OnStateChanged(); | 154 OnStateChanged(); |
145 } | 155 } |
146 | 156 |
147 void ArcDeferredFileSystemOperationRunner::OnStateChanged() { | 157 void ArcFileSystemOperationRunner::OnStateChanged() { |
148 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 158 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
149 SetShouldDefer(ArcSessionManager::Get()->IsArcEnabled() && | 159 SetShouldDefer(ArcSessionManager::Get()->IsArcEnabled() && |
150 !arc_bridge_service()->file_system()->has_instance()); | 160 !arc_bridge_service()->file_system()->has_instance()); |
151 } | 161 } |
152 | 162 |
153 void ArcDeferredFileSystemOperationRunner::SetShouldDefer(bool should_defer) { | 163 void ArcFileSystemOperationRunner::SetShouldDefer(bool should_defer) { |
154 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 164 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
155 | 165 |
156 should_defer_ = should_defer; | 166 should_defer_ = should_defer; |
157 | 167 |
158 if (should_defer_) | 168 if (should_defer_) |
159 return; | 169 return; |
160 | 170 |
161 // Run deferred operations. | 171 // Run deferred operations. |
162 std::vector<base::Closure> deferred_operations; | 172 std::vector<base::Closure> deferred_operations; |
163 deferred_operations.swap(deferred_operations_); | 173 deferred_operations.swap(deferred_operations_); |
164 for (const base::Closure& operation : deferred_operations) { | 174 for (const base::Closure& operation : deferred_operations) { |
165 operation.Run(); | 175 operation.Run(); |
166 } | 176 } |
167 | 177 |
168 // No deferred operations should be left at this point. | 178 // No deferred operations should be left at this point. |
169 DCHECK(deferred_operations_.empty()); | 179 DCHECK(deferred_operations_.empty()); |
170 } | 180 } |
171 | 181 |
172 } // namespace arc | 182 } // namespace arc |
OLD | NEW |