1 // Copyright (c) 2012 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_CHANGE_LIST_LOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_LOADER_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/chromeos/drive/file_errors.h"
17 #include "google_apis/drive/drive_api_error_codes.h"
18 #include "google_apis/drive/drive_common_callbacks.h"
23 class ScopedClosureRunner
;
24 class SequencedTaskRunner
;
28 namespace google_apis
{
30 } // namespace google_apis
41 class ChangeListLoaderObserver
;
42 class ChangeListProcessor
;
43 class DirectoryLoader
;
44 class ResourceMetadata
;
46 // Delays execution of tasks as long as more than one lock is alive.
47 // Used to ensure that ChangeListLoader does not cause race condition by adding
48 // new entries created by sync tasks before they do.
49 // All code which may add entries found on the server to the local metadata
50 // should use this class.
51 class LoaderController
{
56 // Increments the lock count and returns an object which decrements the count
57 // on its destruction.
58 // While the lock count is positive, tasks will be pending.
59 scoped_ptr
<base::ScopedClosureRunner
> GetLock();
61 // Runs the task if the lock count is 0, otherwise it will be pending.
62 void ScheduleRun(const base::Closure
& task
);
65 // Decrements the lock count.
69 std::vector
<base::Closure
> pending_tasks_
;
71 base::WeakPtrFactory
<LoaderController
> weak_ptr_factory_
;
72 DISALLOW_COPY_AND_ASSIGN(LoaderController
);
75 // This class is responsible to load AboutResource from the server and cache it.
76 class AboutResourceLoader
{
78 explicit AboutResourceLoader(JobScheduler
* scheduler
);
79 ~AboutResourceLoader();
81 // Returns the cached about resource.
82 // NULL is returned if the cache is not available.
83 const google_apis::AboutResource
* cached_about_resource() const {
84 return cached_about_resource_
.get();
87 // Gets the 'latest' about resource and asynchronously runs |callback|. I.e.,
88 // 1) If the last call to UpdateAboutResource call is in-flight, wait for it.
89 // 2) Otherwise, if the resource is cached, just returns the cached value.
90 // 3) If neither of the above hold, queries the API server by calling
91 // |UpdateAboutResource|.
92 void GetAboutResource(const google_apis::AboutResourceCallback
& callback
);
94 // Gets the about resource from the server, and caches it if successful. This
95 // function calls JobScheduler::GetAboutResource internally. The cache will be
96 // used in |GetAboutResource|.
97 void UpdateAboutResource(const google_apis::AboutResourceCallback
& callback
);
100 // Part of UpdateAboutResource().
101 // This function should be called when the latest about resource is being
102 // fetched from the server. The retrieved about resource is cloned, and one is
103 // cached and the other is passed to callbacks associated with |task_id|.
104 void UpdateAboutResourceAfterGetAbout(
106 google_apis::DriveApiErrorCode status
,
107 scoped_ptr
<google_apis::AboutResource
> about_resource
);
109 JobScheduler
* scheduler_
;
110 scoped_ptr
<google_apis::AboutResource
> cached_about_resource_
;
112 // Identifier to denote the latest UpdateAboutResource call.
113 int current_update_task_id_
;
114 // Mapping from each UpdateAboutResource task ID to the corresponding
115 // callbacks. Note that there will be multiple callbacks for a single task
116 // when GetAboutResource is called before the task completes.
117 std::map
<int, std::vector
<google_apis::AboutResourceCallback
> >
120 base::WeakPtrFactory
<AboutResourceLoader
> weak_ptr_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(AboutResourceLoader
);
124 // ChangeListLoader is used to load the change list, the full resource list,
125 // and directory contents, from Google Drive API. The class also updates the
126 // resource metadata with the change list loaded from the server.
128 // Note that the difference between "resource list" and "change list" is
129 // subtle hence the two words are often used interchangeably. To be precise,
130 // "resource list" refers to metadata from the server when fetching the full
131 // resource metadata, or fetching directory contents, whereas "change list"
132 // refers to metadata from the server when fetching changes (delta).
133 class ChangeListLoader
{
135 // Resource feed fetcher from the server.
138 ChangeListLoader(EventLogger
* logger
,
139 base::SequencedTaskRunner
* blocking_task_runner
,
140 ResourceMetadata
* resource_metadata
,
141 JobScheduler
* scheduler
,
142 AboutResourceLoader
* about_resource_loader
,
143 LoaderController
* apply_task_controller
);
146 // Indicates whether there is a request for full resource list or change
147 // list fetching is in flight (i.e. directory contents fetching does not
149 bool IsRefreshing() const;
151 // Adds and removes the observer.
152 void AddObserver(ChangeListLoaderObserver
* observer
);
153 void RemoveObserver(ChangeListLoaderObserver
* observer
);
155 // Checks for updates on the server. Does nothing if the change list is now
156 // being loaded or refreshed. |callback| must not be null.
157 // Note: |callback| will be called if the check for updates actually
158 // runs, i.e. it may NOT be called if the checking is ignored.
159 void CheckForUpdates(const FileOperationCallback
& callback
);
161 // Starts the change list loading if needed. If the locally stored metadata is
162 // available, runs |callback| immediately and starts checking server for
163 // updates in background. If the locally stored metadata is not available,
164 // starts loading from the server, and runs |callback| to tell the result to
165 // the caller when it is finished.
167 // |callback| must not be null.
168 void LoadIfNeeded(const FileOperationCallback
& callback
);
171 // Starts the resource metadata loading and calls |callback| when it's done.
172 void Load(const FileOperationCallback
& callback
);
173 void LoadAfterGetLargestChangestamp(bool is_initial_load
,
174 const int64
* local_changestamp
,
176 void LoadAfterGetAboutResource(
177 int64 local_changestamp
,
178 google_apis::DriveApiErrorCode status
,
179 scoped_ptr
<google_apis::AboutResource
> about_resource
);
182 // This function should be called when the change list load is complete.
183 // Flushes the callbacks for change list loading and all directory loading.
184 void OnChangeListLoadComplete(FileError error
);
186 // Called when the loading about_resource_loader_->UpdateAboutResource is
188 void OnAboutResourceUpdated(google_apis::DriveApiErrorCode error
,
189 scoped_ptr
<google_apis::AboutResource
> resource
);
191 // ================= Implementation for change list loading =================
193 // Part of LoadFromServerIfNeeded().
194 // Starts loading the change list since |start_changestamp|, or the full
195 // resource list if |start_changestamp| is zero.
196 void LoadChangeListFromServer(int64 start_changestamp
);
198 // Part of LoadChangeListFromServer().
199 // Called when the entire change list is loaded.
200 void LoadChangeListFromServerAfterLoadChangeList(
201 scoped_ptr
<google_apis::AboutResource
> about_resource
,
202 bool is_delta_update
,
204 ScopedVector
<ChangeList
> change_lists
);
206 // Part of LoadChangeListFromServer().
207 // Called when the resource metadata is updated.
208 void LoadChangeListFromServerAfterUpdate(
209 ChangeListProcessor
* change_list_processor
,
210 bool should_notify_changed_directories
,
211 const base::Time
& start_time
,
214 EventLogger
* logger_
; // Not owned.
215 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
216 ResourceMetadata
* resource_metadata_
; // Not owned.
217 JobScheduler
* scheduler_
; // Not owned.
218 AboutResourceLoader
* about_resource_loader_
; // Not owned.
219 LoaderController
* loader_controller_
; // Not owned.
220 ObserverList
<ChangeListLoaderObserver
> observers_
;
221 std::vector
<FileOperationCallback
> pending_load_callback_
;
222 FileOperationCallback pending_update_check_callback_
;
224 // Running feed fetcher.
225 scoped_ptr
<FeedFetcher
> change_feed_fetcher_
;
227 // True if the full resource list is loaded (i.e. the resource metadata is
231 // Note: This should remain the last member so it'll be destroyed and
232 // invalidate its weak pointers before any other members are destroyed.
233 base::WeakPtrFactory
<ChangeListLoader
> weak_ptr_factory_
;
234 DISALLOW_COPY_AND_ASSIGN(ChangeListLoader
);
237 } // namespace internal
240 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_LOADER_H_