summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata/gdata_file_system.h
blob: 2b6e11d3dd737ba23a2d5c88fc9b2e1a6af730e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_

#include <sys/stat.h>

#include <map>
#include <string>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/platform_file.h"
#include "base/synchronization/lock.h"
#include "chrome/browser/chromeos/gdata/gdata_files.h"
#include "chrome/browser/chromeos/gdata/gdata_operation_registry.h"
#include "chrome/browser/chromeos/gdata/gdata_params.h"
#include "chrome/browser/chromeos/gdata/gdata_parser.h"
#include "chrome/browser/chromeos/gdata/gdata_uploader.h"
#include "chrome/browser/profiles/profile_keyed_service.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"

namespace base {
class WaitableEvent;
}

namespace gdata {

class DocumentsServiceInterface;

// Callback for completion of cache operation.
typedef base::Callback<void(base::PlatformFileError error,
                            const std::string& resource_id,
                            const std::string& md5)> CacheOperationCallback;

// Callback for GetFromCache.
typedef base::Callback<void(base::PlatformFileError error,
                            const std::string& resource_id,
                            const std::string& md5,
                            const FilePath& gdata_file_path,
                            const FilePath& cache_file_path)>
    GetFromCacheCallback;

// Used to get result of file search. Please note that |file| is a live
// object provided to this callback under lock. It must not be used outside
// of the callback method. This callback can be invoked on different thread
// than one that started FileFileByPath() request.
typedef base::Callback<void(base::PlatformFileError error,
                            const FilePath& directory_path,
                            GDataFileBase* file)>
    FindFileCallback;

// Used for file operations like removing files.
typedef base::Callback<void(base::PlatformFileError error)>
    FileOperationCallback;

// Used to get files from the file system.
typedef base::Callback<void(base::PlatformFileError error,
                            const FilePath& file_path,
                            GDataFileType file_type)>
    GetFileCallback;

// Used for file operations like removing files.
typedef base::Callback<void(base::PlatformFileError error,
                            scoped_ptr<base::Value> value)>
    GetJsonDocumentCallback;

// Used to get available space for the account from GData.
typedef base::Callback<void(base::PlatformFileError error,
                            int bytes_total,
                            int bytes_used)>
    GetAvailableSpaceCallback;

// Callback type for DocumentServiceInterface::ResumeUpload.
typedef base::Callback<void(
    const ResumeUploadResponse& response,
    scoped_ptr<DocumentEntry> entry)> ResumeFileUploadCallback;


// Delegate class used to deal with results synchronous read-only search
// over virtual file system.
class FindFileDelegate {
 public:
  virtual ~FindFileDelegate();

  // Called when FindFileByPathSync() completes search.
  virtual void OnDone(base::PlatformFileError error,
                      const FilePath& directory_path,
                      GDataFileBase* file) = 0;
};

// Delegate used to find a directory element for file system updates.
class ReadOnlyFindFileDelegate : public FindFileDelegate {
 public:
  ReadOnlyFindFileDelegate();

  // Returns found file.
  GDataFileBase* file() { return file_; }

 private:
  // FindFileDelegate overrides.
  virtual void OnDone(base::PlatformFileError error,
                      const FilePath& directory_path,
                      GDataFileBase* file) OVERRIDE;

  // File entry that was found.
  GDataFileBase* file_;
};

// Helper structure used for extracting key properties from GDataFile object.
struct GDataFileProperties {
  GDataFileProperties();
  ~GDataFileProperties();

  base::PlatformFileInfo file_info;
  std::string resource_id;
  std::string file_md5;
  GURL content_url;
  GURL edit_url;
  bool is_hosted_document;
};

// GData file system abstraction layer.
// The interface is defined to make GDataFileSystem mockable.
class GDataFileSystemInterface {
 public:
  virtual ~GDataFileSystemInterface() {}

  // Used to notify events on the file system.
  // Hence, the observers should relay these events to a certain thread
  // themselves if needed.
  class Observer {
   public:
    // Triggered when the cache has been initialized.
    // For this method, there is no guarantee on what thread the observers will
    // get notified .
    virtual void OnCacheInitialized() {}

    // Triggered when a file has been pinned, after the cache state is
    // updated.
    // For this method, there is no guarantee on what thread the observers will
    // get notified .
    virtual void OnFilePinned(const std::string& resource_id,
                              const std::string& md5) {}

    // Triggered when a file has been unpinned, after the cache state is
    // updated.
    // For this method, there is no guarantee on what thread the observers will
    // get notified .
    virtual void OnFileUnpinned(const std::string& resource_id,
                                const std::string& md5) {}

    // Triggered when a content of a directory has been changed.
    // |directory_path| is a virtual directory path (/gdata/...) representing
    // changed directory.
    // For this method, observers will be notified on UI thread.
    virtual void OnDirectoryChanged(const FilePath& directory_path) {}

   protected:
    virtual ~Observer() {}
  };

  // Initializes the object. This function should be called before any
  // other functions.
  virtual void Initialize() = 0;

  // Adds and removes the observer.
  virtual void AddObserver(Observer* observer) = 0;
  virtual void RemoveObserver(Observer* observer) = 0;

  // Enum defining origin of a cached file.
  enum CachedFileOrigin {
    CACHED_FILE_FROM_SERVER = 0,
    CACHED_FILE_LOCALLY_MODIFIED,
  };

  // Authenticates the user by fetching the auth token as
  // needed. |callback| will be run with the error code and the auth
  // token, on the thread this function is run.
  //
  // Must be called on UI thread.
  virtual void Authenticate(const AuthStatusCallback& callback) = 0;

  // Finds file info by using virtual |file_path|. This call will also
  // retrieve and refresh file system content from server and disk cache.
  //
  // Can be called from any thread.
  virtual void FindFileByPathAsync(const FilePath& file_path,
                                   const FindFileCallback& callback) = 0;

  // Finds file info by using virtual |file_path|. This call does not initiate
  // content refreshing and will invoke one of |delegate| methods directly as
  // it executes.
  //
  // Can be called from any thread.
  virtual void FindFileByPathSync(const FilePath& file_path,
                                  FindFileDelegate* delegate) = 0;

  // Copies |src_file_path| to |dest_file_path| on the file system.
  // |src_file_path| can be a hosted document (see limitations below).
  // |dest_file_path| is expected to be of the same type of |src_file_path|
  // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
  // a file).
  //
  // This method also has the following assumptions/limitations that may be
  // relaxed or addressed later:
  // - |src_file_path| cannot be a regular file (i.e. non-hosted document)
  //   or a directory.
  // - |dest_file_path| must not exist.
  // - The parent of |dest_file_path| must already exist.
  //
  // The file entries represented by |src_file_path| and the parent directory
  // of |dest_file_path| need to be present in the in-memory representation
  // of the file system.
  //
  // Can be called from any thread.
  virtual void Copy(const FilePath& src_file_path,
                    const FilePath& dest_file_path,
                    const FileOperationCallback& callback) = 0;

  // Moves |src_file_path| to |dest_file_path| on the file system.
  // |src_file_path| can be a file (regular or hosted document) or a directory.
  // |dest_file_path| is expected to be of the same type of |src_file_path|
  // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
  // a file).
  //
  // This method also has the following assumptions/limitations that may be
  // relaxed or addressed later:
  // - |dest_file_path| must not exist.
  // - The parent of |dest_file_path| must already exist.
  //
  // The file entries represented by |src_file_path| and the parent directory
  // of |dest_file_path| need to be present in the in-memory representation
  // of the file system.
  //
  // Can be called from any thread.
  virtual void Move(const FilePath& src_file_path,
                    const FilePath& dest_file_path,
                    const FileOperationCallback& callback) = 0;

  // Removes |file_path| from the file system.  If |is_recursive| is set and
  // |file_path| represents a directory, we will also delete all of its
  // contained children elements. The file entry represented by |file_path|
  // needs to be present in in-memory representation of the file system that
  // in order to be removed.
  //
  // TODO(zelidrag): Wire |is_recursive| through gdata api
  // (find appropriate calls for it).
  //
  // Can be called from any thread. |callback| is run on the calling thread.
  virtual void Remove(const FilePath& file_path,
                      bool is_recursive,
                      const FileOperationCallback& callback) = 0;

  // Creates new directory under |directory_path|. If |is_exclusive| is true,
  // an error is raised in case a directory is already present at the
  // |directory_path|. If |is_recursive| is true, the call creates parent
  // directories as needed just like mkdir -p does.
  //
  // Can be called from any thread. |callback| is run on the calling thread.
  virtual void CreateDirectory(const FilePath& directory_path,
                               bool is_exclusive,
                               bool is_recursive,
                               const FileOperationCallback& callback) = 0;

  // Gets |file_path| from the file system. The file entry represented by
  // |file_path| needs to be present in in-memory representation of the file
  // system in order to be retrieved. If the file is not cached, the file
  // will be downloaded through gdata api.
  //
  // Can be called from any thread. |callback| is run on the calling thread.
  virtual void GetFile(const FilePath& file_path,
                       const GetFileCallback& callback) = 0;

  // Gets absolute path of cache file corresponding to |gdata_file_path|.
  // Upon completion, |callback| is invoked on the same thread where this method
  // was called, with path if it exists and is accessible or empty FilePath
  // otherwise.
  virtual void GetFromCacheForPath(const FilePath& gdata_file_path,
                                   const GetFromCacheCallback& callback) = 0;

  // Obtains the list of currently active operations.
  virtual std::vector<GDataOperationRegistry::ProgressStatus>
  GetProgressStatusList() = 0;

  // Cancels ongoing operation for a given |file_path|. Returns true if
  // the operation was found and canceled.
  virtual bool CancelOperation(const FilePath& file_path) = 0;

  // Add operation observer.
  virtual void AddOperationObserver(
      GDataOperationRegistry::Observer* observer) = 0;

  // Remove operation observer.
  virtual void RemoveOperationObserver(
      GDataOperationRegistry::Observer* observer) = 0;

  // Gets the cache state of file corresponding to |resource_id| and |md5| if it
  // exists on disk.
  // Initializes cache if it has not been initialized.
  // Upon completion, |callback| is invoked on the thread where this method was
  // called with the cache state if file exists in cache or CACHE_STATE_NONE
  // otherwise.
  virtual void GetCacheState(const std::string& resource_id,
                             const std::string& md5,
                             const GetCacheStateCallback& callback) = 0;

  // Finds file object by |file_path| and returns its key |properties|.
  // Returns true if file was found.
  virtual bool GetFileInfoFromPath(const FilePath& gdata_file_path,
                                   GDataFileProperties* properties) = 0;

  // Returns the tmp sub-directory under gdata cache directory, i.e.
  // <user_profile_dir>/GCache/v1/tmp
  virtual FilePath GetGDataCacheTmpDirectory() const = 0;

  // Returns the tmp downloads sub-directory under gdata cache directory, i.e.
  // <user_profile_dir>/GCache/v1/tmp/downloads/
  virtual FilePath GetGDataTempDownloadFolderPath() const = 0;

  // Returns the pinned sub-directory under gdata cache directory, i.e.
  // <user_profile_dir>/GCache/v1/pinned
  virtual FilePath GetGDataCachePinnedDirectory() const = 0;

  // Returns the pinned sub-directory under gdata cache directory, i.e.
  // <user_profile_dir>/GCache/v1/pinned
  virtual FilePath GetGDataCachePersistentDirectory() const = 0;

  // Returns absolute path of the file if it were cached or to be cached.
  virtual FilePath GetCacheFilePath(
      const std::string& resource_id,
      const std::string& md5,
      GDataRootDirectory::CacheSubDirectoryType sub_dir_type,
      CachedFileOrigin file_orign) const = 0;

  // Fetches the user's Account Metadata to find out current quota information
  // and returns it to the callback.
  virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0;

  // Creates a new file from |entry| under |virtual_dir_path|. Stored its
  // content from |file_content_path| into the cache.
  virtual void AddDownloadedFile(const FilePath& virtual_dir_path,
                                 scoped_ptr<DocumentEntry> entry,
                                 const FilePath& file_content_path) = 0;
};

// The production implementation of GDataFileSystemInterface.
class GDataFileSystem : public GDataFileSystemInterface {
 public:
  GDataFileSystem(Profile* profile,
                  DocumentsServiceInterface* documents_service);
  virtual ~GDataFileSystem();

  // Shuts down the file system. All pending operations are canceled.
  void Shutdown();

  // GDataFileSystem overrides.
  virtual void Initialize() OVERRIDE;
  virtual void AddObserver(Observer* observer) OVERRIDE;
  virtual void RemoveObserver(Observer* observer) OVERRIDE;
  virtual void Authenticate(const AuthStatusCallback& callback) OVERRIDE;
  virtual void FindFileByPathAsync(const FilePath& file_path,
                                   const FindFileCallback& callback) OVERRIDE;
  virtual void FindFileByPathSync(const FilePath& file_path,
                                  FindFileDelegate* delegate) OVERRIDE;
  virtual void Copy(const FilePath& src_file_path,
                    const FilePath& dest_file_path,
                    const FileOperationCallback& callback) OVERRIDE;
  virtual void Move(const FilePath& src_file_path,
                    const FilePath& dest_file_path,
                    const FileOperationCallback& callback) OVERRIDE;
  virtual void Remove(const FilePath& file_path,
                      bool is_recursive,
                      const FileOperationCallback& callback) OVERRIDE;
  virtual void CreateDirectory(const FilePath& directory_path,
                               bool is_exclusive,
                               bool is_recursive,
                               const FileOperationCallback& callback) OVERRIDE;
  virtual void GetFile(const FilePath& file_path,
                       const GetFileCallback& callback) OVERRIDE;
  virtual void GetFromCacheForPath(
      const FilePath& gdata_file_path,
      const GetFromCacheCallback& callback) OVERRIDE;
  virtual std::vector<GDataOperationRegistry::ProgressStatus>
  GetProgressStatusList() OVERRIDE;
  virtual bool CancelOperation(const FilePath& file_path) OVERRIDE;
  virtual void AddOperationObserver(
      GDataOperationRegistry::Observer* observer) OVERRIDE;
  virtual void RemoveOperationObserver(
      GDataOperationRegistry::Observer* observer) OVERRIDE;
  virtual void GetCacheState(const std::string& resource_id,
                             const std::string& md5,
                             const GetCacheStateCallback& callback) OVERRIDE;
  virtual bool GetFileInfoFromPath(const FilePath& gdata_file_path,
                                   GDataFileProperties* properties) OVERRIDE;
  virtual FilePath GetGDataCacheTmpDirectory() const OVERRIDE;
  virtual FilePath GetGDataTempDownloadFolderPath() const OVERRIDE;
  virtual FilePath GetGDataCachePinnedDirectory() const OVERRIDE;
  virtual FilePath GetGDataCachePersistentDirectory() const OVERRIDE;
  virtual FilePath GetCacheFilePath(
      const std::string& resource_id,
      const std::string& md5,
      GDataRootDirectory::CacheSubDirectoryType sub_dir_type,
      CachedFileOrigin file_orign) const OVERRIDE;
  virtual void GetAvailableSpace(
      const GetAvailableSpaceCallback& callback) OVERRIDE;
  virtual void AddDownloadedFile(const FilePath& virtual_dir_path,
                                 scoped_ptr<DocumentEntry> entry,
                                 const FilePath& file_content_path) OVERRIDE;

 private:
  friend class GDataUploader;
  friend class GDataFileSystemTest;
  FRIEND_TEST_ALL_PREFIXES(GDataFileSystemTest,
                           FindFirstMissingParentDirectory);
  FRIEND_TEST_ALL_PREFIXES(GDataFileSystemTest,
                           GetGDataFileInfoFromPath);
  FRIEND_TEST_ALL_PREFIXES(GDataFileSystemTest,
                           GetFromCacheForPath);

  // Defines possible search results of FindFirstMissingParentDirectory().
  enum FindMissingDirectoryResult {
    // Target directory found, it's not a directory.
    FOUND_INVALID,
    // Found missing directory segment while searching for given directory.
    FOUND_MISSING,
    // Found target directory, it already exists.
    DIRECTORY_ALREADY_PRESENT,
  };

  // Defines set of parameters passes to intermediate callbacks during
  // execution of CreateDirectory() method.
  struct CreateDirectoryParams {
    CreateDirectoryParams(const FilePath& created_directory_path,
                          const FilePath& target_directory_path,
                          bool is_exclusive,
                          bool is_recursive,
                          const FileOperationCallback& callback);
    ~CreateDirectoryParams();

    const FilePath created_directory_path;
    const FilePath target_directory_path;
    const bool is_exclusive;
    const bool is_recursive;
    FileOperationCallback callback;
  };

  // Internal intermediate callback OnGetCacheState for GetCacheStateOnIOThread,
  // runs on calling thread, allows OnGetCacheState to lock GDataFile for safe
  // access by |callback|.
  typedef base::Callback<void(base::PlatformFileError error,
                              GDataFile* file,
                              int cache_state,
                              const GetCacheStateCallback& callback)>
      GetCacheStateIntermediateCallback;

  // Internal intermediate callback OnFilePinned and OnFileUnpinned for
  // PinOnIOThreadPool and UnpinOnIOThreadPool respectively, runs on calling
  // thread, allows OnFilePinned and OnFileUnpinned to notify observers.
  typedef base::Callback<void(base::PlatformFileError error,
                              const std::string& resource_id,
                              const std::string& md5,
                              const CacheOperationCallback& callback)>
      CacheOperationIntermediateCallback;

  // Parameters to pass from calling thread to all cache file operations tasks
  // on IO thread pool.
  struct ModifyCacheStateParams {
    ModifyCacheStateParams(
        const std::string& resource_id,
        const std::string& md5,
        const FilePath& source_path,
        const CacheOperationCallback& final_callback,
        const CacheOperationIntermediateCallback& intermediate_callback,
        scoped_refptr<base::MessageLoopProxy> relay_proxy);
    virtual ~ModifyCacheStateParams();

    const std::string resource_id;
    const std::string md5;
    const FilePath source_path;
    const CacheOperationCallback final_callback;
    const CacheOperationIntermediateCallback intermediate_callback;
    const scoped_refptr<base::MessageLoopProxy> relay_proxy;
  };

  // Defines set of parameters passed to intermediate callbacks during
  // execution of GetFile() method.
  struct GetFileFromCacheParams {
    GetFileFromCacheParams(const FilePath& virtual_file_path,
        const FilePath& local_tmp_path,
        const GURL& content_url,
        const std::string& resource_id,
        const std::string& md5,
        scoped_refptr<base::MessageLoopProxy> proxy,
        const GetFileCallback& callback);
    ~GetFileFromCacheParams();

    FilePath virtual_file_path;
    FilePath local_tmp_path;
    GURL content_url;
    std::string resource_id;
    std::string md5;
    scoped_refptr<base::MessageLoopProxy> proxy;
    const GetFileCallback callback;
  };

  // Callback similar to FileOperationCallback but with a given |file_path|.
  typedef base::Callback<void(base::PlatformFileError error,
                              const FilePath& file_path)>
      FilePathUpdateCallback;

  // Finds file object by |file_path| and returns the file info.
  // Returns NULL if it does not find the file.
  GDataFileBase* GetGDataFileInfoFromPath(const FilePath& file_path);

  // Initiates upload operation of file defined with |file_name|,
  // |content_type| and |content_length|. The operation will place the newly
  // created file entity into |destination_directory|.
  //
  // Can be called from any thread. |callback| is run on the calling thread.
  void InitiateUpload(const std::string& file_name,
                      const std::string& content_type,
                      int64 content_length,
                      const FilePath& destination_directory,
                      const FilePath& virtual_path,
                      const InitiateUploadCallback& callback);

  // Resumes upload operation for chunk of file defined in |params..
  //
  // Can be called from any thread. |callback| is run on the calling thread.
  void ResumeUpload(const ResumeUploadParams& params,
                    const ResumeFileUploadCallback& callback);

  // Unsafe (unlocked) version of FindFileByPathSync method.
  void UnsafeFindFileByPath(const FilePath& file_path,
                            FindFileDelegate* delegate);

  // Converts document feed from gdata service into DirectoryInfo. On failure,
  // returns NULL and fills in |error| with an appropriate value.
  GDataDirectory* ParseGDataFeed(GDataErrorCode status,
                                 base::Value* data,
                                 base::PlatformFileError *error);

  // Creates a temporary JSON file representing a document with |edit_url|
  // and |resource_id| on IO thread pool. Upon completion it will invoke
  // |callback| with the path of the created temporary file on thread
  // represented by |relay_proxy|.
  static void CreateDocumentJsonFileOnIOThreadPool(
      const GURL& edit_url,
      const std::string& resource_id,
      const GetFileCallback& callback,
      scoped_refptr<base::MessageLoopProxy> relay_proxy);

  // Renames a file or directory at |file_path| to |new_name|.
  void Rename(const FilePath& file_path,
              const FilePath::StringType& new_name,
              const FilePathUpdateCallback& callback);

  // Adds a file or directory at |file_path| to the directory at |dir_path|.
  void AddFileToDirectory(const FilePath& dir_path,
                          const FileOperationCallback& callback,
                          base::PlatformFileError error,
                          const FilePath& file_path);

  // Removes a file or directory at |file_path| from the directory at
  // |dir_path| and moves it to the root directory.
  void RemoveFileFromDirectory(const FilePath& dir_path,
                               const FilePathUpdateCallback& callback,
                               base::PlatformFileError error,
                               const FilePath& file_path);

  // Removes file under |file_path| from in-memory snapshot of the file system.
  // |resource_id| contains the resource id of the removed file if it was a
  // file.
  // Return PLATFORM_FILE_OK if successful.
  base::PlatformFileError RemoveFileFromGData(const FilePath& file_path,
                                              std::string* resource_id);

  // Callback for handling feed content fetching while searching for file info.
  // This callback is invoked after async feed fetch operation that was
  // invoked by StartDirectoryRefresh() completes. This callback will update
  // the content of the refreshed directory object and continue initially
  // started FindFileByPath() request.
  void OnGetDocuments(const FilePath& search_file_path,
                      scoped_ptr<base::ListValue> feed_list,
                      scoped_refptr<base::MessageLoopProxy> proxy,
                      const FindFileCallback& callback,
                      GDataErrorCode status,
                      scoped_ptr<base::Value> data);

  // A pass-through callback used for bridging from
  // FilePathUpdateCallback to FileOperationCallback.
  void OnFilePathUpdated(const FileOperationCallback& cllback,
                         base::PlatformFileError error,
                         const FilePath& file_path);

  // Callback for handling resource rename attempt.
  void OnRenameResourceCompleted(const FilePath& file_path,
                                 const FilePath::StringType& new_name,
                                 const FilePathUpdateCallback& callback,
                                 GDataErrorCode status,
                                 const GURL& document_url);

  // Callback for handling document copy attempt.
  void OnCopyDocumentCompleted(const FilePathUpdateCallback& callback,
                               GDataErrorCode status,
                               scoped_ptr<base::Value> data);

  // Callback for handling an attempt to add a file or directory to another
  // directory.
  void OnAddFileToDirectoryCompleted(const FileOperationCallback& callback,
                                     const FilePath& file_path,
                                     const FilePath& dir_path,
                                     GDataErrorCode status,
                                     const GURL& document_url);

  // Callback for handling an attempt to remove a file or directory from
  // another directory.
  void OnRemoveFileFromDirectoryCompleted(
      const FilePathUpdateCallback& callback,
      const FilePath& file_path,
      const FilePath& dir_path,
      GDataErrorCode status,
      const GURL& document_url);

  // Callback for handling account metadata fetch.
  void OnGetAvailableSpace(
      const GetAvailableSpaceCallback& callback,
      GDataErrorCode status,
      scoped_ptr<base::Value> data);

  // Callback for handling document remove attempt.
  void OnRemovedDocument(
      const FileOperationCallback& callback,
      const FilePath& file_path,
      GDataErrorCode status,
      const GURL& document_url);

  // Callback for handling directory create requests.
  void OnCreateDirectoryCompleted(
      const CreateDirectoryParams& params,
      GDataErrorCode status,
      scoped_ptr<base::Value> created_entry);

  // Callback for handling file downloading requests.
  void OnFileDownloaded(
    const GetFileFromCacheParams& params,
    GDataErrorCode status,
    const GURL& content_url,
    const FilePath& downloaded_file_path);

  // Callback for handling internal StoreToCache() calls after downloading
  // file content.
  void OnDownloadStoredToCache(base::PlatformFileError error,
                               const std::string& resource_id,
                               const std::string& md5);

  // Callback for handling file upload initialization requests.
  void OnUploadLocationReceived(
      const InitiateUploadCallback& callback,
      scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
      GDataErrorCode code,
      const GURL& upload_location);

  // Callback for handling file upload resume requests.
  void OnResumeUpload(
      scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
      const ResumeFileUploadCallback& callback,
      const ResumeUploadResponse& response,
      scoped_ptr<DocumentEntry> new_entry);

  // Renames a file or directory at |file_path| on in-memory snapshot
  // of the file system. Returns PLATFORM_FILE_OK if successful.
  base::PlatformFileError RenameFileOnFilesystem(
      const FilePath& file_path, const FilePath::StringType& new_name,
      FilePath* updated_file_path);

  // Adds a file or directory at |file_path| to another directory at
  // |dir_path| on in-memory snapshot of the file system.
  // Returns PLATFORM_FILE_OK if successful.
  base::PlatformFileError AddFileToDirectoryOnFilesystem(
      const FilePath& file_path, const FilePath& dir_path);

  // Removes a file or directory at |file_path| from another directory at
  // |dir_path| on in-memory snapshot of the file system.
  // Returns PLATFORM_FILE_OK if successful.
  base::PlatformFileError RemoveFileFromDirectoryOnFilesystem(
      const FilePath& file_path, const FilePath& dir_path,
      FilePath* updated_file_path);

  // Removes file under |file_path| from in-memory snapshot of the file system
  // and the corresponding file from cache if it exists.
  // Return PLATFORM_FILE_OK if successful.
  base::PlatformFileError RemoveFileFromFileSystem(const FilePath& file_path);

  // Parses the content of |feed_data| and returns DocumentFeed instance
  // represeting it.
  DocumentFeed* ParseDocumentFeed(base::Value* feed_data);

  // Updates whole directory structure feeds collected in |feed_list|.
  // On success, returns PLATFORM_FILE_OK.
  base::PlatformFileError UpdateDirectoryWithDocumentFeed(
      base::ListValue* feed_list,
      ContentOrigin origin);

  // Converts |entry_value| into GFileDocument instance and adds it
  // to virtual file system at |directory_path|.
  base::PlatformFileError AddNewDirectory(const FilePath& directory_path,
                                          base::Value* entry_value);

  // Given non-existing |directory_path|, finds the first missing parent
  // directory of |directory_path|.
  FindMissingDirectoryResult FindFirstMissingParentDirectory(
      const FilePath& directory_path,
      GURL* last_dir_content_url,
      FilePath* first_missing_parent_path);

  // Starts root feed load from the server. If successful, it will try to find
  // the file upon retrieval completion.
  void LoadFeedFromServer(const FilePath& search_file_path,
                          scoped_refptr<base::MessageLoopProxy> proxy,
                          const FindFileCallback& callback);

  // Starts root feed load from the cache. If successful, it will try to find
  // the file upon retrieval completion. In addition to that, it will
  // initate retrieval of the root feed from the server if |load_from_server|
  // is set.
  void LoadRootFeedFromCache(const FilePath& search_file_path,
                             bool load_from_server,
                             scoped_refptr<base::MessageLoopProxy> proxy,
                             const FindFileCallback& callback);

  // Loads root feed content from |file_path| on IO thread pool. Upon
  // completion it will invoke |callback| on thread represented by
  // |relay_proxy|.
  static void LoadRootFeedOnIOThreadPool(
      const FilePath& meta_cache_path,
      scoped_refptr<base::MessageLoopProxy> relay_proxy,
      const GetJsonDocumentCallback& callback);

  // Callback for handling root directory refresh from the cache.
  void OnLoadRootFeed(const FilePath& search_file_path,
                      bool load_from_server,
                      scoped_refptr<base::MessageLoopProxy> proxy,
                      FindFileCallback callback,
                      base::PlatformFileError error,
                      scoped_ptr<base::Value> feed_list);

  // Saves a collected feed in GCache directory under
  // <user_profile_dir>/GCache/v1/meta/|name| for later reloading when offline.
  void SaveFeed(scoped_ptr<base::Value> feed_vector,
                const FilePath& name);
  static void SaveFeedOnIOThreadPool(
      const FilePath& meta_cache_path,
      scoped_ptr<base::Value> feed_vector,
      const FilePath& name);

  // Finds and returns upload url of a given directory. Returns empty url
  // if directory can't be found.
  GURL GetUploadUrlForDirectory(const FilePath& destination_directory);

  void NotifyDirectoryChanged(const FilePath& directory_path);

  // Cache entry points from within GDataFileSystem.
  // The functionalities of GData blob cache include:
  // - stores downloaded gdata files on disk, indexed by the resource_id and md5
  //   of the gdata file.
  // - provides absolute path for files to be cached or cached.
  // - updates the cached file on disk after user has edited it locally
  // - handles eviction when disk runs out of space
  // - uploads dirty files to gdata server.
  // - etc.

  // Checks if file corresponding to |resource_id| and |md5| exists in cache.
  // Initializes cache if it has not been initialized.
  // Upon completion, |callback| is invoked on the thread where this method was
  // called, with the cache file path if it exists or empty otherwise.
  // otherwise.
  void GetFromCache(const std::string& resource_id,
                    const std::string& md5,
                    const GetFromCacheCallback& callback);

  // Stores |source_path| corresponding to |resource_id| and |md5| to cache.
  // Initializes cache if it has not been initialized.
  // If file was previously pinned, it is stored in persistent directory, with
  // the symlink in pinned dir updated to point to this new file (refer to
  // comments for Pin for explanation of symlinks for pinned files).
  // Otherwise, the file is stored in tmp dir.
  // Upon completion, |callback| is invoked on the thread where this method was
  // called.
  void StoreToCache(const std::string& resource_id,
                    const std::string& md5,
                    const FilePath& source_path,
                    const CacheOperationCallback& callback);

  // Pin file corresponding to |resource_id| and |md5|.
  // Initializes cache if it has not been initialized.
  // Pinned files have symlinks in pinned dir, that reference actual blob files
  // in persistent dir.
  // If the file to be pinned does not exists in cache, a special symlink (with
  // target /dev/null) is created in pinned dir, and base::PLATFORM_FILE_OK is
  // be returned in |callback|.
  // So unless there's an error with file operations involving pinning, no error
  // , i.e. base::PLATFORM_FILE_OK, will be returned in |callback|.
  // GDataSyncClient will pick up these special symlinks during low time and
  // download pinned non-existent files.
  // We'll try not to evict pinned cache files unless there's not enough space
  // on disk and pinned files are the only ones left.
  // Upon completion, |callback| is invoked on the thread where this method was
  // called.
  void Pin(const std::string& resource_id,
           const std::string& md5,
           const CacheOperationCallback& callback);

  // Unpin file corresponding to |resource_id| and |md5|, opposite of Pin.
  // Initializes cache if it has not been initialized.
  // If the file was pinned, delete the symlink and if file blob exists, move it
  // to tmp directory.
  // If the file is not known to cache i.e. wasn't pinned or cached,
  // base::PLATFORM_FILE_ERROR_NOT_FOUND is returned to |callback|.
  // Unpinned files would be evicted when space on disk runs out.
  // Upon completion, |callback| is invoked on the thread where this method was
  // called.
  void Unpin(const std::string& resource_id,
             const std::string& md5,
             const CacheOperationCallback& callback);

  // Removes all files corresponding to |resource_id| from cache persistent,
  // tmp and pinned directories and in-memory cache map.
  // Initializes cache if it has not been initialized.
  // Upon completion, |callback| is invoked on the thread where this method was
  // called.
  void RemoveFromCache(const std::string& resource_id,
                       const CacheOperationCallback& callback);

  // Initializes cache if it hasn't been initialized by posting
  // InitializeCacheOnIOThreadPool task to IO thread pool.
  void InitializeCacheIfNecessary();

  // Cache tasks that run on IO thread pool, posted from above cache entry
  // points.

  // Task posted from InitializeCacheIfNecessary to run on IO thread pool.
  // Creates cache directory and its sub-directories if they don't exist,
  // or scans blobs sub-directory for files and their attributes and updates the
  // info into cache map.
  void InitializeCacheOnIOThreadPool();

  // Task posted from GetFromCacheInternal to run on IO thread pool.
  // Checks if file corresponding to |resource_id| and |md5| exists in cache
  // map.
  // Even though this task doesn't involve IO operations, it still runs on the
  // IO thread pool, to force synchronization of all tasks on IO thread pool,
  // e.g. this absolute must execute after InitailizeCacheOnIOTheadPool.
  // Upon completion, invokes |callback| on the thread where GetFromCache was
  // called.
  void GetFromCacheOnIOThreadPool(
      const std::string& resource_id,
      const std::string& md5,
      const FilePath& gdata_file_path,
      const GetFromCacheCallback& callback,
      scoped_refptr<base::MessageLoopProxy> relay_proxy);

  // Task posted from GetCacheState to run on IO thread pool.
  // Checks if file corresponding to |resource_id| and |md5| exists in cache
  // map.  If yes, returns its cache state; otherwise, returns CACHE_STATE_NONE.
  // Even though this task doesn't involve IO operations, it still runs on the
  // IO thread pool, to force synchronization of all tasks on IO thread pool,
  // e.g. this absolutely must execute after InitailizeCacheOnIOTheadPool.
  // Upon completion, invokes OnGetCacheState i.e. |intermediate_callback| on
  // the thread where GetCacheState was called.
  void GetCacheStateOnIOThreadPool(
      const std::string& resource_id,
      const std::string& md5,
      const GetCacheStateCallback& callback,
      const GetCacheStateIntermediateCallback& intermediate_callback,
      scoped_refptr<base::MessageLoopProxy> relay_proxy);

  // Task posted from StoreToCache to run on IO thread pool:
  // - moves |params.source_path| to |params.dest_path| in the cache dir
  // - if necessary, creates symlink
  // - deletes stale cached versions of |params.resource_id| in
  //   |params.dest_path|'s directory.
  // Upon completion, callback is is invoked on the thread where StoreToCache
  // was called.
  void StoreToCacheOnIOThreadPool(const ModifyCacheStateParams& params);

  // Task posted from Pin to modify cache state on the IO thread pool, which
  // involves the following:
  // - moves |params.source_path| to |params.dest_path| in persistent dir
  // - creates symlink in pinned dir
  // Upon completion, OnFilePinned (i.e. |params.intermediate_callback| is
  // invoked on the same thread where Pin was called.
  void PinOnIOThreadPool(const ModifyCacheStateParams& params);

  // Task posted from Unpin to modify cache state on the IO thread pool, which
  // involves the following:
  // - moves |params.source_path| to |params.dest_path| in tmp dir
  // - deletes symlink from pinned dir
  // Upon completion, OnFileUnpinned (i.e. |params.intermediate_callback| is
  // invoked on the same thread where Unpin was called.
  void UnpinOnIOThreadPool(const ModifyCacheStateParams& params);

  // Task posted from RemoveFromCache to do the following on the IO thread pool:
  // - remove all delete stale cache versions corresponding to |resource_id| in
  //   persistent, tmp and pinned directories
  // - remove entry corresponding to |resource_id| from cache map.
  // Upon completion, |callback| is invoked on the thread where RemoveFromCache
  // was called.
  void RemoveFromCacheOnIOThreadPool(
      const std::string& resource_id,
      const CacheOperationCallback& callback,
      scoped_refptr<base::MessageLoopProxy> relay_proxy);

  // Cache intermediate callbacks, that run on calling thread, for above cache
  // tasks that were run on IO thread pool.

  // Callback for GetCacheState.  Simply locks to allow safe access of GDataFile
  // by |callback|, then invokes callback.
  void OnGetCacheState(base::PlatformFileError error,
                       GDataFile* file,
                       int cache_state,
                       const GetCacheStateCallback& callback);

  // Callback for Pin. Runs |callback| and notifies the observers.
  void OnFilePinned(base::PlatformFileError error,
                    const std::string& resource_id,
                    const std::string& md5,
                    const CacheOperationCallback& callback);

  // Callback for Unpin. Runs |callback| and notifies the observers.
  void OnFileUnpinned(base::PlatformFileError error,
                    const std::string& resource_id,
                    const std::string& md5,
                    const CacheOperationCallback& callback);

  // Helper function for internally handling responses from GetFromCache()
  // calls during processing of GetFile() request.
  void OnGetFileFromCache(const GetFileFromCacheParams& params,
                          base::PlatformFileError error,
                          const std::string& resource_id,
                          const std::string& md5,
                          const FilePath& gdata_file_path,
                          const FilePath& cache_file_path);

  // Cache internal helper functions.

  // Unsafe (unlocked) version of InitializeCacheIfnecessary method.
  void UnsafeInitializeCacheIfNecessary();

  // Scans cache subdirectory |sub_dir_type| and build or update |cache_map|
  // with found file blobs or symlinks.
  void ScanCacheDirectory(
      GDataRootDirectory::CacheSubDirectoryType sub_dir_type,
      GDataRootDirectory::CacheMap* cache_map);

  // Called from GetFromCache and GetFromCacheForPath.
  void GetFromCacheInternal(const std::string& resource_id,
                            const std::string& md5,
                            const FilePath& gdata_file_path,
                            const GetFromCacheCallback& callback);

  // Wrapper task around any sequenced task that runs on IO thread pool that
  // makes sure |in_shutdown_| and |on_io_completed_| are handled properly in
  // the right order.
  void RunTaskOnIOThreadPool(const base::Closure& task);

  // Wrapper around BrowserThread::PostBlockingPoolTask to post
  // RunTaskOnIOThreadPool task on IO thread pool.
  bool PostBlockingPoolSequencedTask(
      const std::string& sequence_token_name,
      const tracked_objects::Location& from_here,
      const base::Closure& task);

  // Helper function used to perform file search on the calling thread of
  // FindFileByPath() request.
  void FindFileByPathOnCallingThread(const FilePath& search_file_path,
                                     const FindFileCallback& callback);

  scoped_ptr<GDataRootDirectory> root_;

  base::Lock lock_;

  // The profile hosts the GDataFileSystem via GDataSystemService.
  Profile* profile_;

  // The document service for the GDataFileSystem.
  scoped_ptr<DocumentsServiceInterface> documents_service_;

  // Base path for GData cache, e.g. <user_profile_dir>/user/GCache/v1.
  FilePath gdata_cache_path_;

  // Paths for all subdirectories of GCache, one for each
  // GDataRootDirectory::CacheSubDirectoryType enum.
  std::vector<FilePath> cache_paths_;

  scoped_ptr<base::WaitableEvent> on_io_completed_;

  // True if cache initialization has started, is in progress or has completed,
  // we only want to initialize cache once.
  bool cache_initialization_started_;

  bool in_shutdown_;  // True if GDatafileSystem is shutting down.

  base::WeakPtrFactory<GDataFileSystem> weak_ptr_factory_;
  base::WeakPtr<GDataFileSystem> weak_ptr_bound_to_ui_thread_;

  ObserverList<Observer> observers_;
};

}  // namespace gdata

#endif  // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_