| 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 mount_point_name_to_id_map_.erase(mount_point_name); |
| 187 |
| 174 delete file_system_it->second; | 188 delete file_system_it->second; |
| 175 file_system_map_.erase(file_system_it); | 189 file_system_map_.erase(file_system_it); |
| 190 |
| 176 return true; | 191 return true; |
| 177 } | 192 } |
| 178 | 193 |
| 179 bool Service::RequestUnmount(int file_system_id) { | 194 bool Service::RequestUnmount(int file_system_id) { |
| 180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 195 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 181 | 196 |
| 182 ProvidedFileSystemMap::iterator file_system_it = | 197 ProvidedFileSystemMap::iterator file_system_it = |
| 183 file_system_map_.find(file_system_id); | 198 file_system_map_.find(file_system_id); |
| 184 if (file_system_it == file_system_map_.end()) | 199 if (file_system_it == file_system_map_.end()) |
| 185 return false; | 200 return false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 200 result.push_back(it->second->GetFileSystemInfo()); | 215 result.push_back(it->second->GetFileSystemInfo()); |
| 201 } | 216 } |
| 202 return result; | 217 return result; |
| 203 } | 218 } |
| 204 | 219 |
| 205 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( | 220 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
| 206 const std::string& extension_id, | 221 const std::string& extension_id, |
| 207 int file_system_id) { | 222 int file_system_id) { |
| 208 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 223 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 209 | 224 |
| 210 ProvidedFileSystemMap::iterator file_system_it = | 225 const ProvidedFileSystemMap::const_iterator file_system_it = |
| 211 file_system_map_.find(file_system_id); | 226 file_system_map_.find(file_system_id); |
| 212 if (file_system_it == file_system_map_.end() || | 227 if (file_system_it == file_system_map_.end() || |
| 213 file_system_it->second->GetFileSystemInfo().extension_id() != | 228 file_system_it->second->GetFileSystemInfo().extension_id() != |
| 214 extension_id) { | 229 extension_id) { |
| 215 return NULL; | 230 return NULL; |
| 216 } | 231 } |
| 217 | 232 |
| 218 return file_system_it->second; | 233 return file_system_it->second; |
| 219 } | 234 } |
| 220 | 235 |
| 236 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
| 237 const std::string& mount_point_name) { |
| 238 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 239 |
| 240 const MountPointNameToIdMap::const_iterator mapping_it = |
| 241 mount_point_name_to_id_map_.find(mount_point_name); |
| 242 if (mapping_it == mount_point_name_to_id_map_.end()) |
| 243 return NULL; |
| 244 |
| 245 const ProvidedFileSystemMap::const_iterator file_system_it = |
| 246 file_system_map_.find(mapping_it->second); |
| 247 if (file_system_it == file_system_map_.end()) |
| 248 return NULL; |
| 249 |
| 250 return file_system_it->second; |
| 251 } |
| 252 |
| 221 void Service::Shutdown() {} | 253 void Service::Shutdown() {} |
| 222 | 254 |
| 223 void Service::OnRequestUnmountStatus( | 255 void Service::OnRequestUnmountStatus( |
| 224 const ProvidedFileSystemInfo& file_system_info, | 256 const ProvidedFileSystemInfo& file_system_info, |
| 225 base::File::Error error) { | 257 base::File::Error error) { |
| 226 // Notify observers about failure in unmounting, since mount() will not be | 258 // 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 | 259 // 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. | 260 // invoked, and observers notified, so there is no need to call them now. |
| 229 if (error != base::File::FILE_OK) { | 261 if (error != base::File::FILE_OK) { |
| 230 FOR_EACH_OBSERVER(Observer, | 262 FOR_EACH_OBSERVER(Observer, |
| 231 observers_, | 263 observers_, |
| 232 OnProvidedFileSystemUnmount(file_system_info, error)); | 264 OnProvidedFileSystemUnmount(file_system_info, error)); |
| 233 } | 265 } |
| 234 } | 266 } |
| 235 | 267 |
| 236 } // namespace file_system_provider | 268 } // namespace file_system_provider |
| 237 } // namespace chromeos | 269 } // namespace chromeos |
| OLD | NEW |