| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <map> |
| 8 #include <memory> | 11 #include <memory> |
| 9 #include <string> | 12 #include <string> |
| 10 #include <vector> | 13 #include <vector> |
| 11 | 14 |
| 12 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" |
| 13 #include "base/macros.h" | 16 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/observer_list.h" |
| 15 #include "chrome/browser/chromeos/arc/arc_session_manager.h" | 19 #include "chrome/browser/chromeos/arc/arc_session_manager.h" |
| 16 #include "components/arc/arc_service.h" | 20 #include "components/arc/arc_service.h" |
| 17 #include "components/arc/common/file_system.mojom.h" | 21 #include "components/arc/common/file_system.mojom.h" |
| 18 #include "components/arc/instance_holder.h" | 22 #include "components/arc/instance_holder.h" |
| 23 #include "mojo/public/cpp/bindings/binding.h" |
| 24 #include "storage/browser/fileapi/watcher_manager.h" |
| 19 | 25 |
| 20 namespace arc { | 26 namespace arc { |
| 21 | 27 |
| 22 // Runs ARC file system operations. | 28 // Runs ARC file system operations. |
| 23 // | 29 // |
| 24 // This is an abstraction layer on top of mojom::FileSystemInstance. All ARC | 30 // This is an abstraction layer on top of mojom::FileSystemInstance. All ARC |
| 25 // file system operations should go through this class, rather than invoking | 31 // file system operations should go through this class, rather than invoking |
| 26 // mojom::FileSystemInstance directly. | 32 // mojom::FileSystemInstance directly. |
| 27 // | 33 // |
| 28 // When ARC is disabled or ARC has already booted, file system operations are | 34 // When ARC is disabled or ARC has already booted, file system operations are |
| 29 // performed immediately. While ARC boot is under progress, file operations are | 35 // performed immediately. While ARC boot is under progress, file operations are |
| 30 // deferred until ARC boot finishes or the user disables ARC. | 36 // deferred until ARC boot finishes or the user disables ARC. |
| 31 // | 37 // |
| 32 // This file system operation runner provides better UX when the user attempts | 38 // This file system operation runner provides better UX when the user attempts |
| 33 // to perform file operations while ARC is booting. For example: | 39 // to perform file operations while ARC is booting. For example: |
| 34 // | 40 // |
| 35 // - Media views are mounted in Files app soon after the user logs into | 41 // - Media views are mounted in Files app soon after the user logs into |
| 36 // the system. If the user attempts to open media views before ARC boots, | 42 // the system. If the user attempts to open media views before ARC boots, |
| 37 // a spinner is shown until file system gets ready because ReadDirectory | 43 // a spinner is shown until file system gets ready because ReadDirectory |
| 38 // operations are deferred. | 44 // operations are deferred. |
| 39 // - When an Android content URL is opened soon after the user logs into | 45 // - When an Android content URL is opened soon after the user logs into |
| 40 // the system (because the user opened the tab before they logged out for | 46 // the system (because the user opened the tab before they logged out for |
| 41 // instance), the tab keeps loading until ARC boot finishes, instead of | 47 // instance), the tab keeps loading until ARC boot finishes, instead of |
| 42 // failing immediately. | 48 // failing immediately. |
| 43 // | 49 // |
| 44 // All member functions must be called on the UI thread. | 50 // All member functions must be called on the UI thread. |
| 45 class ArcFileSystemOperationRunner | 51 class ArcFileSystemOperationRunner |
| 46 : public ArcService, | 52 : public ArcService, |
| 53 public mojom::FileSystemHost, |
| 47 public ArcSessionManager::Observer, | 54 public ArcSessionManager::Observer, |
| 48 public InstanceHolder<mojom::FileSystemInstance>::Observer { | 55 public InstanceHolder<mojom::FileSystemInstance>::Observer { |
| 49 public: | 56 public: |
| 50 using GetFileSizeCallback = mojom::FileSystemInstance::GetFileSizeCallback; | 57 using GetFileSizeCallback = mojom::FileSystemInstance::GetFileSizeCallback; |
| 51 using OpenFileToReadCallback = | 58 using OpenFileToReadCallback = |
| 52 mojom::FileSystemInstance::OpenFileToReadCallback; | 59 mojom::FileSystemInstance::OpenFileToReadCallback; |
| 53 using GetDocumentCallback = mojom::FileSystemInstance::GetDocumentCallback; | 60 using GetDocumentCallback = mojom::FileSystemInstance::GetDocumentCallback; |
| 54 using GetChildDocumentsCallback = | 61 using GetChildDocumentsCallback = |
| 55 mojom::FileSystemInstance::GetChildDocumentsCallback; | 62 mojom::FileSystemInstance::GetChildDocumentsCallback; |
| 63 using AddWatcherCallback = base::Callback<void(int64_t watcher_id)>; |
| 64 using RemoveWatcherCallback = base::Callback<void(bool success)>; |
| 65 using ChangeType = storage::WatcherManager::ChangeType; |
| 66 using WatcherCallback = base::Callback<void(ChangeType type)>; |
| 67 |
| 68 class Observer { |
| 69 public: |
| 70 // Called when the installed watchers are invalidated. |
| 71 // This can happen when Android system restarts, for example. |
| 72 // After this event is fired, watcher IDs issued before the event can be |
| 73 // reused. |
| 74 virtual void OnWatchersCleared() = 0; |
| 75 |
| 76 protected: |
| 77 virtual ~Observer() = default; |
| 78 }; |
| 56 | 79 |
| 57 // For supporting ArcServiceManager::GetService<T>(). | 80 // For supporting ArcServiceManager::GetService<T>(). |
| 58 static const char kArcServiceName[]; | 81 static const char kArcServiceName[]; |
| 59 | 82 |
| 60 // Creates an instance suitable for unit tests. | 83 // Creates an instance suitable for unit tests. |
| 61 // This instance will run all operations immediately without deferring by | 84 // This instance will run all operations immediately without deferring by |
| 62 // default. Also, deferring can be enabled/disabled by calling | 85 // default. Also, deferring can be enabled/disabled by calling |
| 63 // SetShouldDefer() from friend classes. | 86 // SetShouldDefer() from friend classes. |
| 64 static std::unique_ptr<ArcFileSystemOperationRunner> CreateForTesting( | 87 static std::unique_ptr<ArcFileSystemOperationRunner> CreateForTesting( |
| 65 ArcBridgeService* bridge_service); | 88 ArcBridgeService* bridge_service); |
| 66 | 89 |
| 67 // The standard constructor. A production instance should be created by | 90 // The standard constructor. A production instance should be created by |
| 68 // this constructor. | 91 // this constructor. |
| 69 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service, | 92 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service, |
| 70 const Profile* profile); | 93 const Profile* profile); |
| 71 ~ArcFileSystemOperationRunner() override; | 94 ~ArcFileSystemOperationRunner() override; |
| 72 | 95 |
| 96 // Adds or removes observers. |
| 97 void AddObserver(Observer* observer); |
| 98 void RemoveObserver(Observer* observer); |
| 99 |
| 73 // Runs file system operations. See file_system.mojom for documentation. | 100 // Runs file system operations. See file_system.mojom for documentation. |
| 74 void GetFileSize(const GURL& url, const GetFileSizeCallback& callback); | 101 void GetFileSize(const GURL& url, const GetFileSizeCallback& callback); |
| 75 void OpenFileToRead(const GURL& url, const OpenFileToReadCallback& callback); | 102 void OpenFileToRead(const GURL& url, const OpenFileToReadCallback& callback); |
| 76 void GetDocument(const std::string& authority, | 103 void GetDocument(const std::string& authority, |
| 77 const std::string& document_id, | 104 const std::string& document_id, |
| 78 const GetDocumentCallback& callback); | 105 const GetDocumentCallback& callback); |
| 79 void GetChildDocuments(const std::string& authority, | 106 void GetChildDocuments(const std::string& authority, |
| 80 const std::string& parent_document_id, | 107 const std::string& parent_document_id, |
| 81 const GetChildDocumentsCallback& callback); | 108 const GetChildDocumentsCallback& callback); |
| 109 void AddWatcher(const std::string& authority, |
| 110 const std::string& document_id, |
| 111 const WatcherCallback& watcher_callback, |
| 112 const AddWatcherCallback& callback); |
| 113 void RemoveWatcher(int64_t watcher_id, const RemoveWatcherCallback& callback); |
| 114 |
| 115 // FileSystemHost overrides: |
| 116 void OnDocumentChanged(int64_t watcher_id, mojom::ChangeType type) override; |
| 82 | 117 |
| 83 // ArcSessionManager::Observer overrides: | 118 // ArcSessionManager::Observer overrides: |
| 84 void OnArcPlayStoreEnabledChanged(bool enabled) override; | 119 void OnArcPlayStoreEnabledChanged(bool enabled) override; |
| 85 | 120 |
| 86 // InstanceHolder<mojom::FileSystemInstance>::Observer overrides: | 121 // InstanceHolder<mojom::FileSystemInstance>::Observer overrides: |
| 87 void OnInstanceReady() override; | 122 void OnInstanceReady() override; |
| 88 void OnInstanceClosed() override; | 123 void OnInstanceClosed() override; |
| 89 | 124 |
| 90 private: | 125 private: |
| 91 friend class ArcFileSystemOperationRunnerTest; | 126 friend class ArcFileSystemOperationRunnerTest; |
| 92 | 127 |
| 93 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service, | 128 ArcFileSystemOperationRunner(ArcBridgeService* bridge_service, |
| 94 const Profile* profile, | 129 const Profile* profile, |
| 95 bool observe_events); | 130 bool set_should_defer_by_events); |
| 131 |
| 132 void OnWatcherAdded(const WatcherCallback& watcher_callback, |
| 133 const AddWatcherCallback& callback, |
| 134 int64_t watcher_id); |
| 96 | 135 |
| 97 // Called whenever ARC states related to |should_defer_| are changed. | 136 // Called whenever ARC states related to |should_defer_| are changed. |
| 98 void OnStateChanged(); | 137 void OnStateChanged(); |
| 99 | 138 |
| 100 // Enables/disables deferring. | 139 // Enables/disables deferring. |
| 101 // Friend unit tests can call this function to simulate enabling/disabling | 140 // Friend unit tests can call this function to simulate enabling/disabling |
| 102 // deferring. | 141 // deferring. |
| 103 void SetShouldDefer(bool should_defer); | 142 void SetShouldDefer(bool should_defer); |
| 104 | 143 |
| 105 // Profile this runner is associated with. This can be nullptr in | 144 // Profile this runner is associated with. This can be nullptr in |
| 106 // unit tests. | 145 // unit tests. |
| 107 const Profile* const profile_; | 146 const Profile* const profile_; |
| 108 | 147 |
| 109 // Indicates if this instance should register observers to receive events. | 148 // Indicates if this instance should enable/disable deferring by events. |
| 110 // Usually true, but set to false in unit tests. | 149 // Usually true, but set to false in unit tests. |
| 111 const bool observe_events_; | 150 bool set_should_defer_by_events_; |
| 112 | 151 |
| 113 // Set to true if operations should be deferred at this moment. | 152 // Set to true if operations should be deferred at this moment. |
| 114 // The default is set to false so that operations are not deferred in | 153 // The default is set to false so that operations are not deferred in |
| 115 // unit tests. | 154 // unit tests. |
| 116 bool should_defer_ = false; | 155 bool should_defer_ = false; |
| 117 | 156 |
| 118 // List of deferred operations. | 157 // List of deferred operations. |
| 119 std::vector<base::Closure> deferred_operations_; | 158 std::vector<base::Closure> deferred_operations_; |
| 120 | 159 |
| 160 // Map from a watcher ID to a watcher callback. |
| 161 std::map<int64_t, WatcherCallback> watcher_callbacks_; |
| 162 |
| 163 base::ObserverList<Observer> observer_list_; |
| 164 |
| 165 mojo::Binding<mojom::FileSystemHost> binding_; |
| 166 |
| 121 base::WeakPtrFactory<ArcFileSystemOperationRunner> weak_ptr_factory_; | 167 base::WeakPtrFactory<ArcFileSystemOperationRunner> weak_ptr_factory_; |
| 122 | 168 |
| 123 DISALLOW_COPY_AND_ASSIGN(ArcFileSystemOperationRunner); | 169 DISALLOW_COPY_AND_ASSIGN(ArcFileSystemOperationRunner); |
| 124 }; | 170 }; |
| 125 | 171 |
| 126 } // namespace arc | 172 } // namespace arc |
| 127 | 173 |
| 128 #endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_
H_ | 174 #endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_FILE_SYSTEM_OPERATION_RUNNER_
H_ |
| OLD | NEW |