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 FileWriteHelper; 33 class JobListInterface; 34 35 namespace internal { 36 class FileCache; 37 class ResourceMetadata; 38 class ResourceMetadataStorage; 39 } // namespace internal 40 41 // Interface for classes that need to observe events from 42 // DriveIntegrationService. All events are notified on UI thread. 43 class DriveIntegrationServiceObserver { 44 public: 45 // Triggered when the file system is mounted. 46 virtual void OnFileSystemMounted() { 47 } 48 49 // Triggered when the file system is being unmounted. 50 virtual void OnFileSystemBeingUnmounted() { 51 } 52 53 protected: 54 virtual ~DriveIntegrationServiceObserver() {} 55 }; 56 57 // DriveIntegrationService is used to integrate Drive to Chrome. This class 58 // exposes the file system representation built on top of Drive and some 59 // other Drive related objects to the file manager, and some other sub 60 // systems. 61 // 62 // The class is essentially a container that manages lifetime of the objects 63 // that are used to integrate Drive to Chrome. The object of this class is 64 // created per-profile. 65 class DriveIntegrationService 66 : public BrowserContextKeyedService, 67 public DriveNotificationObserver { 68 public: 69 // test_drive_service, test_cache_root and test_file_system are used by tests 70 // to inject customized instances. 71 // Pass NULL or the empty value when not interested. 72 DriveIntegrationService( 73 Profile* profile, 74 DriveServiceInterface* test_drive_service, 75 const base::FilePath& test_cache_root, 76 FileSystemInterface* test_file_system); 77 virtual ~DriveIntegrationService(); 78 79 // Initializes the object. This function should be called before any 80 // other functions. 81 void Initialize(); 82 83 // BrowserContextKeyedService override: 84 virtual void Shutdown() OVERRIDE; 85 86 // Adds and removes the observer. 87 void AddObserver(DriveIntegrationServiceObserver* observer); 88 void RemoveObserver(DriveIntegrationServiceObserver* observer); 89 90 // DriveNotificationObserver implementation. 91 virtual void OnNotificationReceived() OVERRIDE; 92 virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE; 93 94 DriveServiceInterface* drive_service() { 95 return drive_service_.get(); 96 } 97 98 DebugInfoCollector* debug_info_collector() { 99 return debug_info_collector_.get(); 100 } 101 FileSystemInterface* file_system() { return file_system_.get(); } 102 FileWriteHelper* file_write_helper() { return file_write_helper_.get(); } 103 DownloadHandler* download_handler() { return download_handler_.get(); } 104 DriveAppRegistry* drive_app_registry() { return drive_app_registry_.get(); } 105 JobListInterface* job_list() { return scheduler_.get(); } 106 107 // Clears all the local cache file, the local resource metadata, and 108 // in-memory Drive app registry, and remounts the file system. |callback| 109 // is called with true when this operation is done successfully. Otherwise, 110 // |callback| is called with false. |callback| must not be null. 111 void ClearCacheAndRemountFileSystem( 112 const base::Callback<void(bool)>& callback); 113 114 private: 115 // Returns true if Drive is enabled. 116 // Must be called on UI thread. 117 bool IsDriveEnabled(); 118 119 // Registers remote file system for drive mount point. 120 void AddDriveMountPoint(); 121 // Unregisters drive mount point from File API. 122 void RemoveDriveMountPoint(); 123 124 // Adds back the drive mount point. 125 // Used to implement ClearCacheAndRemountFileSystem(). 126 void AddBackDriveMountPoint(const base::Callback<void(bool)>& callback, 127 bool success); 128 129 // Called when metadata initialization is done. Continues initialization if 130 // the metadata initialization is successful. 131 void InitializeAfterMetadataInitialized(FileError error); 132 133 // Disables Drive. Used to disable Drive when needed (ex. initialization of 134 // the Drive cache failed). 135 // Must be called on UI thread. 136 void DisableDrive(); 137 138 friend class DriveIntegrationServiceFactory; 139 140 Profile* profile_; 141 // True if Drive is disabled due to initialization errors. 142 bool drive_disabled_; 143 144 base::FilePath cache_root_directory_; 145 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 146 scoped_ptr<internal::ResourceMetadataStorage, 147 util::DestroyHelper> metadata_storage_; 148 scoped_ptr<internal::FileCache, util::DestroyHelper> cache_; 149 scoped_ptr<DriveServiceInterface> drive_service_; 150 scoped_ptr<JobScheduler> scheduler_; 151 scoped_ptr<DriveAppRegistry> drive_app_registry_; 152 scoped_ptr<internal::ResourceMetadata, 153 util::DestroyHelper> resource_metadata_; 154 scoped_ptr<FileSystemInterface> file_system_; 155 scoped_ptr<FileWriteHelper> file_write_helper_; 156 scoped_ptr<DownloadHandler> download_handler_; 157 scoped_ptr<DebugInfoCollector> debug_info_collector_; 158 159 ObserverList<DriveIntegrationServiceObserver> observers_; 160 161 // Note: This should remain the last member so it'll be destroyed and 162 // invalidate its weak pointers before any other members are destroyed. 163 base::WeakPtrFactory<DriveIntegrationService> weak_ptr_factory_; 164 DISALLOW_COPY_AND_ASSIGN(DriveIntegrationService); 165 }; 166 167 // Singleton that owns all instances of DriveIntegrationService and 168 // associates them with Profiles. 169 class DriveIntegrationServiceFactory 170 : public BrowserContextKeyedServiceFactory { 171 public: 172 // Factory function used by tests. 173 typedef base::Callback<DriveIntegrationService*(Profile* profile)> 174 FactoryCallback; 175 176 // Returns the DriveIntegrationService for |profile|, creating it if it is 177 // not yet created. 178 // 179 // This function starts returning NULL if Drive is disabled, even if this 180 // function previously returns a non-NULL object. In other words, clients 181 // can assume that Drive is enabled if this function returns a non-NULL 182 // object. 183 static DriveIntegrationService* GetForProfile(Profile* profile); 184 185 // Similar to GetForProfile(), but returns the instance regardless of if 186 // Drive is enabled/disabled. 187 static DriveIntegrationService* GetForProfileRegardlessOfStates( 188 Profile* profile); 189 190 // Returns the DriveIntegrationService that is already associated with 191 // |profile|, if it is not yet created it will return NULL. 192 // 193 // This function starts returning NULL if Drive is disabled. See also the 194 // comment at GetForProfile(). 195 static DriveIntegrationService* FindForProfile(Profile* profile); 196 197 // Similar to FindForProfile(), but returns the instance regardless of if 198 // Drive is enabled/disabled. 199 static DriveIntegrationService* FindForProfileRegardlessOfStates( 200 Profile* profile); 201 202 // Returns the DriveIntegrationServiceFactory instance. 203 static DriveIntegrationServiceFactory* GetInstance(); 204 205 // Sets a factory function for tests. 206 static void SetFactoryForTest(const FactoryCallback& factory_for_test); 207 208 private: 209 friend struct DefaultSingletonTraits<DriveIntegrationServiceFactory>; 210 211 DriveIntegrationServiceFactory(); 212 virtual ~DriveIntegrationServiceFactory(); 213 214 // BrowserContextKeyedServiceFactory: 215 virtual BrowserContextKeyedService* BuildServiceInstanceFor( 216 content::BrowserContext* context) const OVERRIDE; 217 218 FactoryCallback factory_for_test_; 219 }; 220 221 } // namespace drive 222 223 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_ 224