blob: 776c5fd4bd702b8c9c973aea9c2c77cfb1ba0cea [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
lukasza2e964692015-08-31 18:57:185#ifndef COMPONENTS_DRIVE_DIRECTORY_LOADER_H_
6#define COMPONENTS_DRIVE_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>
11#include <set>
12#include <string>
13#include <vector>
14
15#include "base/callback.h"
avibc5337b2015-12-25 23:16:3316#include "base/macros.h"
[email protected]54cb8e02014-02-24 09:29:1917#include "base/memory/ref_counted.h"
18#include "base/memory/scoped_ptr.h"
[email protected]54cb8e02014-02-24 09:29:1919#include "base/observer_list.h"
lukasza037c10b12015-06-12 04:21:2520#include "base/threading/thread_checker.h"
lukasza76b4a982015-08-08 00:36:3921#include "components/drive/file_errors.h"
lukasza3fb22622015-08-27 21:04:3422#include "components/drive/file_system_interface.h"
satorux1e04b422015-01-29 07:50:5323#include "google_apis/drive/drive_api_error_codes.h"
[email protected]54cb8e02014-02-24 09:29:1924#include "google_apis/drive/drive_common_callbacks.h"
[email protected]54cb8e02014-02-24 09:29:1925
26namespace base {
27class SequencedTaskRunner;
28} // namespace base
29
30namespace google_apis {
31class AboutResource;
32} // namespace google_apis
33
34namespace drive {
35
[email protected]54cb8e02014-02-24 09:29:1936class EventLogger;
37class JobScheduler;
38class ResourceEntry;
39
40namespace internal {
41
42class AboutResourceLoader;
43class ChangeList;
44class ChangeListLoaderObserver;
45class DirectoryFetchInfo;
46class LoaderController;
47class ResourceMetadata;
48
49// DirectoryLoader is used to load directory contents.
50class DirectoryLoader {
51 public:
52 DirectoryLoader(EventLogger* logger,
53 base::SequencedTaskRunner* blocking_task_runner,
54 ResourceMetadata* resource_metadata,
55 JobScheduler* scheduler,
[email protected]54cb8e02014-02-24 09:29:1956 AboutResourceLoader* about_resource_loader,
57 LoaderController* apply_task_controller);
58 ~DirectoryLoader();
59
60 // Adds and removes the observer.
61 void AddObserver(ChangeListLoaderObserver* observer);
62 void RemoveObserver(ChangeListLoaderObserver* observer);
63
[email protected]b71475f2014-03-03 08:09:1164 // Reads the directory contents.
[email protected]f173c6d2014-04-14 10:49:5065 // |entries_callback| can be null.
66 // |completion_callback| must not be null.
[email protected]b71475f2014-03-03 08:09:1167 void ReadDirectory(const base::FilePath& directory_path,
[email protected]f173c6d2014-04-14 10:49:5068 const ReadDirectoryEntriesCallback& entries_callback,
69 const FileOperationCallback& completion_callback);
[email protected]54cb8e02014-02-24 09:29:1970
71 private:
72 class FeedFetcher;
[email protected]bbf59f22014-03-24 13:55:2673 struct ReadDirectoryCallbackState;
[email protected]54cb8e02014-02-24 09:29:1974
[email protected]b71475f2014-03-03 08:09:1175 // Part of ReadDirectory().
[email protected]f173c6d2014-04-14 10:49:5076 void ReadDirectoryAfterGetEntry(
77 const base::FilePath& directory_path,
78 const ReadDirectoryEntriesCallback& entries_callback,
79 const FileOperationCallback& completion_callback,
80 bool should_try_loading_parent,
81 const ResourceEntry* entry,
82 FileError error);
83 void ReadDirectoryAfterLoadParent(
84 const base::FilePath& directory_path,
85 const ReadDirectoryEntriesCallback& entries_callback,
86 const FileOperationCallback& completion_callback,
87 FileError error);
[email protected]c8f46ab2014-03-12 17:54:3088 void ReadDirectoryAfterGetAboutResource(
[email protected]8542d3c2014-03-19 05:57:2289 const std::string& local_id,
satorux1e04b422015-01-29 07:50:5390 google_apis::DriveApiErrorCode status,
[email protected]54cb8e02014-02-24 09:29:1991 scoped_ptr<google_apis::AboutResource> about_resource);
[email protected]8542d3c2014-03-19 05:57:2292 void ReadDirectoryAfterCheckLocalState(
93 scoped_ptr<google_apis::AboutResource> about_resource,
94 const std::string& local_id,
95 const ResourceEntry* entry,
avibc5337b2015-12-25 23:16:3396 const int64_t* local_changestamp,
[email protected]8542d3c2014-03-19 05:57:2297 FileError error);
[email protected]54cb8e02014-02-24 09:29:1998
[email protected]c8f46ab2014-03-12 17:54:3099 // Part of ReadDirectory().
[email protected]54cb8e02014-02-24 09:29:19100 // This function should be called when the directory load is complete.
101 // Flushes the callbacks waiting for the directory to be loaded.
[email protected]8542d3c2014-03-19 05:57:22102 void OnDirectoryLoadComplete(const std::string& local_id, FileError error);
[email protected]bbf59f22014-03-24 13:55:26103 void OnDirectoryLoadCompleteAfterRead(const std::string& local_id,
104 const ResourceEntryVector* entries,
105 FileError error);
106
107 // Sends |entries| to the callbacks.
108 void SendEntries(const std::string& local_id,
[email protected]f173c6d2014-04-14 10:49:50109 const ResourceEntryVector& entries);
[email protected]54cb8e02014-02-24 09:29:19110
111 // ================= Implementation for directory loading =================
112 // Loads the directory contents from server, and updates the local metadata.
113 // Runs |callback| when it is finished.
114 void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info);
115
116 // Part of LoadDirectoryFromServer() for a normal directory.
117 void LoadDirectoryFromServerAfterLoad(
118 const DirectoryFetchInfo& directory_fetch_info,
119 FeedFetcher* fetcher,
[email protected]bbf59f22014-03-24 13:55:26120 FileError error);
[email protected]54cb8e02014-02-24 09:29:19121
122 // Part of LoadDirectoryFromServer().
[email protected]bbf59f22014-03-24 13:55:26123 void LoadDirectoryFromServerAfterUpdateChangestamp(
[email protected]54cb8e02014-02-24 09:29:19124 const DirectoryFetchInfo& directory_fetch_info,
125 const base::FilePath* directory_path,
126 FileError error);
127
128 EventLogger* logger_; // Not owned.
129 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
130 ResourceMetadata* resource_metadata_; // Not owned.
131 JobScheduler* scheduler_; // Not owned.
[email protected]54cb8e02014-02-24 09:29:19132 AboutResourceLoader* about_resource_loader_; // Not owned.
133 LoaderController* loader_controller_; // Not owned.
brettwd195c952015-06-02 17:31:12134 base::ObserverList<ChangeListLoaderObserver> observers_;
[email protected]bbf59f22014-03-24 13:55:26135 typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> >
[email protected]54cb8e02014-02-24 09:29:19136 LoadCallbackMap;
137 LoadCallbackMap pending_load_callback_;
138
139 // Set of the running feed fetcher for the fast fetch.
140 std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_;
141
lukasza037c10b12015-06-12 04:21:25142 base::ThreadChecker thread_checker_;
143
[email protected]54cb8e02014-02-24 09:29:19144 // Note: This should remain the last member so it'll be destroyed and
145 // invalidate its weak pointers before any other members are destroyed.
146 base::WeakPtrFactory<DirectoryLoader> weak_ptr_factory_;
147 DISALLOW_COPY_AND_ASSIGN(DirectoryLoader);
148};
149
150} // namespace internal
151} // namespace drive
152
lukasza2e964692015-08-31 18:57:18153#endif // COMPONENTS_DRIVE_DIRECTORY_LOADER_H_