blob: 7e5afbfdd7cf056ba1a138816c72c71eae3779e5 [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
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"
lukasza037c10b12015-06-12 04:21:2518#include "base/threading/thread_checker.h"
[email protected]b71475f2014-03-03 08:09:1119#include "chrome/browser/chromeos/drive/file_system_interface.h"
lukasza76b4a982015-08-08 00:36:3920#include "components/drive/file_errors.h"
satorux1e04b422015-01-29 07:50:5321#include "google_apis/drive/drive_api_error_codes.h"
[email protected]54cb8e02014-02-24 09:29:1922#include "google_apis/drive/drive_common_callbacks.h"
[email protected]54cb8e02014-02-24 09:29:1923
24namespace base {
25class SequencedTaskRunner;
26} // namespace base
27
28namespace google_apis {
29class AboutResource;
30} // namespace google_apis
31
32namespace drive {
33
[email protected]54cb8e02014-02-24 09:29:1934class EventLogger;
35class JobScheduler;
36class ResourceEntry;
37
38namespace internal {
39
40class AboutResourceLoader;
41class ChangeList;
42class ChangeListLoaderObserver;
43class DirectoryFetchInfo;
44class LoaderController;
45class ResourceMetadata;
46
47// DirectoryLoader is used to load directory contents.
48class DirectoryLoader {
49 public:
50 DirectoryLoader(EventLogger* logger,
51 base::SequencedTaskRunner* blocking_task_runner,
52 ResourceMetadata* resource_metadata,
53 JobScheduler* scheduler,
[email protected]54cb8e02014-02-24 09:29:1954 AboutResourceLoader* about_resource_loader,
55 LoaderController* apply_task_controller);
56 ~DirectoryLoader();
57
58 // Adds and removes the observer.
59 void AddObserver(ChangeListLoaderObserver* observer);
60 void RemoveObserver(ChangeListLoaderObserver* observer);
61
[email protected]b71475f2014-03-03 08:09:1162 // Reads the directory contents.
[email protected]f173c6d2014-04-14 10:49:5063 // |entries_callback| can be null.
64 // |completion_callback| must not be null.
[email protected]b71475f2014-03-03 08:09:1165 void ReadDirectory(const base::FilePath& directory_path,
[email protected]f173c6d2014-04-14 10:49:5066 const ReadDirectoryEntriesCallback& entries_callback,
67 const FileOperationCallback& completion_callback);
[email protected]54cb8e02014-02-24 09:29:1968
69 private:
70 class FeedFetcher;
[email protected]bbf59f22014-03-24 13:55:2671 struct ReadDirectoryCallbackState;
[email protected]54cb8e02014-02-24 09:29:1972
[email protected]b71475f2014-03-03 08:09:1173 // Part of ReadDirectory().
[email protected]f173c6d2014-04-14 10:49:5074 void ReadDirectoryAfterGetEntry(
75 const base::FilePath& directory_path,
76 const ReadDirectoryEntriesCallback& entries_callback,
77 const FileOperationCallback& completion_callback,
78 bool should_try_loading_parent,
79 const ResourceEntry* entry,
80 FileError error);
81 void ReadDirectoryAfterLoadParent(
82 const base::FilePath& directory_path,
83 const ReadDirectoryEntriesCallback& entries_callback,
84 const FileOperationCallback& completion_callback,
85 FileError error);
[email protected]c8f46ab2014-03-12 17:54:3086 void ReadDirectoryAfterGetAboutResource(
[email protected]8542d3c2014-03-19 05:57:2287 const std::string& local_id,
satorux1e04b422015-01-29 07:50:5388 google_apis::DriveApiErrorCode status,
[email protected]54cb8e02014-02-24 09:29:1989 scoped_ptr<google_apis::AboutResource> about_resource);
[email protected]8542d3c2014-03-19 05:57:2290 void ReadDirectoryAfterCheckLocalState(
91 scoped_ptr<google_apis::AboutResource> about_resource,
92 const std::string& local_id,
93 const ResourceEntry* entry,
[email protected]8542d3c2014-03-19 05:57:2294 const int64* local_changestamp,
95 FileError error);
[email protected]54cb8e02014-02-24 09:29:1996
[email protected]c8f46ab2014-03-12 17:54:3097 // Part of ReadDirectory().
[email protected]54cb8e02014-02-24 09:29:1998 // This function should be called when the directory load is complete.
99 // Flushes the callbacks waiting for the directory to be loaded.
[email protected]8542d3c2014-03-19 05:57:22100 void OnDirectoryLoadComplete(const std::string& local_id, FileError error);
[email protected]bbf59f22014-03-24 13:55:26101 void OnDirectoryLoadCompleteAfterRead(const std::string& local_id,
102 const ResourceEntryVector* entries,
103 FileError error);
104
105 // Sends |entries| to the callbacks.
106 void SendEntries(const std::string& local_id,
[email protected]f173c6d2014-04-14 10:49:50107 const ResourceEntryVector& entries);
[email protected]54cb8e02014-02-24 09:29:19108
109 // ================= Implementation for directory loading =================
110 // Loads the directory contents from server, and updates the local metadata.
111 // Runs |callback| when it is finished.
112 void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info);
113
114 // Part of LoadDirectoryFromServer() for a normal directory.
115 void LoadDirectoryFromServerAfterLoad(
116 const DirectoryFetchInfo& directory_fetch_info,
117 FeedFetcher* fetcher,
[email protected]bbf59f22014-03-24 13:55:26118 FileError error);
[email protected]54cb8e02014-02-24 09:29:19119
120 // Part of LoadDirectoryFromServer().
[email protected]bbf59f22014-03-24 13:55:26121 void LoadDirectoryFromServerAfterUpdateChangestamp(
[email protected]54cb8e02014-02-24 09:29:19122 const DirectoryFetchInfo& directory_fetch_info,
123 const base::FilePath* directory_path,
124 FileError error);
125
126 EventLogger* logger_; // Not owned.
127 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
128 ResourceMetadata* resource_metadata_; // Not owned.
129 JobScheduler* scheduler_; // Not owned.
[email protected]54cb8e02014-02-24 09:29:19130 AboutResourceLoader* about_resource_loader_; // Not owned.
131 LoaderController* loader_controller_; // Not owned.
brettwd195c952015-06-02 17:31:12132 base::ObserverList<ChangeListLoaderObserver> observers_;
[email protected]bbf59f22014-03-24 13:55:26133 typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> >
[email protected]54cb8e02014-02-24 09:29:19134 LoadCallbackMap;
135 LoadCallbackMap pending_load_callback_;
136
137 // Set of the running feed fetcher for the fast fetch.
138 std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_;
139
lukasza037c10b12015-06-12 04:21:25140 base::ThreadChecker thread_checker_;
141
[email protected]54cb8e02014-02-24 09:29:19142 // 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);
146};
147
148} // namespace internal
149} // namespace drive
150
151#endif // CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_