| 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/provided_file_system.h" | 5 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "chrome/browser/chromeos/file_system_provider/notification_manager.h" | 11 #include "chrome/browser/chromeos/file_system_provider/notification_manager.h" |
| 12 #include "chrome/browser/chromeos/file_system_provider/operations/abort.h" | 12 #include "chrome/browser/chromeos/file_system_provider/operations/abort.h" |
| 13 #include "chrome/browser/chromeos/file_system_provider/operations/close_file.h" | 13 #include "chrome/browser/chromeos/file_system_provider/operations/close_file.h" |
| 14 #include "chrome/browser/chromeos/file_system_provider/operations/copy_entry.h" | 14 #include "chrome/browser/chromeos/file_system_provider/operations/copy_entry.h" |
| 15 #include "chrome/browser/chromeos/file_system_provider/operations/create_directo
ry.h" | 15 #include "chrome/browser/chromeos/file_system_provider/operations/create_directo
ry.h" |
| 16 #include "chrome/browser/chromeos/file_system_provider/operations/create_file.h" | 16 #include "chrome/browser/chromeos/file_system_provider/operations/create_file.h" |
| 17 #include "chrome/browser/chromeos/file_system_provider/operations/delete_entry.h
" | 17 #include "chrome/browser/chromeos/file_system_provider/operations/delete_entry.h
" |
| 18 #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h
" | 18 #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h
" |
| 19 #include "chrome/browser/chromeos/file_system_provider/operations/move_entry.h" | 19 #include "chrome/browser/chromeos/file_system_provider/operations/move_entry.h" |
| 20 #include "chrome/browser/chromeos/file_system_provider/operations/observe_direct
ory.h" |
| 20 #include "chrome/browser/chromeos/file_system_provider/operations/open_file.h" | 21 #include "chrome/browser/chromeos/file_system_provider/operations/open_file.h" |
| 21 #include "chrome/browser/chromeos/file_system_provider/operations/read_directory
.h" | 22 #include "chrome/browser/chromeos/file_system_provider/operations/read_directory
.h" |
| 22 #include "chrome/browser/chromeos/file_system_provider/operations/read_file.h" | 23 #include "chrome/browser/chromeos/file_system_provider/operations/read_file.h" |
| 23 #include "chrome/browser/chromeos/file_system_provider/operations/truncate.h" | 24 #include "chrome/browser/chromeos/file_system_provider/operations/truncate.h" |
| 24 #include "chrome/browser/chromeos/file_system_provider/operations/unmount.h" | 25 #include "chrome/browser/chromeos/file_system_provider/operations/unmount.h" |
| 26 #include "chrome/browser/chromeos/file_system_provider/operations/unobserve_entr
y.h" |
| 25 #include "chrome/browser/chromeos/file_system_provider/operations/write_file.h" | 27 #include "chrome/browser/chromeos/file_system_provider/operations/write_file.h" |
| 26 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | 28 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/common/extensions/api/file_system_provider.h" | 30 #include "chrome/common/extensions/api/file_system_provider.h" |
| 29 #include "extensions/browser/event_router.h" | 31 #include "extensions/browser/event_router.h" |
| 30 | 32 |
| 31 namespace net { | 33 namespace net { |
| 32 class IOBuffer; | 34 class IOBuffer; |
| 33 } // namespace net | 35 } // namespace net |
| 34 | 36 |
| 35 namespace chromeos { | 37 namespace chromeos { |
| 36 namespace file_system_provider { | 38 namespace file_system_provider { |
| 37 namespace { | 39 namespace { |
| 38 | 40 |
| 39 // Dicards the result of Abort() when called from the destructor. | 41 // Discards the result of Abort() when called from the destructor. |
| 40 void EmptyStatusCallback(base::File::Error /* result */) { | 42 void EmptyStatusCallback(base::File::Error /* result */) { |
| 41 } | 43 } |
| 42 | 44 |
| 43 } // namespace | 45 } // namespace |
| 44 | 46 |
| 47 AutoUpdater::AutoUpdater(const base::Closure& update_callback) |
| 48 : update_callback_(update_callback), |
| 49 created_callbacks_(0), |
| 50 pending_callbacks_(0) { |
| 51 } |
| 52 |
| 53 base::Closure AutoUpdater::CreateCallback() { |
| 54 ++created_callbacks_; |
| 55 ++pending_callbacks_; |
| 56 return base::Bind(&AutoUpdater::OnPendingCallback, this); |
| 57 } |
| 58 |
| 59 void AutoUpdater::OnPendingCallback() { |
| 60 DCHECK_LT(0, pending_callbacks_); |
| 61 if (--pending_callbacks_ == 0) |
| 62 update_callback_.Run(); |
| 63 } |
| 64 |
| 65 AutoUpdater::~AutoUpdater() { |
| 66 // If no callbacks are created, then we need to invoke updating in the |
| 67 // destructor. |
| 68 if (!created_callbacks_) |
| 69 update_callback_.Run(); |
| 70 else if (pending_callbacks_) |
| 71 LOG(ERROR) << "Not all callbacks called. This may happen on shutdown."; |
| 72 } |
| 73 |
| 45 ProvidedFileSystem::ProvidedFileSystem( | 74 ProvidedFileSystem::ProvidedFileSystem( |
| 46 Profile* profile, | 75 Profile* profile, |
| 47 const ProvidedFileSystemInfo& file_system_info) | 76 const ProvidedFileSystemInfo& file_system_info) |
| 48 : profile_(profile), | 77 : profile_(profile), |
| 49 event_router_(extensions::EventRouter::Get(profile)), // May be NULL. | 78 event_router_(extensions::EventRouter::Get(profile)), // May be NULL. |
| 50 file_system_info_(file_system_info), | 79 file_system_info_(file_system_info), |
| 51 notification_manager_( | 80 notification_manager_( |
| 52 new NotificationManager(profile_, file_system_info_)), | 81 new NotificationManager(profile_, file_system_info_)), |
| 53 request_manager_(notification_manager_.get()), | 82 request_manager_(new RequestManager(notification_manager_.get())), |
| 54 weak_ptr_factory_(this) { | 83 weak_ptr_factory_(this) { |
| 55 } | 84 } |
| 56 | 85 |
| 57 ProvidedFileSystem::~ProvidedFileSystem() { | 86 ProvidedFileSystem::~ProvidedFileSystem() { |
| 58 const std::vector<int> request_ids = request_manager_.GetActiveRequestIds(); | 87 const std::vector<int> request_ids = request_manager_->GetActiveRequestIds(); |
| 59 for (size_t i = 0; i < request_ids.size(); ++i) { | 88 for (size_t i = 0; i < request_ids.size(); ++i) { |
| 60 Abort(request_ids[i], base::Bind(&EmptyStatusCallback)); | 89 Abort(request_ids[i], base::Bind(&EmptyStatusCallback)); |
| 61 } | 90 } |
| 62 } | 91 } |
| 63 | 92 |
| 93 void ProvidedFileSystem::SetEventRouterForTesting( |
| 94 extensions::EventRouter* event_router) { |
| 95 event_router_ = event_router; |
| 96 } |
| 97 |
| 98 void ProvidedFileSystem::SetNotificationManagerForTesting( |
| 99 scoped_ptr<NotificationManagerInterface> notification_manager) { |
| 100 notification_manager_ = notification_manager.Pass(); |
| 101 request_manager_.reset(new RequestManager(notification_manager_.get())); |
| 102 } |
| 103 |
| 64 ProvidedFileSystem::AbortCallback ProvidedFileSystem::RequestUnmount( | 104 ProvidedFileSystem::AbortCallback ProvidedFileSystem::RequestUnmount( |
| 65 const storage::AsyncFileUtil::StatusCallback& callback) { | 105 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 66 const int request_id = request_manager_.CreateRequest( | 106 const int request_id = request_manager_->CreateRequest( |
| 67 REQUEST_UNMOUNT, | 107 REQUEST_UNMOUNT, |
| 68 scoped_ptr<RequestManager::HandlerInterface>( | 108 scoped_ptr<RequestManager::HandlerInterface>( |
| 69 new operations::Unmount(event_router_, file_system_info_, callback))); | 109 new operations::Unmount(event_router_, file_system_info_, callback))); |
| 70 if (!request_id) { | 110 if (!request_id) { |
| 71 callback.Run(base::File::FILE_ERROR_SECURITY); | 111 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 72 return AbortCallback(); | 112 return AbortCallback(); |
| 73 } | 113 } |
| 74 | 114 |
| 75 return base::Bind( | 115 return base::Bind( |
| 76 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 116 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 77 } | 117 } |
| 78 | 118 |
| 79 ProvidedFileSystem::AbortCallback ProvidedFileSystem::GetMetadata( | 119 ProvidedFileSystem::AbortCallback ProvidedFileSystem::GetMetadata( |
| 80 const base::FilePath& entry_path, | 120 const base::FilePath& entry_path, |
| 81 MetadataFieldMask fields, | 121 MetadataFieldMask fields, |
| 82 const GetMetadataCallback& callback) { | 122 const GetMetadataCallback& callback) { |
| 83 const int request_id = request_manager_.CreateRequest( | 123 const int request_id = request_manager_->CreateRequest( |
| 84 GET_METADATA, | 124 GET_METADATA, |
| 85 scoped_ptr<RequestManager::HandlerInterface>(new operations::GetMetadata( | 125 scoped_ptr<RequestManager::HandlerInterface>(new operations::GetMetadata( |
| 86 event_router_, file_system_info_, entry_path, fields, callback))); | 126 event_router_, file_system_info_, entry_path, fields, callback))); |
| 87 if (!request_id) { | 127 if (!request_id) { |
| 88 callback.Run(make_scoped_ptr<EntryMetadata>(NULL), | 128 callback.Run(make_scoped_ptr<EntryMetadata>(NULL), |
| 89 base::File::FILE_ERROR_SECURITY); | 129 base::File::FILE_ERROR_SECURITY); |
| 90 return AbortCallback(); | 130 return AbortCallback(); |
| 91 } | 131 } |
| 92 | 132 |
| 93 return base::Bind( | 133 return base::Bind( |
| 94 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 134 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 95 } | 135 } |
| 96 | 136 |
| 97 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ReadDirectory( | 137 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ReadDirectory( |
| 98 const base::FilePath& directory_path, | 138 const base::FilePath& directory_path, |
| 99 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) { | 139 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) { |
| 100 const int request_id = request_manager_.CreateRequest( | 140 const int request_id = request_manager_->CreateRequest( |
| 101 READ_DIRECTORY, | 141 READ_DIRECTORY, |
| 102 scoped_ptr<RequestManager::HandlerInterface>( | 142 scoped_ptr<RequestManager::HandlerInterface>( |
| 103 new operations::ReadDirectory( | 143 new operations::ReadDirectory( |
| 104 event_router_, file_system_info_, directory_path, callback))); | 144 event_router_, file_system_info_, directory_path, callback))); |
| 105 if (!request_id) { | 145 if (!request_id) { |
| 106 callback.Run(base::File::FILE_ERROR_SECURITY, | 146 callback.Run(base::File::FILE_ERROR_SECURITY, |
| 107 storage::AsyncFileUtil::EntryList(), | 147 storage::AsyncFileUtil::EntryList(), |
| 108 false /* has_more */); | 148 false /* has_more */); |
| 109 return AbortCallback(); | 149 return AbortCallback(); |
| 110 } | 150 } |
| 111 | 151 |
| 112 return base::Bind( | 152 return base::Bind( |
| 113 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 153 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 114 } | 154 } |
| 115 | 155 |
| 116 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ReadFile( | 156 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ReadFile( |
| 117 int file_handle, | 157 int file_handle, |
| 118 net::IOBuffer* buffer, | 158 net::IOBuffer* buffer, |
| 119 int64 offset, | 159 int64 offset, |
| 120 int length, | 160 int length, |
| 121 const ReadChunkReceivedCallback& callback) { | 161 const ReadChunkReceivedCallback& callback) { |
| 122 TRACE_EVENT1( | 162 TRACE_EVENT1( |
| 123 "file_system_provider", "ProvidedFileSystem::ReadFile", "length", length); | 163 "file_system_provider", "ProvidedFileSystem::ReadFile", "length", length); |
| 124 const int request_id = request_manager_.CreateRequest( | 164 const int request_id = request_manager_->CreateRequest( |
| 125 READ_FILE, | 165 READ_FILE, |
| 126 make_scoped_ptr<RequestManager::HandlerInterface>( | 166 make_scoped_ptr<RequestManager::HandlerInterface>( |
| 127 new operations::ReadFile(event_router_, | 167 new operations::ReadFile(event_router_, |
| 128 file_system_info_, | 168 file_system_info_, |
| 129 file_handle, | 169 file_handle, |
| 130 buffer, | 170 buffer, |
| 131 offset, | 171 offset, |
| 132 length, | 172 length, |
| 133 callback))); | 173 callback))); |
| 134 if (!request_id) { | 174 if (!request_id) { |
| 135 callback.Run(0 /* chunk_length */, | 175 callback.Run(0 /* chunk_length */, |
| 136 false /* has_more */, | 176 false /* has_more */, |
| 137 base::File::FILE_ERROR_SECURITY); | 177 base::File::FILE_ERROR_SECURITY); |
| 138 return AbortCallback(); | 178 return AbortCallback(); |
| 139 } | 179 } |
| 140 | 180 |
| 141 return base::Bind( | 181 return base::Bind( |
| 142 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 182 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 143 } | 183 } |
| 144 | 184 |
| 145 ProvidedFileSystem::AbortCallback ProvidedFileSystem::OpenFile( | 185 ProvidedFileSystem::AbortCallback ProvidedFileSystem::OpenFile( |
| 146 const base::FilePath& file_path, | 186 const base::FilePath& file_path, |
| 147 OpenFileMode mode, | 187 OpenFileMode mode, |
| 148 const OpenFileCallback& callback) { | 188 const OpenFileCallback& callback) { |
| 149 const int request_id = request_manager_.CreateRequest( | 189 const int request_id = request_manager_->CreateRequest( |
| 150 OPEN_FILE, | 190 OPEN_FILE, |
| 151 scoped_ptr<RequestManager::HandlerInterface>(new operations::OpenFile( | 191 scoped_ptr<RequestManager::HandlerInterface>(new operations::OpenFile( |
| 152 event_router_, file_system_info_, file_path, mode, callback))); | 192 event_router_, file_system_info_, file_path, mode, callback))); |
| 153 if (!request_id) { | 193 if (!request_id) { |
| 154 callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY); | 194 callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY); |
| 155 return AbortCallback(); | 195 return AbortCallback(); |
| 156 } | 196 } |
| 157 | 197 |
| 158 return base::Bind( | 198 return base::Bind( |
| 159 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 199 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 160 } | 200 } |
| 161 | 201 |
| 162 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CloseFile( | 202 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CloseFile( |
| 163 int file_handle, | 203 int file_handle, |
| 164 const storage::AsyncFileUtil::StatusCallback& callback) { | 204 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 165 const int request_id = request_manager_.CreateRequest( | 205 const int request_id = request_manager_->CreateRequest( |
| 166 CLOSE_FILE, | 206 CLOSE_FILE, |
| 167 scoped_ptr<RequestManager::HandlerInterface>(new operations::CloseFile( | 207 scoped_ptr<RequestManager::HandlerInterface>(new operations::CloseFile( |
| 168 event_router_, file_system_info_, file_handle, callback))); | 208 event_router_, file_system_info_, file_handle, callback))); |
| 169 if (!request_id) { | 209 if (!request_id) { |
| 170 callback.Run(base::File::FILE_ERROR_SECURITY); | 210 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 171 return AbortCallback(); | 211 return AbortCallback(); |
| 172 } | 212 } |
| 173 | 213 |
| 174 return base::Bind( | 214 return base::Bind( |
| 175 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 215 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 176 } | 216 } |
| 177 | 217 |
| 178 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CreateDirectory( | 218 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CreateDirectory( |
| 179 const base::FilePath& directory_path, | 219 const base::FilePath& directory_path, |
| 180 bool recursive, | 220 bool recursive, |
| 181 const storage::AsyncFileUtil::StatusCallback& callback) { | 221 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 182 const int request_id = request_manager_.CreateRequest( | 222 const int request_id = request_manager_->CreateRequest( |
| 183 CREATE_DIRECTORY, | 223 CREATE_DIRECTORY, |
| 184 scoped_ptr<RequestManager::HandlerInterface>( | 224 scoped_ptr<RequestManager::HandlerInterface>( |
| 185 new operations::CreateDirectory(event_router_, | 225 new operations::CreateDirectory(event_router_, |
| 186 file_system_info_, | 226 file_system_info_, |
| 187 directory_path, | 227 directory_path, |
| 188 recursive, | 228 recursive, |
| 189 callback))); | 229 callback))); |
| 190 if (!request_id) { | 230 if (!request_id) { |
| 191 callback.Run(base::File::FILE_ERROR_SECURITY); | 231 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 192 return AbortCallback(); | 232 return AbortCallback(); |
| 193 } | 233 } |
| 194 | 234 |
| 195 return base::Bind( | 235 return base::Bind( |
| 196 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 236 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 197 } | 237 } |
| 198 | 238 |
| 199 ProvidedFileSystem::AbortCallback ProvidedFileSystem::DeleteEntry( | 239 ProvidedFileSystem::AbortCallback ProvidedFileSystem::DeleteEntry( |
| 200 const base::FilePath& entry_path, | 240 const base::FilePath& entry_path, |
| 201 bool recursive, | 241 bool recursive, |
| 202 const storage::AsyncFileUtil::StatusCallback& callback) { | 242 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 203 const int request_id = request_manager_.CreateRequest( | 243 const int request_id = request_manager_->CreateRequest( |
| 204 DELETE_ENTRY, | 244 DELETE_ENTRY, |
| 205 scoped_ptr<RequestManager::HandlerInterface>(new operations::DeleteEntry( | 245 scoped_ptr<RequestManager::HandlerInterface>(new operations::DeleteEntry( |
| 206 event_router_, file_system_info_, entry_path, recursive, callback))); | 246 event_router_, file_system_info_, entry_path, recursive, callback))); |
| 207 if (!request_id) { | 247 if (!request_id) { |
| 208 callback.Run(base::File::FILE_ERROR_SECURITY); | 248 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 209 return AbortCallback(); | 249 return AbortCallback(); |
| 210 } | 250 } |
| 211 | 251 |
| 212 return base::Bind( | 252 return base::Bind( |
| 213 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 253 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 214 } | 254 } |
| 215 | 255 |
| 216 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CreateFile( | 256 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CreateFile( |
| 217 const base::FilePath& file_path, | 257 const base::FilePath& file_path, |
| 218 const storage::AsyncFileUtil::StatusCallback& callback) { | 258 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 219 const int request_id = request_manager_.CreateRequest( | 259 const int request_id = request_manager_->CreateRequest( |
| 220 CREATE_FILE, | 260 CREATE_FILE, |
| 221 scoped_ptr<RequestManager::HandlerInterface>(new operations::CreateFile( | 261 scoped_ptr<RequestManager::HandlerInterface>(new operations::CreateFile( |
| 222 event_router_, file_system_info_, file_path, callback))); | 262 event_router_, file_system_info_, file_path, callback))); |
| 223 if (!request_id) { | 263 if (!request_id) { |
| 224 callback.Run(base::File::FILE_ERROR_SECURITY); | 264 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 225 return AbortCallback(); | 265 return AbortCallback(); |
| 226 } | 266 } |
| 227 | 267 |
| 228 return base::Bind( | 268 return base::Bind( |
| 229 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 269 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 230 } | 270 } |
| 231 | 271 |
| 232 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CopyEntry( | 272 ProvidedFileSystem::AbortCallback ProvidedFileSystem::CopyEntry( |
| 233 const base::FilePath& source_path, | 273 const base::FilePath& source_path, |
| 234 const base::FilePath& target_path, | 274 const base::FilePath& target_path, |
| 235 const storage::AsyncFileUtil::StatusCallback& callback) { | 275 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 236 const int request_id = request_manager_.CreateRequest( | 276 const int request_id = request_manager_->CreateRequest( |
| 237 COPY_ENTRY, | 277 COPY_ENTRY, |
| 238 scoped_ptr<RequestManager::HandlerInterface>( | 278 scoped_ptr<RequestManager::HandlerInterface>( |
| 239 new operations::CopyEntry(event_router_, | 279 new operations::CopyEntry(event_router_, |
| 240 file_system_info_, | 280 file_system_info_, |
| 241 source_path, | 281 source_path, |
| 242 target_path, | 282 target_path, |
| 243 callback))); | 283 callback))); |
| 244 if (!request_id) { | 284 if (!request_id) { |
| 245 callback.Run(base::File::FILE_ERROR_SECURITY); | 285 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 246 return AbortCallback(); | 286 return AbortCallback(); |
| 247 } | 287 } |
| 248 | 288 |
| 249 return base::Bind( | 289 return base::Bind( |
| 250 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 290 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 251 } | 291 } |
| 252 | 292 |
| 253 ProvidedFileSystem::AbortCallback ProvidedFileSystem::WriteFile( | 293 ProvidedFileSystem::AbortCallback ProvidedFileSystem::WriteFile( |
| 254 int file_handle, | 294 int file_handle, |
| 255 net::IOBuffer* buffer, | 295 net::IOBuffer* buffer, |
| 256 int64 offset, | 296 int64 offset, |
| 257 int length, | 297 int length, |
| 258 const storage::AsyncFileUtil::StatusCallback& callback) { | 298 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 259 TRACE_EVENT1("file_system_provider", | 299 TRACE_EVENT1("file_system_provider", |
| 260 "ProvidedFileSystem::WriteFile", | 300 "ProvidedFileSystem::WriteFile", |
| 261 "length", | 301 "length", |
| 262 length); | 302 length); |
| 263 const int request_id = request_manager_.CreateRequest( | 303 const int request_id = request_manager_->CreateRequest( |
| 264 WRITE_FILE, | 304 WRITE_FILE, |
| 265 make_scoped_ptr<RequestManager::HandlerInterface>( | 305 make_scoped_ptr<RequestManager::HandlerInterface>( |
| 266 new operations::WriteFile(event_router_, | 306 new operations::WriteFile(event_router_, |
| 267 file_system_info_, | 307 file_system_info_, |
| 268 file_handle, | 308 file_handle, |
| 269 make_scoped_refptr(buffer), | 309 make_scoped_refptr(buffer), |
| 270 offset, | 310 offset, |
| 271 length, | 311 length, |
| 272 callback))); | 312 callback))); |
| 273 if (!request_id) { | 313 if (!request_id) { |
| 274 callback.Run(base::File::FILE_ERROR_SECURITY); | 314 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 275 return AbortCallback(); | 315 return AbortCallback(); |
| 276 } | 316 } |
| 277 | 317 |
| 278 return base::Bind( | 318 return base::Bind( |
| 279 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 319 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 280 } | 320 } |
| 281 | 321 |
| 282 ProvidedFileSystem::AbortCallback ProvidedFileSystem::MoveEntry( | 322 ProvidedFileSystem::AbortCallback ProvidedFileSystem::MoveEntry( |
| 283 const base::FilePath& source_path, | 323 const base::FilePath& source_path, |
| 284 const base::FilePath& target_path, | 324 const base::FilePath& target_path, |
| 285 const storage::AsyncFileUtil::StatusCallback& callback) { | 325 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 286 const int request_id = request_manager_.CreateRequest( | 326 const int request_id = request_manager_->CreateRequest( |
| 287 MOVE_ENTRY, | 327 MOVE_ENTRY, |
| 288 scoped_ptr<RequestManager::HandlerInterface>( | 328 scoped_ptr<RequestManager::HandlerInterface>( |
| 289 new operations::MoveEntry(event_router_, | 329 new operations::MoveEntry(event_router_, |
| 290 file_system_info_, | 330 file_system_info_, |
| 291 source_path, | 331 source_path, |
| 292 target_path, | 332 target_path, |
| 293 callback))); | 333 callback))); |
| 294 if (!request_id) { | 334 if (!request_id) { |
| 295 callback.Run(base::File::FILE_ERROR_SECURITY); | 335 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 296 return AbortCallback(); | 336 return AbortCallback(); |
| 297 } | 337 } |
| 298 | 338 |
| 299 return base::Bind( | 339 return base::Bind( |
| 300 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 340 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 301 } | 341 } |
| 302 | 342 |
| 303 ProvidedFileSystem::AbortCallback ProvidedFileSystem::Truncate( | 343 ProvidedFileSystem::AbortCallback ProvidedFileSystem::Truncate( |
| 304 const base::FilePath& file_path, | 344 const base::FilePath& file_path, |
| 305 int64 length, | 345 int64 length, |
| 306 const storage::AsyncFileUtil::StatusCallback& callback) { | 346 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 307 const int request_id = request_manager_.CreateRequest( | 347 const int request_id = request_manager_->CreateRequest( |
| 308 TRUNCATE, | 348 TRUNCATE, |
| 309 scoped_ptr<RequestManager::HandlerInterface>(new operations::Truncate( | 349 scoped_ptr<RequestManager::HandlerInterface>(new operations::Truncate( |
| 310 event_router_, file_system_info_, file_path, length, callback))); | 350 event_router_, file_system_info_, file_path, length, callback))); |
| 311 if (!request_id) { | 351 if (!request_id) { |
| 312 callback.Run(base::File::FILE_ERROR_SECURITY); | 352 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 313 return AbortCallback(); | 353 return AbortCallback(); |
| 314 } | 354 } |
| 315 | 355 |
| 316 return base::Bind( | 356 return base::Bind( |
| 317 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 357 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 318 } | 358 } |
| 319 | 359 |
| 360 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ObserveDirectory( |
| 361 const base::FilePath& directory_path, |
| 362 bool recursive, |
| 363 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 364 // TODO(mtomasz): Wrap the entire method body with an asynchronous queue to |
| 365 // avoid races. |
| 366 const ObservedEntries::const_iterator it = |
| 367 observed_entries_.find(directory_path); |
| 368 if (it != observed_entries_.end()) { |
| 369 if (!recursive || it->second.recursive) { |
| 370 callback.Run(base::File::FILE_ERROR_EXISTS); |
| 371 return AbortCallback(); |
| 372 } |
| 373 } |
| 374 |
| 375 const int request_id = request_manager_->CreateRequest( |
| 376 OBSERVE_DIRECTORY, |
| 377 scoped_ptr<RequestManager::HandlerInterface>( |
| 378 new operations::ObserveDirectory( |
| 379 event_router_, |
| 380 file_system_info_, |
| 381 directory_path, |
| 382 recursive, |
| 383 base::Bind(&ProvidedFileSystem::OnObserveDirectoryCompleted, |
| 384 weak_ptr_factory_.GetWeakPtr(), |
| 385 directory_path, |
| 386 recursive, |
| 387 callback)))); |
| 388 if (!request_id) { |
| 389 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 390 return AbortCallback(); |
| 391 } |
| 392 |
| 393 return base::Bind( |
| 394 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 395 } |
| 396 |
| 397 void ProvidedFileSystem::UnobserveEntry( |
| 398 const base::FilePath& entry_path, |
| 399 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 400 const ObservedEntries::const_iterator it = observed_entries_.find(entry_path); |
| 401 if (it == observed_entries_.end()) { |
| 402 callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
| 403 return; |
| 404 } |
| 405 |
| 406 // Delete the watcher in advance since the list of observed entries is owned |
| 407 // by the C++ layer, not by the extension. |
| 408 observed_entries_.erase(it); |
| 409 |
| 410 FOR_EACH_OBSERVER(ProvidedFileSystemObserver, |
| 411 observers_, |
| 412 OnObservedEntryListChanged(file_system_info_)); |
| 413 |
| 414 // TODO(mtomasz): Consider returning always an OK error code, since for the |
| 415 // callers it's important that the entry is not watched anymore. The watcher |
| 416 // is removed even if the extension returns an error. |
| 417 const int request_id = request_manager_->CreateRequest( |
| 418 UNOBSERVE_ENTRY, |
| 419 scoped_ptr<RequestManager::HandlerInterface>( |
| 420 new operations::UnobserveEntry( |
| 421 event_router_, file_system_info_, entry_path, callback))); |
| 422 if (!request_id) |
| 423 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 424 } |
| 425 |
| 320 const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const { | 426 const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const { |
| 321 return file_system_info_; | 427 return file_system_info_; |
| 322 } | 428 } |
| 323 | 429 |
| 324 RequestManager* ProvidedFileSystem::GetRequestManager() { | 430 RequestManager* ProvidedFileSystem::GetRequestManager() { |
| 325 return &request_manager_; | 431 return request_manager_.get(); |
| 432 } |
| 433 |
| 434 ProvidedFileSystem::ObservedEntries* ProvidedFileSystem::GetObservedEntries() { |
| 435 return &observed_entries_; |
| 436 } |
| 437 |
| 438 void ProvidedFileSystem::AddObserver(ProvidedFileSystemObserver* observer) { |
| 439 DCHECK(observer); |
| 440 observers_.AddObserver(observer); |
| 441 } |
| 442 |
| 443 void ProvidedFileSystem::RemoveObserver(ProvidedFileSystemObserver* observer) { |
| 444 DCHECK(observer); |
| 445 observers_.RemoveObserver(observer); |
| 446 } |
| 447 |
| 448 bool ProvidedFileSystem::Notify( |
| 449 const base::FilePath& observed_path, |
| 450 ProvidedFileSystemObserver::ChangeType change_type, |
| 451 const ProvidedFileSystemObserver::ChildChanges& child_changes, |
| 452 const std::string& tag) { |
| 453 const ObservedEntries::iterator it = observed_entries_.find(observed_path); |
| 454 if (it == observed_entries_.end()) |
| 455 return false; |
| 456 |
| 457 // The tag must be provided if and only if it's explicitly supported. |
| 458 if (file_system_info_.supports_notify_tag() == tag.empty()) |
| 459 return false; |
| 460 |
| 461 scoped_refptr<AutoUpdater> auto_updater( |
| 462 new AutoUpdater(base::Bind(&ProvidedFileSystem::OnNotifyCompleted, |
| 463 weak_ptr_factory_.GetWeakPtr(), |
| 464 observed_path, |
| 465 change_type, |
| 466 it->second.last_tag, |
| 467 tag))); |
| 468 |
| 469 FOR_EACH_OBSERVER(ProvidedFileSystemObserver, |
| 470 observers_, |
| 471 OnObservedEntryChanged(file_system_info_, |
| 472 observed_path, |
| 473 change_type, |
| 474 child_changes, |
| 475 auto_updater->CreateCallback())); |
| 476 |
| 477 return true; |
| 326 } | 478 } |
| 327 | 479 |
| 328 base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() { | 480 base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() { |
| 329 return weak_ptr_factory_.GetWeakPtr(); | 481 return weak_ptr_factory_.GetWeakPtr(); |
| 330 } | 482 } |
| 331 | 483 |
| 332 void ProvidedFileSystem::Abort( | 484 void ProvidedFileSystem::Abort( |
| 333 int operation_request_id, | 485 int operation_request_id, |
| 334 const storage::AsyncFileUtil::StatusCallback& callback) { | 486 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 335 request_manager_.RejectRequest(operation_request_id, | 487 request_manager_->RejectRequest(operation_request_id, |
| 336 make_scoped_ptr(new RequestValue()), | 488 make_scoped_ptr(new RequestValue()), |
| 337 base::File::FILE_ERROR_ABORT); | 489 base::File::FILE_ERROR_ABORT); |
| 338 if (!request_manager_.CreateRequest( | 490 if (!request_manager_->CreateRequest( |
| 339 ABORT, | 491 ABORT, |
| 340 scoped_ptr<RequestManager::HandlerInterface>( | 492 scoped_ptr<RequestManager::HandlerInterface>( |
| 341 new operations::Abort(event_router_, | 493 new operations::Abort(event_router_, |
| 342 file_system_info_, | 494 file_system_info_, |
| 343 operation_request_id, | 495 operation_request_id, |
| 344 callback)))) { | 496 callback)))) { |
| 345 callback.Run(base::File::FILE_ERROR_SECURITY); | 497 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 346 } | 498 } |
| 347 } | 499 } |
| 348 | 500 |
| 501 void ProvidedFileSystem::OnObserveDirectoryCompleted( |
| 502 const base::FilePath& directory_path, |
| 503 bool recursive, |
| 504 const storage::AsyncFileUtil::StatusCallback& callback, |
| 505 base::File::Error result) { |
| 506 if (result != base::File::FILE_OK) { |
| 507 callback.Run(result); |
| 508 return; |
| 509 } |
| 510 |
| 511 observed_entries_[directory_path].entry_path = directory_path; |
| 512 observed_entries_[directory_path].recursive |= recursive; |
| 513 |
| 514 FOR_EACH_OBSERVER(ProvidedFileSystemObserver, |
| 515 observers_, |
| 516 OnObservedEntryListChanged(file_system_info_)); |
| 517 |
| 518 callback.Run(result); |
| 519 } |
| 520 |
| 521 void ProvidedFileSystem::OnNotifyCompleted( |
| 522 const base::FilePath& observed_path, |
| 523 ProvidedFileSystemObserver::ChangeType change_type, |
| 524 const std::string& last_tag, |
| 525 const std::string& tag) { |
| 526 const ObservedEntries::iterator it = observed_entries_.find(observed_path); |
| 527 // Check if the entry is still observed. |
| 528 if (it == observed_entries_.end()) |
| 529 return; |
| 530 |
| 531 // Another following notification finished earlier. |
| 532 if (it->second.last_tag != last_tag) |
| 533 return; |
| 534 |
| 535 // It's illegal to provide a tag which is not unique. As for now only an error |
| 536 // message is printed, but we may want to pass the error to the providing |
| 537 // extension. TODO(mtomasz): Consider it. |
| 538 if (!tag.empty() && tag == it->second.last_tag) |
| 539 LOG(ERROR) << "Tag specified, but same as the previous one."; |
| 540 |
| 541 it->second.last_tag = tag; |
| 542 |
| 543 FOR_EACH_OBSERVER( |
| 544 ProvidedFileSystemObserver, |
| 545 observers_, |
| 546 OnObservedEntryTagUpdated(file_system_info_, observed_path)); |
| 547 |
| 548 // If the observed entry is deleted, then unobserve it. |
| 549 if (change_type == ProvidedFileSystemObserver::DELETED) |
| 550 UnobserveEntry(observed_path, base::Bind(&EmptyStatusCallback)); |
| 551 } |
| 552 |
| 349 } // namespace file_system_provider | 553 } // namespace file_system_provider |
| 350 } // namespace chromeos | 554 } // namespace chromeos |
| OLD | NEW |