- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / drive_integration_service.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_
7
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/singleton.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "chrome/browser/chromeos/drive/file_errors.h"
14 #include "chrome/browser/chromeos/drive/file_system_util.h"
15 #include "chrome/browser/chromeos/drive/job_scheduler.h"
16 #include "chrome/browser/drive/drive_notification_observer.h"
17 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
18 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
19
20 namespace base {
21 class FilePath;
22 class SequencedTaskRunner;
23 }
24
25 namespace drive {
26
27 class DebugInfoCollector;
28 class DownloadHandler;
29 class DriveAppRegistry;
30 class DriveServiceInterface;
31 class FileSystemInterface;
32 class JobListInterface;
33
34 namespace internal {
35 class FileCache;
36 class ResourceMetadata;
37 class ResourceMetadataStorage;
38 }  // namespace internal
39
40 // Interface for classes that need to observe events from
41 // DriveIntegrationService.  All events are notified on UI thread.
42 class DriveIntegrationServiceObserver {
43  public:
44   // Triggered when the file system is mounted.
45   virtual void OnFileSystemMounted() {
46   }
47
48   // Triggered when the file system is being unmounted.
49   virtual void OnFileSystemBeingUnmounted() {
50   }
51
52  protected:
53   virtual ~DriveIntegrationServiceObserver() {}
54 };
55
56 // DriveIntegrationService is used to integrate Drive to Chrome. This class
57 // exposes the file system representation built on top of Drive and some
58 // other Drive related objects to the file manager, and some other sub
59 // systems.
60 //
61 // The class is essentially a container that manages lifetime of the objects
62 // that are used to integrate Drive to Chrome. The object of this class is
63 // created per-profile.
64 class DriveIntegrationService
65     : public BrowserContextKeyedService,
66       public DriveNotificationObserver {
67  public:
68   class PreferenceWatcher;
69
70   // test_drive_service, test_cache_root and test_file_system are used by tests
71   // to inject customized instances.
72   // Pass NULL or the empty value when not interested.
73   // |preference_watcher| observes the drive enable preference, and sets the
74   // enable state when changed. It can be NULL. The ownership is taken by
75   // the DriveIntegrationService.
76   DriveIntegrationService(
77       Profile* profile,
78       PreferenceWatcher* preference_watcher,
79       DriveServiceInterface* test_drive_service,
80       const base::FilePath& test_cache_root,
81       FileSystemInterface* test_file_system);
82   virtual ~DriveIntegrationService();
83
84   // BrowserContextKeyedService override:
85   virtual void Shutdown() OVERRIDE;
86
87   void SetEnabled(bool enabled);
88   bool is_enabled() const { return enabled_; }
89
90   bool IsMounted() const;
91
92   // Adds and removes the observer.
93   void AddObserver(DriveIntegrationServiceObserver* observer);
94   void RemoveObserver(DriveIntegrationServiceObserver* observer);
95
96   // DriveNotificationObserver implementation.
97   virtual void OnNotificationReceived() OVERRIDE;
98   virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
99
100   DriveServiceInterface* drive_service() {
101     return drive_service_.get();
102   }
103
104   DebugInfoCollector* debug_info_collector() {
105     return debug_info_collector_.get();
106   }
107   FileSystemInterface* file_system() { return file_system_.get(); }
108   DownloadHandler* download_handler() { return download_handler_.get(); }
109   DriveAppRegistry* drive_app_registry() { return drive_app_registry_.get(); }
110   JobListInterface* job_list() { return scheduler_.get(); }
111
112   // Clears all the local cache file, the local resource metadata, and
113   // in-memory Drive app registry, and remounts the file system. |callback|
114   // is called with true when this operation is done successfully. Otherwise,
115   // |callback| is called with false. |callback| must not be null.
116   void ClearCacheAndRemountFileSystem(
117       const base::Callback<void(bool)>& callback);
118
119  private:
120   enum State {
121     NOT_INITIALIZED,
122     INITIALIZING,
123     INITIALIZED,
124     REMOUNTING,
125   };
126
127   // Returns true if Drive is enabled.
128   // Must be called on UI thread.
129   bool IsDriveEnabled();
130
131   // Registers remote file system for drive mount point.
132   void AddDriveMountPoint();
133   // Unregisters drive mount point from File API.
134   void RemoveDriveMountPoint();
135
136   // Adds back the drive mount point.
137   // Used to implement ClearCacheAndRemountFileSystem().
138   void AddBackDriveMountPoint(const base::Callback<void(bool)>& callback,
139                               FileError error);
140
141   // Initializes the object. This function should be called before any
142   // other functions.
143   void Initialize();
144
145   // Called when metadata initialization is done. Continues initialization if
146   // the metadata initialization is successful.
147   void InitializeAfterMetadataInitialized(FileError error);
148
149   friend class DriveIntegrationServiceFactory;
150
151   Profile* profile_;
152   State state_;
153   bool enabled_;
154
155   base::FilePath cache_root_directory_;
156   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
157   scoped_ptr<internal::ResourceMetadataStorage,
158              util::DestroyHelper> metadata_storage_;
159   scoped_ptr<internal::FileCache, util::DestroyHelper> cache_;
160   scoped_ptr<DriveServiceInterface> drive_service_;
161   scoped_ptr<JobScheduler> scheduler_;
162   scoped_ptr<DriveAppRegistry> drive_app_registry_;
163   scoped_ptr<internal::ResourceMetadata,
164              util::DestroyHelper> resource_metadata_;
165   scoped_ptr<FileSystemInterface> file_system_;
166   scoped_ptr<DownloadHandler> download_handler_;
167   scoped_ptr<DebugInfoCollector> debug_info_collector_;
168
169   ObserverList<DriveIntegrationServiceObserver> observers_;
170   scoped_ptr<PreferenceWatcher> preference_watcher_;
171
172   // Note: This should remain the last member so it'll be destroyed and
173   // invalidate its weak pointers before any other members are destroyed.
174   base::WeakPtrFactory<DriveIntegrationService> weak_ptr_factory_;
175   DISALLOW_COPY_AND_ASSIGN(DriveIntegrationService);
176 };
177
178 // Singleton that owns all instances of DriveIntegrationService and
179 // associates them with Profiles.
180 class DriveIntegrationServiceFactory
181     : public BrowserContextKeyedServiceFactory {
182  public:
183   // Factory function used by tests.
184   typedef base::Callback<DriveIntegrationService*(Profile* profile)>
185       FactoryCallback;
186
187   // Returns the DriveIntegrationService for |profile|, creating it if it is
188   // not yet created.
189   static DriveIntegrationService* GetForProfile(Profile* profile);
190
191   // Same as GetForProfile. TODO(hidehiko): Remove this.
192   static DriveIntegrationService* GetForProfileRegardlessOfStates(
193       Profile* profile);
194
195   // Returns the DriveIntegrationService that is already associated with
196   // |profile|, if it is not yet created it will return NULL.
197   static DriveIntegrationService* FindForProfile(Profile* profile);
198
199   // Same as FindForProfile. TODO(hidehiko): Remove this.
200   static DriveIntegrationService* FindForProfileRegardlessOfStates(
201       Profile* profile);
202
203   // Returns the DriveIntegrationServiceFactory instance.
204   static DriveIntegrationServiceFactory* GetInstance();
205
206   // Sets a factory function for tests.
207   static void SetFactoryForTest(const FactoryCallback& factory_for_test);
208
209  private:
210   friend struct DefaultSingletonTraits<DriveIntegrationServiceFactory>;
211
212   DriveIntegrationServiceFactory();
213   virtual ~DriveIntegrationServiceFactory();
214
215   // BrowserContextKeyedServiceFactory:
216   virtual BrowserContextKeyedService* BuildServiceInstanceFor(
217       content::BrowserContext* context) const OVERRIDE;
218
219   FactoryCallback factory_for_test_;
220 };
221
222 }  // namespace drive
223
224 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_