Home | History | Annotate | Download | only in file_manager
      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_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
      6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/files/file_path_watcher.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
     16 #include "chrome/browser/chromeos/drive/file_system_observer.h"
     17 #include "chrome/browser/chromeos/drive/job_list.h"
     18 #include "chrome/browser/chromeos/drive/sync_client.h"
     19 #include "chrome/browser/chromeos/file_manager/file_watcher.h"
     20 #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
     21 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
     22 #include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
     23 #include "chrome/browser/drive/drive_service_interface.h"
     24 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
     25 #include "chrome/common/extensions/api/file_browser_private.h"
     26 #include "chromeos/disks/disk_mount_manager.h"
     27 #include "chromeos/network/network_state_handler_observer.h"
     28 #include "content/public/browser/notification_observer.h"
     29 #include "content/public/browser/notification_registrar.h"
     30 #include "webkit/browser/fileapi/file_system_operation.h"
     31 
     32 class PrefChangeRegistrar;
     33 class Profile;
     34 
     35 using file_manager::util::EntryDefinition;
     36 
     37 namespace base {
     38 class ListValue;
     39 }
     40 
     41 namespace chromeos {
     42 class NetworkState;
     43 }
     44 
     45 namespace file_manager {
     46 
     47 // Monitors changes in disk mounts, network connection state and preferences
     48 // affecting File Manager. Dispatches appropriate File Browser events.
     49 class EventRouter
     50     : public chromeos::NetworkStateHandlerObserver,
     51       public drive::FileSystemObserver,
     52       public drive::JobListObserver,
     53       public drive::DriveServiceObserver,
     54       public VolumeManagerObserver,
     55       public content::NotificationObserver,
     56       public chrome::MultiUserWindowManager::Observer {
     57  public:
     58   explicit EventRouter(Profile* profile);
     59   virtual ~EventRouter();
     60 
     61   void Shutdown();
     62 
     63   // Starts observing file system change events.
     64   void ObserveEvents();
     65 
     66   typedef base::Callback<void(bool success)> BoolCallback;
     67 
     68   // Adds a file watch at |local_path|, associated with |virtual_path|, for
     69   // an extension with |extension_id|.
     70   //
     71   // |callback| will be called with true on success, or false on failure.
     72   // |callback| must not be null.
     73   void AddFileWatch(const base::FilePath& local_path,
     74                     const base::FilePath& virtual_path,
     75                     const std::string& extension_id,
     76                     const BoolCallback& callback);
     77 
     78   // Removes a file watch at |local_path| for an extension with |extension_id|.
     79   void RemoveFileWatch(const base::FilePath& local_path,
     80                        const std::string& extension_id);
     81 
     82   // Called when a copy task is completed.
     83   void OnCopyCompleted(
     84       int copy_id, const GURL& source_url, const GURL& destination_url,
     85       base::File::Error error);
     86 
     87   // Called when a copy task progress is updated.
     88   void OnCopyProgress(int copy_id,
     89                       fileapi::FileSystemOperation::CopyProgressType type,
     90                       const GURL& source_url,
     91                       const GURL& destination_url,
     92                       int64 size);
     93 
     94   // Register observer to the multi user window manager.
     95   void RegisterMultiUserWindowManagerObserver();
     96 
     97   // chromeos::NetworkStateHandlerObserver overrides.
     98   virtual void DefaultNetworkChanged(
     99       const chromeos::NetworkState* network) OVERRIDE;
    100 
    101   // drive::JobListObserver overrides.
    102   virtual void OnJobAdded(const drive::JobInfo& job_info) OVERRIDE;
    103   virtual void OnJobUpdated(const drive::JobInfo& job_info) OVERRIDE;
    104   virtual void OnJobDone(const drive::JobInfo& job_info,
    105                          drive::FileError error) OVERRIDE;
    106 
    107   // drive::DriveServiceObserver overrides.
    108   virtual void OnRefreshTokenInvalid() OVERRIDE;
    109 
    110   // drive::FileSystemObserver overrides.
    111   virtual void OnDirectoryChanged(const base::FilePath& drive_path) OVERRIDE;
    112   virtual void OnDriveSyncError(drive::file_system::DriveSyncErrorType type,
    113                                 const base::FilePath& drive_path) OVERRIDE;
    114 
    115   // VolumeManagerObserver overrides.
    116   virtual void OnDiskAdded(
    117       const chromeos::disks::DiskMountManager::Disk& disk,
    118       bool mounting) OVERRIDE;
    119   virtual void OnDiskRemoved(
    120       const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE;
    121   virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE;
    122   virtual void OnDeviceRemoved(const std::string& device_path,
    123                                bool hard_unplugged) OVERRIDE;
    124   virtual void OnVolumeMounted(chromeos::MountError error_code,
    125                                const VolumeInfo& volume_info,
    126                                bool is_remounting) OVERRIDE;
    127   virtual void OnVolumeUnmounted(chromeos::MountError error_code,
    128                                  const VolumeInfo& volume_info) OVERRIDE;
    129   virtual void OnFormatStarted(
    130       const std::string& device_path, bool success) OVERRIDE;
    131   virtual void OnFormatCompleted(
    132       const std::string& device_path, bool success) OVERRIDE;
    133 
    134   // content::NotificationObserver overrides.
    135   virtual void Observe(int type,
    136                        const content::NotificationSource& source,
    137                        const content::NotificationDetails& details) OVERRIDE;
    138 
    139   // chrome::MultiUserWindowManager::Observer overrides:
    140   virtual void OnOwnerEntryChanged(aura::Window* window) OVERRIDE;
    141 
    142  private:
    143   typedef std::map<base::FilePath, FileWatcher*> WatcherMap;
    144 
    145   // Called when prefs related to file manager change.
    146   void OnFileManagerPrefsChanged();
    147 
    148   // Process file watch notifications.
    149   void HandleFileWatchNotification(const base::FilePath& path,
    150                                    bool got_error);
    151 
    152   // Sends directory change event.
    153   void DispatchDirectoryChangeEvent(
    154       const base::FilePath& path,
    155       bool error,
    156       const std::vector<std::string>& extension_ids);
    157 
    158   // Sends directory change event, after converting the file definition to entry
    159   // definition.
    160   void DispatchDirectoryChangeEventWithEntryDefinition(
    161       bool watcher_error,
    162       const EntryDefinition& entry_definition);
    163 
    164   // If needed, opens a file manager window for the removable device mounted at
    165   // |mount_path|. Disk.mount_path() is empty, since it is being filled out
    166   // after calling notifying observers by DiskMountManager.
    167   void ShowRemovableDeviceInFileManager(VolumeType type,
    168                                         const base::FilePath& mount_path);
    169 
    170   // Dispatches an onDeviceChanged event containing |type| and |path| to
    171   // extensions.
    172   void DispatchDeviceEvent(
    173       extensions::api::file_browser_private::DeviceEventType type,
    174       const std::string& path);
    175 
    176   // Sends onFileTranferUpdated to extensions if needed. If |always| is true,
    177   // it sends the event always. Otherwise, it sends the event if enough time has
    178   // passed from the previous event so as not to make extension busy.
    179   void SendDriveFileTransferEvent(bool always);
    180 
    181   // Manages the list of currently active Drive file transfer jobs.
    182   struct DriveJobInfoWithStatus {
    183     DriveJobInfoWithStatus();
    184     DriveJobInfoWithStatus(const drive::JobInfo& info,
    185                            const std::string& status);
    186     drive::JobInfo job_info;
    187     std::string status;
    188   };
    189   std::map<drive::JobID, DriveJobInfoWithStatus> drive_jobs_;
    190   base::Time last_file_transfer_event_;
    191   base::Time last_copy_progress_event_;
    192 
    193   WatcherMap file_watchers_;
    194   scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
    195   Profile* profile_;
    196 
    197   content::NotificationRegistrar notification_registrar_;
    198   bool multi_user_window_manager_observer_registered_;
    199 
    200   // Note: This should remain the last member so it'll be destroyed and
    201   // invalidate the weak pointers before any other members are destroyed.
    202   base::WeakPtrFactory<EventRouter> weak_factory_;
    203   DISALLOW_COPY_AND_ASSIGN(EventRouter);
    204 };
    205 
    206 }  // namespace file_manager
    207 
    208 #endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_EVENT_ROUTER_H_
    209