15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/singleton.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/observer_list.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/file_errors.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/file_system_util.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/job_scheduler.h"
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/drive/drive_notification_observer.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "content/public/browser/notification_observer.h"
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath;
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class SequencedTaskRunner;
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace drive {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class DebugInfoCollector;
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class DownloadHandler;
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class DriveAppRegistry;
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass DriveServiceInterface;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class EventLogger;
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class FileSystemInterface;
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class JobListInterface;
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace internal {
38b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class FileCache;
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class ResourceMetadata;
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass ResourceMetadataStorage;
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace internal
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Interface for classes that need to observe events from
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// DriveIntegrationService.  All events are notified on UI thread.
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class DriveIntegrationServiceObserver {
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Triggered when the file system is mounted.
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void OnFileSystemMounted() {
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Triggered when the file system is being unmounted.
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void OnFileSystemBeingUnmounted() {
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) protected:
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~DriveIntegrationServiceObserver() {}
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// DriveIntegrationService is used to integrate Drive to Chrome. This class
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// exposes the file system representation built on top of Drive and some
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// other Drive related objects to the file manager, and some other sub
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// systems.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The class is essentially a container that manages lifetime of the objects
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// that are used to integrate Drive to Chrome. The object of this class is
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// created per-profile.
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class DriveIntegrationService : public KeyedService,
6846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                public DriveNotificationObserver,
6946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                public content::NotificationObserver {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  class PreferenceWatcher;
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // test_drive_service, test_mount_point_name, test_cache_root and
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // test_file_system are used by tests to inject customized instances.
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Pass NULL or the empty value when not interested.
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // |preference_watcher| observes the drive enable preference, and sets the
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // enable state when changed. It can be NULL. The ownership is taken by
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // the DriveIntegrationService.
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DriveIntegrationService(
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      Profile* profile,
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      PreferenceWatcher* preference_watcher,
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DriveServiceInterface* test_drive_service,
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& test_mount_point_name,
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const base::FilePath& test_cache_root,
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      FileSystemInterface* test_file_system);
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~DriveIntegrationService();
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService override:
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Shutdown() OVERRIDE;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void SetEnabled(bool enabled);
9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool is_enabled() const { return enabled_; }
93d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
94d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool IsMounted() const;
9558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Adds and removes the observer.
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void AddObserver(DriveIntegrationServiceObserver* observer);
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void RemoveObserver(DriveIntegrationServiceObserver* observer);
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // DriveNotificationObserver implementation.
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void OnNotificationReceived() OVERRIDE;
102b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EventLogger* event_logger() { return logger_.get(); }
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DriveServiceInterface* drive_service() { return drive_service_.get(); }
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DebugInfoCollector* debug_info_collector() {
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return debug_info_collector_.get();
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FileSystemInterface* file_system() { return file_system_.get(); }
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DownloadHandler* download_handler() { return download_handler_.get(); }
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DriveAppRegistry* drive_app_registry() { return drive_app_registry_.get(); }
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  JobListInterface* job_list() { return scheduler_.get(); }
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Clears all the local cache file, the local resource metadata, and
1157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // in-memory Drive app registry, and remounts the file system. |callback|
1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // is called with true when this operation is done successfully. Otherwise,
1177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // |callback| is called with false. |callback| must not be null.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ClearCacheAndRemountFileSystem(
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const base::Callback<void(bool)>& callback);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
12258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  enum State {
12358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    NOT_INITIALIZED,
12458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    INITIALIZING,
12558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    INITIALIZED,
12658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    REMOUNTING,
12758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  };
12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if Drive is enabled.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must be called on UI thread.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsDriveEnabled();
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
133ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Registers remote file system for drive mount point.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddDriveMountPoint();
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unregisters drive mount point from File API.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveDriveMountPoint();
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Adds back the drive mount point.
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Used to implement ClearCacheAndRemountFileSystem().
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddBackDriveMountPoint(const base::Callback<void(bool)>& callback,
141d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                              FileError error);
142d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
143d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Initializes the object. This function should be called before any
144d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // other functions.
145d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void Initialize();
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Called when metadata initialization is done. Continues initialization if
1487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // the metadata initialization is successful.
1497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void InitializeAfterMetadataInitialized(FileError error);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
151a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Change the download directory to the local "Downloads" if the download
152a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // destination is set under Drive. This must be called when disabling Drive.
153a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void AvoidDriveAsDownloadDirecotryPreference();
154a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
15546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // content::NotificationObserver overrides.
15646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void Observe(int type,
15746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       const content::NotificationSource& source,
15846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE;
15946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  friend class DriveIntegrationServiceFactory;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile_;
16358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  State state_;
16458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool enabled_;
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Custom mount point name that can be injected for testing in constructor.
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string mount_point_name_;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath cache_root_directory_;
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<EventLogger> logger_;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<internal::ResourceMetadataStorage,
172eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch             util::DestroyHelper> metadata_storage_;
173b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  scoped_ptr<internal::FileCache, util::DestroyHelper> cache_;
174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<DriveServiceInterface> drive_service_;
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<JobScheduler> scheduler_;
17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<DriveAppRegistry> drive_app_registry_;
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<internal::ResourceMetadata,
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)             util::DestroyHelper> resource_metadata_;
179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<FileSystemInterface> file_system_;
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DownloadHandler> download_handler_;
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DebugInfoCollector> debug_info_collector_;
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
18390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ObserverList<DriveIntegrationServiceObserver> observers_;
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_ptr<PreferenceWatcher> preference_watcher_;
18546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<content::NotificationRegistrar> profile_notification_registrar_;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note: This should remain the last member so it'll be destroyed and
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // invalidate its weak pointers before any other members are destroyed.
18990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::WeakPtrFactory<DriveIntegrationService> weak_ptr_factory_;
19090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DriveIntegrationService);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
19390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Singleton that owns all instances of DriveIntegrationService and
19490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// associates them with Profiles.
19590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class DriveIntegrationServiceFactory
19690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : public BrowserContextKeyedServiceFactory {
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Factory function used by tests.
19990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  typedef base::Callback<DriveIntegrationService*(Profile* profile)>
20090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      FactoryCallback;
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets and resets a factory function for tests. See below for why we can't
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // use BrowserContextKeyedServiceFactory::SetTestingFactory().
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ScopedFactoryForTest {
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   public:
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    explicit ScopedFactoryForTest(FactoryCallback* factory_for_test);
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ~ScopedFactoryForTest();
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
21090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns the DriveIntegrationService for |profile|, creating it if it is
21190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // not yet created.
21290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static DriveIntegrationService* GetForProfile(Profile* profile);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
21490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns the DriveIntegrationService that is already associated with
21590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // |profile|, if it is not yet created it will return NULL.
21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static DriveIntegrationService* FindForProfile(Profile* profile);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
21890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns the DriveIntegrationServiceFactory instance.
21990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static DriveIntegrationServiceFactory* GetInstance();
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
22290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  friend struct DefaultSingletonTraits<DriveIntegrationServiceFactory>;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
22490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DriveIntegrationServiceFactory();
22590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~DriveIntegrationServiceFactory();
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
22746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // BrowserContextKeyedServiceFactory overrides.
22846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual content::BrowserContext* GetBrowserContextToUse(
22946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      content::BrowserContext* context) const OVERRIDE;
230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual KeyedService* BuildServiceInstanceFor(
231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::BrowserContext* context) const OVERRIDE;
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This is static so it can be set without instantiating the factory. This
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // allows factory creation to be delayed until it normally happens (on profile
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // creation) rather than when tests are set up. DriveIntegrationServiceFactory
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // transitively depends on ExtensionSystemFactory which crashes if created too
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // soon (i.e. before the BrowserProcess exists).
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static FactoryCallback* factory_for_test_;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace drive
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
24390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_INTEGRATION_SERVICE_H_
244