OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 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/fileapi/syncable/sync_file_system_backend.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/metrics/histogram.h" | |
9 #include "webkit/browser/fileapi/async_file_util_adapter.h" | |
10 #include "webkit/browser/fileapi/file_system_context.h" | |
11 #include "webkit/browser/fileapi/obfuscated_file_util.h" | |
12 #include "webkit/browser/fileapi/sandbox_quota_observer.h" | |
13 #include "webkit/browser/fileapi/syncable/syncable_file_system_operation.h" | |
14 #include "webkit/browser/fileapi/syncable/syncable_file_system_util.h" | |
15 #include "webkit/common/fileapi/file_system_util.h" | |
16 | |
17 namespace sync_file_system { | |
18 | |
19 namespace { | |
20 const char kSyncableOriginsCountLabel[] = "FileSystem.SyncableOriginsCount"; | |
21 } // namespace | |
22 | |
23 SyncFileSystemBackend::SyncFileSystemBackend( | |
24 base::SequencedTaskRunner* file_task_runner, | |
25 const fileapi::FileSystemOptions& file_system_options) | |
26 : SandboxFileSystemBackend(NULL, file_task_runner, file_system_options) { | |
27 } | |
28 | |
29 SyncFileSystemBackend::~SyncFileSystemBackend() { | |
30 file_task_runner_->DeleteSoon(FROM_HERE, change_tracker_.release()); | |
31 } | |
32 | |
33 bool SyncFileSystemBackend::CanHandleType( | |
34 fileapi::FileSystemType type) const { | |
35 return type == fileapi::kFileSystemTypeSyncable || | |
36 type == fileapi::kFileSystemTypeSyncableForInternalSync; | |
37 } | |
38 | |
39 void SyncFileSystemBackend::InitializeFileSystem( | |
40 const GURL& origin_url, | |
41 fileapi::FileSystemType type, | |
42 fileapi::OpenFileSystemMode mode, | |
43 fileapi::FileSystemContext* context, | |
44 const InitializeFileSystemCallback& callback) { | |
45 if (!sandbox_context_) | |
46 LazyInitialize(context->sandbox_context()); | |
47 SandboxFileSystemBackend::InitializeFileSystem( | |
48 origin_url, type, mode, context, callback); | |
49 } | |
50 | |
51 fileapi::FileSystemFileUtil* SyncFileSystemBackend::GetFileUtil( | |
52 fileapi::FileSystemType type, | |
53 const fileapi::FileSystemContext* context) { | |
54 if (!sandbox_context_) | |
55 LazyInitialize(context->sandbox_context()); | |
56 return sandbox_context_->sync_file_util(); | |
kinuko
2013/07/17 15:01:30
Have you checked when these GetFileUtil() and GetA
nhiroki
2013/07/22 04:34:14
Yes, I have checked. There is possibility to call
kinuko
2013/07/22 05:11:30
1) This is happening because we try to initialize
nhiroki
2013/07/23 05:02:12
Both cases looks inevitable, so I introduced the s
| |
57 } | |
58 | |
59 fileapi::AsyncFileUtil* SyncFileSystemBackend::GetAsyncFileUtil( | |
60 fileapi::FileSystemType type, | |
61 const fileapi::FileSystemContext* context) { | |
62 if (!sandbox_context_) | |
63 LazyInitialize(context->sandbox_context()); | |
64 return sandbox_context_->file_util(); | |
65 } | |
66 | |
67 fileapi::FileSystemOperation* | |
68 SyncFileSystemBackend::CreateFileSystemOperation( | |
69 const fileapi::FileSystemURL& url, | |
70 fileapi::FileSystemContext* context, | |
71 base::PlatformFileError* error_code) const { | |
72 if (!IsAccessValid(url)) { | |
73 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; | |
74 return NULL; | |
75 } | |
76 | |
77 if (url.type() == fileapi::kFileSystemTypeSyncableForInternalSync) { | |
78 return SandboxFileSystemBackend::CreateFileSystemOperation( | |
79 url, context, error_code); | |
80 } | |
81 | |
82 scoped_ptr<fileapi::FileSystemOperationContext> operation_context( | |
83 new fileapi::FileSystemOperationContext(context)); | |
84 operation_context->set_update_observers(syncable_update_observers_); | |
85 operation_context->set_change_observers(syncable_change_observers_); | |
86 | |
87 return new SyncableFileSystemOperation( | |
88 url, context, operation_context.Pass()); | |
89 } | |
90 | |
91 void SyncFileSystemBackend::GetOriginsForTypeOnFileThread( | |
92 fileapi::FileSystemType type, | |
93 std::set<GURL>* origins) { | |
94 DCHECK(CanHandleType(type)); | |
95 DCHECK(origins); | |
96 scoped_ptr<OriginEnumerator> enumerator(CreateOriginEnumerator()); | |
97 GURL origin; | |
98 while (!(origin = enumerator->Next()).is_empty()) { | |
99 if (enumerator->HasFileSystemType(type)) | |
100 origins->insert(origin); | |
101 } | |
102 switch (type) { | |
103 case fileapi::kFileSystemTypeSyncable: | |
104 UMA_HISTOGRAM_COUNTS(kSyncableOriginsCountLabel, origins->size()); | |
105 break; | |
106 default: | |
107 break; | |
108 } | |
109 } | |
110 | |
111 void SyncFileSystemBackend::AddFileUpdateObserver( | |
112 fileapi::FileSystemType type, | |
113 fileapi::FileUpdateObserver* observer, | |
114 base::SequencedTaskRunner* task_runner) { | |
115 DCHECK(CanHandleType(type)); | |
116 if (type == fileapi::kFileSystemTypeSyncableForInternalSync) { | |
117 SandboxFileSystemBackend::AddFileUpdateObserver( | |
118 type, observer, task_runner); | |
119 return; | |
120 } | |
121 fileapi::UpdateObserverList* list = &syncable_update_observers_; | |
122 *list = list->AddObserver(observer, task_runner); | |
123 } | |
124 | |
125 void SyncFileSystemBackend::AddFileChangeObserver( | |
126 fileapi::FileSystemType type, | |
127 fileapi::FileChangeObserver* observer, | |
128 base::SequencedTaskRunner* task_runner) { | |
129 DCHECK(CanHandleType(type)); | |
130 if (type == fileapi::kFileSystemTypeSyncableForInternalSync) { | |
131 SandboxFileSystemBackend::AddFileChangeObserver( | |
132 type, observer, task_runner); | |
133 return; | |
134 } | |
135 fileapi::ChangeObserverList* list = &syncable_change_observers_; | |
136 *list = list->AddObserver(observer, task_runner); | |
137 } | |
138 | |
139 const fileapi::UpdateObserverList* SyncFileSystemBackend::GetUpdateObservers( | |
140 fileapi::FileSystemType type) const { | |
141 DCHECK(CanHandleType(type)); | |
142 if (type == fileapi::kFileSystemTypeSyncableForInternalSync) | |
143 return &update_observers_; | |
144 return &syncable_update_observers_; | |
145 } | |
146 | |
147 const fileapi::ChangeObserverList* SyncFileSystemBackend::GetChangeObservers( | |
148 fileapi::FileSystemType type) const { | |
149 DCHECK(CanHandleType(type)); | |
150 if (type == fileapi::kFileSystemTypeSyncableForInternalSync) | |
151 return &change_observers_; | |
152 return &syncable_change_observers_; | |
153 } | |
154 | |
155 // static | |
156 SyncFileSystemBackend* SyncFileSystemBackend::GetBackend( | |
157 const fileapi::FileSystemContext* file_system_context) { | |
158 return static_cast<SyncFileSystemBackend*>( | |
159 file_system_context->GetFileSystemBackend( | |
160 fileapi::kFileSystemTypeSyncable)); | |
161 } | |
162 | |
163 void SyncFileSystemBackend::set_change_tracker( | |
164 scoped_ptr<LocalFileChangeTracker> tracker) { | |
165 DCHECK(!change_tracker_.get()); | |
166 DCHECK(tracker.get()); | |
167 change_tracker_ = tracker.Pass(); | |
168 AddFileUpdateObserver(fileapi::kFileSystemTypeSyncable, | |
169 change_tracker_.get(), | |
170 file_task_runner_); | |
171 AddFileChangeObserver(fileapi::kFileSystemTypeSyncable, | |
172 change_tracker_.get(), | |
173 file_task_runner_); | |
174 } | |
175 | |
176 void SyncFileSystemBackend::set_sync_context( | |
177 sync_file_system::LocalFileSyncContext* sync_context) { | |
178 sync_context_ = sync_context; | |
179 } | |
180 | |
181 void SyncFileSystemBackend::LazyInitialize( | |
182 fileapi::SandboxContext* sandbox_context) { | |
183 DCHECK(!sandbox_context_); | |
184 sandbox_context_ = sandbox_context; | |
185 // Set quota observers. | |
186 if (enable_usage_tracking_) { | |
187 update_observers_ = update_observers_.AddObserver( | |
188 sandbox_context_->quota_observer(), file_task_runner_); | |
189 access_observers_ = access_observers_.AddObserver( | |
190 sandbox_context_->quota_observer(), NULL); | |
191 syncable_update_observers_ = syncable_update_observers_.AddObserver( | |
192 sandbox_context_->quota_observer(), file_task_runner_); | |
193 } | |
194 } | |
195 | |
196 } // namespace sync_file_system | |
OLD | NEW |