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 // |callback| must not be null.
65 void ReadDirectory(const base::FilePath& directory_path,
66 const ReadDirectoryCallback& callback);
70 struct ReadDirectoryCallbackState;
72 // Part of ReadDirectory().
73 void ReadDirectoryAfterGetEntry(const base::FilePath& directory_path,
74 const ReadDirectoryCallback& callback,
75 bool should_try_loading_parent,
76 const ResourceEntry* entry,
78 void ReadDirectoryAfterLoadParent(const base::FilePath& directory_path,
79 const ReadDirectoryCallback& callback,
81 scoped_ptr<ResourceEntryVector> entries,
83 void ReadDirectoryAfterGetAboutResource(
84 const std::string& local_id,
85 google_apis::GDataErrorCode status,
86 scoped_ptr<google_apis::AboutResource> about_resource);
87 void ReadDirectoryAfterCheckLocalState(
88 scoped_ptr<google_apis::AboutResource> about_resource,
89 const std::string& local_id,
90 const ResourceEntry* entry,
91 const ResourceEntryVector* child_entries,
92 const int64* local_changestamp,
95 // Part of ReadDirectory().
96 // This function should be called when the directory load is complete.
97 // Flushes the callbacks waiting for the directory to be loaded.
98 void OnDirectoryLoadComplete(const std::string& local_id, FileError error);
99 void OnDirectoryLoadCompleteAfterRead(const std::string& local_id,
100 const ResourceEntryVector* entries,
103 // Sends |entries| to the callbacks.
104 void SendEntries(const std::string& local_id,
105 const ResourceEntryVector& entries,
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);
113 // Part of LoadDirectoryFromServer() for a normal directory.
114 void LoadDirectoryFromServerAfterLoad(
115 const DirectoryFetchInfo& directory_fetch_info,
116 FeedFetcher* fetcher,
119 // Part of LoadDirectoryFromServer().
120 void LoadDirectoryFromServerAfterUpdateChangestamp(
121 const DirectoryFetchInfo& directory_fetch_info,
122 const base::FilePath* directory_path,
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 DriveServiceInterface* drive_service_; // Not owned.
130 AboutResourceLoader* about_resource_loader_; // Not owned.
131 LoaderController* loader_controller_; // Not owned.
132 ObserverList<ChangeListLoaderObserver> observers_;
133 typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> >
135 LoadCallbackMap pending_load_callback_;
137 // Set of the running feed fetcher for the fast fetch.
138 std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_;
140 // Note: This should remain the last member so it'll be destroyed and
141 // invalidate its weak pointers before any other members are destroyed.
142 base::WeakPtrFactory<DirectoryLoader> weak_ptr_factory_;
143 DISALLOW_COPY_AND_ASSIGN(DirectoryLoader);
146 } // namespace internal
149 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_