Home | History | Annotate | Download | only in drive
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_
      6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/callback.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/scoped_vector.h"
     17 #include "base/observer_list.h"
     18 #include "chrome/browser/chromeos/drive/file_errors.h"
     19 #include "chrome/browser/chromeos/drive/file_system_interface.h"
     20 #include "google_apis/drive/drive_common_callbacks.h"
     21 #include "google_apis/drive/gdata_errorcode.h"
     22 
     23 namespace base {
     24 class SequencedTaskRunner;
     25 }  // namespace base
     26 
     27 namespace google_apis {
     28 class AboutResource;
     29 }  // namespace google_apis
     30 
     31 namespace drive {
     32 
     33 class EventLogger;
     34 class JobScheduler;
     35 class ResourceEntry;
     36 
     37 namespace internal {
     38 
     39 class AboutResourceLoader;
     40 class ChangeList;
     41 class ChangeListLoaderObserver;
     42 class DirectoryFetchInfo;
     43 class LoaderController;
     44 class ResourceMetadata;
     45 
     46 // DirectoryLoader is used to load directory contents.
     47 class DirectoryLoader {
     48  public:
     49   DirectoryLoader(EventLogger* logger,
     50                   base::SequencedTaskRunner* blocking_task_runner,
     51                   ResourceMetadata* resource_metadata,
     52                   JobScheduler* scheduler,
     53                   AboutResourceLoader* about_resource_loader,
     54                   LoaderController* apply_task_controller);
     55   ~DirectoryLoader();
     56 
     57   // Adds and removes the observer.
     58   void AddObserver(ChangeListLoaderObserver* observer);
     59   void RemoveObserver(ChangeListLoaderObserver* observer);
     60 
     61   // Reads the directory contents.
     62   // |entries_callback| can be null.
     63   // |completion_callback| must not be null.
     64   void ReadDirectory(const base::FilePath& directory_path,
     65                      const ReadDirectoryEntriesCallback& entries_callback,
     66                      const FileOperationCallback& completion_callback);
     67 
     68  private:
     69   class FeedFetcher;
     70   struct ReadDirectoryCallbackState;
     71 
     72   // Part of ReadDirectory().
     73   void ReadDirectoryAfterGetEntry(
     74       const base::FilePath& directory_path,
     75       const ReadDirectoryEntriesCallback& entries_callback,
     76       const FileOperationCallback& completion_callback,
     77       bool should_try_loading_parent,
     78       const ResourceEntry* entry,
     79       FileError error);
     80   void ReadDirectoryAfterLoadParent(
     81       const base::FilePath& directory_path,
     82       const ReadDirectoryEntriesCallback& entries_callback,
     83       const FileOperationCallback& completion_callback,
     84       FileError error);
     85   void ReadDirectoryAfterGetAboutResource(
     86       const std::string& local_id,
     87       google_apis::GDataErrorCode status,
     88       scoped_ptr<google_apis::AboutResource> about_resource);
     89   void ReadDirectoryAfterCheckLocalState(
     90       scoped_ptr<google_apis::AboutResource> about_resource,
     91       const std::string& local_id,
     92       const ResourceEntry* entry,
     93       const int64* local_changestamp,
     94       FileError error);
     95 
     96   // Part of ReadDirectory().
     97   // This function should be called when the directory load is complete.
     98   // Flushes the callbacks waiting for the directory to be loaded.
     99   void OnDirectoryLoadComplete(const std::string& local_id, FileError error);
    100   void OnDirectoryLoadCompleteAfterRead(const std::string& local_id,
    101                                         const ResourceEntryVector* entries,
    102                                         FileError error);
    103 
    104   // Sends |entries| to the callbacks.
    105   void SendEntries(const std::string& local_id,
    106                    const ResourceEntryVector& entries);
    107 
    108   // ================= Implementation for directory loading =================
    109   // Loads the directory contents from server, and updates the local metadata.
    110   // Runs |callback| when it is finished.
    111   void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info);
    112 
    113   // Part of LoadDirectoryFromServer() for a normal directory.
    114   void LoadDirectoryFromServerAfterLoad(
    115       const DirectoryFetchInfo& directory_fetch_info,
    116       FeedFetcher* fetcher,
    117       FileError error);
    118 
    119   // Part of LoadDirectoryFromServer().
    120   void LoadDirectoryFromServerAfterUpdateChangestamp(
    121       const DirectoryFetchInfo& directory_fetch_info,
    122       const base::FilePath* directory_path,
    123       FileError error);
    124 
    125   EventLogger* logger_;  // Not owned.
    126   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    127   ResourceMetadata* resource_metadata_;  // Not owned.
    128   JobScheduler* scheduler_;  // Not owned.
    129   AboutResourceLoader* about_resource_loader_;  // Not owned.
    130   LoaderController* loader_controller_;  // Not owned.
    131   ObserverList<ChangeListLoaderObserver> observers_;
    132   typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> >
    133       LoadCallbackMap;
    134   LoadCallbackMap pending_load_callback_;
    135 
    136   // Set of the running feed fetcher for the fast fetch.
    137   std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_;
    138 
    139   // Note: This should remain the last member so it'll be destroyed and
    140   // invalidate its weak pointers before any other members are destroyed.
    141   base::WeakPtrFactory<DirectoryLoader> weak_ptr_factory_;
    142   DISALLOW_COPY_AND_ASSIGN(DirectoryLoader);
    143 };
    144 
    145 }  // namespace internal
    146 }  // namespace drive
    147 
    148 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_
    149