Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / directory_loader.h
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.
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"
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"
22
23 namespace base {
24 class SequencedTaskRunner;
25 }  // namespace base
26
27 namespace google_apis {
28 class AboutResource;
29 }  // namespace google_apis
30
31 namespace drive {
32
33 class DriveServiceInterface;
34 class EventLogger;
35 class JobScheduler;
36 class ResourceEntry;
37
38 namespace internal {
39
40 class AboutResourceLoader;
41 class ChangeList;
42 class ChangeListLoaderObserver;
43 class DirectoryFetchInfo;
44 class LoaderController;
45 class ResourceMetadata;
46
47 // DirectoryLoader is used to load directory contents.
48 class DirectoryLoader {
49  public:
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);
57   ~DirectoryLoader();
58
59   // Adds and removes the observer.
60   void AddObserver(ChangeListLoaderObserver* observer);
61   void RemoveObserver(ChangeListLoaderObserver* observer);
62
63   // Reads the directory contents.
64   // |entries_callback| can be null.
65   // |completion_callback| must not be null.
66   void ReadDirectory(const base::FilePath& directory_path,
67                      const ReadDirectoryEntriesCallback& entries_callback,
68                      const FileOperationCallback& completion_callback);
69
70  private:
71   class FeedFetcher;
72   struct ReadDirectoryCallbackState;
73
74   // Part of ReadDirectory().
75   void ReadDirectoryAfterGetEntry(
76       const base::FilePath& directory_path,
77       const ReadDirectoryEntriesCallback& entries_callback,
78       const FileOperationCallback& completion_callback,
79       bool should_try_loading_parent,
80       const ResourceEntry* entry,
81       FileError error);
82   void ReadDirectoryAfterLoadParent(
83       const base::FilePath& directory_path,
84       const ReadDirectoryEntriesCallback& entries_callback,
85       const FileOperationCallback& completion_callback,
86       FileError error);
87   void ReadDirectoryAfterGetAboutResource(
88       const std::string& local_id,
89       google_apis::GDataErrorCode status,
90       scoped_ptr<google_apis::AboutResource> about_resource);
91   void ReadDirectoryAfterCheckLocalState(
92       scoped_ptr<google_apis::AboutResource> about_resource,
93       const std::string& local_id,
94       const ResourceEntry* entry,
95       const int64* local_changestamp,
96       FileError error);
97
98   // Part of ReadDirectory().
99   // This function should be called when the directory load is complete.
100   // Flushes the callbacks waiting for the directory to be loaded.
101   void OnDirectoryLoadComplete(const std::string& local_id, FileError error);
102   void OnDirectoryLoadCompleteAfterRead(const std::string& local_id,
103                                         const ResourceEntryVector* entries,
104                                         FileError error);
105
106   // Sends |entries| to the callbacks.
107   void SendEntries(const std::string& local_id,
108                    const ResourceEntryVector& entries);
109
110   // ================= Implementation for directory loading =================
111   // Loads the directory contents from server, and updates the local metadata.
112   // Runs |callback| when it is finished.
113   void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info);
114
115   // Part of LoadDirectoryFromServer() for a normal directory.
116   void LoadDirectoryFromServerAfterLoad(
117       const DirectoryFetchInfo& directory_fetch_info,
118       FeedFetcher* fetcher,
119       FileError error);
120
121   // Part of LoadDirectoryFromServer().
122   void LoadDirectoryFromServerAfterUpdateChangestamp(
123       const DirectoryFetchInfo& directory_fetch_info,
124       const base::FilePath* directory_path,
125       FileError error);
126
127   EventLogger* logger_;  // Not owned.
128   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
129   ResourceMetadata* resource_metadata_;  // Not owned.
130   JobScheduler* scheduler_;  // Not owned.
131   DriveServiceInterface* drive_service_;  // Not owned.
132   AboutResourceLoader* about_resource_loader_;  // Not owned.
133   LoaderController* loader_controller_;  // Not owned.
134   ObserverList<ChangeListLoaderObserver> observers_;
135   typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> >
136       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
142   // 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_