| 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/strings/string_number_conversions.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/values.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" |
| 11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" | 11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| 12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
| 13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
| 12 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | 14 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" |
| 13 #include "chrome/browser/chromeos/login/user.h" | |
| 14 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 15 #include "chrome/common/extensions/api/file_system_provider.h" | |
| 16 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 17 #include "extensions/browser/event_router.h" | |
| 18 #include "extensions/browser/extension_system.h" | |
| 19 #include "webkit/browser/fileapi/external_mount_points.h" | 16 #include "webkit/browser/fileapi/external_mount_points.h" |
| 20 | 17 |
| 21 namespace chromeos { | 18 namespace chromeos { |
| 22 namespace file_system_provider { | 19 namespace file_system_provider { |
| 23 namespace { | 20 namespace { |
| 24 | 21 |
| 25 // Root mount path for all of the provided file systems. | |
| 26 const base::FilePath::CharType kProvidedMountPointRoot[] = | |
| 27 FILE_PATH_LITERAL("/provided"); | |
| 28 | |
| 29 // Maximum number of file systems to be mounted in the same time, per profile. | 22 // Maximum number of file systems to be mounted in the same time, per profile. |
| 30 const size_t kMaxFileSystems = 16; | 23 const size_t kMaxFileSystems = 16; |
| 31 | 24 |
| 32 // Constructs a safe mount point path for the provided file system represented | |
| 33 // by |file_system_handle|. The handle is a numeric part of the file system id. | |
| 34 base::FilePath GetMountPointPath(Profile* profile, | |
| 35 std::string extension_id, | |
| 36 int file_system_id) { | |
| 37 chromeos::User* const user = | |
| 38 chromeos::UserManager::IsInitialized() | |
| 39 ? chromeos::UserManager::Get()->GetUserByProfile( | |
| 40 profile->GetOriginalProfile()) | |
| 41 : NULL; | |
| 42 const std::string user_suffix = user ? "-" + user->username_hash() : ""; | |
| 43 return base::FilePath(kProvidedMountPointRoot).AppendASCII( | |
| 44 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); | |
| 45 } | |
| 46 | |
| 47 // Creates values to be passed to request events. These values can be extended | |
| 48 // by additional fields. | |
| 49 scoped_ptr<base::ListValue> CreateRequestValues(int file_system_id, | |
| 50 int request_id) { | |
| 51 scoped_ptr<base::ListValue> values(new base::ListValue()); | |
| 52 values->AppendInteger(file_system_id); | |
| 53 values->AppendInteger(request_id); | |
| 54 return values.Pass(); | |
| 55 } | |
| 56 | |
| 57 } // namespace | 25 } // namespace |
| 58 | 26 |
| 59 Service::Service(Profile* profile) | 27 Service::Service(Profile* profile) |
| 60 : profile_(profile), next_id_(1), weak_ptr_factory_(this) { | 28 : profile_(profile), |
| 29 file_system_factory_(new ProvidedFileSystemFactory), |
| 30 next_id_(1), |
| 31 weak_ptr_factory_(this) { |
| 61 AddObserver(&request_manager_); | 32 AddObserver(&request_manager_); |
| 62 } | 33 } |
| 63 | 34 |
| 64 Service::~Service() {} | 35 Service::Service( |
| 36 Profile* profile, |
| 37 scoped_ptr<ProvidedFileSystemFactoryInterface> file_system_factory) |
| 38 : profile_(profile), |
| 39 file_system_factory_(file_system_factory.Pass()), |
| 40 next_id_(1), |
| 41 weak_ptr_factory_(this) { |
| 42 AddObserver(&request_manager_); |
| 43 } |
| 44 |
| 45 Service::~Service() { STLDeleteValues(&file_system_map_); } |
| 46 |
| 47 // static |
| 48 Service* Service::CreateForTesting( |
| 49 Profile* profile, |
| 50 scoped_ptr<ProvidedFileSystemFactoryInterface> file_system_factory) { |
| 51 return new Service(profile, file_system_factory.Pass()); |
| 52 } |
| 65 | 53 |
| 66 // static | 54 // static |
| 67 Service* Service::Get(content::BrowserContext* context) { | 55 Service* Service::Get(content::BrowserContext* context) { |
| 68 return ServiceFactory::Get(context); | 56 return ServiceFactory::Get(context); |
| 69 } | 57 } |
| 70 | 58 |
| 71 void Service::AddObserver(Observer* observer) { | 59 void Service::AddObserver(Observer* observer) { |
| 72 DCHECK(observer); | 60 DCHECK(observer); |
| 73 observers_.AddObserver(observer); | 61 observers_.AddObserver(observer); |
| 74 } | 62 } |
| 75 | 63 |
| 76 void Service::RemoveObserver(Observer* observer) { | 64 void Service::RemoveObserver(Observer* observer) { |
| 77 DCHECK(observer); | 65 DCHECK(observer); |
| 78 observers_.RemoveObserver(observer); | 66 observers_.RemoveObserver(observer); |
| 79 } | 67 } |
| 80 | 68 |
| 81 int Service::MountFileSystem(const std::string& extension_id, | 69 int Service::MountFileSystem(const std::string& extension_id, |
| 82 const std::string& file_system_name) { | 70 const std::string& file_system_name) { |
| 83 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 84 | 72 |
| 85 // Restrict number of file systems to prevent system abusing. | 73 // Restrict number of file systems to prevent system abusing. |
| 86 if (file_systems_.size() + 1 > kMaxFileSystems) { | 74 if (file_system_map_.size() + 1 > kMaxFileSystems) { |
| 87 FOR_EACH_OBSERVER( | 75 FOR_EACH_OBSERVER( |
| 88 Observer, | 76 Observer, |
| 89 observers_, | 77 observers_, |
| 90 OnProvidedFileSystemMount(ProvidedFileSystem(), | 78 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
| 91 base::File::FILE_ERROR_TOO_MANY_OPENED)); | 79 base::File::FILE_ERROR_TOO_MANY_OPENED)); |
| 92 return 0; | 80 return 0; |
| 93 } | 81 } |
| 94 | 82 |
| 95 // The file system id is unique per service, so per profile. | 83 // The provided file system id is unique per service, so per profile. |
| 96 int file_system_id = next_id_; | 84 int file_system_id = next_id_; |
| 97 | 85 |
| 98 fileapi::ExternalMountPoints* const mount_points = | 86 fileapi::ExternalMountPoints* const mount_points = |
| 99 fileapi::ExternalMountPoints::GetSystemInstance(); | 87 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 100 DCHECK(mount_points); | 88 DCHECK(mount_points); |
| 101 | 89 |
| 102 // The mount point path and name are unique per system, since they are system | 90 // The mount point path and name are unique per system, since they are system |
| 103 // wide. This is necessary for copying between profiles. | 91 // wide. This is necessary for copying between profiles. |
| 104 const base::FilePath& mount_point_path = | 92 const base::FilePath& mount_point_path = |
| 105 GetMountPointPath(profile_, extension_id, file_system_id); | 93 util::GetMountPointPath(profile_, extension_id, file_system_id); |
| 106 const std::string mount_point_name = | 94 const std::string mount_point_name = |
| 107 mount_point_path.BaseName().AsUTF8Unsafe(); | 95 mount_point_path.BaseName().AsUTF8Unsafe(); |
| 108 | 96 |
| 109 if (!mount_points->RegisterFileSystem(mount_point_name, | 97 if (!mount_points->RegisterFileSystem(mount_point_name, |
| 110 fileapi::kFileSystemTypeProvided, | 98 fileapi::kFileSystemTypeProvided, |
| 111 fileapi::FileSystemMountOption(), | 99 fileapi::FileSystemMountOption(), |
| 112 mount_point_path)) { | 100 mount_point_path)) { |
| 113 FOR_EACH_OBSERVER( | 101 FOR_EACH_OBSERVER( |
| 114 Observer, | 102 Observer, |
| 115 observers_, | 103 observers_, |
| 116 OnProvidedFileSystemMount(ProvidedFileSystem(), | 104 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
| 117 base::File::FILE_ERROR_INVALID_OPERATION)); | 105 base::File::FILE_ERROR_INVALID_OPERATION)); |
| 118 return 0; | 106 return 0; |
| 119 } | 107 } |
| 120 | 108 |
| 121 // Store the file system descriptor. Use the mount point name as the file | 109 // Store the file system descriptor. Use the mount point name as the file |
| 122 // system provider file system id. | 110 // system provider file system id. |
| 123 // Examples: | 111 // Examples: |
| 124 // file_system_id = 41 | 112 // file_system_id = 41 |
| 125 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 113 // mount_point_name = file_system_id = b33f1337-41-5aa5 |
| 126 // mount_point_path = /provided/b33f1337-41-5aa5 | 114 // mount_point_path = /provided/b33f1337-41-5aa5 |
| 127 ProvidedFileSystem file_system( | 115 ProvidedFileSystemInfo file_system_info( |
| 128 extension_id, file_system_id, file_system_name, mount_point_path); | 116 extension_id, file_system_id, file_system_name, mount_point_path); |
| 129 file_systems_[file_system_id] = file_system; | 117 |
| 118 ProvidedFileSystemInterface* file_system = file_system_factory_->Create( |
| 119 profile_, &request_manager_, file_system_info); |
| 120 DCHECK(file_system); |
| 121 file_system_map_[file_system_id] = file_system; |
| 130 | 122 |
| 131 FOR_EACH_OBSERVER( | 123 FOR_EACH_OBSERVER( |
| 132 Observer, | 124 Observer, |
| 133 observers_, | 125 observers_, |
| 134 OnProvidedFileSystemMount(file_system, base::File::FILE_OK)); | 126 OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); |
| 135 | 127 |
| 136 next_id_++; | 128 next_id_++; |
| 137 return file_system_id; | 129 return file_system_id; |
| 138 } | 130 } |
| 139 | 131 |
| 140 bool Service::UnmountFileSystem(const std::string& extension_id, | 132 bool Service::UnmountFileSystem(const std::string& extension_id, |
| 141 int file_system_id) { | 133 int file_system_id) { |
| 142 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 134 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 143 | 135 |
| 144 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 136 ProvidedFileSystemMap::iterator file_system_it = |
| 145 if (file_system_it == file_systems_.end() || | 137 file_system_map_.find(file_system_id); |
| 146 file_system_it->second.extension_id() != extension_id) { | 138 if (file_system_it == file_system_map_.end() || |
| 147 const ProvidedFileSystem empty_file_system; | 139 file_system_it->second->GetFileSystemInfo().extension_id() != |
| 148 FOR_EACH_OBSERVER(Observer, | 140 extension_id) { |
| 149 observers_, | 141 const ProvidedFileSystemInfo empty_file_system_info; |
| 150 OnProvidedFileSystemUnmount( | 142 FOR_EACH_OBSERVER( |
| 151 empty_file_system, base::File::FILE_ERROR_NOT_FOUND)); | 143 Observer, |
| 144 observers_, |
| 145 OnProvidedFileSystemUnmount(empty_file_system_info, |
| 146 base::File::FILE_ERROR_NOT_FOUND)); |
| 152 return false; | 147 return false; |
| 153 } | 148 } |
| 154 | 149 |
| 155 fileapi::ExternalMountPoints* const mount_points = | 150 fileapi::ExternalMountPoints* const mount_points = |
| 156 fileapi::ExternalMountPoints::GetSystemInstance(); | 151 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 157 DCHECK(mount_points); | 152 DCHECK(mount_points); |
| 158 | 153 |
| 154 const ProvidedFileSystemInfo& file_system_info = |
| 155 file_system_it->second->GetFileSystemInfo(); |
| 156 |
| 159 const std::string mount_point_name = | 157 const std::string mount_point_name = |
| 160 file_system_it->second.mount_path().BaseName().value(); | 158 file_system_info.mount_path().BaseName().value(); |
| 161 if (!mount_points->RevokeFileSystem(mount_point_name)) { | 159 if (!mount_points->RevokeFileSystem(mount_point_name)) { |
| 162 FOR_EACH_OBSERVER( | 160 FOR_EACH_OBSERVER( |
| 163 Observer, | 161 Observer, |
| 164 observers_, | 162 observers_, |
| 165 OnProvidedFileSystemUnmount(file_system_it->second, | 163 OnProvidedFileSystemUnmount(file_system_info, |
| 166 base::File::FILE_ERROR_INVALID_OPERATION)); | 164 base::File::FILE_ERROR_INVALID_OPERATION)); |
| 167 return false; | 165 return false; |
| 168 } | 166 } |
| 169 | 167 |
| 170 FOR_EACH_OBSERVER( | 168 FOR_EACH_OBSERVER( |
| 171 Observer, | 169 Observer, |
| 172 observers_, | 170 observers_, |
| 173 OnProvidedFileSystemUnmount(file_system_it->second, base::File::FILE_OK)); | 171 OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); |
| 174 | 172 |
| 175 file_systems_.erase(file_system_it); | 173 delete file_system_it->second; |
| 174 file_system_map_.erase(file_system_it); |
| 176 return true; | 175 return true; |
| 177 } | 176 } |
| 178 | 177 |
| 179 std::vector<ProvidedFileSystem> Service::GetMountedFileSystems() { | |
| 180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 181 | |
| 182 std::vector<ProvidedFileSystem> result; | |
| 183 for (FileSystemMap::const_iterator it = file_systems_.begin(); | |
| 184 it != file_systems_.end(); | |
| 185 ++it) { | |
| 186 result.push_back(it->second); | |
| 187 } | |
| 188 return result; | |
| 189 } | |
| 190 | |
| 191 bool Service::FulfillRequest(const std::string& extension_id, | |
| 192 int file_system_id, | |
| 193 int request_id, | |
| 194 scoped_ptr<base::DictionaryValue> result, | |
| 195 bool has_next) { | |
| 196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 197 | |
| 198 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | |
| 199 if (file_system_it == file_systems_.end() || | |
| 200 file_system_it->second.extension_id() != extension_id) { | |
| 201 return false; | |
| 202 } | |
| 203 | |
| 204 return request_manager_.FulfillRequest( | |
| 205 file_system_it->second, request_id, result.Pass(), has_next); | |
| 206 } | |
| 207 | |
| 208 bool Service::RejectRequest(const std::string& extension_id, | |
| 209 int file_system_id, | |
| 210 int request_id, | |
| 211 base::File::Error error) { | |
| 212 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 213 | |
| 214 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | |
| 215 if (file_system_it == file_systems_.end() || | |
| 216 file_system_it->second.extension_id() != extension_id) { | |
| 217 return false; | |
| 218 } | |
| 219 | |
| 220 return request_manager_.RejectRequest( | |
| 221 file_system_it->second, request_id, error); | |
| 222 } | |
| 223 | |
| 224 bool Service::RequestUnmount(int file_system_id) { | 178 bool Service::RequestUnmount(int file_system_id) { |
| 225 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 179 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 226 | 180 |
| 227 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 181 ProvidedFileSystemMap::iterator file_system_it = |
| 228 if (file_system_it == file_systems_.end()) | 182 file_system_map_.find(file_system_id); |
| 183 if (file_system_it == file_system_map_.end()) |
| 229 return false; | 184 return false; |
| 230 | 185 |
| 231 int request_id = | 186 return file_system_it->second->RequestUnmount( |
| 232 request_manager_.CreateRequest(file_system_it->second, | 187 base::Bind(&Service::OnRequestUnmountStatus, |
| 233 SuccessCallback(), | 188 weak_ptr_factory_.GetWeakPtr(), |
| 234 base::Bind(&Service::OnRequestUnmountError, | 189 file_system_it->second->GetFileSystemInfo())); |
| 235 weak_ptr_factory_.GetWeakPtr(), | 190 } |
| 236 file_system_it->second)); | |
| 237 | 191 |
| 238 if (!request_id) | 192 std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { |
| 239 return false; | 193 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 240 | 194 |
| 241 scoped_ptr<base::ListValue> values( | 195 std::vector<ProvidedFileSystemInfo> result; |
| 242 CreateRequestValues(file_system_id, request_id)); | 196 for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); |
| 197 it != file_system_map_.end(); |
| 198 ++it) { |
| 199 result.push_back(it->second->GetFileSystemInfo()); |
| 200 } |
| 201 return result; |
| 202 } |
| 243 | 203 |
| 244 extensions::EventRouter* event_router = | 204 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
| 245 extensions::ExtensionSystem::Get(profile_)->event_router(); | 205 const std::string& extension_id, |
| 246 DCHECK(event_router); | 206 int file_system_id) { |
| 207 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 247 | 208 |
| 248 event_router->DispatchEventToExtension( | 209 ProvidedFileSystemMap::iterator file_system_it = |
| 249 file_system_it->second.extension_id(), | 210 file_system_map_.find(file_system_id); |
| 250 make_scoped_ptr(new extensions::Event( | 211 if (file_system_it == file_system_map_.end() || |
| 251 extensions::api::file_system_provider::OnUnmountRequested::kEventName, | 212 file_system_it->second->GetFileSystemInfo().extension_id() != |
| 252 values.Pass()))); | 213 extension_id) { |
| 214 return NULL; |
| 215 } |
| 253 | 216 |
| 254 return true; | 217 return file_system_it->second; |
| 255 } | 218 } |
| 256 | 219 |
| 257 void Service::Shutdown() { RemoveObserver(&request_manager_); } | 220 void Service::Shutdown() { RemoveObserver(&request_manager_); } |
| 258 | 221 |
| 259 void Service::OnRequestUnmountError(const ProvidedFileSystem& file_system, | 222 void Service::OnRequestUnmountStatus( |
| 260 base::File::Error error) { | 223 const ProvidedFileSystemInfo& file_system_info, |
| 261 FOR_EACH_OBSERVER( | 224 base::File::Error error) { |
| 262 Observer, observers_, OnProvidedFileSystemUnmount(file_system, error)); | 225 // Notify observers about failure in unmounting, since mount() will not be |
| 226 // called by the provided file system. In case of success mount() will be |
| 227 // invoked, and observers notified, so there is no need to call them now. |
| 228 if (error != base::File::FILE_OK) { |
| 229 FOR_EACH_OBSERVER(Observer, |
| 230 observers_, |
| 231 OnProvidedFileSystemUnmount(file_system_info, error)); |
| 232 } |
| 263 } | 233 } |
| 264 | 234 |
| 265 } // namespace file_system_provider | 235 } // namespace file_system_provider |
| 266 } // namespace chromeos | 236 } // namespace chromeos |
| OLD | NEW |