OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/file_system_provider/service.h" | 5 #include "chrome/browser/chromeos/file_system_provider/service.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" | 9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
10 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 10 #include "chrome/browser/chromeos/file_system_provider/observer.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 } | 33 } |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 Service::Service(Profile* profile) | 37 Service::Service(Profile* profile) |
38 : profile_(profile), | 38 : profile_(profile), |
39 file_system_factory_(base::Bind(CreateProvidedFileSystem)), | 39 file_system_factory_(base::Bind(CreateProvidedFileSystem)), |
40 next_id_(1), | 40 next_id_(1), |
41 weak_ptr_factory_(this) {} | 41 weak_ptr_factory_(this) {} |
42 | 42 |
43 Service::~Service() { STLDeleteValues(&file_system_map_); } | 43 Service::~Service() { |
44 ProvidedFileSystemMap::iterator it = file_system_map_.begin(); | |
45 while (it != file_system_map_.end()) { | |
46 const int file_system_id = it->first; | |
47 const std::string extension_id = | |
48 it->second->GetFileSystemInfo().extension_id(); | |
49 ++it; | |
50 UnmountFileSystem(extension_id, file_system_id); | |
51 } | |
52 | |
53 DCHECK_EQ(0u, file_system_map_.size()); | |
54 STLDeleteValues(&file_system_map_); | |
55 } | |
44 | 56 |
45 // static | 57 // static |
46 Service* Service::Get(content::BrowserContext* context) { | 58 Service* Service::Get(content::BrowserContext* context) { |
47 return ServiceFactory::Get(context); | 59 return ServiceFactory::Get(context); |
48 } | 60 } |
49 | 61 |
50 void Service::AddObserver(Observer* observer) { | 62 void Service::AddObserver(Observer* observer) { |
51 DCHECK(observer); | 63 DCHECK(observer); |
52 observers_.AddObserver(observer); | 64 observers_.AddObserver(observer); |
53 } | 65 } |
(...skipping 25 matching lines...) Expand all Loading... | |
79 | 91 |
80 // The provided file system id is unique per service, so per profile. | 92 // The provided file system id is unique per service, so per profile. |
81 int file_system_id = next_id_; | 93 int file_system_id = next_id_; |
82 | 94 |
83 fileapi::ExternalMountPoints* const mount_points = | 95 fileapi::ExternalMountPoints* const mount_points = |
84 fileapi::ExternalMountPoints::GetSystemInstance(); | 96 fileapi::ExternalMountPoints::GetSystemInstance(); |
85 DCHECK(mount_points); | 97 DCHECK(mount_points); |
86 | 98 |
87 // The mount point path and name are unique per system, since they are system | 99 // The mount point path and name are unique per system, since they are system |
88 // wide. This is necessary for copying between profiles. | 100 // wide. This is necessary for copying between profiles. |
89 const base::FilePath& mount_point_path = | 101 const base::FilePath& mount_path = |
90 util::GetMountPointPath(profile_, extension_id, file_system_id); | 102 util::GetMountPath(profile_, extension_id, file_system_id); |
91 const std::string mount_point_name = | 103 const std::string mount_point_name = mount_path.BaseName().AsUTF8Unsafe(); |
92 mount_point_path.BaseName().AsUTF8Unsafe(); | |
93 | 104 |
94 if (!mount_points->RegisterFileSystem(mount_point_name, | 105 if (!mount_points->RegisterFileSystem(mount_point_name, |
95 fileapi::kFileSystemTypeProvided, | 106 fileapi::kFileSystemTypeProvided, |
96 fileapi::FileSystemMountOption(), | 107 fileapi::FileSystemMountOption(), |
97 mount_point_path)) { | 108 mount_path)) { |
98 FOR_EACH_OBSERVER( | 109 FOR_EACH_OBSERVER( |
99 Observer, | 110 Observer, |
100 observers_, | 111 observers_, |
101 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), | 112 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
102 base::File::FILE_ERROR_INVALID_OPERATION)); | 113 base::File::FILE_ERROR_INVALID_OPERATION)); |
103 return 0; | 114 return 0; |
104 } | 115 } |
105 | 116 |
106 // Store the file system descriptor. Use the mount point name as the file | 117 // Store the file system descriptor. Use the mount point name as the file |
107 // system provider file system id. | 118 // system provider file system id. |
108 // Examples: | 119 // Examples: |
109 // file_system_id = 41 | 120 // file_system_id = 41 |
110 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 121 // mount_point_name = b33f1337-41-5aa5 |
111 // mount_point_path = /provided/b33f1337-41-5aa5 | 122 // mount_path = /provided/b33f1337-41-5aa5 |
112 ProvidedFileSystemInfo file_system_info( | 123 ProvidedFileSystemInfo file_system_info( |
113 extension_id, file_system_id, file_system_name, mount_point_path); | 124 extension_id, file_system_id, file_system_name, mount_path); |
114 | 125 |
115 // The event router may be NULL for unit tests. | 126 // The event router may be NULL for unit tests. |
116 extensions::EventRouter* event_router = | 127 extensions::EventRouter* event_router = |
117 extensions::ExtensionSystem::Get(profile_)->event_router(); | 128 extensions::ExtensionSystem::Get(profile_)->event_router(); |
118 | 129 |
119 ProvidedFileSystemInterface* file_system = | 130 ProvidedFileSystemInterface* file_system = |
120 file_system_factory_.Run(event_router, file_system_info); | 131 file_system_factory_.Run(event_router, file_system_info); |
121 DCHECK(file_system); | 132 DCHECK(file_system); |
122 file_system_map_[file_system_id] = file_system; | 133 file_system_map_[file_system_id] = file_system; |
134 mount_point_name_to_id_map_[mount_point_name] = file_system_id; | |
123 | 135 |
124 FOR_EACH_OBSERVER( | 136 FOR_EACH_OBSERVER( |
125 Observer, | 137 Observer, |
126 observers_, | 138 observers_, |
127 OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); | 139 OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); |
128 | 140 |
129 next_id_++; | 141 next_id_++; |
130 return file_system_id; | 142 return file_system_id; |
131 } | 143 } |
132 | 144 |
133 bool Service::UnmountFileSystem(const std::string& extension_id, | 145 bool Service::UnmountFileSystem(const std::string& extension_id, |
134 int file_system_id) { | 146 int file_system_id) { |
135 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
136 | 148 |
137 ProvidedFileSystemMap::iterator file_system_it = | 149 const ProvidedFileSystemMap::iterator file_system_it = |
138 file_system_map_.find(file_system_id); | 150 file_system_map_.find(file_system_id); |
139 if (file_system_it == file_system_map_.end() || | 151 if (file_system_it == file_system_map_.end() || |
140 file_system_it->second->GetFileSystemInfo().extension_id() != | 152 file_system_it->second->GetFileSystemInfo().extension_id() != |
141 extension_id) { | 153 extension_id) { |
142 const ProvidedFileSystemInfo empty_file_system_info; | 154 const ProvidedFileSystemInfo empty_file_system_info; |
143 FOR_EACH_OBSERVER( | 155 FOR_EACH_OBSERVER( |
144 Observer, | 156 Observer, |
145 observers_, | 157 observers_, |
146 OnProvidedFileSystemUnmount(empty_file_system_info, | 158 OnProvidedFileSystemUnmount(empty_file_system_info, |
147 base::File::FILE_ERROR_NOT_FOUND)); | 159 base::File::FILE_ERROR_NOT_FOUND)); |
(...skipping 16 matching lines...) Expand all Loading... | |
164 OnProvidedFileSystemUnmount(file_system_info, | 176 OnProvidedFileSystemUnmount(file_system_info, |
165 base::File::FILE_ERROR_INVALID_OPERATION)); | 177 base::File::FILE_ERROR_INVALID_OPERATION)); |
166 return false; | 178 return false; |
167 } | 179 } |
168 | 180 |
169 FOR_EACH_OBSERVER( | 181 FOR_EACH_OBSERVER( |
170 Observer, | 182 Observer, |
171 observers_, | 183 observers_, |
172 OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); | 184 OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); |
173 | 185 |
186 const MountPointNameToIdMap::iterator mapping_it = | |
187 mount_point_name_to_id_map_.find(mount_point_name); | |
188 mount_point_name_to_id_map_.erase(mapping_it); | |
kinaba
2014/04/18 05:51:30
Simply
mount_point_name_to_id_map_.erase(mount_poi
mtomasz
2014/04/18 06:56:00
Done.
| |
189 | |
174 delete file_system_it->second; | 190 delete file_system_it->second; |
175 file_system_map_.erase(file_system_it); | 191 file_system_map_.erase(file_system_it); |
192 | |
176 return true; | 193 return true; |
177 } | 194 } |
178 | 195 |
179 bool Service::RequestUnmount(int file_system_id) { | 196 bool Service::RequestUnmount(int file_system_id) { |
180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 197 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
181 | 198 |
182 ProvidedFileSystemMap::iterator file_system_it = | 199 ProvidedFileSystemMap::iterator file_system_it = |
183 file_system_map_.find(file_system_id); | 200 file_system_map_.find(file_system_id); |
184 if (file_system_it == file_system_map_.end()) | 201 if (file_system_it == file_system_map_.end()) |
185 return false; | 202 return false; |
(...skipping 14 matching lines...) Expand all Loading... | |
200 result.push_back(it->second->GetFileSystemInfo()); | 217 result.push_back(it->second->GetFileSystemInfo()); |
201 } | 218 } |
202 return result; | 219 return result; |
203 } | 220 } |
204 | 221 |
205 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( | 222 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
206 const std::string& extension_id, | 223 const std::string& extension_id, |
207 int file_system_id) { | 224 int file_system_id) { |
208 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 225 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
209 | 226 |
210 ProvidedFileSystemMap::iterator file_system_it = | 227 const ProvidedFileSystemMap::const_iterator file_system_it = |
211 file_system_map_.find(file_system_id); | 228 file_system_map_.find(file_system_id); |
212 if (file_system_it == file_system_map_.end() || | 229 if (file_system_it == file_system_map_.end() || |
213 file_system_it->second->GetFileSystemInfo().extension_id() != | 230 file_system_it->second->GetFileSystemInfo().extension_id() != |
214 extension_id) { | 231 extension_id) { |
215 return NULL; | 232 return NULL; |
216 } | 233 } |
217 | 234 |
218 return file_system_it->second; | 235 return file_system_it->second; |
219 } | 236 } |
220 | 237 |
238 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( | |
239 const std::string& mount_point_name) { | |
240 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
241 | |
242 const MountPointNameToIdMap::const_iterator mapping_it = | |
243 mount_point_name_to_id_map_.find(mount_point_name); | |
244 if (mapping_it == mount_point_name_to_id_map_.end()) | |
245 return NULL; | |
246 | |
247 const ProvidedFileSystemMap::const_iterator file_system_it = | |
248 file_system_map_.find(mapping_it->second); | |
249 if (file_system_it == file_system_map_.end()) | |
250 return NULL; | |
251 | |
252 return file_system_it->second; | |
253 } | |
254 | |
221 void Service::Shutdown() {} | 255 void Service::Shutdown() {} |
222 | 256 |
223 void Service::OnRequestUnmountStatus( | 257 void Service::OnRequestUnmountStatus( |
224 const ProvidedFileSystemInfo& file_system_info, | 258 const ProvidedFileSystemInfo& file_system_info, |
225 base::File::Error error) { | 259 base::File::Error error) { |
226 // Notify observers about failure in unmounting, since mount() will not be | 260 // Notify observers about failure in unmounting, since mount() will not be |
227 // called by the provided file system. In case of success mount() will be | 261 // called by the provided file system. In case of success mount() will be |
228 // invoked, and observers notified, so there is no need to call them now. | 262 // invoked, and observers notified, so there is no need to call them now. |
229 if (error != base::File::FILE_OK) { | 263 if (error != base::File::FILE_OK) { |
230 FOR_EACH_OBSERVER(Observer, | 264 FOR_EACH_OBSERVER(Observer, |
231 observers_, | 265 observers_, |
232 OnProvidedFileSystemUnmount(file_system_info, error)); | 266 OnProvidedFileSystemUnmount(file_system_info, error)); |
233 } | 267 } |
234 } | 268 } |
235 | 269 |
236 } // namespace file_system_provider | 270 } // namespace file_system_provider |
237 } // namespace chromeos | 271 } // namespace chromeos |
OLD | NEW |