Home | History | Annotate | Download | only in drive
      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   // 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();
    152 
    153   friend class DriveIntegrationServiceFactory;
    154 
    155   Profile* profile_;
    156   State state_;
    157   bool enabled_;
    158 
    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_;
    172 
    173   ObserverList<DriveIntegrationServiceObserver> observers_;
    174   scoped_ptr<PreferenceWatcher> preference_watcher_;
    175 
    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);
    180 };
    181 
    182 // Singleton that owns all instances of DriveIntegrationService and
    183 // associates them with Profiles.
    184 class DriveIntegrationServiceFactory
    185     : public BrowserContextKeyedServiceFactory {
    186  public:
    187   // Factory function used by tests.
    188   typedef base::Callback<DriveIntegrationService*(Profile* profile)>
    189       FactoryCallback;
    190 
    191   // Returns the DriveIntegrationService for |profile|, creating it if it is
    192   // not yet created.
    193   static DriveIntegrationService* GetForProfile(Profile* profile);
    194 
    195   // Same as GetForProfile. TODO(hidehiko): Remove this.
    196   static DriveIntegrationService* GetForProfileRegardlessOfStates(
    197       Profile* profile);
    198 
    199   // Returns the DriveIntegrationService that is already associated with
    200   // |profile|, if it is not yet created it will return NULL.
    201   static DriveIntegrationService* FindForProfile(Profile* profile);
    202 
    203   // Same as FindForProfile. TODO(hidehiko): Remove this.
    204   static DriveIntegrationService* FindForProfileRegardlessOfStates(
    205       Profile* profile);
    206 
    207   // Returns the DriveIntegrationServiceFactory instance.
    208   static DriveIntegrationServiceFactory* GetInstance();
    209 
    210   // Sets a factory function for tests.
    211   static void SetFactoryForTest(const FactoryCallback& factory_for_test);
    212 
    213  private:
    214   friend struct DefaultSingletonTraits<DriveIntegrationServiceFactory>;
    215 
    216   DriveIntegrationServiceFactory();
    217   virtual ~DriveIntegrationServiceFactory();
    218 
    219   // BrowserContextKeyedServiceFactory:
    220   virtual BrowserContextKeyedService* BuildServiceInstanceFor(
    221       content::BrowserContext* context) const OVERRIDE;
    222 
    223   FactoryCallback factory_for_test_;
    224 };
    225 
    226 }  // namespace drive
    227 
    228 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_
    229