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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_
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"
24 class SequencedTaskRunner;
27 namespace google_apis {
29 } // namespace google_apis
33 class DriveServiceInterface;
40 class AboutResourceLoader;
42 class ChangeListLoaderObserver;
43 class DirectoryFetchInfo;
44 class LoaderController;
45 class ResourceMetadata;
47 // DirectoryLoader is used to load directory contents.
48 class DirectoryLoader {
50 DirectoryLoader(EventLogger* logger,
51 base::SequencedTaskRunner* blocking_task_runner,
52 ResourceMetadata* resource_metadata,
53 JobScheduler* scheduler,
54 DriveServiceInterface* drive_service,
55 AboutResourceLoader* about_resource_loader,
56 LoaderController* apply_task_controller);
59 // Adds and removes the observer.
60 void AddObserver(ChangeListLoaderObserver* observer);
61 void RemoveObserver(ChangeListLoaderObserver* observer);
63 // Reads the directory contents.
64 // |entries_callback| can be null.
65 // |completion_callback| must not be null.
66 void ReadDirectory(const base::FilePath& directory_path,
67 const ReadDirectoryEntriesCallback& entries_callback,
68 const FileOperationCallback& completion_callback);
72 struct ReadDirectoryCallbackState;
74 // Part of ReadDirectory().
75 void ReadDirectoryAfterGetEntry(
76 const base::FilePath& directory_path,
77 const ReadDirectoryEntriesCallback& entries_callback,
78 const FileOperationCallback& completion_callback,
79 bool should_try_loading_parent,
80 const ResourceEntry* entry,
82 void ReadDirectoryAfterLoadParent(
83 const base::FilePath& directory_path,
84 const ReadDirectoryEntriesCallback& entries_callback,
85 const FileOperationCallback& completion_callback,
87 void ReadDirectoryAfterGetAboutResource(
88 const std::string& local_id,
89 google_apis::GDataErrorCode status,
90 scoped_ptr<google_apis::AboutResource> about_resource);
91 void ReadDirectoryAfterCheckLocalState(
92 scoped_ptr<google_apis::AboutResource> about_resource,
93 const std::string& local_id,
94 const ResourceEntry* entry,
95 const int64* local_changestamp,
98 // Part of ReadDirectory().
99 // This function should be called when the directory load is complete.
100 // Flushes the callbacks waiting for the directory to be loaded.
101 void OnDirectoryLoadComplete(const std::string& local_id, FileError error);
102 void OnDirectoryLoadCompleteAfterRead(const std::string& local_id,
103 const ResourceEntryVector* entries,
106 // Sends |entries| to the callbacks.
107 void SendEntries(const std::string& local_id,
108 const ResourceEntryVector& entries);
110 // ================= Implementation for directory loading =================
111 // Loads the directory contents from server, and updates the local metadata.
112 // Runs |callback| when it is finished.
113 void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info);
115 // Part of LoadDirectoryFromServer() for a normal directory.
116 void LoadDirectoryFromServerAfterLoad(
117 const DirectoryFetchInfo& directory_fetch_info,
118 FeedFetcher* fetcher,
121 // Part of LoadDirectoryFromServer().
122 void LoadDirectoryFromServerAfterUpdateChangestamp(
123 const DirectoryFetchInfo& directory_fetch_info,
124 const base::FilePath* directory_path,
127 EventLogger* logger_; // Not owned.
128 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
129 ResourceMetadata* resource_metadata_; // Not owned.
130 JobScheduler* scheduler_; // Not owned.
131 DriveServiceInterface* drive_service_; // Not owned.
132 AboutResourceLoader* about_resource_loader_; // Not owned.
133 LoaderController* loader_controller_; // Not owned.
134 ObserverList<ChangeListLoaderObserver> observers_;
135 typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> >
137 LoadCallbackMap pending_load_callback_;
139 // Set of the running feed fetcher for the fast fetch.
140 std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_;
142 // Note: This should remain the last member so it'll be destroyed and
143 // invalidate its weak pointers before any other members are destroyed.
144 base::WeakPtrFactory<DirectoryLoader> weak_ptr_factory_;
145 DISALLOW_COPY_AND_ASSIGN(DirectoryLoader);
148 } // namespace internal
151 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_