Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / drive_integration_service.h
blobdbb1e63818edcfed76ec20e568c259ea6372b181
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 "components/drive/drive_notification_observer.h"
14 #include "components/drive/file_errors.h"
15 #include "components/drive/file_system_core_util.h"
16 #include "components/drive/job_scheduler.h"
17 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
22 class Profile;
24 namespace base {
25 class FilePath;
26 class SequencedTaskRunner;
29 namespace drive {
31 class DebugInfoCollector;
32 class DownloadHandler;
33 class DriveAppRegistry;
34 class DriveServiceInterface;
35 class EventLogger;
36 class FileSystemInterface;
37 class JobListInterface;
39 namespace internal {
40 class FileCache;
41 class ResourceMetadata;
42 class ResourceMetadataStorage;
43 } // namespace internal
45 // Interface for classes that need to observe events from
46 // DriveIntegrationService. All events are notified on UI thread.
47 class DriveIntegrationServiceObserver {
48 public:
49 // Triggered when the file system is mounted.
50 virtual void OnFileSystemMounted() {
53 // Triggered when the file system is being unmounted.
54 virtual void OnFileSystemBeingUnmounted() {
57 protected:
58 virtual ~DriveIntegrationServiceObserver() {}
61 // DriveIntegrationService is used to integrate Drive to Chrome. This class
62 // exposes the file system representation built on top of Drive and some
63 // other Drive related objects to the file manager, and some other sub
64 // systems.
66 // The class is essentially a container that manages lifetime of the objects
67 // that are used to integrate Drive to Chrome. The object of this class is
68 // created per-profile.
69 class DriveIntegrationService : public KeyedService,
70 public DriveNotificationObserver,
71 public content::NotificationObserver {
72 public:
73 class PreferenceWatcher;
75 // test_drive_service, test_mount_point_name, test_cache_root and
76 // test_file_system are used by tests to inject customized instances.
77 // Pass NULL or the empty value when not interested.
78 // |preference_watcher| observes the drive enable preference, and sets the
79 // enable state when changed. It can be NULL. The ownership is taken by
80 // the DriveIntegrationService.
81 DriveIntegrationService(
82 Profile* profile,
83 PreferenceWatcher* preference_watcher,
84 DriveServiceInterface* test_drive_service,
85 const std::string& test_mount_point_name,
86 const base::FilePath& test_cache_root,
87 FileSystemInterface* test_file_system);
88 ~DriveIntegrationService() override;
90 // KeyedService override:
91 void Shutdown() override;
93 void SetEnabled(bool enabled);
94 bool is_enabled() const { return enabled_; }
96 bool IsMounted() const;
98 // Adds and removes the observer.
99 void AddObserver(DriveIntegrationServiceObserver* observer);
100 void RemoveObserver(DriveIntegrationServiceObserver* observer);
102 // DriveNotificationObserver implementation.
103 void OnNotificationReceived() override;
104 void OnPushNotificationEnabled(bool enabled) override;
106 EventLogger* event_logger() { return logger_.get(); }
107 DriveServiceInterface* drive_service() { return drive_service_.get(); }
108 DebugInfoCollector* debug_info_collector() {
109 return debug_info_collector_.get();
111 FileSystemInterface* file_system() { return file_system_.get(); }
112 DownloadHandler* download_handler() { return download_handler_.get(); }
113 DriveAppRegistry* drive_app_registry() { return drive_app_registry_.get(); }
114 JobListInterface* job_list() { return scheduler_.get(); }
116 // Clears all the local cache file, the local resource metadata, and
117 // in-memory Drive app registry, and remounts the file system. |callback|
118 // is called with true when this operation is done successfully. Otherwise,
119 // |callback| is called with false. |callback| must not be null.
120 void ClearCacheAndRemountFileSystem(
121 const base::Callback<void(bool)>& callback);
123 private:
124 enum State {
125 NOT_INITIALIZED,
126 INITIALIZING,
127 INITIALIZED,
128 REMOUNTING,
131 // Returns true if Drive is enabled.
132 // Must be called on UI thread.
133 bool IsDriveEnabled();
135 // Registers remote file system for drive mount point.
136 void AddDriveMountPoint();
137 // Unregisters drive mount point from File API.
138 void RemoveDriveMountPoint();
140 // Adds back the drive mount point.
141 // Used to implement ClearCacheAndRemountFileSystem().
142 void AddBackDriveMountPoint(const base::Callback<void(bool)>& callback,
143 FileError error);
145 // Initializes the object. This function should be called before any
146 // other functions.
147 void Initialize();
149 // Called when metadata initialization is done. Continues initialization if
150 // the metadata initialization is successful.
151 void InitializeAfterMetadataInitialized(FileError error);
153 // Change the download directory to the local "Downloads" if the download
154 // destination is set under Drive. This must be called when disabling Drive.
155 void AvoidDriveAsDownloadDirecotryPreference();
157 // content::NotificationObserver overrides.
158 void Observe(int type,
159 const content::NotificationSource& source,
160 const content::NotificationDetails& details) override;
162 friend class DriveIntegrationServiceFactory;
164 Profile* profile_;
165 State state_;
166 bool enabled_;
167 // Custom mount point name that can be injected for testing in constructor.
168 std::string mount_point_name_;
170 base::FilePath cache_root_directory_;
171 scoped_ptr<EventLogger> logger_;
172 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
173 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
174 scoped_ptr<internal::ResourceMetadataStorage,
175 util::DestroyHelper> metadata_storage_;
176 scoped_ptr<internal::FileCache, util::DestroyHelper> cache_;
177 scoped_ptr<DriveServiceInterface> drive_service_;
178 scoped_ptr<JobScheduler> scheduler_;
179 scoped_ptr<DriveAppRegistry> drive_app_registry_;
180 scoped_ptr<internal::ResourceMetadata,
181 util::DestroyHelper> resource_metadata_;
182 scoped_ptr<FileSystemInterface> file_system_;
183 scoped_ptr<DownloadHandler> download_handler_;
184 scoped_ptr<DebugInfoCollector> debug_info_collector_;
186 base::ObserverList<DriveIntegrationServiceObserver> observers_;
187 scoped_ptr<PreferenceWatcher> preference_watcher_;
188 scoped_ptr<content::NotificationRegistrar> profile_notification_registrar_;
190 // Note: This should remain the last member so it'll be destroyed and
191 // invalidate its weak pointers before any other members are destroyed.
192 base::WeakPtrFactory<DriveIntegrationService> weak_ptr_factory_;
193 DISALLOW_COPY_AND_ASSIGN(DriveIntegrationService);
196 // Singleton that owns all instances of DriveIntegrationService and
197 // associates them with Profiles.
198 class DriveIntegrationServiceFactory
199 : public BrowserContextKeyedServiceFactory {
200 public:
201 // Factory function used by tests.
202 typedef base::Callback<DriveIntegrationService*(Profile* profile)>
203 FactoryCallback;
205 // Sets and resets a factory function for tests. See below for why we can't
206 // use BrowserContextKeyedServiceFactory::SetTestingFactory().
207 class ScopedFactoryForTest {
208 public:
209 explicit ScopedFactoryForTest(FactoryCallback* factory_for_test);
210 ~ScopedFactoryForTest();
213 // Returns the DriveIntegrationService for |profile|, creating it if it is
214 // not yet created.
215 static DriveIntegrationService* GetForProfile(Profile* profile);
217 // Returns the DriveIntegrationService that is already associated with
218 // |profile|, if it is not yet created it will return NULL.
219 static DriveIntegrationService* FindForProfile(Profile* profile);
221 // Returns the DriveIntegrationServiceFactory instance.
222 static DriveIntegrationServiceFactory* GetInstance();
224 private:
225 friend struct base::DefaultSingletonTraits<DriveIntegrationServiceFactory>;
227 DriveIntegrationServiceFactory();
228 ~DriveIntegrationServiceFactory() override;
230 // BrowserContextKeyedServiceFactory overrides.
231 content::BrowserContext* GetBrowserContextToUse(
232 content::BrowserContext* context) const override;
233 KeyedService* BuildServiceInstanceFor(
234 content::BrowserContext* context) const override;
236 // This is static so it can be set without instantiating the factory. This
237 // allows factory creation to be delayed until it normally happens (on profile
238 // creation) rather than when tests are set up. DriveIntegrationServiceFactory
239 // transitively depends on ExtensionSystemFactory which crashes if created too
240 // soon (i.e. before the BrowserProcess exists).
241 static FactoryCallback* factory_for_test_;
244 } // namespace drive
246 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_