Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: chrome/browser/chromeos/file_system_provider/provided_file_system.cc

Issue 625463002: [fsp] Add support for observing entries and notifying about changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Dicards 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
45 // Handles
46
43 } // namespace 47 } // namespace
44 48
45 ProvidedFileSystem::ProvidedFileSystem( 49 ProvidedFileSystem::ProvidedFileSystem(
46 Profile* profile, 50 Profile* profile,
47 const ProvidedFileSystemInfo& file_system_info) 51 const ProvidedFileSystemInfo& file_system_info)
48 : profile_(profile), 52 : profile_(profile),
49 event_router_(extensions::EventRouter::Get(profile)), // May be NULL. 53 event_router_(extensions::EventRouter::Get(profile)), // May be NULL.
50 file_system_info_(file_system_info), 54 file_system_info_(file_system_info),
51 notification_manager_( 55 notification_manager_(
52 new NotificationManager(profile_, file_system_info_)), 56 new NotificationManager(profile_, file_system_info_)),
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 event_router_, file_system_info_, file_path, length, callback))); 314 event_router_, file_system_info_, file_path, length, callback)));
311 if (!request_id) { 315 if (!request_id) {
312 callback.Run(base::File::FILE_ERROR_SECURITY); 316 callback.Run(base::File::FILE_ERROR_SECURITY);
313 return AbortCallback(); 317 return AbortCallback();
314 } 318 }
315 319
316 return base::Bind( 320 return base::Bind(
317 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); 321 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id);
318 } 322 }
319 323
324 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ObserveDirectory(
325 const base::FilePath& directory_path,
326 bool recursive,
327 const storage::AsyncFileUtil::StatusCallback& callback) {
328 // TODO(mtomasz): Wrap with an asynchronous queue.
329 const int request_id = request_manager_.CreateRequest(
330 OBSERVE_DIRECTORY,
331 scoped_ptr<RequestManager::HandlerInterface>(
332 new operations::ObserveDirectory(
333 event_router_,
334 file_system_info_,
335 directory_path,
336 recursive,
337 base::Bind(&ProvidedFileSystem::OnObserveDirectoryCompleted,
338 weak_ptr_factory_.GetWeakPtr(),
339 directory_path,
340 recursive,
341 callback))));
342 if (!request_id) {
343 callback.Run(base::File::FILE_ERROR_SECURITY);
344 return AbortCallback();
345 }
346
347 return base::Bind(
348 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id);
349 }
350
351 ProvidedFileSystem::AbortCallback ProvidedFileSystem::UnobserveEntry(
352 const base::FilePath& entry_path,
353 const storage::AsyncFileUtil::StatusCallback& callback) {
354 // TODO(mtomasz): Wrap with an asynchronous queue.
355 const ObservedEntries::const_iterator it = observed_entries_.find(entry_path);
356 if (it != observed_entries_.end()) {
357 callback.Run(base::File::FILE_ERROR_NOT_FOUND);
358 return AbortCallback();
359 }
360
361 // Delete the watcher in advance since the list of observed entries is owned
362 // by the C++ layer, not by the extension.
363 observed_entries_.erase(it);
364
365 FOR_EACH_OBSERVER(Observer, observers_, OnObservedEntriesListChanged());
366
367 // TODO(mtomasz): Consider returning always an OK error code, since for the
368 // callers it's important that the entry is not watched anymore. The watcher
369 // is removed even if the extension returns an error.
370 const int request_id = request_manager_.CreateRequest(
371 UNOBSERVE_ENTRY,
372 scoped_ptr<RequestManager::HandlerInterface>(
373 new operations::UnobserveEntry(
374 event_router_, file_system_info_, entry_path, callback)));
375 if (!request_id) {
376 callback.Run(base::File::FILE_ERROR_SECURITY);
377 return AbortCallback();
378 }
379
380 return base::Bind(
381 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id);
382 }
383
320 const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const { 384 const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const {
321 return file_system_info_; 385 return file_system_info_;
322 } 386 }
323 387
324 RequestManager* ProvidedFileSystem::GetRequestManager() { 388 RequestManager* ProvidedFileSystem::GetRequestManager() {
325 return &request_manager_; 389 return &request_manager_;
326 } 390 }
327 391
392 ProvidedFileSystem::ObservedEntries* ProvidedFileSystem::GetObservedEntries() {
393 return &observed_entries_;
394 }
395
396 void ProvidedFileSystem::AddObserver(Observer* observer) {
397 DCHECK(observer);
398 observers_.AddObserver(observer);
399 }
400
401 void ProvidedFileSystem::RemoveObserver(Observer* observer) {
402 DCHECK(observer);
403 observers_.RemoveObserver(observer);
404 }
405
406 ObserverList<ProvidedFileSystemInterface::Observer>*
407 ProvidedFileSystem::GetObservers() {
408 return &observers_;
409 }
410
328 base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() { 411 base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() {
329 return weak_ptr_factory_.GetWeakPtr(); 412 return weak_ptr_factory_.GetWeakPtr();
330 } 413 }
331 414
332 void ProvidedFileSystem::Abort( 415 void ProvidedFileSystem::Abort(
333 int operation_request_id, 416 int operation_request_id,
334 const storage::AsyncFileUtil::StatusCallback& callback) { 417 const storage::AsyncFileUtil::StatusCallback& callback) {
335 request_manager_.RejectRequest(operation_request_id, 418 request_manager_.RejectRequest(operation_request_id,
336 make_scoped_ptr(new RequestValue()), 419 make_scoped_ptr(new RequestValue()),
337 base::File::FILE_ERROR_ABORT); 420 base::File::FILE_ERROR_ABORT);
338 if (!request_manager_.CreateRequest( 421 if (!request_manager_.CreateRequest(
339 ABORT, 422 ABORT,
340 scoped_ptr<RequestManager::HandlerInterface>( 423 scoped_ptr<RequestManager::HandlerInterface>(
341 new operations::Abort(event_router_, 424 new operations::Abort(event_router_,
342 file_system_info_, 425 file_system_info_,
343 operation_request_id, 426 operation_request_id,
344 callback)))) { 427 callback)))) {
345 callback.Run(base::File::FILE_ERROR_SECURITY); 428 callback.Run(base::File::FILE_ERROR_SECURITY);
346 } 429 }
347 } 430 }
348 431
432 void ProvidedFileSystem::OnObserveDirectoryCompleted(
433 const base::FilePath& directory_path,
434 bool recursive,
435 const storage::AsyncFileUtil::StatusCallback& callback,
436 base::File::Error result) {
437 if (result != base::File::FILE_OK) {
438 callback.Run(result);
439 return;
440 }
441
442 observed_entries_[directory_path].entry_path = directory_path;
443 observed_entries_[directory_path].recursive |= recursive;
444
445 FOR_EACH_OBSERVER(Observer, observers_, OnObservedEntriesListChanged());
446
447 callback.Run(result);
448 }
449
349 } // namespace file_system_provider 450 } // namespace file_system_provider
350 } // namespace chromeos 451 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698