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_DRIVE_INTEGRATION_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_
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"
22 class SequencedTaskRunner
;
27 class DebugInfoCollector
;
28 class DownloadHandler
;
29 class DriveAppRegistry
;
30 class DriveServiceInterface
;
31 class FileSystemInterface
;
32 class JobListInterface
;
36 class ResourceMetadata
;
37 class ResourceMetadataStorage
;
38 } // namespace internal
40 // Interface for classes that need to observe events from
41 // DriveIntegrationService. All events are notified on UI thread.
42 class DriveIntegrationServiceObserver
{
44 // Triggered when the file system is mounted.
45 virtual void OnFileSystemMounted() {
48 // Triggered when the file system is being unmounted.
49 virtual void OnFileSystemBeingUnmounted() {
53 virtual ~DriveIntegrationServiceObserver() {}
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
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
{
68 class PreferenceWatcher
;
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(
78 PreferenceWatcher
* preference_watcher
,
79 DriveServiceInterface
* test_drive_service
,
80 const base::FilePath
& test_cache_root
,
81 FileSystemInterface
* test_file_system
);
82 virtual ~DriveIntegrationService();
84 // BrowserContextKeyedService override:
85 virtual void Shutdown() OVERRIDE
;
87 void SetEnabled(bool enabled
);
88 bool is_enabled() const { return enabled_
; }
90 bool IsMounted() const;
92 // Adds and removes the observer.
93 void AddObserver(DriveIntegrationServiceObserver
* observer
);
94 void RemoveObserver(DriveIntegrationServiceObserver
* observer
);
96 // DriveNotificationObserver implementation.
97 virtual void OnNotificationReceived() OVERRIDE
;
98 virtual void OnPushNotificationEnabled(bool enabled
) OVERRIDE
;
100 DriveServiceInterface
* drive_service() {
101 return drive_service_
.get();
104 DebugInfoCollector
* debug_info_collector() {
105 return debug_info_collector_
.get();
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(); }
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
);
127 // Returns true if Drive is enabled.
128 // Must be called on UI thread.
129 bool IsDriveEnabled();
131 // Registers remote file system for drive mount point.
132 void AddDriveMountPoint();
133 // Unregisters drive mount point from File API.
134 void RemoveDriveMountPoint();
136 // Adds back the drive mount point.
137 // Used to implement ClearCacheAndRemountFileSystem().
138 void AddBackDriveMountPoint(const base::Callback
<void(bool)>& callback
,
141 // Initializes the object. This function should be called before any
145 // Called when metadata initialization is done. Continues initialization if
146 // the metadata initialization is successful.
147 void InitializeAfterMetadataInitialized(FileError error
);
149 // Change the download directory to the local "Downloads" if the download
150 // destination is set under Drive. This must be called when disabling Drive.
151 void AvoidDriveAsDownloadDirecotryPreference();
153 friend class DriveIntegrationServiceFactory
;
159 base::FilePath cache_root_directory_
;
160 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
161 scoped_ptr
<internal::ResourceMetadataStorage
,
162 util::DestroyHelper
> metadata_storage_
;
163 scoped_ptr
<internal::FileCache
, util::DestroyHelper
> cache_
;
164 scoped_ptr
<DriveServiceInterface
> drive_service_
;
165 scoped_ptr
<JobScheduler
> scheduler_
;
166 scoped_ptr
<DriveAppRegistry
> drive_app_registry_
;
167 scoped_ptr
<internal::ResourceMetadata
,
168 util::DestroyHelper
> resource_metadata_
;
169 scoped_ptr
<FileSystemInterface
> file_system_
;
170 scoped_ptr
<DownloadHandler
> download_handler_
;
171 scoped_ptr
<DebugInfoCollector
> debug_info_collector_
;
173 ObserverList
<DriveIntegrationServiceObserver
> observers_
;
174 scoped_ptr
<PreferenceWatcher
> preference_watcher_
;
176 // Note: This should remain the last member so it'll be destroyed and
177 // invalidate its weak pointers before any other members are destroyed.
178 base::WeakPtrFactory
<DriveIntegrationService
> weak_ptr_factory_
;
179 DISALLOW_COPY_AND_ASSIGN(DriveIntegrationService
);
182 // Singleton that owns all instances of DriveIntegrationService and
183 // associates them with Profiles.
184 class DriveIntegrationServiceFactory
185 : public BrowserContextKeyedServiceFactory
{
187 // Factory function used by tests.
188 typedef base::Callback
<DriveIntegrationService
*(Profile
* profile
)>
191 // Sets and resets a factory function for tests. See below for why we can't
192 // use BrowserContextKeyedServiceFactory::SetTestingFactory().
193 class ScopedFactoryForTest
{
195 explicit ScopedFactoryForTest(FactoryCallback
* factory_for_test
);
196 ~ScopedFactoryForTest();
199 // Returns the DriveIntegrationService for |profile|, creating it if it is
201 static DriveIntegrationService
* GetForProfile(Profile
* profile
);
203 // Same as GetForProfile. TODO(hidehiko): Remove this.
204 static DriveIntegrationService
* GetForProfileRegardlessOfStates(
207 // Returns the DriveIntegrationService that is already associated with
208 // |profile|, if it is not yet created it will return NULL.
209 static DriveIntegrationService
* FindForProfile(Profile
* profile
);
211 // Same as FindForProfile. TODO(hidehiko): Remove this.
212 static DriveIntegrationService
* FindForProfileRegardlessOfStates(
215 // Returns the DriveIntegrationServiceFactory instance.
216 static DriveIntegrationServiceFactory
* GetInstance();
219 friend struct DefaultSingletonTraits
<DriveIntegrationServiceFactory
>;
221 DriveIntegrationServiceFactory();
222 virtual ~DriveIntegrationServiceFactory();
224 // BrowserContextKeyedServiceFactory:
225 virtual BrowserContextKeyedService
* BuildServiceInstanceFor(
226 content::BrowserContext
* context
) const OVERRIDE
;
228 // This is static so it can be set without instantiating the factory. This
229 // allows factory creation to be delayed until it normally happens (on profile
230 // creation) rather than when tests are set up. DriveIntegrationServiceFactory
231 // transitively depends on ExtensionSystemFactory which crashes if created too
232 // soon (i.e. before the BrowserProcess exists).
233 static FactoryCallback
* factory_for_test_
;
238 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_