blob: 744cd84b2f6a03a91c92934b51b16e5ebed3aac4 [file] [log] [blame]
[email protected]54cb8e02014-02-24 09:29:191// 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
yawano3513e142016-04-20 00:42:425#ifndef COMPONENTS_DRIVE_CHROMEOS_DIRECTORY_LOADER_H_
6#define COMPONENTS_DRIVE_CHROMEOS_DIRECTORY_LOADER_H_
[email protected]54cb8e02014-02-24 09:29:197
avibc5337b2015-12-25 23:16:338#include <stdint.h>
9
[email protected]54cb8e02014-02-24 09:29:1910#include <map>
dchengf42750232016-04-12 04:12:2711#include <memory>
[email protected]54cb8e02014-02-24 09:29:1912#include <set>
13#include <string>
14#include <vector>
15
16#include "base/callback.h"
David Benjamin8048aac2019-04-08 01:33:4617#include "base/containers/unique_ptr_adapters.h"
avibc5337b2015-12-25 23:16:3318#include "base/macros.h"
[email protected]54cb8e02014-02-24 09:29:1919#include "base/memory/ref_counted.h"
[email protected]54cb8e02014-02-24 09:29:1920#include "base/observer_list.h"
lukasza037c10b12015-06-12 04:21:2521#include "base/threading/thread_checker.h"
Stuart Langleyd2c0cb02018-09-27 02:07:0022#include "base/time/default_clock.h"
yawano3513e142016-04-20 00:42:4223#include "components/drive/chromeos/file_system_interface.h"
lukasza76b4a982015-08-08 00:36:3924#include "components/drive/file_errors.h"
satorux1e04b422015-01-29 07:50:5325#include "google_apis/drive/drive_api_error_codes.h"
[email protected]54cb8e02014-02-24 09:29:1926#include "google_apis/drive/drive_common_callbacks.h"
[email protected]54cb8e02014-02-24 09:29:1927
28namespace base {
Stuart Langleyd2c0cb02018-09-27 02:07:0029class Clock;
[email protected]54cb8e02014-02-24 09:29:1930class SequencedTaskRunner;
31} // namespace base
32
[email protected]54cb8e02014-02-24 09:29:1933namespace drive {
34
[email protected]54cb8e02014-02-24 09:29:1935class EventLogger;
36class JobScheduler;
37class ResourceEntry;
38
39namespace internal {
40
Stuart Langley24e4ff52018-05-25 00:29:1941class RootFolderIdLoader;
[email protected]54cb8e02014-02-24 09:29:1942class ChangeListLoaderObserver;
43class DirectoryFetchInfo;
44class LoaderController;
45class ResourceMetadata;
Stuart Langleyc92ed252018-05-16 08:38:3446class StartPageTokenLoader;
[email protected]54cb8e02014-02-24 09:29:1947
48// DirectoryLoader is used to load directory contents.
49class DirectoryLoader {
50 public:
Stuart Langleyd2c0cb02018-09-27 02:07:0051 // |clock| can be mocked for testing
[email protected]54cb8e02014-02-24 09:29:1952 DirectoryLoader(EventLogger* logger,
53 base::SequencedTaskRunner* blocking_task_runner,
54 ResourceMetadata* resource_metadata,
55 JobScheduler* scheduler,
Stuart Langley24e4ff52018-05-25 00:29:1956 RootFolderIdLoader* root_folder_id_loader,
Stuart Langleyc92ed252018-05-16 08:38:3457 StartPageTokenLoader* start_page_token_loader,
Stuart Langley95c21502018-05-25 04:21:2958 LoaderController* apply_task_controller,
Stuart Langleyd03e9be2018-09-06 23:42:5259 const base::FilePath& root_entry_path,
Stuart Langleyd2c0cb02018-09-27 02:07:0060 const std::string& team_drive_id,
61 const base::Clock* clock = base::DefaultClock::GetInstance());
[email protected]54cb8e02014-02-24 09:29:1962 ~DirectoryLoader();
63
64 // Adds and removes the observer.
65 void AddObserver(ChangeListLoaderObserver* observer);
66 void RemoveObserver(ChangeListLoaderObserver* observer);
67
[email protected]b71475f2014-03-03 08:09:1168 // Reads the directory contents.
[email protected]f173c6d2014-04-14 10:49:5069 // |entries_callback| can be null.
70 // |completion_callback| must not be null.
[email protected]b71475f2014-03-03 08:09:1171 void ReadDirectory(const base::FilePath& directory_path,
Stuart Langley4819fdb2018-08-27 00:12:2872 ReadDirectoryEntriesCallback entries_callback,
[email protected]f173c6d2014-04-14 10:49:5073 const FileOperationCallback& completion_callback);
[email protected]54cb8e02014-02-24 09:29:1974
75 private:
76 class FeedFetcher;
[email protected]bbf59f22014-03-24 13:55:2677 struct ReadDirectoryCallbackState;
[email protected]54cb8e02014-02-24 09:29:1978
[email protected]b71475f2014-03-03 08:09:1179 // Part of ReadDirectory().
[email protected]f173c6d2014-04-14 10:49:5080 void ReadDirectoryAfterGetEntry(
81 const base::FilePath& directory_path,
Stuart Langley4819fdb2018-08-27 00:12:2882 ReadDirectoryEntriesCallback entries_callback,
[email protected]f173c6d2014-04-14 10:49:5083 const FileOperationCallback& completion_callback,
84 bool should_try_loading_parent,
85 const ResourceEntry* entry,
86 FileError error);
87 void ReadDirectoryAfterLoadParent(
88 const base::FilePath& directory_path,
Stuart Langley4819fdb2018-08-27 00:12:2889 ReadDirectoryEntriesCallback entries_callback,
[email protected]f173c6d2014-04-14 10:49:5090 const FileOperationCallback& completion_callback,
91 FileError error);
Stuart Langley24e4ff52018-05-25 00:29:1992 void ReadDirectoryAfterGetRootFolderId(
Stuart Langleyd2c0cb02018-09-27 02:07:0093 const base::FilePath& directory_path,
[email protected]8542d3c2014-03-19 05:57:2294 const std::string& local_id,
Stuart Langley24e4ff52018-05-25 00:29:1995 FileError error,
96 base::Optional<std::string> root_folder_id);
Stuart Langleyc92ed252018-05-16 08:38:3497 void ReadDirectoryAfterGetStartPageToken(
Stuart Langleyd2c0cb02018-09-27 02:07:0098 const base::FilePath& directory_path,
Stuart Langleyc92ed252018-05-16 08:38:3499 const std::string& local_id,
100 const std::string& root_folder_id,
101 google_apis::DriveApiErrorCode status,
102 std::unique_ptr<google_apis::StartPageToken> start_page_token);
103
[email protected]8542d3c2014-03-19 05:57:22104 void ReadDirectoryAfterCheckLocalState(
Stuart Langleyd2c0cb02018-09-27 02:07:00105 const base::FilePath& directory_path,
Stuart Langleyc92ed252018-05-16 08:38:34106 const std::string& remote_start_page_token,
[email protected]8542d3c2014-03-19 05:57:22107 const std::string& local_id,
Stuart Langley24e4ff52018-05-25 00:29:19108 const std::string& root_folder_id,
[email protected]8542d3c2014-03-19 05:57:22109 const ResourceEntry* entry,
Stuart Langleyc92ed252018-05-16 08:38:34110 const std::string* local_start_page_token,
[email protected]8542d3c2014-03-19 05:57:22111 FileError error);
[email protected]54cb8e02014-02-24 09:29:19112
[email protected]c8f46ab2014-03-12 17:54:30113 // Part of ReadDirectory().
[email protected]54cb8e02014-02-24 09:29:19114 // This function should be called when the directory load is complete.
115 // Flushes the callbacks waiting for the directory to be loaded.
[email protected]8542d3c2014-03-19 05:57:22116 void OnDirectoryLoadComplete(const std::string& local_id, FileError error);
[email protected]bbf59f22014-03-24 13:55:26117 void OnDirectoryLoadCompleteAfterRead(const std::string& local_id,
118 const ResourceEntryVector* entries,
119 FileError error);
120
121 // Sends |entries| to the callbacks.
122 void SendEntries(const std::string& local_id,
[email protected]f173c6d2014-04-14 10:49:50123 const ResourceEntryVector& entries);
[email protected]54cb8e02014-02-24 09:29:19124
125 // ================= Implementation for directory loading =================
126 // Loads the directory contents from server, and updates the local metadata.
127 // Runs |callback| when it is finished.
Stuart Langley24e4ff52018-05-25 00:29:19128 void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info,
129 const std::string& root_folder_id);
[email protected]54cb8e02014-02-24 09:29:19130
131 // Part of LoadDirectoryFromServer() for a normal directory.
132 void LoadDirectoryFromServerAfterLoad(
133 const DirectoryFetchInfo& directory_fetch_info,
134 FeedFetcher* fetcher,
[email protected]bbf59f22014-03-24 13:55:26135 FileError error);
[email protected]54cb8e02014-02-24 09:29:19136
137 // Part of LoadDirectoryFromServer().
Stuart Langleyc92ed252018-05-16 08:38:34138 void LoadDirectoryFromServerAfterUpdateStartPageToken(
[email protected]54cb8e02014-02-24 09:29:19139 const DirectoryFetchInfo& directory_fetch_info,
140 const base::FilePath* directory_path,
141 FileError error);
142
143 EventLogger* logger_; // Not owned.
144 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
145 ResourceMetadata* resource_metadata_; // Not owned.
146 JobScheduler* scheduler_; // Not owned.
Stuart Langley24e4ff52018-05-25 00:29:19147 RootFolderIdLoader* root_folder_id_loader_; // Not owned.
Stuart Langleyc92ed252018-05-16 08:38:34148 StartPageTokenLoader* start_page_token_loader_; // Not owned
[email protected]54cb8e02014-02-24 09:29:19149 LoaderController* loader_controller_; // Not owned.
Trent Apteda250ec3ab2018-08-19 08:52:19150 base::ObserverList<ChangeListLoaderObserver>::Unchecked observers_;
[email protected]bbf59f22014-03-24 13:55:26151 typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> >
[email protected]54cb8e02014-02-24 09:29:19152 LoadCallbackMap;
153 LoadCallbackMap pending_load_callback_;
154
155 // Set of the running feed fetcher for the fast fetch.
David Benjamin8048aac2019-04-08 01:33:46156 std::set<std::unique_ptr<FeedFetcher>, base::UniquePtrComparator>
157 fast_fetch_feed_fetcher_set_;
[email protected]54cb8e02014-02-24 09:29:19158
Stuart Langley95c21502018-05-25 04:21:29159 // The root entry path for changes being loaded by this directory loader.
160 // Can be a team drive root entry or for the users default corpus will be the
161 // drive root entry.
162 const base::FilePath root_entry_path_;
163
Stuart Langleyd03e9be2018-09-06 23:42:52164 // The team drive id for this directory loader. Used to retrieve the start
165 // page token when performing a fast fetch.
166 const std::string team_drive_id_;
167
Stuart Langleyd2c0cb02018-09-27 02:07:00168 const base::Clock* clock_; // Not Owned
169
Stuart Langleybe4035742018-05-10 05:32:27170 THREAD_CHECKER(thread_checker_);
lukasza037c10b12015-06-12 04:21:25171
[email protected]54cb8e02014-02-24 09:29:19172 // Note: This should remain the last member so it'll be destroyed and
173 // invalidate its weak pointers before any other members are destroyed.
Jeremy Roman47d432e2019-08-20 14:24:00174 base::WeakPtrFactory<DirectoryLoader> weak_ptr_factory_{this};
[email protected]54cb8e02014-02-24 09:29:19175 DISALLOW_COPY_AND_ASSIGN(DirectoryLoader);
176};
177
178} // namespace internal
179} // namespace drive
180
yawano3513e142016-04-20 00:42:42181#endif // COMPONENTS_DRIVE_CHROMEOS_DIRECTORY_LOADER_H_